# 畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30409    Accepted Submission(s): 15974

Problem Description

Input

3 3
1 2
1 2
2 1

Output

Sample Input
4 21 34 33 31 21 32 35 21 23 5999 00

Sample Output
102998
Hint
Hint
Huge input, scanf is recommended.

Source

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1233 1272 1875 1879 1213

http://blog.csdn.net/ljfbest/article/details/6642769

#include<stdio.h>int pre[1000000];int find(int x){    int r=x,i,j;    while(r!=pre[r])        r=pre[r];    i=x;    while(i!=r)    {        j=pre[i];        pre[i]=r;        i=j;    }    return r;}int main(){    int n,m;    while(scanf("%d",&n)!=EOF,n)    {        int i,fx,fy,t=n-1,x,y;        scanf("%d",&m);        for(i=0;i<=n;i++)//小于等于n，错这了        {            pre[i]=i;        }        while(m--)        {            scanf("%d%d",&x,&y);            fx=find(x);            fy=find(y);            if(fx!=fy)            {                pre[fx]=fy;                t--;            }        }        printf("%d\n",t);    }}

0 0