ZOJ3641——Information Sharing(基礎並查集)

Description

There is going to be a test in the kindergarten. Since the kids would cry if they get a low score in the test, the teacher has already told every kid some information about the test in advance.
But the kids are not satisfied with the information teacher gave. They want to get more. On the testing day, some kids arrived to the classroom early enough, and then shared his/her information with another. kids are honest, if A shares with B, B can get all the information A knows, so does A.
At first the classroom is empty. As time pass by, a kid would arrive, or share information with other. However, the teacher hides somewhere, watching everything. She wants to know how much information some kid has gotten.

Input

There are multiple cases.
The first line of each case contains an integer n, indicating there is n actions.
The following n actions contain 3 types.
1: "arrive Name m a1 a2 ..am", means the kid called Name arrives at the classroom. He has m information, their id is a1 a2 ...am.
2: "share Name1 Name2", means that the kids called Name1 and Name2 share their information. (The sharing state will keep on, that means, if A share with B, later B share with CA can also get all C's information via B. One kid may share with himself, but it doesn't mean anything.)
3: "check Name", means teacher wants to know the number of information kid called Name has got. 

n is less than 100000, and is positive. The information id is among [0,1000000].
Each Name has at most 15 characters.
There would appears at most 1000 distinct information.
Each kid carry no more than 10 information when arriving(10 is included).

Output

For every "check" statement, output a single number. If there's no check statement, don't output anything.

Sample Input

8
arrive FatSheep 3 4 7 5
arrive riversouther 2 4 1
share FatSheep riversouther
check FatSheep
arrive delta 2 10 4
check delta
share delta FatSheep
check riversouther

Sample Output

4
2
5

Hint

check 1: FatSheep has 1 4 5 7, having all the information. So answer is 4.
check 2: delta has only 4 10 , doesn't have 1 5 7. So answer is 2
check 3: riversouther has 1 4 5 7 10, having all the information. So answer is 5

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#define INF 0x7fffffff
using namespace std;

typedef long long LL;
const int N = 1e5 + 10;

map<string,int> name;
set<int> info[N];
set<int>::iterator it;
int a[N];
int tot;

int Find(int x)
{
    if(x==a[x]) return x;
    else return a[x]=Find(a[x]);
}
void Merge(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
    {
        a[fx]=fy;
        for(it=info[fx].begin();it!=info[fx].end();it++)
        {
            info[fy].insert(*it);
        }
        info[fx].clear();
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        name.clear();
        for(int i=0; i<N; i++)
        {
            info[i].clear();
            a[i]=i;
        }
        char s[10],c[100];
        tot=0;
        while(n--)
        {
            scanf("%s",s);
            if(s[0]=='a')
            {
                int x,k;
                scanf("%s%d",c,&k);
                name[c]=tot;
                while(k--)
                {
                    scanf("%d",&x);
                    info[tot].insert(x);
                }
                tot++;
            }
            else if(s[0]=='s')
            {
                char s1[30],s2[30];
                scanf("%s%s",s1,s2);
                Merge(name[s1],name[s2]);
            }
            else
            {
                scanf("%s",c);
                int ans=info[Find(name[c])].size();
                printf("%d\n",ans);
            }
        }
    }

    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章