FZU 2112 并查集、欧拉通路

原题:http://acm.fzu.edu.cn/problem.php?pid=2112

  首先是,票上没有提到的点是不需要去的。

  然后我们先考虑这个图有几个联通分量,我们可以用一个并查集来维护,假设有n个联通分量,我们就需要n-1条边把他们连起来。

  最后对于每个联通分量来说,我们要使它能一次走完,就是要求他是否满足欧拉通路,也就是这个联通分量中至多有2个度为奇数的点,每多出2个度为奇数的点,就多需要一条边(因为单个连通分量的所有点的度数之和为偶数,所以不可能存在奇数个奇数度数的点)。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #define maxn 111111
 5 using namespace std;
 6 int f[maxn],du[maxn],odd[maxn],book[maxn];
 7 int getf(int v){
 8     if(f[v] == v){
 9         return v;
10     }else{
11         f[v] = getf(f[v]);
12         return f[v];
13     }
14 }
15 void merge(int u,int v){
16     int a = getf(u);
17     int b = getf(v);
18     if(a!=b)
19         f[b] = a;
20 }
21 int main(){
22     int t;
23     scanf("%d",&t);
24     while(t--){
25         memset(du,0,sizeof(du));//统计每个节点的度数
26         memset(odd,0,sizeof(odd));//统计每个联通分量度数为奇数的节点个数
27         memset(book,0,sizeof(book));//标记该点是否需要去
28         int n,m;
29         scanf("%d%d",&n,&m);
30         for(int i = 1;i<=n;i++)//初始化
31             f[i] = i;
32         int u,v;
33         while(m--){
34             scanf("%d%d",&u,&v);
35             du[u]++;
36             du[v]++;
37             book[u] = 1;
38             book[v] = 1;
39             merge(u,v);
40         }
41         int cnt = 0;//需要边的数量
42         for(int i = 1;i<=n;i++){
43             if(book[i]){
44                 if(f[i] == i)
45                     cnt++;
46                 if(du[i]&1)//度数为奇数时,对其并查集根节点的值进行更新
47                     odd[getf(i)]++;
48             }
49         }
50         for(int i = 1;i<=n;i++){
51             if(f[i] == i)
52                 cnt += max(0,(odd[i]-2)/2);//统计每个联通分量的度数为奇数的节点所需要的边的个数
53         }
54         printf("%d\n",cnt-1);
55     }
56     return 0;
57 }
时间: 2024-08-24 21:12:32

FZU 2112 并查集、欧拉通路的相关文章

Play on Words HDU - 1116 (并查集 + 欧拉通路)

Play on Words HDU - 1116 Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. There is a

POJ 2513(字典树hash+并查集+欧拉通路)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 31015   Accepted: 8180 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

POJ-2513 Colored Sticks(字典树+并查集+欧拉)

题目链接:Colored Sticks 一道3个知识点结合的题目,可以说单个知识点的题目,都会做,一旦知识点结合起来,题目就不简单了 思路:这个题开始看就知道是并查集,但是不好处理的不同种单词的统计,所以理所应当联想到字典树,上次做字典树的题目啸爷出的是统计相同单词数,这个题目和那个一样,把flag加个编号即可,再利用并查集. 1750ms  水过 #include <iostream> #include <cstdio> #include <cstdlib> #inc

[ACM] POJ 2513 Colored Sticks (Trie树,欧拉通路,并查集)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 29736   Accepted: 7843 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

POJ 2337 Catenyms (有向图欧拉通路)

Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9914   Accepted: 2588 Description A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For e

UVA 10129 Play on Words (欧拉通路)

题意: 输入N(N <= 100000)个单词,是否可以把所有这些单词排成一个序列,使得每个单词的第一个字母和上一个单词的最后一个字母相同(例如:acm,malform,mouse).每个单词最多包含 1000 个小写字母.输入中可以有重复的单词. 思路: 把一个字母的两端开成节点,单词看成有向边,若问题有借,当且仅当图中存在欧拉通路.所有只需要判断由单词而构建的图是否存在欧拉通路,由于是有向边,所以利用有向图欧拉通路的判定就可以了. 判定条件 (1):底图是连通图 (2):可以有两个奇点,其中

POJ--1300--Door Man【推断无向图欧拉通路】

链接:http://poj.org/problem?id=1300 题意:有n个房间.每一个房间有若干个门和别的房间相连.管家从m房间開始走.要回到自己的住处(0),问是否有一条路能够走遍全部的门而且没有反复的路. 无向图欧拉通路充要条件:G为连通图,而且G仅有两个奇度结点(度数为奇数的顶点)或者无奇度结点. 无向图欧拉回路充要条件:G为无奇度结点的连通图. 思路:推断是否存在欧拉通路.依据欧拉通路.欧拉回路的性质来做.有两种情况:一种是欧拉回路.全部房间的门的个数都是偶数个,而且此时初始房间不

hdu 1116 Play on Words(欧拉通路)

Problem Description Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. There is a large

POJ--1300--Door Man【判断欧拉通路】

链接:http://poj.org/problem?id=1300 题意:有n个房间,每个房间有若干个门和别的房间相连,管家从m房间开始走,要回到自己的住处(0),问是否有一条路可以走遍所有的门并且没有重复的路. 思路:判断是否存在欧拉通路,根据欧拉通路.欧拉回路的性质来做.有两种情况:一种是欧拉回路,所有房间的门的个数都是偶数个,并且此时初始房间不是0,此时存在要求的路径,如果初始是0则不行.另一种是欧拉通路,只有两个房间门是奇数个,剩下都是偶数个,并且这两个房间一个是0,一个是当前起点,并且