Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by gfv ( 13 years ago )
#include <cstdio>
#include <cstdlib>
#include <cstring>

template <class T>
T maj(T a, T b)
{
 return (a > b) ? a : b;
}

template <class T>
T min(T a, T b)
{
 return (a < b) ? a : b;
}

template <class PropSizeT>
class Banks
{
protected:
 PropSizeT bankTable[100000];
public:
 Banks(PropSizeT bNum)
 {
  memset(this->bankTable, 0, sizeof(this->bankTable));
  for(int i = 0; i < bNum; i++) this->bankTable[i] = i + 1;
 }
 PropSizeT verifyID(PropSizeT uid);
 void conflictID(PropSizeT tid, PropSizeT oid);
};

template <class PropSizeT>
PropSizeT Banks<PropSizeT>::verifyID(PropSizeT uid)
{
 PropSizeT realID = uid;
 if(this->bankTable[uid - 1] != uid) realID = this->verifyID(bankTable[uid - 1]);
 return realID;
}

template <class PropSizeT>
void Banks<PropSizeT>::conflictID(PropSizeT tid, PropSizeT oid)
{
 if(this->bankTable[tid - 1] != tid) conflictID(this->bankTable[tid - 1], oid);
 else if(this->bankTable[oid - 1] != oid) conflictID(tid, this->bankTable[oid - 1]);
 else if(tid != oid) this->bankTable[maj<PropSizeT>(tid, oid) - 1] = min<PropSizeT>(tid, oid);
}

int main()
{
 long n, m, id1, id2;
 char buf;
 Banks<long> *banks;

 scanf("%ld %ld", &n, &m);

 if(n)
 {
  banks = new Banks<long>(n);
 }

 if(m)
 {
  for(int j = 0; j < m; j++)
  {
   scanf(" %c %ld %ld", &buf;, &id1;, &id2;);
   if(buf == 'F') banks->conflictID(id1, id2);
   else if(buf == 'C')
   {
    if(banks->verifyID(id1) == banks->verifyID(id2)) printf("S\n");
    else printf("N\n");
   }
  }
 }
}

 

Revise this Paste

Your Name: Code Language: