九野的博客,转载请注明出处:  http://blog.csdn.net/acmmmm/article/details/10858065

题意:n个点m条边的无向图,求用几笔可以把所有边画完(画过的边不再画)

思路:

并查集+欧拉回路

对于每个连通分量,若是欧拉回路则一笔画完,若不是则 需要: 奇度数点个数/2

然后把每个连通分量所需的笔数相加

这里要注意一个点是不用画的

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<set>
#include<math.h>
#include<string.h>
#define N 100010
using namespace std;
int f[N],d[N],fenliang[N];
int find(int x){
if(x==f[x])return x;
return f[x]=find(f[x]);
}
int main(){
int n,m,i,j,u,v;
while(~scanf("%d%d",&n,&m)){
for(i=1;i<=n;i++)f[i]=i;
memset(d,0,sizeof(d));
memset(fenliang,-1,sizeof(fenliang));
while(m--)
{
scanf("%d%d",&u,&v);
if(u>v){j=u;u=v;v=j;}
else if(u==v)continue;
f[find(u)]=find(v);
d[u]++,d[v]++;
}
int ans=0;
for(i=1;i<=n;i++)
{
find(i);
if(fenliang[f[i]]==-1 && d[i]>0)fenliang[f[i]]=0;
if(d[i]&1)fenliang[f[i]]++;
}
for(i=1;i<=n;i++)
if(fenliang[i]>0)ans+=fenliang[i]/2;
else if(fenliang[i]==0)ans++;
printf("%d\n",ans);
}
return 0;
}
/*
4 7
1 2
1 3
1 4
2 3
2 4
3 4
3 3 4 5
1 2
2 3
4 4
4 4
4 4 */

HDU 3018 Ant Trip的更多相关文章

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

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

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

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

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

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

  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 (并查集求连通块数+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...

  6. HDU 3108 Ant Trip

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

  7. hdoj 3018 Ant Trip(无向图欧拉路||一笔画+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 思路分析:题目可以看做一笔画问题,求最少画多少笔可以把所有的边画一次并且只画一次: 首先可以求出 ...

  8. HDU 3018 欧拉回路

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

  9. 一本通1530 Ant Trip

    1530:Ant Trip [题目描述] 原题来自:2009 Multi-University Training Contest 12 - Host by FZU 给你无向图的 N 个点和 M 条边, ...

随机推荐

  1. jQuery慢慢啃之文档处理(五)

    1.append(content|fn)//向每个匹配的元素内部追加内容. $("p").append("<b>Hello</b>"); ...

  2. Valid Phone Numbers

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  3. jquery获取form表单内容以及绑定数据到form表单

    在日常开发的过程中,难免会用到form表单,我们需要获取表单的数据保存到数据库,或者拿到后台的一串json数据,要将数据绑定到form表单上,这里我写了一个基于jquery的,formHelp插件,使 ...

  4. iOS 检测版本更新

    如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bundle versio ...

  5. EasyUI篇の日期控件

    页面代码: <input type="text" id='astartTime' class="easyui-datebox" style="w ...

  6. Github博客地址

    欢迎访问我的Github博客: J.R.Smith_blog

  7. cf B. Number Busters

    http://codeforces.com/contest/382/problem/B 题意:给你Aa,b,w,x,c,然后每经过1秒,c=c-1;  如果b>=x,b=b-x;否则 a=a-1 ...

  8. 关于用POI和EXCEL交互的问题

    废话不多说,直接通过例子来说明POI的使用: 1.一个创建excel并写入数据的小例子,参照网上的一个例子: public class CreateXL { /** * @param args */ ...

  9. delphi编程里的bool跟boolean类型有什么区别

    bool是LongBool类型. Delphi中定义了四种布尔类型:Boolean,ByteBool,WordBool和LongBool.后面三种布尔类型是为了与其他语言兼容而引入的,一般情况下建议使 ...

  10. 【HDOJ】3325 Arithmetically Challenged

    简单DFS. /* 3325 */ #include <iostream> #include <set> #include <cstdio> #include &l ...