UVALive 7456 Least Crucial Node (并查集)
2016-08-17 19:14
302 查看
Least Crucial Node
题目链接:
http://acm.hust.edu.cn/vjudge/contest/127401#problem/CDescription
http://7xjob4.com1.z0.glb.clouddn.com/e15d7d11650607e5795d28e1120d7109Input
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 givennetwork.
Sample Input
44
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
32
题意:
求标号最小的最大割点.(删除该点后,指定点#sink能到达的点数减少最多).
题解:
由于数据规模巨水,直接对枚举各点跑一遍并查集(或dfs)计算删除后的减少量.(不要对源点#sink跑并查集,因为割点不能是#sink).
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <vector> #include <list> #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[maxn*maxn], y[maxn*maxn]; 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; }
相关文章推荐
- 【割点 && dfs】UVALive - 7456 Least Crucial Node
- UVaLive 7456 Least Crucial Node (并查集+暴力 或者 求割点)
- UVALive 7456 Least Crucial Node
- Least Crucial Node UVALive - 7456 【】
- UVALive Problem 7456 Least Crucial Node——Regionals 2015 :: Asia - Taipei
- LA 7456 Least Crucial Node
- UVALive 4487 Exclusive-OR 加权并查集神题
- uvalive 4730(并查集+区间修改单点查询)
- uvalive 3027 Corporative Network(并查集)
- UVALive - 3644 X-Plosives 并查集
- 并查集 - UVALive 6889 City Park
- UVALive 6910 Cutting Tree 并查集
- UVALive 3027 并查集
- uvalive 3644 X-Plosives(并查集)
- UVALive 3027---Corporative Network+并查集的应用
- UVALive 4035 Undetectable Tour (二分 + 并查集)
- 指南 第三章 例题6 UVALive 3027 Corporative Network(并查集的应用)
- UVALive 3027 Corporative Network 带权并查集
- UVALive 4627 -- Islands (并查集)
- UVALive_6168_Fat Ninjas(二分+并查集)