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. Mac使用小结

    1.修改pip的镜像地址及更新pip https://www.cnblogs.com/techroad4ca/p/5922389.html 2.更新python的库,比如更新six sudo pip ...

  2. 网页 H5“线条” 特效实现方式(canvas-nest)

    先上图 (看博客空白处也可以呦): 前一阵浏览网站的时候,发现了这个好玩的东西,一直想找找怎么实现的,今天忙里偷闲,上网搜了一下,发现实现起来特别简单. 只需要在网页body里引入一个<scri ...

  3. MapReduce算法形式六:只有Map独自作战

    案例六:Map独自直接输出 之前一直没有用过这个map独自输出的模式,就算是输出一些简单的我也会经过一次reduce输出,但是,发现这个map输出的结果跟我预想的有点不一样,我一直以为shuffle的 ...

  4. 【酷Q&C++】如何利用酷Q制作一个机器人?

    ——酷Q Air教程 要想阅读本篇教程,读者需要基本的C++知识以及Visual Studio 2010/2015/2017 首先,需要从官方网站下载软件 https://cqp.cc/t/23253 ...

  5. C语言操作SQLite数据库

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

  6. Java面试手写代码题

    1.栈实现 2.Iterator实现 3.单例 4.多线和控制(暂停,恢复,停止) 5.生产者消费者

  7. Vue源码探究-源码文件组织

    Vue源码探究-源码文件组织 源码探究基于最新开发分支,当前发布版本为v2.5.17-beta.0 Vue 2.0版本的大整改不仅在于使用功能上的优化和调整,整个代码库也发生了天翻地覆的重组.可见随着 ...

  8. ORACLE 创建表空间及用户

    /*创建存放原始数据的表空间*/ create tablespace Djzh_original datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\Djzh_or ...

  9. jstl标签: c:Foreach详解

    为循环控制,它可以将集合(Collection)中的成员循序浏览一遍.运作方式为当条件符合时,就会持续重复执行的本体内容. 为循环控制,它可以将集合(Collection)中的成员循序浏览一遍.运作方 ...

  10. 「网络流24题」「Codevs1237」 餐巾计划问题

    1237 餐巾计划问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond   题目描述 Description 一个餐厅在相继的 N 天里,每天需用的餐巾数不尽相 ...