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 ...
随机推荐
- Windows下使用MinGW在命令行编译运行C++程序
之前学习C语言的时候都是用IDE类似CodeBlocks的工具写完直接编译运行的,今天突然心血来潮,自己下一个编译器,在命令行下,编译运行C++程序,了解一下编译过程. 一.安装编译器 首先你需要下载 ...
- FM算法 的总结
FM的总结: 1.FM算法与线性回归相比增加了特征的交叉.自动选择了所有特征的两两组合,并且给出了两两组合的权重. 2.上一条所说的,如果给两两特征的组合都给一个权重的话,需要训练的参数太多了.比如我 ...
- ng2 学习笔记(三)依赖注入与服务
前两篇文章简单介绍了ng2的一些基础用法,基本和ng1的使用风格差不多,只是写法和开发方式变化比较大. 这一篇,来总结一下ng的依赖注入与服务.官方的教程上是把他分开来讲的,个人感觉放在一起比较容易理 ...
- selenium学习笔记(HTMLTestRunner测试报告)
之前提到selenium加入unittest框架.可以引入HTMLTestRunner扩展.以此来生成测试报告 首先是分享下载的百度云地址 http://pan.baidu.com/s/1pKUItW ...
- 为啥YII2 会出现 mcrypt_generic_init(): Key size is 0
解决方案如下: (关键) 示例代码中,加密解密类的实例创见通过 Class 同名方法 的方式创建. public function WXBizMsgCrypt($token, $encodingAes ...
- Django进阶Template篇002 - 模板包含和继承
包含 {% include %} 允许在模板中包含其他模板的内容. {% include "foo/bar.html" %} {% include template_name %} ...
- Codeforces Round #451 (Div. 2)
水题场.... 结果因为D题看错题意,B题手贱写残了...现场只出了A,C,E A:水题.. #include<bits/stdc++.h> #define fi first #defin ...
- LeetCode OJ:Valid Anagram(有效字谜问题)
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- PostgreSQL修改表空间
创建两个目录做表空间: mkdir /var/lib/pgsql/mydb_tbspace/ mkdir /var/lib/pgsql/java_tbspace/ 创建表空间: postgres=# ...
- 《Drools7.0.0.Final规则引擎教程》第3章 3.2 KIE概念&FACT对象
3.2.1 什么是KIE KIE(Knowledge Is Everything),知识就是一切的简称.JBoss一系列项目的总称,在<Drools使用概述>章节已经介绍了KIE包含的大部 ...