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 ...
随机推荐
-
mysql myisam简单分表设计
一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来详细说说分表的一些方法.目前我所知道的方法都是MYISAM的,INNODB如何做分表并且保留事务和外键,我还不是 ...
-
uwsgi出现invalid request block size: 21573 (max 4096)...skip解决办法
buffer-size uwsgi内部解析的数据包大小,默认4k. 如果准备接收大请求,你可以增长到64k. 允许uwsgi接收到32k,更大的会被丢弃. xweb.ini [uwsgi]socket ...
-
[手机取证] 绕过屏幕锁定启用调试模式-For Android 4.4.2
Google在Android 4.x中引入了调试信任机制,类似于iOS,在设备有屏幕密码的情况下首次连接(或未记住计算机)的情况下, 需要首先打开屏幕锁定后才可进行调试启用操作. 在Android 4 ...
-
CF#335 Freelancer&#39;s Dreams
Freelancer's Dreams time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
-
转 Flash与PS交互动画
FLASH是可以点击体验的,不是图片哦. UI中国不能上传flash,但是站酷可以,UI中国的就下载载附件看看吧 本人学生党兼网页设计师菜鸟一名,因为无聊练习做了个FLASH的交互 所以很多学弟学妹们 ...
-
Tomcat中部署WEB项目的四种方法
对Tomcat部署web应用的方式总结,常见的有以下四种: 1.[使用控制台部署] 访问Http://localhost:8080,并通过Tomcat Manager登录,进入部署界面即可. 2.[利 ...
-
关于npoi不能导入07以上版本的Excel
因为在npoi版本中1.几之下的版本的不能导入07版本以上的excel是因为在其中的封装的方法中不支持, 所以很简单我们去网站上下一新的版本的npoi就是了,最好是新出来的2.1.1版本的就完全zhi ...
-
c 存储类型
1,c语言中的存储类型(定义变量和函数的可见范围和生命周期)这些说明符放置在它们所修饰的类型之前.下面列出 C 程序中可用的存储类: auto register static extern 2,aut ...
-
tomcat部署jfinal项目
1:创建一个目录: /var/www 2:为将要部署的项目创建一个目录, /var/www/my_project 3:将项目打成 war 包, 然后解压到 /var/www/my_project ...
-
P1091 合唱队形 DP 最长升序列维护
题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2,…,K,他 ...