http://acm.hdu.edu.cn/showproblem.php?pid=3018

Ant Trip

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3794    Accepted Submission(s): 1502

Problem Description
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.

 
Input
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.
 
Output
For 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
题目大意:给一个图【不保证连通】,求最少需要几画才能在不重复使用边的情况下走完全部边。
题目分析:一笔画模板题。
需要画数=每个连通图的画数之和。    每个连通图的画数=如果各个点的度全部是大于0的偶数则根据欧拉回路为1,如果存在奇数,则为奇数个数/2。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int in[];
int pre[];
int tot1[],tot2[];
int n,m;
int find(int x)
{
int xx=x;
while(x!=pre[x])
{
x=pre[x];
}
while(pre[xx]!=x)
{
int t=pre[xx];
pre[xx]=x;
xx=t;
}
return x;
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
memset(tot1,,sizeof(tot1));
memset(tot2,,sizeof(tot2));
memset(in,,sizeof(in));
for(int i = ; i <= n ; i++)
{
pre[i]=i;
}
memset(in,,sizeof(in));
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
in[a]++;
in[b]++;
if(find(a)!=find(b))
{
pre[find(a)]=find(b);
}
}
for(int i = ; i <= n ; i++)
{
int q=find(i);
if(in[i]%==&&in[i])
tot1[q]++;
else if(in[i]%)
tot2[q]++;
}
int sum=;
for(int i = ; i <= n ;i++)
{
if(tot2[i])
{
sum+=tot2[i]/;
}
else if(tot1[i])
{
sum++;
}
}
cout << sum << endl; }
return ;
}
 

【HDOJ3018】【一笔画问题】【欧拉回路+并查集】的更多相关文章

  1. nyist 42 一笔画 (欧拉回路 + 并查集)

    nyoj42 分析: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径. 若该路径是一个圈,则称为欧拉(Euler)回路. 具有欧拉回路的图称为欧拉图(简称E图).具有欧拉路 ...

  2. NYOJ 42 一笔画问题 (并查集+欧拉回路 )

    题目链接 描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下来. 规定,所有的边都只能画一次,不能重复画.   输入 第一行只有一个正整数 ...

  3. POJ2513——Colored Sticks(Trie树+欧拉回路+并查集)

    Colored Sticks DescriptionYou are given a bunch of wooden sticks. Each endpoint of each stick is col ...

  4. POJ 2513 Colored Sticks (欧拉回路+并查集+字典树)

    题目链接 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with ...

  5. ACM: FZU 2112 Tickets - 欧拉回路 - 并查集

     FZU 2112 Tickets Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u P ...

  6. HDU 1116 || POJ 1386 || ZOJ 2016 Play on Words (欧拉回路+并查集)

    题目链接 题意 : 有很多门,每个门上有很多磁盘,每个盘上一个单词,必须重新排列磁盘使得每个单词的第一个字母与前一个单词的最后一个字母相同.给你一组单词问能不能排成上述形式. 思路 :把每个单词看成有 ...

  7. UVA - 10129 Play on Words(欧拉回路+并查集)

    2.解题思路:本题利用欧拉回路存在条件解决.可以将所有的单词看做边,26个字母看做端点,那么本题其实就是问是否存在一条路径,可以到达所有出现过的字符端点.由于本题还要求了两个单词拼在一起的条件是前一个 ...

  8. poj 1386 Play on Words门上的单词【欧拉回路&&并查集】

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  9. LOJ-10108(欧拉回路+并查集)一个图至少用几笔画成

    题目链接:传送门 思路: 用并查集统计出每个区块奇数个节点的个数x,每个区块对笔画的贡献是max(x/2,1): 然后每个区块求和即可. #include<iostream> #inclu ...

  10. PAT甲题题解-1126. Eulerian Path (25)-欧拉回路+并查集判断图的连通性

    题目已经告诉如何判断欧拉回路了,剩下的有一点要注意,可能图本身并不连通. 所以这里用并查集来判断图的联通性. #include <iostream> #include <cstdio ...

随机推荐

  1. laravel中判断当前页面与连接地址是否一致,并添加效果:

  2. TNS

    Oracle中TNS的完整定义:transparence Network Substrate透明网络底层,监听服务是它重要的一部分,不是全部,不要把TNS当作只是监听器 ORACLE当中,如果想访问某 ...

  3. 笨办法11提问-raw_input

    源代码如下,有个改动 print "How old are you?", age = raw_input() print "How tall are you?" ...

  4. 【资料收集】OpenCV入门指南 系列文章

    <OpenCV入门指南>系列文章地址:http://blog.csdn.net/morewindows/article/category/1291764 目录: 第一篇  安装OpenCV ...

  5. (C/C++学习笔记) 七. 类型转换

    七. 类型转换 ● 隐式类型转换 隐式类型转换 implicit type conversions #include<iostream> using namespace std; void ...

  6. Python select 详解(转)

    I/O多路复用是在单线程模式下实现多线程的效果,实现一个多I/O并发的效果.看一个简单socket例子: import socket SOCKET_FAMILY = socket.AF_INET SO ...

  7. 浅议APC

    0x01  APC中断请求级别   在Intel x86体系结构中,外部硬件中断是通过处理器上的"中断管脚"或者一个称为"本地APIC(local APIC)" ...

  8. 数字签名-MD5

    MD5是信息摘要的意思,报文产生摘要是唯一的(1:1),而且是单向的(通过摘要反推不出源报文) java中的java.security.MessageDigest类,参考MessageDigest的功 ...

  9. org.quartz-scheduler 动态添加自动任务

    1.添加pom.xml <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId> ...

  10. Linux平台搭建-----C语言

    下面内容是新手上路,各位高手路过勿喷!因为我第一次发布,可能页面设置或者其他做的不好,还请见谅~该文章只是作为我学习C语言的笔记以及记录学习进程的. 零基础学习C语言---搭建Linux平台开发环境 ...