hdu 3172 Virtual Friends

http://acm.hdu.edu.cn/showproblem.php?pid=3172

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <string>

 4 #include <map>

 5 #include <algorithm>

 6 #define maxn 200010

 7 using namespace std;

 8 

 9 int f[maxn],num[maxn];

10 int n;

11 int ans;

12 

13 int find1(int x)

14 {

15     if(x==f[x]) return x;

16     return f[x]=find1(f[x]);

17 }

18 

19 void merge1(int a,int b)

20 {

21     int fa=find1(a);

22     int fb=find1(b);

23     if(fa!=fb)

24     {

25         f[fa]=fb;

26         num[fb]+=num[fa];

27     }

28 }

29 

30 void inti()

31 {

32     for(int i=1; i<maxn; i++)

33     {

34         f[i]=i;

35         num[i]=1;

36     }

37 }

38 

39 int main()

40 {

41     int t;

42     while(scanf("%d",&t)!=EOF)

43     {

44         while(t--)

45         {

46             inti();

47             char str1[maxn],str2[maxn];

48             map<string,int>q;

49             scanf("%d",&n);

50             ans=1;

51             for(int i=0; i<n; i++)

52             {

53                 scanf("%s %s",str1,str2);

54                 if(q[str1]==0) q[str1]=ans++;

55                 if(q[str2]==0) q[str2]=ans++;

56                 //printf("####%d %d\n",q[str1],q[str2]);

57                 merge1(q[str1],q[str2]);

58                 int f1=find1(q[str1]);

59                 printf("%d\n",num[f1]);

60             }

61         }

62     }

63     return 0;

64 }
View Code

 

你可能感兴趣的:(virtual)