hdu-3018 Ant Trip(欧拉路径)
2016-04-21 17:47
411 查看
题目链接:
Memory Limit: 32768/32768 K (Java/Others)
[align=left]Problem Description[/align]
[align=left] [/align]
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.
[align=left]Input[/align]
[align=left] [/align]
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.
[align=left]Output[/align]
[align=left] [/align]
For each test case ,output the least groups that needs to form to achieve their goal.
[align=left]Sample Input[/align]
3 3
1 2
2 3
1 3
4 2
1 2
3 4
[align=left]Sample Output[/align]
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.
题意:
就是给你无向图,问多少笔能把这些边都画完,所以孤立的点不用管;
思路:
NND ,一开始bfs写的有问题,我还以为有什么WA点,就在网上找题解,我擦,TMD全都是并查集,而且代码都一样,真想说一句去他大爷的;
最后发现vis的位置不能乱放;
bfs找到的一个连通块中,如果是一个欧拉回路ans+1,如果里面有奇度的点,那么肯定是成对出现的,因为一条边对应两个点的度,那么一次可以选一对奇度的点为起始点和终止点,那么这个连通块的答案就是这个连通块中奇度点的个数/2;
AC代码:
Ant Trip
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
[align=left]Problem Description[/align]
[align=left] [/align]
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.
[align=left]Input[/align]
[align=left] [/align]
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.
[align=left]Output[/align]
[align=left] [/align]
For each test case ,output the least groups that needs to form to achieve their goal.
[align=left]Sample Input[/align]
3 3
1 2
2 3
1 3
4 2
1 2
3 4
[align=left]Sample Output[/align]
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.
题意:
就是给你无向图,问多少笔能把这些边都画完,所以孤立的点不用管;
思路:
NND ,一开始bfs写的有问题,我还以为有什么WA点,就在网上找题解,我擦,TMD全都是并查集,而且代码都一样,真想说一句去他大爷的;
最后发现vis的位置不能乱放;
bfs找到的一个连通块中,如果是一个欧拉回路ans+1,如果里面有奇度的点,那么肯定是成对出现的,因为一条边对应两个点的度,那么一次可以选一对奇度的点为起始点和终止点,那么这个连通块的答案就是这个连通块中奇度点的个数/2;
AC代码:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e6+5; const ll mod=1e9+7; int n,m,vis ,ind ,u,v; vector<int>ve ; queue<int>qu; int bfs(int num) { int cnt=0; qu.push(num); vis[num]=1; while(!qu.empty()) { int fr=qu.front(); qu.pop(); if(ind[fr]%2==1)cnt++; int len=ve[fr].size(); for(int i=0;i<len;i++) { int y=ve[fr][i]; if(!vis[y]) { vis[y]=1; qu.push(y); } } } return cnt; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i=0;i<=n;i++) { ind[i]=vis[i]=0; ve[i].clear(); } for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); ve[u].push_back(v); ve[v].push_back(u); ind[u]++; ind[v]++; } for(int i=1;i<=n;i++) { if(ind[i]==0)vis[i]=1; } int ans=0; for(int i=1;i<=n;i++) { if(!vis[i]) { int s=bfs(i); if(s==0)ans++; else ans+=s/2; } } printf("%d\n",ans); } return 0; }
相关文章推荐
- 安卓系统之五大布局
- Centos 7 中 QT出现QSqlDatabase: MYSQL driver not loaded
- 多线程程序设计的8个规则
- ubuntu-14.04 源码安装cntk笔记
- 推广下layer——一款含有弹层、日期、分页、模板组件的ku
- java小系统 数据库 图书馆
- java中的多线程
- 【POJ 3693】Maximum repetition substring 重复次数最多的连续重复子串
- 扑克牌顺子
- java事务的处理
- ZOJ3487-Ordinal Numbers
- 阵列卡高速缓存保护
- 简单的-写入txt文本,追加写入与覆盖写入
- 简单的JDBC编程步骤
- 仿QQ实现ListView中item的左右滑动同时实现ListView的上拉刷新和下拉加载更多
- kafka consumer频繁reblance
- 填充
- python编程关键字
- GCD多线程
- 十一、访问者模式(行为型模式)