您的位置:首页 > 其它

HDU 2120-Ice_cream's world I

2017-09-05 17:17 537 查看


Ice_cream's world I

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1802    Accepted Submission(s): 1044


题目链接:点击打开链接

Problem Description

ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_cream wants award land to diligent ACMers. So there are some watchtowers are set up, and wall between watchtowers be build, in order to partition
the ice_cream’s world. But how many ACMers at most can be awarded by the queen is a big problem. One wall-surrounded land must be given to only one ACMer and no walls are crossed, if you can help the queen solve this problem, you will be get a land.

 

Input

In the case, first two integers N, M (N<=1000, M<=10000) is represent the number of watchtower and the number of wall. The watchtower numbered from 0 to N-1. Next following M lines, every line contain two integers A, B mean between
A and B has a wall(A and B are distinct). Terminate by end of file.

 

Output

Output the maximum number of ACMers who will be awarded.

One answer one line.

Sample Input

8 10

0 1

1 2

1 3

2 4

3 4

0 5

5 6

6 7

3 6

4 7

 

Sample Output

3

题意:

在一块地方建立 n 个岗楼,m 堵墙,然后有 m 行,每行有两个数为 a 和 b,代表 a,b 之间有一堵墙,问所有的墙能把这块地方分割成多少部分。

分析:

本题主要用并查集判断成环,找出有多少个环,我刚开始认为在两个岗楼之间建墙是分割两个岗楼,但怎样都理解不了题上所给数据怎样才能围成 3 个环,后来仔细理解一下题意,题意中的wall between watchtowers be build, in order to partition the ice_cream’s world(墙在岗楼之间建立是为了分割开冰淇淋的世界),我们来想象一下,在冰淇淋的世界中的某一块地上先建几个岗楼,再在岗楼之间建墙,但这墙是为了分割开冰淇淋的世界,所以肯定是从
a 岗楼连接 b 岗楼来建,所以只要想通这一点,其实就不难想到用并查集判断成环来做,主要就是先开始想的建墙的方式不对才一直想不通本题的意思。代码就很ok啦~

#include <iostream>
#include<stdio.h>
using namespace std;
int pre[1010],sum;
int Find(int x)
{
if(pre[x]==x)
return x;
else
return Find(pre[x]);
}
void pei(int x,int y)
{
int f1=Find(x);
int f2=Find(y);
if(f1!=f2)
pre[f1]=f2;
else
sum++;
}
int main()
{
int m,n,a,b;
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<n;i++)
pre[i]=i;
sum=0;
while(m--)
{
scanf("%d %d",&a,&b);
pei(a,b);
}
printf("%d\n",sum);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: