Guest User

Untitled

a guest
Nov 20th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.16 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<vector>
  6.  
  7. #define ISDIGIT(x) ((x)>='0'&&(x)<='9'?1:0)
  8. #define IS_SPACE_CHARACTER(x) ((x)==' '||(x)=='\t'||(x)=='\n')
  9.  
  10. using namespace std;
  11.  
  12. class Stream
  13. {
  14.     char *array,*readHead,*lastEnd;
  15.     int size;
  16.  
  17.     int getSize()
  18.     {
  19.         int size;
  20.         fseek(stdin,0,SEEK_END);
  21.         size=ftell(stdin);
  22.         rewind(stdin);
  23.         return size;
  24.     }
  25.  
  26. public:
  27.     Stream()
  28.     {
  29.         this->size=getSize();
  30.         array=new char[size+1];
  31.         size=fread(array,1,size,stdin);
  32.         array[size]=0;
  33.         readHead=array;
  34.         lastEnd=array+size;
  35.     }
  36.  
  37.     ~Stream()
  38.     {
  39.         size=0;
  40.         if(array!=NULL)
  41.             delete []array;
  42.         array=NULL;
  43.     }
  44.  
  45.     int nextInt()
  46.     {
  47.         bool minusFlag=false;
  48.         int variable=0;
  49.         for(;!ISDIGIT(*readHead)&&*readHead!='-'&&*readHead;readHead++);
  50.         if(*readHead=='-')
  51.         {
  52.             minusFlag=true;
  53.             readHead++;
  54.         }
  55.         else if(!*readHead)
  56.             throw EOF;
  57.         for(;ISDIGIT(*readHead);readHead++)
  58.             variable=10*variable+*readHead-'0';
  59.         if(minusFlag)
  60.             return -variable;
  61.         return variable;
  62.     }
  63.  
  64.     void nextWord(char *charArray)
  65.     {
  66.         for(;*readHead&&IS_SPACE_CHARACTER(*readHead);readHead++);
  67.         if(!*readHead)
  68.             throw EOF;
  69.         for(;!IS_SPACE_CHARACTER(*readHead)&&*readHead;readHead++,charArray++)
  70.             *charArray=*readHead;
  71.         *charArray=0;
  72.         if(*readHead)
  73.             readHead++;
  74.     }
  75. };
  76.  
  77. struct UnionMaker
  78. {
  79.     int parent;
  80.     int rank;
  81.     int treeSize;
  82. }student[1000000];
  83.  
  84. char people[1000000][22];
  85. vector<int> v[10009];
  86. int vectors[10009];
  87.  
  88.  
  89. int unify(int a,int b);
  90. int unionFind(int a);
  91. int hashValue(char *str);
  92. int getIndex(char *name);
  93.  
  94. int main()
  95. {
  96.     //freopen("a.txt","r",stdin);
  97.     Stream input;
  98.  
  99.     int vertex,edge,i,s1,s2,totalVectors=0,hashVal,testCase;
  100.  
  101.     testCase=input.nextInt();
  102.  
  103.     while(testCase--)
  104.     {
  105.         vertex=0;
  106.         edge=input.nextInt();
  107.  
  108.         for(i=0;i<totalVectors;i++)
  109.             v[vectors[i]].clear();
  110.  
  111.         for(i=0;i<vertex;i++)
  112.         {
  113.             input.nextWord(people[i]);
  114.             hashVal=hashValue(people[i]);
  115.             vectors[i]=hashVal;
  116.             v[hashVal].push_back(i);
  117.             student[i].parent=i;
  118.             student[i].rank=0;
  119.             student[i].treeSize=1;
  120.         }
  121.  
  122.         while(edge--)
  123.         {
  124.             input.nextWord(people[vertex]);
  125.             hashVal=hashValue(people[vertex]);
  126.             s1=getIndex(people[vertex]);
  127.             if(s1==-1)
  128.             {
  129.                 v[hashVal].push_back(vertex);
  130.                 vectors[vertex]=hashVal;
  131.                 student[vertex].parent=vertex;
  132.                 student[vertex].rank=0;
  133.                 student[vertex].treeSize=1;
  134.                 s1=vertex;
  135.                 vertex++;
  136.             }
  137.             input.nextWord(people[vertex]);
  138.             hashVal=hashValue(people[vertex]);
  139.             s2=getIndex(people[vertex]);
  140.             if(s2==-1)
  141.             {
  142.                 v[hashVal].push_back(vertex);
  143.                 vectors[vertex]=hashVal;
  144.                 student[vertex].parent=vertex;
  145.                 student[vertex].rank=0;
  146.                 student[vertex].treeSize=1;
  147.                 s2=vertex;
  148.                 vertex++;
  149.             }
  150.             printf("%d\n",unify(s1,s2));
  151.         }
  152.         totalVectors=vertex;
  153.     }
  154.     return 0;
  155. }
  156.  
  157. int unify(int a,int b)
  158. {
  159.     int rootA=unionFind(a);
  160.     int rootB=unionFind(b);
  161.     if(rootA!=rootB)
  162.     {
  163.         if(student[rootA].rank<student[rootB].rank)
  164.         {
  165.             student[rootA].parent=rootB;
  166.             student[rootB].treeSize+=student[rootA].treeSize;
  167.             return student[rootB].treeSize;
  168.         }
  169.         if(student[rootA].rank>student[rootB].rank)
  170.         {
  171.             student[rootB].parent=rootA;
  172.             student[rootA].treeSize+=student[rootB].treeSize;
  173.             return student[rootA].treeSize;
  174.         }
  175.         student[rootB].parent=rootA;
  176.         student[rootA].treeSize+=student[rootB].treeSize;
  177.         student[rootA].rank++;
  178.         return student[rootA].treeSize;
  179.     }
  180.     return student[rootA].treeSize;
  181. }
  182.  
  183. int unionFind(int a)
  184. {
  185.     if(a!=student[a].parent)
  186.         student[a].parent=unionFind(student[a].parent);
  187.     return student[a].parent;
  188. }
  189.  
  190. int hashValue(char *str)
  191. {
  192.      int len=strlen(str);
  193.      int intLength = len / 4  , k;
  194.      unsigned int sum = 0,mult;
  195.      for (int j = 0; j < intLength; j++)
  196.      {
  197.          mult = 1;
  198.          for (k = 0; k < 4; k++)
  199.          {
  200.              sum += str[4*j+k] * mult;
  201.              mult *= 256;
  202.          }
  203.      }
  204.      mult = 1;
  205.      for (k = intLength*4; k < len ; k++)
  206.      {
  207.          sum += str[k] * mult;
  208.          mult *= 256;
  209.      }
  210.  
  211.      return sum%10007;
  212. }
  213.  
  214. int getIndex(char *name)
  215. {
  216.     int key=hashValue(name),total=v[key].size();
  217.     int i;
  218.     for(i=0;i<total;i++)
  219.         if(!strcmp(name,people[v[key][i]]))
  220.             return v[key][i];
  221.     return -1;
  222. }
Add Comment
Please, Sign In to add comment