题目链接

题目描述

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.

输入格式

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.

输出格式

For each test case ,output the least groups that needs to form to achieve their goal.

输入样例

3 3
1 2
2 3
1 3
4 2
1 2
3 4

输出样例

1
2

提示/说明

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.

分析

显然,本题是一个欧拉通路/回路笔画问题。因此可以分为两个方向讨论:

  1. 欧拉通路/回路的判定
  2. 一笔画/多笔画问题

欧拉通路/回路的判定

有向图

  • 欧拉通路:图是连通的,图中只有两个奇度点,分别是欧拉通路的两个端点。对于欧拉通路,除起点、终点外,每个点如果进入,显然一定要出去,因此都是偶点。
  • 欧拉回路:图是连通的,点均为偶度点。对于欧拉回路,每个点进入、出去的次数相等,因此没有奇点。

无向图

  • 欧拉通路:图是连通的,除两顶点外其余点的入度等于出度,且这两个顶点中,一个顶点入度比出度大1(起点),另一个入度比出度小1(终点)
  • 欧拉回路:图是连通的,图中所有点的入度等于出度。

一笔画/多笔画判定

一笔画判定

一笔画的图只需满足以下两个条件,即:

  • 图是连通图
  • 奇度点的个数为0或2

多笔画问题
由于连通图奇度点的个数为偶数,因此有:

  • 最少笔画数=奇度点数/2

思路

由于图中可能存在多个联通分量,每个联通分量都是可以一笔画或者多笔画完成的,那么该问题的答案=欧拉回路数+奇度点数。
因此,首先统计奇度点个数,并将存在奇度点的联通分量标记起来(因为他是需要多笔画完成的,不存在欧拉回路),那么剩下的联通分量都是可以一笔画完成的欧拉回路。

源程序

#include <bits/stdc++.h>
#define MAXN 100005
using namespace std;
int n,m,degree[MAXN],father[MAXN];
bool used[MAXN];
int read()  //快读
{int sum=0;char c=getchar();while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9'){sum=sum*10+c-'0';c=getchar();}return sum;
}
int find(int x) //并查集
{if(x==father[x])return x;return father[x]=find(father[x]);  //路径压缩
}
void Union(int x,int y) //合并
{x=find(x);y=find(y);if(x!=y)father[x]=y;return ;
}
int main()
{while(cin>>n>>m){for(int i=1;i<=n;i++)degree[i]=0,father[i]=i;//初始化 for(int i=1;i<=m;i++){ //统计顶点度数以及并查集 int x=read(),y=read();degree[x]++;degree[y]++;Union(x,y);} int cnt,num=0;  //统计奇度点个数 for(int i=1;i<=n;i++)if(degree[i]&1){num++;used[find(i)]=true;  //标记已计算过的连通分量 } cnt=num/2; //非欧拉回路笔画数=奇度点数除以2 for(int i=1;i<=n;i++)if(degree[i]!=0){  //排除孤点if(!used[find(i)]){       //计算欧拉回路的一笔画数 used[find(i)]=true;  cnt++;     } }cout<<cnt<<endl;}
}

Ant Trip(HDU 3018)---多笔画问题相关推荐

  1. I - Ant Trip (无向图欧拉回路+并查集),判断

    I - Ant Trip 参考博客:Ant Trip(欧拉回路+并查集) 参考:欧拉路径问题与欧拉回路问题 题意:给你无向图的 N 个点和 M 条边,保证这 M 条边都不同且不会存在同一点的自环边,现 ...

  2. HDU 3018 Ant Trip(一笔画问题)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题意:每条边过且只过一次,问至少要画几笔才能全部边都经过..孤立的点忽视. 分析:首先,你用笔来画的 ...

  3. hdu 3018 Ant Trip

    http://acm.hdu.edu.cn/showproblem.php?pid=3018 题意: 给出一张图,不能走重复边,可以几笔画遍历完所有的边 连通子图内如果没有度数为奇数的点,这个子图可以 ...

  4. hdoj 3018 Ant Trip(无向图欧拉路||一笔画+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 思路分析:题目可以看做一笔画问题,求最少画多少笔可以把所有的边画一次并且只画一次: 首先可以求出 ...

  5. hdu 3018 图 欧拉回路 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=3018 题意  --许多蚂蚁 遍历一个图 每一条边只能走一次  问至少要把这些蚂蚁分为几群 说白了就是 至少几笔可 ...

  6. hdu3018 Ant Trip

    题意:拓展一笔画问题.给一个无向图,每条边只能经过一次,问多少笔能将图画完.孤立的点不用考虑. 思路:先把每个连通分量找出来,然后数这个连通分量的度为奇数的点,如果该连通分量是欧拉通路,显然可以一笔画 ...

  7. 蓝书3.7 欧拉回路

    T1 欧拉回路 hdu 1878 题目大意: 判断是否存在欧拉回路 思路: 一个无向图存在欧拉回路的条件为所有点的度为偶数 且图联通 1 #include<iostream> 2 #inc ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. 欧拉图简述---(一笔画问题)

    主要参考大佬博客:https://blog.csdn.net/u011815404/article/details/86590498 欧拉图 欧拉图是在大家小学时学奥数都学习过的一个类型的题,无论你学 ...

最新文章

  1. Field XXXX input value is longer than screen field-BDC
  2. django异常日志_5分钟教你学会Django系统错误监控
  3. NO_CHANGE_ALLOWED error code
  4. 置顶带滚动效果_前端面试:如何实现轮播图效果?
  5. paip.程序不报错自动退出的解决
  6. push bullet matlab,PushBullet(推送)下载|PushBullet(推送)安卓版v16.5.4 官方最新版_软件侠下载站...
  7. 数值分析——Hermite插值
  8. vue-自定义组件之组件的继承
  9. 模拟京东快递单号查询
  10. nacos + springcloud gateway 出现503错误
  11. HC-05蓝牙模块简易说明
  12. 论文笔记—A Review of Visual-LiDAR Fusion based Simultaneous Localization and Mapping
  13. 重庆生小孩可以领那些钱
  14. 互联网电影院带来新突破,5G+4K’
  15. Long和Integer相互转换
  16. 每天都在用电脑、玩手机的你,现在的眼睛有多干?
  17. 微信小程序之图表系列——最简单的表格制作
  18. UE4内存检测工具使用
  19. 用C语言实现杨辉三角
  20. 2020-03-03

热门文章

  1. java中作用是什么_Java在编程中的作用是什么?
  2. python笔记-02(列表和元组)
  3. CELL_FACH和CELL_PCH状态
  4. 在英特尔硬件上部署深度学习模型的无代码方法 OpenVINO 深度学习工作台的三部分系列文章 - CPU AI 第一部
  5. 搜索引擎的原理,架构与细节
  6. MapReduce(中文翻译)
  7. 软件工程论文书写设计步骤及如何降低重复率
  8. 2022CES 智能驾驶供应链盘点
  9. QC部分浏览器不支持、不能加载组件
  10. 杭州端点网络java开发实习生笔试题自我反省