Ant Trip(区别于二分匹配中最小路径覆盖的一笔画问题)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3018

题目:

Problem Description
Ant Country consist of N towns.There are M roads connecting the towns.

Ant Tony,together with his friends,wants to go through every part of the country.

They intend to visit every road , and every road must be visited for
exact one time.However,it may be a mission impossible for only one
group of people.So they are trying to divide all the people into several
groups,and each may start at different town.Now tony wants to know what
is the least groups of ants that needs to form to achieve their goal.
Ant Trip(区别于二分匹配中最小路径覆盖的一笔画问题)

 
Input
Input
contains multiple cases.Test cases are separated by several blank
lines. Each test case starts with two integer
N(1<=N<=100000),M(0<=M<=200000),indicating that there are N
towns and M roads in Ant Country.Followed by M lines,each line contains
two integers a,b,(1<=a,b<=N) indicating that there is a road
connecting town a and town b.No two roads will be the same,and there is
no road connecting the same town.
 
Output
For each test case ,output the least groups that needs to form to achieve their goal.
 
Sample Input
3 3
1 2
2 3
1 3

4 2
1 2
3 4

 
Sample Output
1
2
Hint

New ~~~ Notice: if there are no road connecting one town ,tony may forget about the town.
In sample 1,tony and his friends just form one group,they can start at either town 1,2,or 3.
In sample 2,tony and his friends must form two group.

 
Source
 /*
问题 给出n个顶点和m条边,n<=100000,m<=200000,若要遍历所有的边且每条边只能走一次,问至少需要几个起点
保证没有重复道路,保证道路两端的城市是不同的,存在孤立的点,例如 3个点,1条边,1和2联通,那么3就是孤立的点 解题思路 简单总结就是一笔画问题,画的时候,要么a--->b(一条线),即a和b均为奇点(度数为奇数的点),要么a--->a(一个圈),即该圈中没有奇点
由于连通图中不可能存在奇数个奇点,换句话说连通图中奇点要么不出现,即存在欧拉回路,只需要一笔,要么成对出现,每一对需要一笔
那么总的笔画数等于 所有奇点的个数除以2(孤立的点度数为0,为偶数) 加上 欧拉回路数 输入时计算每个顶点的度数,使用并查集将图分成一块一块,遍历每一个顶点,找出奇点计数并且标记其根节点;再遍历顶点,
如果不是孤立的点 且 是根 且 该根没有被标记过,即为欧拉回路。
*/
#include<stdio.h>
#include<string.h>
int deg[],fat[],book[];
void merge(int a, int b);
int getf(int x); int main()
{
int n,m;
int i,a,b;
while(scanf("%d%d",&n,&m) != EOF)
{
memset(deg,,sizeof(deg));
for(i=;i<=n;i++){
fat[i]=i;
}
for(i=;i<=m;i++){
scanf("%d%d",&a,&b);
deg[a]++;
deg[b]++;
merge(a,b);
} memset(book,,sizeof(book));
int singsum=;
for(i=;i<=n;i++){
if(deg[i] & ){
book[ getf(i) ]=;//标记以该点为根的一块图中存在奇点
singsum++;
}
} int eulenum=;
for(i=;i<=n;i++){
if(deg[i] > && i==fat[i] && == book[i])//不是孤立的点 且 是根 且 该根没有被标记过,即是欧拉回路
eulenum++;
}
printf("%d\n",singsum/+eulenum);
}
return ;
}
void merge(int a, int b)
{
int t1,t2;
t1=getf(a);
t2=getf(b);
if(t1 != t2){
fat[t2]=t1;
}
}
int getf(int x)
{
return fat[x]==x ? x : fat[x]=getf(fat[x]);
}
 
上一篇:IntelliJ Idea中创建package的问题


下一篇:Java课程课后作业190315之从文档中读取随机数并得到最大连续子数组