NOJ——[1143] Irrigation of Farmland
来源:互联网 发布:java反斜杠替换 编辑:程序博客网 时间:2024/05/16 13:55
- 问题描述
- Hungar has bought some farms.There is a river near the farm. It was found that linking these farms is more conducive for hungar. Therefore, he need to calculate that the minimum number of waterway he should build to connect all the farms, either connecting directly or not directly.There is not existing a waterway between river and farm,so you should also connecting them.
- 输入
- Input until EOF.
For each case:
First line: two non-negative integers M (1<=M<= 500) and N(1<=N<=500), M means the number of farms, from 1 to M.
The next N lines: each line have two numbers A, B represents the existing waterway number.
- 输出
- Print the number of waterway in one line.
- 样例输入
-
4 21 34 32 21 21 2
- 样例输出
-
21
- 提示
-
无
- 来源
-
hungar
并查集。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int father[505];
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
int main()
{
int m,n;
while(~scanf("%d%d",&m,&n))
{
for(int i=0;i<=m;i++)
father[i]=i;
int u,v,cnt=0;
while(n--)
{
scanf("%d%d",&u,&v);
int x=find(u);
int y=find(v);
if(x!=y)
father[x]=y;
}
for(int i=1;i<=m;i++)
if(i==father[i])
cnt++;
printf("%d\n",cnt);
}
return 0;
}
0 0
- NOJ——[1143] Irrigation of Farmland
- NOJ 1143 Irrigation of Farmland
- NOJ 1317——Exercise of Sort
- NOJ——1274The battle of Red Cliff(并查集按秩合并)
- Base of Space NOJ
- HDU 1198——Farm Irrigation
- HDU1198Farm Irrigation——并查集
- 模拟noj——打扑克
- NOJ——1559Jump to the Top of Mountain(简单暴力DFS+渣渣代码)
- [noj 1557] Team of Slime
- [noj 1554] Lord of Minecraft
- NOJ [1297] Code Of Keyboard
- NOJ [1317] Exercise of Sort
- NOJ [1320] Hole of Black
- NOJ 1144 Line of Position
- NOJ[1362]—— 最短路线
- NOJ[1313] ——Anger Begins
- NOJ——[1438] Get Up, Soldier!
- NOJ1538——[1538] K-Submartix
- POJ1502——MPI Maelstrom
- NOJ1383——[1383] Palindrome String
- NOJ——[1480] 懒惰的风纪委Elaine
- BEDTools简介、安装与部分工具使用简介
- NOJ——[1143] Irrigation of Farmland
- POJ3254 状压DP入门
- NOJ——[1378] 战斗吧!皇后们!
- NOJ——[1176] Exchange Rate
- Android测试工具小结
- POJ1511——Invitation Cards
- BZOJ 1650: [Usaco2006 Dec]River Hopscotch 跳石子
- POJ2253——Frogger
- NOJ——[1579] 小青蛙找妈妈