【SPOJ-DYNACON1】Dynamic Tree Connectivity【LCT/并查集】
来源:互联网 发布:江西省软件行业办 编辑:程序博客网 时间:2024/06/09 13:47
题意:
和BZOJ2049一样。
换了各种LCT姿势,一直TLE。
后来发现可以用并查集...
参考了Seter聚聚的blog:http://seter.is-programmer.com/posts/29428.html
BZOJ2049:http://blog.csdn.net/braketbn/article/details/50732912
#include <cstdio>#include <algorithm>using namespace std;const int maxn = 100005;int n, m, pre[maxn];inline int iread() {int f = 1, x = 0; char ch = getchar();for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';return f * x;}inline void makeroot(int x) {for(int y = 0, fa = pre[x]; x; fa = pre[x = fa]) {pre[x] = y;y = x;}}inline bool check(int a, int b) {for(; a != b && b; b = pre[b]);return a == b;}int main() {n = iread(); m = iread();while(m--) {char ch = getchar(); for(; ch != 'a' && ch != 'r' && ch != 'c'; ch = getchar());int a = iread(), b = iread();makeroot(a);if(ch == 'a') pre[a] = b;else if(ch == 'r') pre[b] = 0;else printf(check(a, b) ? "YES\n" : "NO\n");}return 0;}
0 0
- 【SPOJ-DYNACON1】Dynamic Tree Connectivity【LCT/并查集】
- SPOJ-PT07Y Is it a tree DFS/并查集
- SPOJ IAPCR2F 【并查集】
- 2959: 长跑|LCT+并查集
- [BZOJ2959]长跑(lct+并查集)
- 【bzoj3669】魔法森林 LCT+并查集
- BZOJ 3069 [LCT][并查集]
- BZOJ 2959: 长跑(LCT+并查集)
- bzoj 2959: 长跑 lct+并查集
- SPOJ QTREE Query on a tree (lct)
- Graph Connectivity UVA, 459(并查集)
- poj2838 Graph Connectivity 邻接表加上并查集
- ARC 065D Connectivity 并查集+map
- Tree(并查集)
- LCT+最小生成树+并查集+离线(BZOJ2594)
- [BZOJ2049]SDOI2008洞穴勘测|LCT|并查集
- BZOJ 4025|二分图|CDQ分治|并查集|LCT
- [CDQ分治 并查集 || LCT] BZOJ 4025 二分图
- GDKOI2016总结
- java反射机制详解
- 在MacBook上安装Android Studio
- 工作技巧总结(进行中...)
- 浏览器缓存-控制刷新-cdn问题
- 【SPOJ-DYNACON1】Dynamic Tree Connectivity【LCT/并查集】
- SAM
- 三个启蒙寓言解读大企业的内部创业困局
- C++11 右值引用
- startActivity构成的Activity链中间断掉会如何?
- 使用MPFR总结
- “知天命”是什么意思?
- 【bzoj3172】[Tjoi2013]单词 AC自动机+fail树
- PAT-1021 个位数统计