Day4 - K - Ant Trip HDU - 3018
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.
简述:一个有回路与通路的混合图,问几笔能画完,忽略孤立点。
思路:不打印解,就使用并查集找到每条路的"root",无向图,统计每条路上奇度数的点,队伍数 = 回路数 + 通路数(奇数点/2)
代码如下:
const int maxm = ; int degree[maxm], fa[maxm], vis[maxm], num[maxm], N, M;
vector<int> root; void init() {
memset(degree, , sizeof(degree)), memset(vis, , sizeof(vis)), memset(num, , sizeof(num));
root.clear();
for (int i = ; i <= N; ++i)
fa[i] = i;
} int Find(int x) {
if(x == fa[x])
return x;
return fa[x] = Find(fa[x]);
} void Union(int x,int y) {
int fx = Find(x), fy = Find(y);
if(fx != fy)
fa[fy] = fx;
} int main() {
while(scanf("%d%d",&N,&M) != EOF) {
init();
for (int i = ; i < M; ++i) {
int t1, t2;
scanf("%d%d", &t1, &t2);
degree[t1]++, degree[t2]++;
Union(t1, t2);
}
for(int i = ; i <= N; ++i) {
int f = Find(i);
if(!vis[f]) {
root.push_back(f);
vis[f] = ;
}
if(degree[i] % ) {
num[f]++;
}
}
int sum = ;
for (auto i = root.begin(); i != root.end(); ++i) {
if(degree[*i] == )
continue;
else if (num[*i] == )
sum++;
else if (num[*i])
sum += num[*i] / ;
}
printf("%d\n", sum);
}
return ;
}
为什么通路数是奇数点/2呢?(没学离散,后面填坑)直接画图就能证明必要性,回路也如此。
Day4 - K - Ant Trip HDU - 3018的更多相关文章
- Ant Trip HDU - 3018(欧拉路的个数 + 并查集)
题意: Ant Tony和他的朋友们想游览蚂蚁国各地. 给你蚂蚁国的N个点和M条边,现在问你至少要几笔才能所有边都画一遍.(一笔画的时候笔不离开纸) 保证这M条边都不同且不会存在同一点的自环边. 也就 ...
- [欧拉回路] 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) Problem ...
- 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(欧拉回路,要几笔)
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 ...
- HDU3018:Ant Trip(欧拉回路)
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu-3018 Ant Trip(欧拉路径)
题目链接: Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3018 欧拉回路
HDU - 3018 Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,together ...
随机推荐
- 面试问题之——给你图片的url,你能知道它所占的字节空间吗?
从一个需求说起 这标题起得有点标题党,实际情况是我最近在做一个公司内部工具时,遇到了这么一个需求,给定一个静态资源站点上某张图片的url(比如https://a.xxxcdn.com/demo.jpg ...
- ElementUI 删除 el-table 当前选中行(不是selection列)
一句话即可: this.表格绑定的data.splice(this.$refs.表格的ref.store.states.selection, 1)
- nginx访问目录是没加/的重定向控制
static 模块提供了root与alias功能:发现目标是目录时:但URI末尾未加/时:会返回301重定向:重定向后会加/ 指令 Syntax: server_name_in_redirect on ...
- 理解错误的 Arrays.asList()
简介 Arrays.asList() 作用是将一个数组转换为一个List 集合. String[] myArray = { "Apple", "Banana", ...
- sparkRDD:第3节 RDD常用的算子操作
4. RDD编程API 4.1 RDD的算子分类 Transformation(转换):根据数据集创建一个新的数据集,计算后返回一个新RDD:例如:一个rdd进行map操作后生了一个新的rd ...
- git rebase 与git merge 小结
git merge是用来合并两个分支的. $ git merge b 将b分支合并到当前分支 同样 $ git rebase b ,也是把 b分支合并到当前分支 ---------------- ...
- Azure DNS-
先看什么是DNS,通常来讲,DNS是将域名解析成IP的服务,例如www.azure.cn对应的IP地址是139.217.8.104 使用域名访问有如下好处: 1. 好记,使用特定的字母组合,代替ip地 ...
- Java连载78-深入自动拆装箱、Date类和SimpleDateFormat格式化
一.深入自动拆装箱 1.直接举例: public class D78_AutomaticUnpackingAndPacking{ public static void main(String[] ar ...
- IOS 错误原因
当xcode提示以下错误时,很可能的原因是由于ViewController中的View在Controller中连接了outlet,然后又删除了Controller中对应的属性,导致xcode找不到这个 ...
- 这两天的pwn学习总结
总是一会儿切到那里,一会儿切到那里,要明确一条主线,就是buu的题,而不是按着什么视频教程还有linux和python教程去学习.那样效率比较低. 一切为了写wp为本,不胡乱点击就是提高效率的最好办法 ...