标签:des style blog http java color
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29362 Accepted Submission(s): 15452
4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
1 0 2 998
题目是中文的。。所以也就不用多讲了。。题意就是要求连通的图。。看有多少断节点数。。而要修的路也就是断节点数-1;
实现代码如下:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<queue> #include<sstream> #include<cmath> using namespace std; #define f1(i, n) for(int i=0; i<n; i++) #define f2(i, n) for(int i=1; i<=n; i++) #define f3(i, n) for(int i=n; i>=1; i--) #define f4(i, n) for(int i=1; i<n; i++) #define M 1005 int f[M]; int ans; int n, m; int a, b; int find(int x) { return f[x] == x ? x:f[x] = find( f[x] ); } void make(int a, int b) { int x = find(a); int y = find(b); if( x!=y ) f[y] = x; } int main() { while(scanf("%d", &n) &&n ) { ans = 0; f2(i, n) f[i] = i; scanf("%d", &m); while( m-- ) { scanf("%d%d", &a, &b); make(a, b); } f2(i, n) if( f[i]==i ) ans++; //断节点个数 printf("%d\n", ans-1); //修的路等于断节点数-1 } return 0; }
HDU 1232:畅通问题(并查集),布布扣,bubuko.com
标签:des style blog http java color
原文地址:http://blog.csdn.net/u013487051/article/details/37773423