zoj 3641 并查集+set
来源:互联网 发布:手机淘宝店铺店招尺寸 编辑:程序博客网 时间:2024/05/16 10:38
然后操作有合并他们的信息 查询他们的信息
解法:直接并查集 然后信息用set搞一下,一直在想用set_union好还是直接写在find函数里面好(当然这个也不是很好)最后还是set直接暴力合并了 因为信息是可能重复的 这里的信息最多也就是100w 然后有一个优化就是合并的时候判断集合的大小 总是把小的并到大的上去 这样子就可以知道最多合并也就是100w了(当然我在代码里没有采用,因为感觉自己MMD)
#include<stdio.h>#include<cstdio>#include<iostream>#include<map>#include<set>using namespace std;set<int>::iterator it;char op[111],name[111],ano[111];int f[111111],n,m,cnt,x;int id,anoid;int find(int x){ return f[x]==x?x:f[x]=find(f[x]);}int main(){ map<string,int>df; set<int>s[111111]; int _; while(~scanf("%d",&_)){ df.clear(); cnt=0; for(int i=1;i<=_;++i)f[i]=i,s[i].clear(); while(_--){ scanf("%s%s",op,name); if(*op=='c'){ id=find(df[name]); printf("%d\n",(int)s[id].size()); }else if(*op=='s'){ scanf("%s",ano); id=df[name];anoid=df[ano]; id=find(id);anoid=find(anoid); if(id==anoid)continue; while(!s[anoid].empty()){ it=s[anoid].begin(); s[id].insert(*it); s[anoid].erase(it); } f[anoid]=id; }else if(*op=='a'){ df[name]=++cnt; scanf("%d",&m); while(m--){ scanf("%d",&x); s[cnt].insert(x); } } } } return 0;}
0 0
- zoj 3641 并查集+set
- ZOJ 3641 Information Sharing(并查集 + set + map)
- zoj 3641 Information Sharing(set+并查集)
- ZOJ 3641Information Sharing (并查集 + set)
- zoj 3641 并查集
- zoj 3641 并查集
- zoj 3641 Information Sharing(并查集)
- ZOJ:3641Information Sharing(并查集)
- ZOJ 3641 Information Sharing 并查集
- zoj 3659 并查集
- zoj 3789 并查集
- zoj Friendship 并查集
- zoj 3659 并查集
- Zoj 3811并查集
- ZOJ 3789 并查集
- ZOJ Problem Set - 3261 Connections in Galaxy War(并查集)
- ZOJ 3521 Fairy Wars(并查集+扫描线+set)
- 【并查集】集合 set
- poj2063 Investment
- 一张图搞懂分布式大型网站的前世今生
- android动画
- POJ2263&ZOJ1952--Heavy Cargo【Floyd】多源最短路变形
- 使用Grunt进行自动任务处理的相关教程
- zoj 3641 并查集+set
- Matlab中legend函数功能
- Verilog-FPGA硬件电路设计之一——if语句优先级问题
- 【JAVA】多线程的应用
- jQuery增加addClass、删除removeClass、toggleClass
- WP -- 应用版本升级提示
- 新站优化的注意事项
- JAXB
- 1.5-替换字符串中的空格为%20