HDU 3018 Ant Trip(欧拉回路,要几笔)
Ant Trip
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3660 Accepted Submission(s):
1455
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.
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.
to form to achieve their goal.
1 2
2 3
1 3
4 2
1 2
3 4
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.
题意:给一个无向图,N个顶点和M条边,问至少需要几笔才能把所有边画一遍
思路:只要知道一个结论就随便做了。
如果该连通分量是一个孤立的点,显然只需要0笔.
如果该连通分量是一个欧拉图或半欧拉图,只需要1笔.
非(半)欧拉图需要的笔数==该图中奇数度的点数目/2
对于每个以i为根的连通分量我们记录属于该连通分量的点数目num[i]和该连通分量中奇度点的个数odd[i]。
详见代码注释:
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
int a[];
int pra[];
int rak[];
int num[];//属于该连通分量的点数目
int odd[];//该连通分量中奇度点
int find(int x)
{
if(pra[x]==x) return x;
else return pra[x]=find(pra[x]);
}
void unite(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(xx==yy) return;
else
{
if(rak[xx]<rak[yy]) pra[xx]=yy;
else
{
pra[yy]=xx;
if(rak[xx]==rak[yy]) rak[xx]++;
}
} }
int main()
{
int n,m;
while(cin>>n&&n)
{
cin>>m;
memset(a,,sizeof(a));
memset(num,,sizeof(num));
memset(odd,,sizeof(odd));
for(int i=;i<=n;i++)
{
pra[i]=i;
rak[i]=;
}
for(int i=;i<=m;i++)
{
int x,y;
cin>>x>>y;
a[x]++;//度数+1
a[y]++;
unite(x,y);//合并
}
int ans=;
for(int i=;i<=n;i++)
{
num[find(i)]++;//属于该连通分量的点数目
if(a[i]%==)
odd[find(i)]++;//该连通分量中奇度点
}
for(int i=;i<=n;i++)
{
if (num[i]<=) //只有一个点,不用走
continue;
else if (odd[i]==) //没有奇度数点,那就+1
ans++;
else //有的话
ans+=odd[i]/; //+奇度数点/2
}
cout<<ans<<endl;
}
return ;
}
HDU 3018 Ant Trip(欧拉回路,要几笔)的更多相关文章
- hdu 3018 Ant Trip 欧拉回路+并查集
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- [欧拉回路] hdu 3018 Ant Trip
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 3018 Ant Trip (欧拉回路)
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
- HDU 3018 Ant Trip
九野的博客,转载请注明出处: http://blog.csdn.net/acmmmm/article/details/10858065 题意:n个点m条边的无向图,求用几笔可以把所有边画完(画过的边 ...
- HDU3018:Ant Trip(欧拉回路)
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 3108 Ant Trip
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdoj 3018 Ant Trip(无向图欧拉路||一笔画+并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 思路分析:题目可以看做一笔画问题,求最少画多少笔可以把所有的边画一次并且只画一次: 首先可以求出 ...
- HDU 3018 欧拉回路
HDU - 3018 Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,together ...
随机推荐
- gitlab库迁移
gitlab 迁移 gitlab上一共有两个分之,一级提交记录. git clone --bare http://111.222.333.xxx/jiqing/test.git 执行成功后,会多一个t ...
- Windows下使用MinGW在命令行编译运行C++程序
之前学习C语言的时候都是用IDE类似CodeBlocks的工具写完直接编译运行的,今天突然心血来潮,自己下一个编译器,在命令行下,编译运行C++程序,了解一下编译过程. 一.安装编译器 首先你需要下载 ...
- Java socket - 使用代理服务器
为什么使用代理服务器不需要多说了. 使用Proxy Java提供了Proxy类实现使用代理进行通信. Proxy类的构造器Proxy(Proxy.Type type, SocketAddress sa ...
- java.util.logging.Logger_01
1.参考网址 1.1.java.util.logging.Logger使用详解 http://lavasoft.blog.51cto.com/62575/184492 1.2.Java内置Logger ...
- angular ng-repeat 如何实现嵌套
slides是一个二维数组 <div ng-repeat="links in slides"> <div ng-repeat="link in link ...
- Java 基于JavaMail的邮件发送
http://blog.csdn.net/xietansheng/article/details/51673073 http://blog.csdn.net/xietansheng/article/d ...
- 使用Monkey对apk做稳定性测试
认识Monkey 官方文档:https://developer.android.com/studio/test/monkey.html 什么是Monkey? Monkey是Android中的一个命令行 ...
- fiddler之使用教程(一)
一. 什么是fiddler&它可以做什么 fiddler是位于客户端和服务器端的HTTP代理,也是目前最常用的http抓包工具之一.它能够记录客户端和服务器之间的所有HTTP请求,可以针对特定 ...
- Go语言实现FastDFS分布式存储系统WebAPI网关
前言 工作需要,第一次使用 Go 来实战项目. 需求:采用 golang 实现一个 webapi 的中转网关,将一些资源文件通过 http 协议上传至 FastDFS 分布式文件存储系统. 一.Fas ...
- python匿名函数 与 内置函数
一.匿名函数 1.定义: 匿名函数顾名思义就是指:是指一类无需定义标识符(函数名)的函数或子程序. 2.语法格式:lambda 参数:表达式 lambda语句中,开头先写关键字lambda,冒号 ...