UVALive 6910 Cutting Tree(并查集应用)
总体来说,这个题给的时间比较长,样例也是比较弱的,别的方法也能做出来。
我第一次使用的是不合并路径的并查集,几乎是一种暴力,花了600多MS,感觉还是不太好的,发现AC的人很多都在300MS之内的过得。
看到他们的做法后,我知道了这个题比较好的做法。
逆向思维的并查集,因为这里只有去边操作,完全可以离线计算,把删边当成加边,然后逆序输出答案即可。
但是,这个却有一个坑,很坑,只有第一次删除的时候,我们才对他进行操作,加边的时候也只能在第一次删除的时候加。当时因为这里,十分困惑……
这是我无路径压缩的并查集的做法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define N 20005
int n,fa[N];
int Find(int x){
while(x != fa[x]){
x = fa[x];
}
return x;
}
int main() {
// freopen("E.in.cpp","r",stdin);
int T,k,ca=,p,a,b;
scanf("%d",&T);
char op[];
while(T--) {
scanf("%d%d",&n,&k);
for(int i = ; i <= n; i++) {
fa[i] = i;
}
for(int i = ; i <= n; i++) {
scanf("%d",&p);
if(p == ) fa[i] = i;
else {
fa[i] = p;
}
}
printf("Case #%d:\n",++ca);
while(k--) {
scanf("%s",op);
if(op[] == 'C') {
scanf("%d",&b);
if(fa[b] == b) continue;
fa[b] = b;
} else {
scanf("%d%d",&a,&b);
if(Find(a) != Find(b)) {
printf("NO\n");
} else printf("YES\n");
}
}
}
return ;
}
这是逆序离线并查集的做法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int N = 2e4+;
int n,fa[N],pa[N],vis[N];
string ans[];
struct Query {
int a,b,kind;
void Set(int A,int B,int k) {
a = A;
b = B;
kind = k;
}
} q[];
int Find(int x) {
return fa[x]==x? x :fa[x]=Find(fa[x]);
}
void Union(int a,int b) {
fa[Find(b)] = Find(a);
}
int main() {
// freopen("E.in.cpp","r",stdin);
int T,k,ca=;
char op[];
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&k);
for(int i = ; i <= n; i++) {
scanf("%d",&pa[i]);
fa[i] = i;
vis[i] = ;
}
printf("Case #%d:\n",++ca);
int a,b;
for(int i = ; i < k; i++) {
scanf("%s",op);
if(op[] == 'C') {
scanf("%d",&a);
if(pa[a]!= && vis[a] != ) q[i].Set(a,,);
else q[i].Set(a,,-);
vis[a] = ;
} else {
scanf("%d%d",&a,&b);
q[i].Set(a,b,);
}
}
for(int i = ; i <= n; i++) {
if(vis[i]== || pa[i]==) continue;
Union(pa[i],i);
}
int cnt = ;
for(int i = k-; i >= ; i--) {
if(q[i].kind == -) continue;
else if(q[i].kind == ) {
if(Find(q[i].a) == Find(q[i].b)) ans[cnt++] = "YES";
else ans[cnt++] = "NO";
} else Union(pa[q[i].a],q[i].a);
}
for(int i = cnt-; i >= ; i--) {
cout<<ans[i]<<endl;
}
}
return ;
}
UVALive 6910 Cutting Tree(并查集应用)的更多相关文章
- UVALive 6910 Cutting Tree 并查集
Cutting Tree 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- uva 6910 - Cutting Tree 并查集的删边操作,逆序
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- UVALive 6910 Cutting Tree(离线逆序并查集)
[题目]:(地址:) http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97671#problem/E [题意]: 给出多棵树和两类操作:操作 ...
- 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 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- [Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...
- 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 ...
随机推荐
- 在 ML2 中配置 OVS vlan network - 每天5分钟玩转 OpenStack(136)
前面我们已经学习了 OVS 的 local 网络 和 falt 网络,今天开始讨论 vlan 网络. vlan network 是带 tag 的网络. 在 Open vSwitch 实现方式下,不同 ...
- Hiberbate中的一对多关联查询
Hibernate中一对多关系的应用 案例:一个人可以拥有多辆小轿车 目的:通过hibernate的相关配置,利用HQL语句成功的查询出某人拥有某些车辆 1. 项目结构 2. domain类的创建详情 ...
- YC(Y Combinator)斯坦福大学《如何创业》课程要点记录(粗糙)
20节课程,每节都是干货满满,时常听说理论无用,但是好的理论,绝对能帮助你少走一些弯路. YC简介: Y Combinator成立于2005年,是美国著名创业孵化器,Y Combinator扶持初创企 ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第五章:排序、分页和路由
本章的重点是对产品信息增加排序和分页的功能,以及使用ASP.NET Routing特性添加更加友好的URL支持. 注意:如果你想按照本章的代码编写示例,你必须完成第四章或者直接从www.apress. ...
- C语言之自增和自减运算符
一 自增和自减 自增(++):就是给自己的值再加1 自减(--):就是给自己的值减1 tips: ++(--)可以放在前面,也可以放在后面,效果都是一样,都是会给自身+1(-1) 前缀++(--):会 ...
- shapeless官方指南翻译写在前面
目录 前言 Shapeless简介 The Type Astronaut's Guide to Shapeless简介 总结 一.前言 在我的2016,感恩.乐观.努力一文中,说2017 ...
- Zookeeper 安装和配置
单机模式 下载zookeeper的安装包之后, 解压到合适目录. 进入zookeeper目录下的conf子目录, 创建zoo.cfg: tickTime=2000 dataDir=/Users/app ...
- 获取select选中的值
$("#CalibrationYear option:selected").text();
- jQuery的css
1.css(name|pro|[,val|fn]) 访问匹配元素的样式属性. 参数name 描述: 取得第一个段落的color样式属性的值. $("p").css("co ...
- Ubuntu 14.04—Eclipse配置Pydev
Eclipse: 1. 下载 Eclipse 最新版 访问官方网站下载 Eclipse 最新版,这个就不多说了,大家自己去下. http://www.eclipse.org/downloads/?o ...