Cutting Tree

题目连接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4922

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):

  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 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.

  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

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 并查集的更多相关文章

  1. uva 6910 - Cutting Tree 并查集的删边操作,逆序

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  2. UVALive 6910 Cutting Tree(离线逆序并查集)

    [题目]:(地址:) http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97671#problem/E [题意]: 给出多棵树和两类操作:操作 ...

  3. UVALive 6910 Cutting Tree(并查集应用)

    总体来说,这个题给的时间比较长,样例也是比较弱的,别的方法也能做出来. 我第一次使用的是不合并路径的并查集,几乎是一种暴力,花了600多MS,感觉还是不太好的,发现AC的人很多都在300MS之内的过得 ...

  4. 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 ...

  5. Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

  6. CF109 C. Lucky Tree 并查集

    Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...

  7. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  8. [Swust OJ 856]--Huge Tree(并查集)

    题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...

  9. 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 ...

随机推荐

  1. 关于Cookie跨域的问题

    Cookie是一个伟大的发明,它允许Web开发者保留他们的用户的登录状态.但是当你的站点有一个以上的域名时就会出现问题了.在Cookie规范上说,一个cookie只能用于一个域名,不能够发给其它的域名 ...

  2. python的__get__、__set__、__delete__(1)

    内容:    描述符引导        摘要        定义和介绍        描述符协议        调用描述符        样例        Properties        函数和 ...

  3. 修改input placeholder样式

    <style> /* 通用 */ ::-webkit-input-placeholder { color: rgb(235, 126, 107); } ::-moz-placeholder ...

  4. C. Ayoub and Lost Array(DP)

    (又是被队友带着上分的一场--) 题目链接:http://codeforces.com/contest/1105/problem/C 题目大意:给你n,l,r.每一个数都是在l,r范围之内,然后问你这 ...

  5. Python 入门基础7 --文件操作

    今日目录: 一.文件处理 1.什么是文件 2.为何用文件 3.如何用文件 4.文件操作 5.常用方法 6.文件内指针的移动 7.with的使用 一.文件处理 1. 什么是文件 文件是操作系统为用户/应 ...

  6. ASP.NET程序发布

    详细流程请参考文章:https://www.cnblogs.com/wangjiming/p/6286045.html 主要补充个人操作过程中遇到的问题: 1)网站发布完成后,站点下没有aspnet_ ...

  7. mybatis延迟加载——(十二)

    1.     什么是延迟加载 resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能 ...

  8. python內建模块之datetime

    from:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143193755 ...

  9. springboot---->集成mybatis开发(一)

    这里面我们介绍一下springboot与mybatis的集成,主要完成了mybatis的真分页.一个成熟的人往往发觉可以责怪的人越来越少,人人都有他的难处. springboot简单集成mytbati ...

  10. 解决spring boot JavaMailSender部分收件人错误导致发送失败的问题

    使用spring boot通常使用spring-boot-starter-mail进行邮件的发送.当进行邮件群发的话,如果一个收件人的地址错误,会导致所有邮件都发送失败.因此我们需要在邮件发送失败的时 ...