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. 

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的更多相关文章

  1. Ant Trip HDU - 3018(欧拉路的个数 + 并查集)

    题意: Ant Tony和他的朋友们想游览蚂蚁国各地. 给你蚂蚁国的N个点和M条边,现在问你至少要几笔才能所有边都画一遍.(一笔画的时候笔不离开纸) 保证这M条边都不同且不会存在同一点的自环边. 也就 ...

  2. [欧拉回路] hdu 3018 Ant Trip

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others) ...

  3. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  4. HDU 3018 Ant Trip (欧拉回路)

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. HDU 3018 Ant Trip(欧拉回路,要几笔)

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. HDU 3108 Ant Trip

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. HDU3018:Ant Trip(欧拉回路)

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. hdu-3018 Ant Trip(欧拉路径)

    题目链接: Ant Trip Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  9. HDU 3018 欧拉回路

    HDU - 3018 Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,together ...

随机推荐

  1. 【代码总结】PHP文件的上传和下载

    ===================== 文件上传和下载 ===================== 一.php.ini的配置信息 file_uploads = On /Off    是否允许文件上 ...

  2. nginx日志模块与HTTP过滤模块与sub模块修改返回内容

    日志格式使用指令 指令介绍 Syntax: log_format name [escape=default|json|none] string ...; Default: log_format com ...

  3. 【转】spring IOC和AOP的理解

    spring 的优点?1.降低了组件之间的耦合性 ,实现了软件各层之间的解耦 2.可以使用容易提供的众多服务,如事务管理,消息服务等 3.容器提供单例模式支持 4.容器提供了AOP技术,利用它很容易实 ...

  4. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:联合使用所有表格类

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. springboot 跨域

    参考: https://blog.csdn.net/qq779446849/article/details/53102925 https://blog.csdn.net/wo541075754/art ...

  6. VS Code 入门

    将VSCode设置成中文语言环境 快捷键[Ctrl+Shift+P]—输入[Configure Display Language]—将“en”改为“zh-CN”—打开extention输入[Chine ...

  7. 物联网协议CoAP协议学习

    CoAP:Constrained Application Protocol协议是为物联网中资源受限的设备制定的应用层协议,即简化版的基于UDP的HTTP协议.其核心内容为资源抽象.REST式交互可扩展 ...

  8. 获取一个元素距离顶部的位置和window的滚动值

    获取一个元素距离顶部的位置: $(".box").offset().top; 获取window的滚动值: $(window).scrollTop();

  9. Codeforces1303E. Erase Subsequences

    转换一下题意,就相当于问t能不能和s中2个不相同的子串相同,我们可以将t串拆成2个子串t1,t2,得到状态dp[i][j][k]=0/1,表示s判断到i位,t1判断到j位,t2判断到k位,0/1表示是 ...

  10. 「luogu1613」跑路

    传送门 Luogu 解题思路 对于所有可以用 \(2^k\) 形式表示的 \(dis(i,j)\),将\(i,j\)之间的 \(dis\) 置为 \(1\),可以用倍增 \(\text{Floyd}\ ...