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

题意是给定一颗森林,然后每次都可以删除一条边,或者询问某两个点是否连通。

如果顺着做是不行的。因为并查集路径压缩了,是删不了边的,(据说不路径压缩能AC,)

所以考虑逆向操作。首先不能把已经删除了的边加进去森林里面,先处理出最终状态,然后倒着模拟,就能把删边操作等价于变成加边操作了。注意有一点坑得地方就是,多次删除某一条边,那么你只能在第一次删除这条边的地方把这条边建立起来,

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
int fa[maxn], baba[maxn];
int tofind(int u) {
if (fa[u] == u) return u;
else return fa[u] = tofind(fa[u]);
}
void tomerge(int x, int y) {
if (x == ) return;
if (y == ) while();
x = tofind(x);
y = tofind(y);
fa[y] = x;
}
struct Node {
char ch;
int a, b;
}query[maxn];
bool ans[maxn];
int del[maxn];
void init() {
for (int i = ; i <= maxn - ; ++i) fa[i] = i;
memset(del, false, sizeof del);
memset(ans, false, sizeof ans);
}
int f;
void work() {
init();
int n, q;
cin >> n >> q;
for (int i = ; i <= n; ++i) {
cin >> baba[i];
}
for (int i = ; i <= q; ++i) {
char str[];
cin >> str;
query[i].ch = str[];
if (str[] == 'C') {
cin >> query[i].a;
if (del[query[i].a]) continue;
else del[query[i].a] = i;
} else {
cin >> query[i].a >> query[i].b;
}
}
for (int i = ; i <= n; ++i) {
if (del[i]) continue;
tomerge(baba[i], i);
}
for (int i = q; i >= ; --i) {
if (query[i].ch == 'C') {
if (del[query[i].a] != i) continue;
tomerge(baba[query[i].a], query[i].a);
continue;
}
int x = tofind(query[i].a);
int y = tofind(query[i].b);
if (x == y) {
ans[i] = true;
}
}
printf("Case #%d:\n", ++f);
for (int i = ; i <= q; ++i) {
if (query[i].ch == 'C') continue;
if (ans[i]) {
printf("YES\n");
} else printf("NO\n");
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
 1 
2 1
0 0
Q 1 2
 
 1
4 3
0 1 2 2
C 2 
Q 2 1
C 2
 
 1 
2 3
0 0
Q 1 2
C 1
C 2

uva 6910 - Cutting Tree 并查集的删边操作,逆序的更多相关文章

  1. UVALive 6910 Cutting Tree 并查集

    Cutting Tree 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

  2. hdu4496并查集的删边操作

    题意:       给你一个图,问你删除一些边后还有几个连通快.. 思路:       典型的并查集删边操作,并查集的删边就是先把不删除的边并查集一边(本题没有不删除的边),然后逆序吧所有要删除的边以 ...

  3. HDU 2473 - Junk-Mail Filter ,并查集的删点

    Problem Description Recognizing junk mails is a tough task. The method used here consists of two ste ...

  4. UVA 11987 - Almost Union-Find(并查集)

    UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...

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

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

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

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

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

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

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

  9. CF109 C. Lucky Tree 并查集

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

随机推荐

  1. firefox 45 版本

    在做项目的时候,发现45版本的firefox浏览器.声明函数要放在调用者的上方.而firefox的47,48版本则没有这种情况发生.

  2. 织梦dedecms中修改标题与简略标题长度的方法

    本文介绍了dedecms中修改标题与简略标题长度的方法,进入dedecms后台,系统——系统基本参数——其他选项——文档标题最大长度——在这修改为200或更大. 一.修改标题 进入dedecms后台, ...

  3. 解决 git branch -a 无法全部显示远程的分支,只显示master分支

    新建分支 若遇到 git branch -a 无法全部显示远程的分支,只显示master分支 可以通过 git fetch 将本地远程跟踪分支进行更新,与远程分支保持一致

  4. hdu 1043 Eight(双向bfs)

    题意:经典八数码问题 思路:双向bfs ps:还有a*算法(还不会)等解法. 代码: #include<iostream> #include<stdio.h> #include ...

  5. 纯CSS画WP8界面

    我的手机是诺基亚920,13年4月份买的.工作之余,就想用css做一下WP8的界面效果,如上图所示.不做不知道,一做还挺难的.尤其是画那个QQ 的企鹅图标,太难画了.怎么画都不像. <!doct ...

  6. 【USACO】 Balanced Photo

    [题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; int i,N,ans,l1,l2; ] ...

  7. Centos7 编译安装 Nginx、MariaDB、PHP

    前言 本文主要大致介绍CentOS 7下编译安装Nginx.MariaDB.PHP.面向有Linux基础且爱好钻研的朋友.技艺不精,疏漏再所难免,还望指正. 环境简介: 系统: CentOS 7,最小 ...

  8. bzoj3998

    后缀自动机+dp 想了挺长时间 后缀自动机的状态图是一个dag,从root走到一个点的路径数代表了这个状态包含的子串,我们先预处理出来每个节点向后走能够形成多少子串,注意这里不是直接在parent树上 ...

  9. A. Vanya and Table

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  10. 2.25-2.26 MapReduce执行流程Shuffle讲解

    原文链接:https://langyu.iteye.com/blog/992916 Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方.要想理解MapReduce, Shuffle是 ...