大致题意:
有三种操作 arrive用来表示某人知道的信息;share用来表示 两个人互相交流信息;check操作的时候,输出这个人知道的信息数量,操作的数量小于100000.
大致思路:
并查集+set去重。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<set>
using namespace std;
const int nMax=1000005;
int n,father[nMax],rank[nMax],N,M,num[nMax],sum; //rank近似树的高度。
int cnt,n1;
set<int>sset[nMax];
map<string,int>map1;
int getnum(string x){
if (map1.find(x)==map1.end()){
map1.insert(make_pair(x,cnt));
cnt++;
return cnt-1;
}else return map1[x];
}
int find(int x){ // 寻找父节点
if(x!=father[x])
return father[x]=find(father[x]);
return x;
}
void setunit(int x,int y)
{
set<int>::iterator iter;
for (iter = sset[x].begin(); iter != sset[x].end(); iter++)
{
sset[y].insert(*iter);
}
// sset[x].clear();
}
void unite(int a,int b){
int x=find(a);
int y=find(b);
if(x==y)
return ;
else{
if(rank[x]>rank[y]){
father[y]=x;
setunit(y,x);
}
else if(rank[x]<rank[y]){
father[x]=y;
setunit(x,y);
}
else{
father[x]=y;
setunit(x,y);
rank[y]++;
}
}
}
void init(){ // 初始化
int i;
for(i=0; i<nMax-1; i++){
father[i]=i;
rank[i]=0;
}
n=0;
sum=N;
}
int main(){
int num,a,b,c,d,i,j,k;
while(cin>>num){
map1.clear();
for(i=1;i<=nMax;i++)sset[i].clear();
init();
cnt=1;
char act[50];
string name;
while(num--){
cin>>act;
if(act[0]=='a'){
cin>>name;
cin>>a;
b=getnum(name);
for(i=0;i<a;i++)
{
cin>>c;
sset[b].insert(c);
}
continue;
}
if(act[0]=='s'){
cin>>name;
b=getnum(name);
cin>>name;
c=getnum(name);
unite(b,c);
continue;
}
if(act[0]=='c'){
cin>>name;
b=getnum(name);
cout<<sset[find(b)].size()<<endl;
}
}
}
return 0;
}
分享到:
相关推荐
ZOJ解题报告ZOJ解题报告ZOJ解题报告ZOJ解题报告
Antique Comedians of Malidinesia would like to play a new discovered comedy of Aristofanes. Putting it on a stage should be a big surprise for the audience so all the preparations must be kept ...
zoj题目简单归类zoj题目简单归类zoj题目简单归类
acm中zoj1002的可运行C++程序
包含了zoj700多道题目的源代码,在做题时可以参考
深度搜索 回溯 int main { string s1 s2; while cin >> s1 >> s2 { count 0; cout << "[" << endl; if s1 length s2 length BackTrake s1 s2 ;... [更多]
Problem Arrangement zoj 3777
ZOJ题目答案源码
一个非常非常非常非常实用的zoj结题代码
ZOJ1805代码
zoj 1003 c语言的,要写这么多描述吗。。
学习ACM程序设计的朋友一定要看,这是训练必备的POJ ZOJ题目分类及解题思路
本代码是zoj上AC的1951的代码,把双重循环简化为O(n),不过素数判断的改进还不够
zoj1027解题指南和代码,还不错,是学校培训给的。
浙大ZOJ题目分类,可以让你更方便快速锁定那你想要联系的题目,是自己快速提高·
ZOJ题解集合-截至2835。共1244个文件,C/C++,有重复
zoj 题库 详细解答 解题代码 acm
zoj4041正确题解源代码,以及运行程序
大学ACM竞赛,ZOJ 1733 运用递归(优化)的方法。ac的代码。
zoj 1002 C语言的为什么描述要这么多字啊。。