HDU2120【并查集判环】

Ice_cream‘s world I

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 600    Accepted Submission(s): 344

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

大意:

题意很简单,刚开始我理解成两个塔之间有一个墙把他们隔开呢T_T

分析:并查集判环  只要在一个集合之中一定会有一块空地

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int maxn = 1005;
 7
 8 int fa[maxn];
 9
10 int find(int x) {
11     if(fa[x] == x) return x;
12     return fa[x] = find(fa[x]);
13 }
14
15 int main() {
16     int n, m;
17     int a, b;
18     while(EOF != scanf("%d %d",&n, &m) ) {
19         for(int i = 0; i < n; i++) fa[i] = i;
20         int cnt = 0;
21         while(m--) {
22             scanf("%d %d",&a, &b);
23             if(find(a) == find(b) ) {
24                 cnt++;
25             } else {
26                 fa[find(a)] = find(b);
27             }
28         }
29         printf("%d\n", cnt);
30     }
31     return 0;
32 }

时间: 2024-08-28 19:16:47

HDU2120【并查集判环】的相关文章

HDU - 4514 湫湫系列故事——设计风景线(并查集判环+树形DP)

题目链接:https://vjudge.net/problem/HDU-4514 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好.  现在已经勘探确定了n个位置可以用来建设,在它们之间也勘探确定了m条可以设计的路线以及他们的长度.请问是否能够建成环形的风景线?如果不能,风景线最长能够达到多少?  其中,可以兴建的路线均是双向的,他们之间的长度均大于0. Input 测试数据有

HDU - 4514 湫湫系列故事——设计风景线(并查集判环)

题目: 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好. 现在已经勘探确定了n个位置可以用来建设,在它们之间也勘探确定了m条可以设计的路线以及他们的长度.请问是否能够建成环形的风景线?如果不能,风景线最长能够达到多少? 其中,可以兴建的路线均是双向的,他们之间的长度均大于0. 思路: 将给出的边的两个端点用并查集放在一起,如果这两个点的祖先相等说明构成了一个环. 在这个用并查

HDU4514湫湫系列故事——设计风景线(并查集判环+最长直径)

题目链接:传送门 题意: 先判一个图是否存在换,不存在的话输出这个图的最长路径. 代码如下: #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <queue> using namespace std; const int maxn = 1e6+10; int par[maxn/10],head[maxn/10]; bool

杭电1272 并查集找环+判断连通

杭电1272 并查集找环+判断连通 E - E Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1272 Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B

POJ 2513--Colored Sticks【字典树编号 &amp;&amp; 并查集判连通 &amp;&amp; 欧拉路】

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 32351   Accepted: 8536 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a st

URAL - 1966 - Cycling Roads(并查集 + 判线段相交)

题意:n 个点,m 条边(1 ≤ m < n ≤ 200),问所有点是否连通(两条边相交,则该 4 点连通). 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1966 -->>对于每条边,边上的两端点并入集合,枚举边与边,判断他们是否相交,是的话各点并入集合,最后看集合内元素的个数是否为n.. #include <cstdio> #include <cmath> const int MAXN = 200

HDU1272(并查集判图连通)

小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 42456    Accepted Submission(s): 13078 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是

cf246 ENew Reform (并查集找环)

Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing r

【hihocoder】欧拉路径 并查集判连通

#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<vector> #include<queue> using namespace std; int n,m; const int maxn=1e4+2; const int maxm=5e4+2; int degree[maxn