1116: [POI2008]CLO 思路题 并查集
来源:互联网 发布:windows 清空arp缓存 编辑:程序博客网 时间:2024/06/07 22:47
开始看错题了囧。。。
要求选一部分边变为单向边。。并不是全都要变QAQ。
这样就比较好考虑了:
如果原图没有环,我们改变边的方向只会构造出一个类似树形结构,总会有点没有入度。而如果原图有环,我们构造一个基环外向树就好啦。。。
于是并查集维护一下:
如果当前两个点在一个联通块内,那么就在根上打一个标记。否则合并的时候如果有一个联通块有标记(证明有环)则合并后的也有标记。。最后check一下是否每个联通块的根都有标记就好啦。
#include<bits/stdc++.h>using namespace std;int n,m,f[100005];bool v[100005];inline int read(){ int a=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();} return a*f;}int find(int i){ return f[i]==i?i:f[i]=find(f[i]);}int main(){ n=read(); m=read(); for (int i=1;i<=n;i++) f[i]=i; for (int i=1;i<=m;i++) { int x=read(),y=read(),p=find(x),q=find(y); if (p!=q) { f[p]=q; v[q]|=v[p]; } else v[p]=1; } for (int i=1;i<=n;i++) if (!v[find(i)]) { puts("NIE"); return 0; } puts("TAK"); return 0;}
0 0
- 1116: [POI2008]CLO 思路题 并查集
- 1116: [POI2008]CLO (并查集)
- 【并查集】【bzoj 1116】: [POI2008]CLO
- BZOJ 1116 POI2008 CLO 并查集
- BZOJ 1116 [POI2008]CLO 并查集
- BZOJ 1116: [POI2008]CLO 并查集
- BZOJ 1116: [POI2008]CLO 并查集
- POI2008 BZOJ 1116 CLO -并查集-基环树
- BZOJ1116: [POI2008]CLO 并查集
- 【BZOJ 1116】 [POI2008]CLO
- BZOJ 1116: [POI2008]CLO
- BZOJ1116 CLO [并查集]
- 1116: [POI2008]CLO|深度搜索
- BZOJ 1116: [POI2008]CLO BZOJ
- POI2008 CLO
- bzoj1116 [POI2008]CLO
- 【BZOJ1116】[POI2008]CLO【BFS】
- bzoj1116: [POI2008]CLO
- 安卓滚动数字的实现(从0开始增加到指定数字)
- PAT_B_1015. 德才论 (25) or PAT_A_1062. Talent and Virtue (25)
- SPI与ADS8345
- 在移动开发中的常见的lbs的组合
- FFmpeg源代码结构图 - 解码
- 1116: [POI2008]CLO 思路题 并查集
- 常规功能和模块自定义系统 (cfcmms)—032开发日志(用GoJS来绘制模块关系图)
- Mybatis 高级结果映射 ResultMap Association Collection
- Android studio快捷键
- 主从mysql服务器
- ios解析html
- BZOJ 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复|最大生成树kruskal
- 2429: [HAOI2006]聪明的猴子 最小生成树
- mycat 主备同步 读写分离注意事项