UVALive 6910 Cutting Tree 并查集
来源:互联网 发布:鱼鳞病网络医院 编辑:程序博客网 时间:2024/06/18 18:41
Tree in graph theory refers to any connected graph (of nodes and edges) which has no simple cycle,while forest corresponds to a collection of one or more trees. In this problem, you are given a forest ofN nodes (of rooted trees) and K queries. Each query is in the form of
:• C x : remove the edge connecting node and its parent. If node has no parent, then ignore thisquery.
• Q a b : output ‘YES’ if there is a path from node to node in the forest; otherwise, ‘NO’.
For example, let the initial forest is shown by Figure 1.Figure 1. Figure 2.
Let’s consider the following queries (in order):
1) Q 5 7 : output YES.
2) C 2 : remove edge (2, 1) — the resulting forest is shown in Figure 2.
3) Q 5 7 : output NO, as there is no path from node 5 to node 7 in Figure 2.
4) Q 4 6 : output YES.
Input
The first line of input contains an integer T (T ≤ 50) denoting the number of cases. Each case beginswith two integers: N and K (1 ≤ N ≤ 20, 000; 1 ≤ K ≤ 5, 000) denoting the number of nodes in theforest and the number of queries respectively. The nodes are numbered from 1 to N. The next linecontains N integers Pi (0 ≤ Pi ≤ N) denoting the parent of i-th node respectively. Pi = 0 means thatnode i does not have any parent (i.e. it’s a root of a tree). You are guaranteed that the given inputcorresponds to a valid forest. The next K lines represent the queries. Each query is in the form of ‘Cx’ or ‘Q a b’ (1 ≤ x, a, b ≤ N), as described in the problem statement above.
Output
For each case, output ‘Case #X:’ in a line, where X is the case number starts from 1. For each ‘Qa b’ query in the input, output either ‘YES’ or ‘NO’ (without quotes) in a line whether there is a pathfrom node a to node b in the forest.
Explanation for 2nd sample case:The initial forest is shown in Figure 3 below.1) C 3 : remove edge (3, 2) — the resulting forest is shown in Figure 4.2) Q 1 2 : output YES.3) C 1 : remove edge (1, 2) — the resulting forest is shown in Figure 5.4) Q 1 2 : output NO as there is no path from node 1 to node 2 in Figure 5.Figure 3. Figure 4. Figure 5.
Sample Input
4
7 4
0 1 1 2 2 2 3
Q 5 7
C 2
Q 5 7
Q 4 6
4 4
2 0 2 3
C 3
Q 1 2
C 1
Q 1 2
3 5
0 3 0
C 1
Q 1 2
C 3
C 1
Q 2 3
1 1
0
Q 1 1
Sample Output
Case #1:
YES
NO
YES
Case #2:
YES
NO
Case #3:
NO
YES
Case #4:
YES
Source
UESTC 2016 Summer Training #19
UVALive 6910
My Solution
简单并查集
给出一片森林, 然后执行
1)切断 x和x的父节点的边, // 查询的时候不进行路径压缩, 然后直接 father[ x] = x
2)查询 x, y 时候是相连通的 //分别找x y的根, _find(x) == _find(y)
复杂度 O(n)
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long LL;const int maxn = 2*1e5 + 8;int father[maxn], _rank[maxn], sz[maxn], trees[maxn];bool flag;inline void DisjointSet(int n){ for(int i = 0; i <= n; i++){ father[i] = i; trees[i] = 1; }}int x, y;int _find(int v, int u) //请忽略这个 _find(){ if(father[v] == u) return -1; return father[v] == v ? v : _find(father[v], u);}int _find(int v){ //if(father[v] == u) return -1; return father[v] == v ? v : _find(father[v]);}int main(){ #ifdef LOCAL freopen("a.txt", "r", stdin); //freopen("b.txt", "w", stdout); #endif // LOCAL int T, n, k, val, u, kase = 0; char op; scanf("%d", &T); while(T--){ scanf("%d%d", &n, &k); for(int i = 1; i <= n; i++){ scanf("%d", &val); if(val != 0) father[i] = val; else father[i] = i; } printf("Case #%d:\n", ++kase); while(k--){ getchar(); //!!!!!! scanf("%c", &op); if(op == 'C'){ scanf("%d", &u); father[u] = u; } else{ scanf("%d%d", &x, &y); if(_find(x) == _find(y)) printf("YES\n"); else printf("NO\n"); } } } return 0;}
Thank you!
------from ProLights
- UVALive 6910 Cutting Tree 并查集
- UVALive 6910 Cutting Tree【并查集】
- UVALive 6910Cutting Tree(并查集)
- UVALive 6910 Cutting Tree(并查集)
- UVA 6910 Cutting Tree 并查集
- Cutting Tree(并查集)
- UVALive - 3644 并查集
- UVALive 6187 并查集
- uvalive 3644(并查集)
- uvalive 3027(并查集)
- Tree(并查集)
- X-Plosives +uvalive+并查集+水题
- Corporative Network +uvalive+并查集
- UVALive 6091 - Trees (并查集)
- 【UVALive】6168 Fat Ninjas 并查集
- UVALive 3644 X-Plosives 并查集
- UVALive 6091 Trees 并查集(水
- UVALive - 3644 X-Plosives 并查集
- ollydbg 调试
- SpringMVC环境快速搭建
- Map的五种遍历方法
- 加载baidu地图报error inflating class com.baidu.mapapi.map.MapView
- 原型模式(重新思考深浅拷贝)
- UVALive 6910 Cutting Tree 并查集
- Aop Joinpoint语法
- Uva 10765 Doves and bombs (点双联通分量 + Block Forest Data Structure)
- Oracle 11g R2 DBA 操作指南(9)
- 使用ckeditor在Ajax 序列化表单提交时不能获取 textarea 的值 解决方案
- 二维码解码器Zbar+VS2012开发环境配置
- 如何打包jar
- 回溯算法
- hdu3177Crixalis's Equipment 差值排序