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 ...
随机推荐
- scrapy中使用 IP 代理
在 scrapy 中使用 ip 代理需要借助中间件的功能 首先在settings 中设置好中间件,中间件优先级数字越小越先被执行 , } 然后编写中间件,拦截请求设置代理 class ProxyMid ...
- 怎么HTML表格中的所有字体居中?
一开始,我在table标签里加入align="center" 发现没什么用.... 后来在css里加入,就可以了 成果如图:
- error C2664: “FILE *fopen(const char *,const char *)”: 无法将参数 1 从“LPCTSTR”转换为“const char *”
遇到这个问题,请打开本项目的Properties(属性)-------> Configuration Properties(配置属性)-------->General(常规)------- ...
- sqlalchemy 连接mysql8.0报 RuntimeError: cryptograpy si requeired for sha256_password 错误
cryptography is required for sha256_password or caching_sha2_password 需要cryptography模块的支持才能连接需要sha25 ...
- How to recover if NMC cound not connect
Some times we suddently find that the NMC can not login,. You would see the sybase database error if ...
- 郁闷的 IE6/7/8 所遇兼容问题
IE6,7只支持inline元素设置为inline-block,但不支持block元素转换成inline-block,所以非inline元素在IE6,7下要转换成inline-block,需先转换成i ...
- Java中进行Md5加密
java文件 https://pan.baidu.com/s/1kXcif35 密码:3cjd 代码案例: package cn.itcast.estore.utils; import java.m ...
- python 中的 赋值 浅拷贝 深拷贝
1.对象的赋值 都是进行对象引用(内存地址)传递,即 b is a ,a 变 b也变 2.浅拷贝 会创建一个新的对象,对于对象中的元素,浅拷贝就只会使用原始元素的引用(内存地址) 当我们使用下面的操作 ...
- 十八 Spring的JDBC模板:引入外部属性文件
配置外部属性文件 配置文件里引入属性文件,两种方式 第一种: 第二种: 引入属性文件的值: 测试: <?xml version="1.0" encoding="UT ...
- mysql遇到java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
在连接数据库的url中,加上allowPublicKeyRetrieval=true from:https://blog.csdn.net/Gushiyuta/article/details/9323 ...