栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3641 Information Sharing

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3641 Information Sharing

#include<cstdio>#include<cstring>#include<string>#include<cstdlib>#include<queue>#include<stack>#include<map>#include<set>#include<vector>#include<iostream>#include<ctime>#include<algorithm>#include<cmath>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define LL long long#define inf (1<<30)#define PI acos(-1)#define modn 1000000007#define maxn 100000+10#define Mod 3#define type 1#define eps 1e-7#define zero(x) fabs(x)<eps#define equal(x,y) zero(x-y)using namespace std;char op[10];map<string,int> hash;map<int,set<int> > ans;set<int>::iterator cao;char tmp[20],tmp1[20],tmp2[20];int fa[maxn],rank[maxn];void init(){ int i,j; for(i=1;i<=100000;i++) { fa[i]=i; rank[i]=0; }}int getfa(int x){ if(x!=fa[x]) { fa[x]=getfa(fa[x]); } return fa[x];}void Union(int a,int b){ int f1=getfa(a); int f2=getfa(b); if(f1!=f2) { if(rank[f1]>rank[f2]) { fa[f2]=f1; for(cao=ans[f2].begin();cao!=ans[f2].end();cao++) { ans[f1].insert(*cao); } } else { fa[f1]=f2; for(cao=ans[f1].begin();cao!=ans[f1].end();cao++) { ans[f2].insert(*cao); } if(rank[f1]==rank[f2])++rank[f2]; } }}int main(){ int tol,n,num,a; int i,j; while(scanf("%d",&n)!=EOF) { hash.clear(); ans.clear(); init(); tol=0; while(n--) { scanf("%s",op); if(op[0]=='c') { scanf("%s",tmp); string name(tmp); int laozi=getfa(hash[name]); printf("%dn",ans[laozi].size()); } else if(op[0]=='a') { scanf("%s",tmp); string name(tmp); if(hash.find(name)==hash.end()) hash[name]=++tol; scanf("%d",&num); for(i=1;i<=num;i++) { scanf("%d",&a); ans[hash[name]].insert(a); } } else if(op[0]=='s') { scanf("%s",tmp1); string name1(tmp1); scanf("%s",tmp2); string name2(tmp2); if(hash.find(name1)==hash.end()) hash[name1]=++tol; if(hash.find(name2)==hash.end()) hash[name2]=++tol; Union(hash[name1],hash[name2]); } } } return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376518.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号