HDU - 3018

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.

InputInput 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.OutputFor 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.
    给出一个图,问几笔画才能经过所有边。
   欧拉回路,知识点已经在上个博客提到。对于每个点的出度,如果存在奇数,那么需要奇数/2笔才能经过所有的点。
    给出的图并没有说明是否为连通图,所以可能有多个图,那么这种情况,ans=奇数度个数/2+欧拉回路个数(只含偶数点的集合)
  解析在代码里
  
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+; //jishu/2+oulatu
int pr[maxn],cnt[maxn],mark[maxn];
int n,m;
int ans=;
void first()
{
for(int i=;i<=n;i++)
{
pr[i]=i;
}
memset(cnt,,sizeof(cnt));
memset(mark,,sizeof(mark));
ans=;
}
int find(int x)
{
if(x!=pr[x])
return pr[x]=find(pr[x]);
return x;
}
void join(int a,int b)
{
int f1=find(a),f2=find(b);
if(f1!=f2)
pr[f1]=f2;
return;
}
void ac()
{
for(int i=;i<=n;i++)
{
if(cnt[i]%!=)
{
int f=find(i);    //统计奇数度点数量。用mark[]数组来记录,如果i点奇度,那么i所在图不是欧拉回路,那么i的根节点标为1,代表此图不是欧拉回路。
mark[f]=;
ans++;
}
}
ans/=;
for(int i=;i<=n;i++)  //统计欧拉回路图
{
if(cnt[i]>)      //比如输入,9 3 9个点只给出了3个关系,肯定有点不算,cnt[i]=0,不能纳入计算。
{     
int f=find(i);    //找到i的根节点,如果没被标为1,说明i出度为偶数,而且满足pr[i]==i(i==f)(即搜到x==pr[x]时还是没被标记)说明此图是个欧拉回路,因为如果存在奇度点,i==pr[i]
                    //处肯定被标记了。      ans++;
if(mark[f]==&&pr[i]==i)
{
ans++;
}
}
}
}
int main()
{
while(cin>>n>>m)
{
first();    //初始化
for(int i=;i<=m;i++)
{
int a,b;
cin>>a>>b;
join(a,b);
cnt[a]++;  //加入并查集,统计入度出度
cnt[b]++;
}
ac();
cout<<ans<<endl;
}
return ;
}

  

												

HDU 3018 欧拉回路的更多相关文章

  1. [欧拉回路] hdu 3018 Ant Trip

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others) ...

  2. HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...

  3. HDU 3018 Ant Trip (欧拉回路)

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. HDU 3018 Ant Trip(欧拉回路,要几笔)

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  6. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  7. HDU 1878 欧拉回路(判断欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878 题目大意:欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一 ...

  8. HDU 1878 欧拉回路

    并查集水题. 一个图存在欧拉回路的判断条件: 无向图存在欧拉回路的充要条件 一个无向图存在欧拉回路,当且仅当该图所有顶点度数都是偶数且该图是连通图. 有向图存在欧拉回路的充要条件 一个有向图存在欧拉回 ...

  9. HDU 1878 欧拉回路 图论

    解题报告:题目大意,给出一个无向图,判断图中是否存在欧拉回路. 判断一个无向图中是否有欧拉回路有一个充要条件,就是这个图中不存在奇度定点,然后还要判断的就是连通分支数是否为1,即这个图是不是连通的,这 ...

随机推荐

  1. windows上通过自定义后缀文件启动Unity应用

    好吧,一个Unity应用并不常见的需求.. 两个步骤 1.关联注册表 2.把自定义后缀文件作为启动参数传给Unity做处理 1.关联注册表  (.XXXX是自定义后缀) Windows Registr ...

  2. maven项目使用mybatis+mysql

    1.添加依赖,在pom.xml中添加 <!--mybatis核心包--> <dependency> <groupId>org.mybatis</groupId ...

  3. Java中用单例模式有什么好处?

    Java Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 使用Singleton的好处还在于可以节省内存,因为它限制了实例的个数,有利于Java垃圾回收( ...

  4. 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包

    将项目复制到其地方的时候编译会报错,按照官网方法也不行,从网上查了一个有用的方法如下 打开CSPROJ文件.删除如下代码,  <Import Project="..\packages\ ...

  5. 定义一个共享数据块DB1 在DB1中定义一个数组 用程序 访问数据里面的某一个成员或者地址连续的成员

    提纲 : 定义一个共享数据块 DB1 在DB1 中定义数组 用SFC21 实现 实现全部数组元素的赋一样的值 实现 给数组中的某一个元素赋值 实现 对数组中的全部元素赋值 实现将数组中的某个 或者 某 ...

  6. UVA - 1423 Guess (拓扑排序)

    题意:已知矩阵S,求序列a.已知矩阵Sij = “ + ” if ai + . . . + aj > 0; Sij = “ − ” if ai + . . . + aj < 0; and ...

  7. PATH环境 变量

    PATH环境 变量 $HOME/.bash_profile和/etc/profile 两个文件在登陆用户的时候会执行, /etc/profile 所有用户都执行,$HOME/.bash_profile ...

  8. Elasticsearch开启试用x-pack license

    一.Elasticsearch 6.7.2开启trial  x-pack license:x-pack的license试用期只有30天 1.ES6.7.2版本默认已经安装了x-pack插件,这里就没有 ...

  9. [YOLO]《You Only Look Once: Unified, Real-Time Object Detection》笔记

    一.简单介绍 目标检测(Objection Detection)算是计算机视觉任务中比较常见的一个任务,该任务主要是对图像中特定的目标进行定位,通常是由一个矩形框来框出目标. 在深度学习CNN之前,传 ...

  10. 2.15 使用web 编写一个简单记事本

    首先陈列问题  (等待解决): 1. 界面是使用 H5   iframe 标签合并而成的,当窗口化之后点击任务栏,显示的内容会在任务栏的下边 希望可以找其他方式替代 (其他方法不熟练,不能应用) 如图 ...