Problem Description
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 注意:两个城市之间可以有多条道路相通,也就是说 3 3 1 2 1 2 2 1 这种输入也是合法的 当N为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最少还需要建设的道路数目。
Sample Input
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
Sample Output
1
0
2
998
这个题目就是要求最少还需增加多少条线可以构成一个连通图。
有n 个节点,那么假设刚开始没有线,则最少需n-1条线。line=n-1;
那么每增加一条线可以用并查集连接两个节点的最祖先,如果两个节点的最祖先是一个人,那么就不必连线,因为是多余的,要不要这根线无所谓,因此line不减一,如果不是一个人,那么所需的线就该少一根,看哪个祖先等级高,等级高的祖先的rank等于等级低的加上自己原来的rank,等级低的祖先的祖先为高的那个。line减到最好则为需要的线的个数。
还有一道题:
Problem Description
Mr Wang wants some boys to help him with a project. Because the project is rather complex,
the more boys come, the better it will be. Of course there are certain requirements.Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
Input
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
Sample Input
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
Sample Output
4
2
还是用并查集解决,这道题求得是可最大连接的节点的个数,line初始化为1,连接两根线,求出连接后的最终祖先的rank,这个祖先的rank其实就是该次连接所包含的节点个数,那么line应该设置为max(line,rank(max_father));
两道题的代码如下(不是我写的)。。。。
#include<stdio.h>
const int maxn=1010;
int n,ans;
int father[maxn],rank[maxn];
void makeSet(int n){
for(int i=1;i<=n;i++){
father[i]=i;
rank[i]=1;
}
}
int findSet(int x){
if(x!=father[x]){
father[x]=findSet(father[x]);
}
return father[x];
}
void Union(int a,int b){
int x=findSet(a);
int y=findSet(b);
if(x==y)
return ;
ans--;
if(rank[x]<=rank[y]){
father[x]=y;
rank[x]+=rank[y];
}else{
father[y]=x;
rank[y]+=rank[x];
}
}
int main(){
//freopen("input.txt","r",stdin);
int m;
int a,b;
while(scanf("%d",&n) && n){
scanf("%d",&m);
makeSet(n);
ans=n-1;
for(int i=0;i<m;i++){
scanf("%d%d",&a,&b);
Union(a,b);
}
printf("%d\n",ans);
}
return 0;
}
#include<stdio.h>
const int maxn=10000010;
int n,ans,father[maxn],rank[maxn];
int max(int a,int b){
return a>b?a:b;
}
void makeSet(int n){
for(int i=1;i<=n;i++){
father[i]=i;
rank[i]=1;
}
}
int findSet(int x){
if(x!=father[x]){
father[x]=findSet(father[x]);
}
return father[x];
}
void Union(int a,int b){
int x=findSet(a);
int y=findSet(b);
if(x==y)
return ;
if(rank[x]<=rank[y]){
father[x]=y;
rank[y]+=rank[x];
ans=max(ans,rank[y]);
}else{
father[y]=x;
rank[x]+=rank[y];
ans=max(ans,rank[x]);
}
}
int main(){
//freopen("input.txt","r",stdin);
int a,b;
while(scanf("%d",&n)!=EOF){
if(n==0){
printf("1\n");
continue;
}
makeSet(maxn);
ans=1;
for(int i=0;i<n;i++){
scanf("%d%d",&a,&b);
Union(a,b);
}
printf("%d\n",ans);
}
return 0;
}
转自:
http://www.cnblogs.com/jackge/