Aizu - 2564 Tree Reconstruction 并查集
2015-08-22 23:49
465 查看
Aizu - 2564 Tree Reconstruction
题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边
思路:这题好像有一个定理之类的,对于每一个连通块,所需要的边数是 M-N(边数-点数) ,这个原理我还不是很清楚。
知道了这个之后,并查集求一下就完事了。
这题我大致看了一下官方 的提交记录,发现有好多种方法可以搞。23:48:40
题意:一个有向图,要使得能确定每一条边的权值,要求是每个点的入权和出权相等,问你最少需要确定多少条边
思路:这题好像有一个定理之类的,对于每一个连通块,所需要的边数是 M-N(边数-点数) ,这个原理我还不是很清楚。
知道了这个之后,并查集求一下就完事了。
这题我大致看了一下官方 的提交记录,发现有好多种方法可以搞。23:48:40
#pragma comment(linker, "/STACK:1000000000") #include <bits/stdc++.h> #define LL long long #define INF 0x3f3f3f3f #define IN freopen("in.txt","r",stdin); using namespace std; #define MAXN 505 vector<int> G[MAXN]; queue<int> Q; int father[MAXN]; int find(int x){ if(father[x] == x) return x; father[x] = find(father[x]); return father[x]; } int main(void) { //IN; int n, m; scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++){ father[i] = i; } int x, y; for(int i = 1; i <= m; i++){ scanf("%d%d", &x, &y); x = find(x); y = find(y); if(x != y){ father[x] = y; } } int ans = m - n; for(int i = 1; i <= n; i++){ if(father[i] == i){ ans++; } } printf("%d\n", ans); }
相关文章推荐
- POJ2010--Moo University-Financial Aid(二分)
- JPA的PAI测试
- Aizu - 2555 Everlasting Zero 模拟
- hdoj 1022 Train problem 1【栈 的理解】
- 调用Process.waitfor导致的进程挂起
- sleep() wait() yeild()三者之间的区别?
- hdu 1239 Calling Extraterrestrial Intelligence Again
- Debug Assertion Failed! File: afx.inl
- PAT 1014. Waiting in Line (30)
- unresolved external symbol _winmain@16
- rails yaml with expression or variable (Using ERB in YAML Configuration File)
- 【LeetCode】(70)Climbing Stairs (Easy)
- POJ 1995 Raising Modulo Numbers【快速幂】
- CodeForcesGym 100676G Training Camp
- HN OJ 13375 Flowery Trails (spfa的路径遍历)
- Aizu 2302 On or Off
- poj1363——Rails
- 转:CDC,CPaintDC,CClientDC,CWindowDC区别
- (转载)BeginPaint和GetDC有什么区别?
- HDU2141Can you find it? (二分计算计算Ai+Bj+Ck = X)