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. HDU 6096 String 排序 + 线段树 + 扫描线

    String Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Problem De ...

  2. HBase在滴滴出行的应用场景和最佳实践

    摘要: 主要介绍了HBase和Phoenix在滴滴内部的一些典型案例.文章已在CSDN极客头条和<程序员>杂志发表,应朋友邀请,分享到云栖社区,希望给大家带来启发和帮助. 背景 对接业务类 ...

  3. Service-level agreement

    Service-level agreement - Wikipedia https://en.wikipedia.org/wiki/Service-level_agreement

  4. C语言操作SQLite数据库

    SQLite头文件和源文件下载地址http://www.sqlite.org/download.html 以下是示例代码 #include <stdio.h> #include " ...

  5. Deep Learning 33:读论文“Densely Connected Convolutional Networks”-------DenseNet 简单理解

    一.读前说明 1.论文"Densely Connected Convolutional Networks"是现在为止效果最好的CNN架构,比Resnet还好,有必要学习一下它为什么 ...

  6. iOS UI控件之间的关系图

  7. Hihocoder #1014 : Trie树 (字典数树统计前缀的出现次数 *【模板】 基于指针结构体实现 )

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...

  8. YTU 2456: 评委打分

    2456: 评委打分 时间限制: 1 Sec  内存限制: 128 MB 提交: 283  解决: 52 题目描述  一个歌唱比赛,比赛每次会从观众中随即抽取几名观众给分(观众至少有5个,分数为0~1 ...

  9. JavaScript SHA-1

    1. [文件] webtoolkit.sha1.js ~ 4KB     /****  Secure Hash Algorithm (SHA1)*  http://www.huiyi8.com/css ...

  10. 「LuoguP4753」濑 River Jumping(贪心

    Description 有一条宽度为 N 的河上,小D位于坐标为 0 的河岸上,他想到达坐标为 N 的河岸上后再回到坐标为 0 的位置.在到达坐标为 N 的河岸之前小D只能向坐标更大的位置跳跃,在到达 ...