uva 6910 - Cutting Tree 并查集的删边操作,逆序
题意是给定一颗森林,然后每次都可以删除一条边,或者询问某两个点是否连通。
如果顺着做是不行的。因为并查集路径压缩了,是删不了边的,(据说不路径压缩能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 ;
}
2 1
0 0
Q 1 2
4 3
0 1 2 2
C 2
Q 2 1
C 2
2 3
0 0
Q 1 2
C 1
C 2
uva 6910 - Cutting Tree 并查集的删边操作,逆序的更多相关文章
- UVALive 6910 Cutting Tree 并查集
Cutting Tree 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- hdu4496并查集的删边操作
题意: 给你一个图,问你删除一些边后还有几个连通快.. 思路: 典型的并查集删边操作,并查集的删边就是先把不删除的边并查集一边(本题没有不删除的边),然后逆序吧所有要删除的边以 ...
- HDU 2473 - Junk-Mail Filter ,并查集的删点
Problem Description Recognizing junk mails is a tough task. The method used here consists of two ste ...
- UVA 11987 - Almost Union-Find(并查集)
UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...
- 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 6068 Classic Quotation KMP+DP
Classic Quotation Problem Description When online chatting, we can save what somebody said to form h ...
- YARN commands are invoked by the bin/yarn script.
Apache Hadoop 2.9.0 – YARN Commands http://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-sit ...
- JS中prototype,js原型扩展
作者:轩脉刃(yjf512)出处:(http://www.cnblogs.com/yjf512/)版权声明:本文的版权归作者与博客园共有.欢迎转载阅读,转载时须注明本文的详细链接. 原文 http:/ ...
- HDU 6119 小小粉丝度度熊 【预处理+尺取法】(2017"百度之星"程序设计大赛 - 初赛(B))
小小粉丝度度熊 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- uboot配置和编译过程详解【转】
本文转载自:http://blog.csdn.net/czg13548930186/article/details/53434566 uboot主Makefile分析1 1.uboot version ...
- POJ1300 Door Man —— 欧拉回路(无向图)
题目链接:http://poj.org/problem?id=1300 Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submiss ...
- 关于eclipse的resource文件没有发布到tomcat上的解决方案
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/luman1991/article/details/53457302
- Java变量和常量声明
一.变量 1.变量的定义 变量是内存中的一个存储区域,该区域有自己的名称(变量名)和类型(数据类型),Java中每个变量必须先声明,后使用 该区域的数据可以在同一类型范围内 ...
- CodeForces-380C:Sereja and Brackets(线段树与括号序列)
Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consistin ...
- [SHOI 2013] 发微博
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4419 [算法] 用std :: set维护每个人的好友集合 当两人成为好友时将每人接 ...