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 ...
随机推荐
- Redis数据库与python的交互
1.安装redis模块:pip install redis 2.安装好以后主要使用redis模块中的StrictRedis对象,用于连接redis服务器 3.代码如下: from redis impo ...
- 循环读取寄存器(QSFP-DD)并且分别保存log
#!/bin/bash ####################################################################### #Created by: Bin ...
- tensorflow版helloworld---拟合线性函数的k和b(02-4)
给不明白深度学习能干什么的同学,感受下深度学习的power import tensorflow as tf import numpy as np #使用numpy生成100个随机点 x_data=np ...
- R rep() 函数
函数 rep(x,...) rep(x,times = n) 将向量 x 重复 n 次 rep(x,each = n) 将向量 x 的每个元素重复 n 次 在参数缺省情况下,为参数 times
- 问题解决 : org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
问题分析: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): ,即在mybatis中da ...
- Py西游攻关之基础数据类型(三)-元组
Py西游攻关之基础数据类型 - Yuan先生 https://www.cnblogs.com/yuanchenqi/articles/5782764.html 六 tuple(元组) 元组被称为只读列 ...
- SpringCloud+Eureka+Feign+Ribbon的简化搭建流程,加入熔断,网关和Redis缓存[2]
目录 前提:本篇是基于 SpringCloud+Eureka+Feign+Ribbon的简化搭建流程和CRUD练习[1] 的修改与拓展 1.修改consumer的CenterFeign.java,把返 ...
- 117、Java中String类之去掉左右空格
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- OC中NSLog函数输出格式罗列
格式 类型 格式 类型 格式 类型 %@ 对象 %d,%i 整数 %u 无符整数 %f 浮点 %x,%X 二进制整数 %o 八进制整数 %zu size_t %p 指针 %e 浮点(科学计算) %g ...
- 题解 P3950 【部落冲突】
树链剖分吼啊 一看就看出是LCT模板题啦 前记 见这么多人写LCT,却很少人写树链剖分,于是我就来一发树链剖分(其实是因为自己不会LCT) 本蒟蒻的写法和诸位写树链剖分的大神有点不同 思路 树链剖分, ...