ZOJ Information Sharing 【并查集+set】

  

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 C, A 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

思路:题意很好理解,当两个同学分享资源的时候,找到他们的各自集合的祖先,其实祖先集合里存的就是整个的资源,把资源合并就可以了;用set的目的是自动去重的作用; 处理同学名字的时候用map处理,相当于每个名字对应一个学号;
代码如下:
View Code
#include<string>
#include<map>
#include<set>
#include<iostream>
#include<iterator>
using namespace std;
#define N 100002
int f[N];
set<int>s[N];
int find(int x)
{
    return x==f[x]?x:f[x]=find(f[x]);
}
int main()
{
    int n, i, j, t, m, pp;
    string str1, str2, str3;
    while(cin>>n)
    {
        for(i=1; i<=n; i++)
            s[i].clear(); 
        map<string, int>M;
        t=1;
        for(i=0; i<=n; i++)
            f[i]=i;
        for(i=0; i<n; i++)
        {
            cin>>str1;
            if(str1=="arrive")
            {
                cin>>str2;
                if(M[str2]==0)
                    M[str2]=t++;
                cin>>m;
                int ttt=M[str2]; 
                for(j=1; j<=m; j++)
                { 
                    cin>>pp;
                    s[ttt].insert(pp);
                }
            }
            if(str1=="share")
            {
                cin>>str2>>str3;
                int x=M[str2], y=M[str3];
                int xx=find(x), yy=find(y);
                if(xx!=yy)
                {
                    f[yy]=xx;
                    set<int>::iterator it;
                    for(it=s[yy].begin(); it!=s[yy].end(); it++)
                        s[xx].insert(*it);
                    s[yy].clear();  //注意这里,或者将少的往多的里和并; 
                }
            }
            if(str1=="check")
            {
                cin>>str2;
                int x=M[str2];
                int xx=find(x);
                cout<<s[xx].size()<<endl;
            }
        }
    }
}

 

 

转载于:https://www.cnblogs.com/Hilda/archive/2012/08/26/2657672.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值