UVALive 6910 Cutting Tree 并查集
Cutting Tree
题目连接:
Description
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 of
N 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 this
query.
• 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):
- Q 5 7 : output YES.
- C 2 : remove edge (2, 1) — the resulting forest is shown in Figure 2.
- Q 5 7 : output NO, as there is no path from node 5 to node 7 in Figure 2.
- Q 4 6 : output YES.
Input
The first line of input contains an integer T (T ≤ 50) denoting the number of cases. Each case begins
with two integers: N and K (1 ≤ N ≤ 20, 000; 1 ≤ K ≤ 5, 000) denoting the number of nodes in the
forest and the number of queries respectively. The nodes are numbered from 1 to N. The next line
contains N integers Pi (0 ≤ Pi ≤ N) denoting the parent of i-th node respectively. Pi = 0 means that
node i does not have any parent (i.e. it’s a root of a tree). You are guaranteed that the given input
corresponds to a valid forest. The next K lines represent the queries. Each query is in the form of ‘C
x’ 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 ‘Q
a b’ query in the input, output either ‘YES’ or ‘NO’ (without quotes) in a line whether there is a path
from node a to node b in the forest.
Explanation for 2nd sample case:
The initial forest is shown in Figure 3 below.
- C 3 : remove edge (3, 2) — the resulting forest is shown in Figure 4.
- Q 1 2 : output YES.
- C 1 : remove edge (1, 2) — the resulting forest is shown in Figure 5.
- Q 1 2 : output NO as there is no path from node 1 to node 2 in 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
Hint
题意
给你个森林,俩操作,1是砍掉与他父亲的连边,2是查询xy是否在同一个连通块里面
题解:
倒着做,砍边就变成连边了,然后并茶几莽一波就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e4+7;
int cas = 0;
int fa[maxn];
int e[maxn];
int flag[maxn];
int a[maxn],b[maxn],c[maxn];;
int fi(int x){
if(x==fa[x])return x;
return fa[x]=fi(fa[x]);
}
void init(){
memset(flag,0,sizeof(flag));
}
void solve(){
init();
vector<int>ans;
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
fa[i]=i;
for(int i=1;i<=n;i++)
scanf("%d",&e[i]);
for(int i=1;i<=m;i++){
string s;cin>>s;
if(s[0]=='C'){
a[i]=1;
scanf("%d",&b[i]);
flag[b[i]]++;
}else{
a[i]=0;
scanf("%d%d",&b[i],&c[i]);
}
}
for(int i=1;i<=n;i++){
if(flag[i]==0&&e[i]!=0){
fa[fi(i)]=fi(e[i]);
}
}
for(int i=m;i>=1;i--){
if(a[i]==1){
flag[b[i]]--;
if(flag[b[i]]==0&&e[b[i]]!=0)
fa[fi(b[i])]=fi(e[b[i]]);
}else{
if(fi(b[i])==fi(c[i]))ans.push_back(1);
else ans.push_back(0);
}
}
for(int i=ans.size()-1;i>=0;i--){
if(ans[i])printf("YES\n");
else printf("NO\n");
}
}
int main(){
//freopen("1.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--){
printf("Case #%d:\n",++cas);
solve();
}
return 0;
}
UVALive 6910 Cutting Tree 并查集的更多相关文章
- uva 6910 - Cutting Tree 并查集的删边操作,逆序
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 6910 Cutting Tree(离线逆序并查集)
[题目]:(地址:) http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97671#problem/E [题意]: 给出多棵树和两类操作:操作 ...
- UVALive 6910 Cutting Tree(并查集应用)
总体来说,这个题给的时间比较长,样例也是比较弱的,别的方法也能做出来. 我第一次使用的是不合并路径的并查集,几乎是一种暴力,花了600多MS,感觉还是不太好的,发现AC的人很多都在300MS之内的过得 ...
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- [Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- raphael.js 给元素 hover 添加glow() 外发光
用raphael.js 给 svg画布里面添加个元素,嗯就圓好了,男人一般都喜欢圆形的东西,比如xx , xxx , 还有xxx $(document).ready(function() { var ...
- HDU 2056 龟兔赛跑 (DP)
题意:见题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 解题报告:以前一直没看出来这题是个DP题,知道是DP题就简单了 .首先要把起点和终点看成 ...
- vue实践中的狗血事件之:mock数据引发的血坑
在项目实践中,遇到了这么一档子事 开发环境下,很快乐,什么事儿都没有,于是想打包一下测一下自动登录的效果 好家伙,一开始登录没有效,改来改去,最后连路由都切换不了, 明明开发环境下好好的,为毛打包后就 ...
- 对package.json的理解和学习
一.初步理解 1. npm安装package.json时 直接转到当前项目目录下用命令npm install 或npm install --save-dev安装即可,自动将package.json中 ...
- 【腾讯云】自己搭建的腾讯云服务器JavaEE环境
0.安装SSH登录 1.生成公钥对 ssh-keygen -t rsa -P '' -P表示密码,-P '' 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车.它在/home/ch ...
- win10下安装MinGW-w64 - for 32 and 64 bit Windows
对于不经常使用c语言的同学来说,只需要安装MinGW-w64 - for 32 and 64 bit Windows,就可以使用GCC在命令行对c源码进行编译. 首先打开命令行检查自己是否已经安装了g ...
- Linux ALSA声卡驱动之五:移动设备中的ALSA(ASoC)
转自http://blog.csdn.net/droidphone/article/details/7165482 1. ASoC的由来 ASoC--ALSA System on Chip ,是建立 ...
- 错误的理解引起的bug async await 执行顺序
今天有幸好碰到一个bug,让我知道了之前我对await async 的理解有点偏差. 错误的理解 之前我一直以为 await 后面的表达式,如果是直接返回一个具体的值就不会等待,而是继续执行asyn ...
- Cling项目demo实现Android+DLNA实现
dlna多屏互动技术在Android和ios上面应用很广,所以自己为了学习,就官方提供的远吗进行了学习. http://4thline.org/projects/cling 由于是一个maven构建的 ...
- Java 语言多态性
https://www.ibm.com/developerworks/cn/java/java-language-polymorphism/index.html 定义多态性 多态性是面向对象编程中的一 ...