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 ...
随机推荐
- SQL如何获取时间的方法?
getdate():当前系统日期与时间 DATEADD(DAY,5,GETDATE()):当前日期的基础上加上x天 DATEDIFF(DAY,'2017-01-02','2017-01-13'):返回 ...
- iOS -不同模拟器字体适配
1.先建立一个UILabel的分类 导入#import <objc/runtime.h>头文件 2.在.m文件中写入如下代码 //不同设备的屏幕比例(当然倍数可以自己控制) #define ...
- 未能加载文件或程序集"Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad的真正解决办法
未能加载文件或程序集"Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3 ...
- private static
static: 静态成员,不能实例化,在你运行的时候他自己在内存中开辟了块空间,不用new, 有点像全局变量 private static 和 public static 都是静态变量,在类加载时 ...
- python爬虫---python3.5---eclipse
解析中文会出现\xbe\c8\90\hd........ 这个和你的编码选择有关.如果是解析成html,则需 fout = open('output.html', 'w',encoding='utf- ...
- Tomcat禁止外网访问
Tomcat中某个应用禁止外网访问 Tomcat中有多个应用,由于权限需要,将某一个主机禁止外网访问.在config/server.xml中设置: <Host name="172.16 ...
- CODE[VS]-寻找子串位置-字符串处理-天梯青铜
题目描述 Description 给出字符串a和字符串b,保证b是a的一个子串,请你输出b在a中第一次出现的位置. 输入描述 Input Description 仅一行包含两个字符串a和b 输出描述 ...
- sql时间转换函数--备忘
总是忘记 一.语法: CAST (expression AS data_type) 参数说明: expression:任何有效的SQServer表达式. AS:用于分隔两个参数,在AS之前的是要处理的 ...
- bitcode 关于讯飞
在真机调试的时候一直报 ld: '/Users/Chenglijuan/Documents/语音识别/lib/iflyMSC.framework/iflyMSC(IFlyRecognizerView. ...
- zabbix 布署实践【3 proxy安装】
使用openstack在生产环境创建的一台虚拟机 环境 CentOS7 4核4G内存40G硬盘 IP:10.120.150.150 镜像默认关闭防火墙,selinux ,NetworkManage ...