UVALive 6910 Cutting Tree(离线逆序并查集)
【题目】:(地址:)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97671#problem/E
【题意】:
给出多棵树和两类操作:操作(C x)删除结点 x 与其父结点的连边;操作(Q a b)询问 a b 是否连通。
【解题思路】:
连通性的查询容易想到用并查集,关键在于如何处理删边。
考虑到删边的难点在于查询时的路径压缩导致某些结点与其父结点"不直接相连",这里使用离线处理,在查询之前把所有该删的边删除,同时逆序处理询问操作;当逆序处理到删边操作时,复原删掉的边(删除变为增边)。
【代码】:(上了个比较标准的并查集模板)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<algorithm>
#define LL long long
#define maxn 25000
#define IN freopen("in.txt","r",stdin);
using namespace std; struct Union_Find_Set{ int fa[maxn]; /*每个结点的父亲节点编号*/
int rank[maxn]; /*树的高度*/ /*构造并查集并初始化*/
void make_set()
{
for(int i=; i<maxn; i++){
fa[i] = i; /*初始时本身构成一个集合,根为本身*/
rank[i] = ;
}
} /*递归查找结点所在树的根节点*/
int find_set(int x)
{
/*路径压缩*/
return x!=fa[x]? fa[x]=find_set(fa[x]) : x;
} /*合并两个集合*/
void unite_set(int x, int y)
{
x = find_set(x);
y = find_set(y);
/*记录树的高度防止合并后退化,rank小的向rank大的连接*/
if(rank[x] < rank[y]) swap(x,y);
fa[y] = x; /*合并*/
if(rank[x] == rank[y]) rank[x]++; /*高度相同则加1*/
} /*判断两结点是否属于同一集合*/
bool same_set(int x, int y)
{
return find_set(x) == find_set(y);
}
}UFS; int n,q;
struct node{
char type;
int first, second;
}; stack<node> s;
stack<bool> ans; int main(int argc, char const *argv[])
{
//IN; int t,ca=;scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&q); while(!s.empty()) s.pop();
while(!ans.empty()) ans.pop();
UFS.make_set(); for(int i=; i<=n; i++){
scanf("%d",&UFS.fa[i]);
if(!UFS.fa[i]) UFS.fa[i] = i;
} for(int i=; i<=q; i++)
{
node tmp_node;
getchar();
scanf("%c",&tmp_node.type);
if(tmp_node.type=='Q'){
scanf("%d %d",&tmp_node.first, &tmp_node.second);
}
else{
scanf("%d",&tmp_node.first);
tmp_node.second = UFS.fa[tmp_node.first];
/*离线处理--询问之前删边,避免路径压缩导致删边失效*/
UFS.fa[tmp_node.first] = tmp_node.first;
}
s.push(tmp_node);
} while(q--)
{
node tmp_node = s.top(); s.pop(); if(tmp_node.type=='Q'){
if(UFS.same_set(tmp_node.first, tmp_node.second)) ans.push();
else ans.push();
}
else{
UFS.fa[tmp_node.first] = tmp_node.second;
}
} printf("Case #%d:\n", ca++);
while(!ans.empty())
{
if(ans.top() == ) puts("YES");
else puts("NO");
ans.pop();
}
} return ;
}
UVALive 6910 Cutting Tree(离线逆序并查集)的更多相关文章
- UVALive - 6910 (离线逆序并查集)
题意:给处编号从1~n这n个节点的父节点,得到含有若干棵树的森林:然后再给出k个操作,分两种'C x'是将节点x与其父节点所连接的支剪短:'Q a b'是询问a和b是否在同一棵树中. 题解:一开始拿到 ...
- UVALive 6910 Cutting Tree(并查集应用)
总体来说,这个题给的时间比较长,样例也是比较弱的,别的方法也能做出来. 我第一次使用的是不合并路径的并查集,几乎是一种暴力,花了600多MS,感觉还是不太好的,发现AC的人很多都在300MS之内的过得 ...
- UVALive 6910 Cutting Tree 并查集
Cutting Tree 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- ZOJ-3261 Connections in Galaxy War---离线操作+逆序并查集
题目链接: https://cn.vjudge.net/problem/ZOJ-3261 题目大意: 给你一些点,还有一些边,每个点上都有一个权值,然后有一些询问,分为两种,query a 询问与a直 ...
- Connections in Galaxy War ZOJ - 3261 离线操作+逆序并查集 并查集删边
#include<iostream> #include<cstring> #include<stdio.h> #include<map> #includ ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- LCA(最近公共祖先)离线算法Tarjan+并查集
本文来自:http://www.cnblogs.com/Findxiaoxun/p/3428516.html 写得很好,一看就懂了. 在这里就复制了一份. LCA问题: 给出一棵有根树T,对于任意两个 ...
- Tree Restoration Gym - 101755F (并查集)
There is a tree of n vertices. For each vertex a list of all its successors is known (not only direc ...
随机推荐
- HDFS小文件处理——Mapper处理
处理小文件的时候,可以通过org.apache.hadoop.io.SequenceFile.Writer类将所有文件写出到一个seq文件中. 大致流程如下: 实现代码: package study. ...
- 转:java提取图片中的像素
本文转自:http://www.infosys.tuwien.ac.at/teaching/courses/WebEngineering/References/java/docs/api/java/a ...
- ViewPager介绍和使用说明
1 ViewPager实现的功能 和实际运行的效果图示意 ViewPager类提供了多界面切换的新效果.新效果有如下特征: [1] 当前显示一组界面中的其中一个界面. [2] 当用户通过左右滑动界 ...
- svn: E230001: Server SSL certificate verification failed
TortoiseSvn是好的 命令行svn 的时候 有问题 ,也加了--no-auth-cache --non-interactive参数 svn list 地址 选下p 就好. http://sta ...
- UVa 12096 The SetStack Computer【STL】
题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...
- listagg 函数
listagg 函数--oracle 11g release 2 转载:http://xpchild.blog.163.com/blog/static/10180985920108485721969/ ...
- UVA 10765 Doves and bombs(双连通分量)
题意:在一个无向连通图上,求任意删除一个点,余下连通块的个数. 对于一个非割顶的点,删除之后,原图仍连通,即余下连通块个数为1:对于割顶,余下连通块个数>=2. 由于是用dfs查找双连通分量,树 ...
- (转)在mac上配置cocos2d-x开发环境
转自:http://www.cnblogs.com/xiaodao/archive/2013/01/08/2850751.html 一.下载cocos2d-x最新安装包 在终端中cd到本地将要存放目录 ...
- dict 字典
Python 学习笔记[dict的操作方法] Python中dict详解
- webapp调试工具weinre的使用
在设计师与前端开发人员的努力下,一个WebApp出炉了,可是测试人员说了一堆的问题:某某机型下页面表现不一致,某某系统下页面如何如何,某某 系统浏览器下页面怎么怎么滴.看着满满的测试汇总文档,我们曾经 ...