UVa Live (LA) 3644 典型并查集
来源:互联网 发布:vm12虚拟机安装mac os 编辑:程序博客网 时间:2024/06/05 17:22
#include<bits/stdc++.h>
using namespace std;
const int maxn=100101;
int father[maxn];
int fa(int u){
if(father[u]==u)return u;
else return father[u]=fa(father[u]);
}
int main(){
int i,j,k,m,n,x,y,z,u,v;
while(scanf("%d%d",&u,&v)!=EOF){
for(i=0;i<=maxn;i++)father[i]=i;
int ans=0;father[u]=v;
while(scanf("%d",&u)){if(u<0)break;
scanf("%d",&v);
int x=fa(u),y=fa(v);
if(fa(u)!=fa(v))father[x]=y;
else ans++;
}
printf("%d\n",ans);
}
return 0;
}
/*
1 2
3 4
3 5
3 1
2 3
4 1
2 6
6 5
-1*/
using namespace std;
const int maxn=100101;
int father[maxn];
int fa(int u){
if(father[u]==u)return u;
else return father[u]=fa(father[u]);
}
int main(){
int i,j,k,m,n,x,y,z,u,v;
while(scanf("%d%d",&u,&v)!=EOF){
for(i=0;i<=maxn;i++)father[i]=i;
int ans=0;father[u]=v;
while(scanf("%d",&u)){if(u<0)break;
scanf("%d",&v);
int x=fa(u),y=fa(v);
if(fa(u)!=fa(v))father[x]=y;
else ans++;
}
printf("%d\n",ans);
}
return 0;
}
/*
1 2
3 4
3 5
3 1
2 3
4 1
2 6
6 5
-1*/
0 0
- UVa Live (LA) 3644 典型并查集
- LA 3644 并查集
- 并查集 LA 3644
- LA 3644 并查集
- 并查集 LA 3644
- LA 3644 (并查集)
- LA 3644并查集
- LA 3644并查集
- LA 3644 X_Plosives(并查集)
- LA 3644 - X-Plosives,并查集
- LA 3644 X-Plosives / 并查集
- LA 3644(并查集)
- LA -3644 X-Plosives -并查集
- LA 3644 X-Plosives 并查集
- LA 3644 X-Plosives [并查集]
- X-Plosives(LA 3644)并查集
- LA 3644 X-Plosives [并查集]
- LA 3027并查集
- PAT 1051. Pop Sequence
- hdu 1080 Human Gene Functions( 类似最长公共子序列)
- 【POJ2155】Matrix-二维树状数组+前缀和
- 20160729 其他小代码
- python3.3网页图片爬虫
- UVa Live (LA) 3644 典型并查集
- 机器学习(周志华) 参考答案 第三章 线性模型
- 无题
- Picasso源码分析
- android 坐标问题
- nyoj 325 zb的生日
- SPDA-CNN:Unifying Semantic Part Detection and Abstraction for Fine-grained Recognition
- 神经网络与深度学习读书笔记第二天
- 2D平移矩阵与3D平移矩阵