UVALive 7456 Least Crucial Node (并查集)
Least Crucial Node
题目链接:
http://acm.hust.edu.cn/vjudge/contest/127401#problem/C
Description
http://7xjob4.com1.z0.glb.clouddn.com/e15d7d11650607e5795d28e1120d7109
Input
There are several input lines to a test case. The first line of each test case contains an integer n
(2 ≤ n ≤ 100), where n is the number of nodes (vertex set of G) labeled with {1, 2, . . . , n} in the
network. The second line contains an integer, which is the label of the unique sink of the network. The
third line contains an integer m, indicating the number of communication links in the network. The
next m lines each contains a pair of integers, say i and j (separated by a space), denoting there is a
communication link (edge in E) between node i and node j. There are at most 10 test cases and n = 0
denotes end of the test cases.
Output
The output for each instance should contain an integer denoting the least crucial node in the given
network.
Sample Input
4
4
3
1 2
2 3
3 4
6
3
8
1 2
2 3
2 4
2 5
3 4
3 5
4 5
5 6
0
Sample Output
3
2
##题意:
求标号最小的最大割点.
(删除该点后,指定点#sink能到达的点数减少最多).
##题解:
由于数据规模巨水,直接对枚举各点跑一遍并查集(或dfs)计算删除后的减少量.
(不要对源点#sink跑并查集,因为割点不能是#sink).
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 110
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;
int fa[maxn];
int _rank[maxn];
void init() {
for(int i=0; i<maxn; i++) {
fa[i] = i;
_rank[i] = 0;
}
}
int find_set(int x) {
return x==fa[x]? x:find_set(fa[x]);
}
void unit_set(int x, int y) {
x = find_set(x);
y = find_set(y);
if(_rank[x] < _rank[y]) swap(x,y);
fa[y] = x;
if(_rank[x] == _rank[y]) _rank[x]++;
}
int x[maxnmaxn], y[maxnmaxn];
int main(int argc, char const *argv[])
{
//IN;
int n;
while(scanf("%d", &n) != EOF && n)
{
int sink, m;
scanf("%d %d", &sink, &m);
int ans = 0;
int init_ans = 0;
init();
for(int i=1; i<=m; i++) {
scanf("%d %d", &x[i], &y[i]);
unit_set(x[i], y[i]);
}
for(int i=1; i<=n; i++) {
if(find_set(i) == find_set(sink)) init_ans++;
}
int p_ans = 0;
for(int i=1; i<=n; i++) if(i!=sink){
init();
for(int j=1; j<=m; j++) {
if(x[j]==i || y[j]==i) continue;
unit_set(x[j], y[j]);
}
int cnt = 0;
for(int j=1; j<=n; j++) {
if(find_set(j) == find_set(sink)) cnt++;
}
if(init_ans-cnt > ans) {
ans = init_ans - cnt;
p_ans = i;
}
}
printf("%d\n", p_ans); //a
}
return 0;
}
UVALive 7456 Least Crucial Node (并查集)的更多相关文章
- UVaLive 7456 Least Crucial Node (并查集+暴力 或者 求割点)
题意:求标号最小的最大割点.(删除该点后,指定点#sink能到达的点数减少最多). 析:由于不知道要去掉哪个结点,又因为只有100个结点,所以我们考虑用一个暴力,把所有的结点都去一次,然后用并查集去判 ...
- UVALive 7456 Least Crucial Node
题目链接 题意: 给定一个无向图,一个汇集点,问哪一个点是最关键的,如果有多个关键点,输出序号最小的那个. 因为数据量比较小,所以暴力搜索就行,每去掉一个点,寻找和汇集点相连的还剩几个点,以此确定哪个 ...
- UVALive - 6910 (离线逆序并查集)
题意:给处编号从1~n这n个节点的父节点,得到含有若干棵树的森林:然后再给出k个操作,分两种'C x'是将节点x与其父节点所连接的支剪短:'Q a b'是询问a和b是否在同一棵树中. 题解:一开始拿到 ...
- uvalive 4730王国kingdom(并查集+线段树)
题意:有T组測试数据.每组数据的N表示有N个城市,接下来的N行里每行给出每一个城市的坐标(0<=x,y<=1000000),然后有M(1<M<200000)个操作,操作有 ...
- UVALive 6910 Cutting Tree(并查集应用)
总体来说,这个题给的时间比较长,样例也是比较弱的,别的方法也能做出来. 我第一次使用的是不合并路径的并查集,几乎是一种暴力,花了600多MS,感觉还是不太好的,发现AC的人很多都在300MS之内的过得 ...
- UVALive - 5031 Graph and Queries (并查集+平衡树/线段树)
给定一个图,支持三种操作: 1.删除一条边 2.查询与x结点相连的第k大的结点 3.修改x结点的权值 解法:离线倒序操作,平衡树or线段树维护连通块中的所有结点信息,加个合并操作就行了. 感觉线段树要 ...
- POJ 1308/并查集
题目链接 /* 判断一棵树: * 1.There is exactly one node, called the root, to which no directed edges point. * 2 ...
- UVALive 6910 Cutting Tree(离线逆序并查集)
[题目]:(地址:) http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97671#problem/E [题意]: 给出多棵树和两类操作:操作 ...
- UVALive 6187 Never Wait for Weights 带权并查集
题意:每次给出每两个数之间的大小差值.在给出关系的过程中插入询问:数a和数b的差值,若不能确定,输出UNKNOWN 解法:相对大小关系的处理:并查集 1.给出两点的相对大小关系后,找到两个点的根节点, ...
随机推荐
- NuGet学习笔记
NuGet学习笔记(1)——初识NuGet及快速安装使用 NuGet学习笔记(2)——使用图形化界面打包自己的类库 NuGet学习笔记(3)——搭建属于自己的NuGet服务器
- 面试题_76_to_81_Java 最佳实践的面试问题
包含 Java 中各个部分的最佳实践,如集合,字符串,IO,多线程,错误和异常处理,设计模式等等. 76)Java 中,编写多线程程序的时候你会遵循哪些最佳实践?(答案)这是我在写Java 并发程序的 ...
- Java [Leetcode 338]Counting Bits
题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...
- POI读取Word与Excel
import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; ...
- ORACLE CLIENT客户端安装步骤详解
下载地址: http://download.oracle.com/otn/nt/oracle11g/112010/win32_11gR2_client.zip 先将下载下来的ZIP文件解压,并运行se ...
- 【转】【玩转cocos2d-x之二十三】多线程和同步03-图片异步加载
原创作品,转载请标明:http://blog.csdn.net/jackystudio/article/details/15334159 cocos2d-x中和Android,Windows都 一样, ...
- 浏览器兼容——jquery的html()不兼容IE
在看着一个页面A,是一个弹出框,用的jquery中的ajax,然后弹出的内容是另一个Div的.而所出现的问题,是在浏览器中都有弹出框,但是只有谷歌和火狐中的弹出框中内容. 当时,我所想到的是另一个问题 ...
- 打通ssh的方法
为了实现密码免输入,可以在安全的内网环境中打通ssh.linux和cygwin均可,步骤如下: 由A机去B机.在A生成密钥:ssh-keygen -t rsa,一路回车 将A的~/.ssh/id_rs ...
- C# winform 若要在加载设计器前避免可能发生的数据丢失,必须纠正以下错误
winform中有时添加了新控件之后编译会报错: 若要在加载设计器前避免可能发生的数据丢失,必须纠正以下错误,如图: 解决方案: 1.“解决方案”→“批生成”→“清理”→“确定”: 2.“解决方案”→ ...
- 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(2)
安装和配置集群(Installing and Configuring your Cluster) 第一步是确保正确安装了 Java SE环境.ElasticSearch需要版本6或更高的版本,可以从下 ...