Information Sharing zoj3641 并查集
2015-08-03 14:14
330 查看
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 Namema1a2 ..am”, means the kid called Name arrives at the classroom. He has m information, their id is a1a2 …am.
2: “share Name1Name2”, 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
425
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
(1)题意:给出n个操作,操作有的三种:
①添加一个人,这个人携带了一些信息。
②分享某两个人的信息。
③查询某个人所拥有的不同的信息的个数。
输出每次查询一个人时他手里的信息个数。
(2)解法:其实就是个并查集,用个set来记录每个人所拥有的信息,当两个人分享信息时,将这两个人合并,把一个人的信息都添加到另一个人上面,然后把这个拥有的信息删除。注意:若A与B分享后,B在与C分享。此时A也知道了C的信息。
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 Namema1a2 ..am”, means the kid called Name arrives at the classroom. He has m information, their id is a1a2 …am.
2: “share Name1Name2”, 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
425
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
(1)题意:给出n个操作,操作有的三种:
①添加一个人,这个人携带了一些信息。
②分享某两个人的信息。
③查询某个人所拥有的不同的信息的个数。
输出每次查询一个人时他手里的信息个数。
(2)解法:其实就是个并查集,用个set来记录每个人所拥有的信息,当两个人分享信息时,将这两个人合并,把一个人的信息都添加到另一个人上面,然后把这个拥有的信息删除。注意:若A与B分享后,B在与C分享。此时A也知道了C的信息。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<set> #include<cmath> #include<vector> using namespace std; int pa[100005]; set<int>you[100005]; //第几个拥有的数字 set<int>:: iterator it; map<string,int>mp; char str[20],name1[30],name2[30]; int Find(int x) { if(x==pa[x]) return x; int y=Find(pa[x]); it=you[x].begin(); while(it!=you[x].end()) { int val=*it; ++it; if(you[y].count(val)) continue; you[y].insert(val); } you[x].clear(); return pa[x]=y; } void Uion(int x,int y) { int a=Find(x); int b=Find(y); if(a!=b) { pa[b]=a; Find(b); } } int main() { int n; while(scanf("%d",&n)!=EOF) { mp.clear(); //清空map int cnt=0; while(n--) { scanf("%s",str); if(strcmp(str,"arrive")==0) { scanf("%s",name1); mp[string(name1)]=++cnt; //把名字对应为一个数字 you[cnt].clear(); //把他拥有的set清空 pa[cnt]=cnt; int m,v; scanf("%d",&m); while(m--) { scanf("%d",&v); you[cnt].insert(v); //把输入的数字全部加到you[cnt]的set中 } } else if(strcmp(str,"share")==0) { scanf("%s%s",name1,name2); int x=mp[string(name1)]; //对应的数字 int y=mp[string(name2)]; Uion(x,y); } else { scanf("%s",name1); int x=Find(mp[string(name1)]); printf("%d\n",you[x].size()); //把拥有的容器中的个数读出来 } } } return 0; }
相关文章推荐
- Splash的实现
- 让div垂直居中的5种方法
- IOS第六天(2:10秒倒计时)
- 编译器的工作过程
- Food Delivery zoj3469 区间dp
- 实习要用到前端知识了
- aaencode - A Xss Code
- 进程与线程关系的形象解释
- nginx限制某个IP同一时间段的访问次数
- 通过PackageManager得到版本信息
- linux 无网安装软件心得
- hbase单机版初步了解
- Hibernate 中出现 users is not mapped 问题
- Eclipse4.5 mars 配置Velocity插件
- find 命令概览
- 基础总结篇之二:Activity的四种launchMode
- Cell上添加UITextField出现的问题解决
- 电商商家后台-easyUI的combox三级联动
- quick2.2与quick3.3的区别(2)
- 基于jQuery 3D旋转明星人物展示特效