HDU 3018 欧拉回路

HDU - 3018

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.    给出一个图,问几笔画才能经过所有边。    欧拉回路,知识点已经在上个博客提到。对于每个点的出度,如果存在奇数,那么需要奇数/2笔才能经过所有的点。    给出的图并没有说明是否为连通图,所以可能有多个图,那么这种情况,ans=奇数度个数/2+欧拉回路个数(只含偶数点的集合)  解析在代码里  
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;    //jishu/2+oulatu
int pr[maxn],cnt[maxn],mark[maxn];
int n,m;
int ans=0;
void first()
{
    for(int i=1;i<=n;i++)
    {
        pr[i]=i;
    }
    memset(cnt,0,sizeof(cnt));
    memset(mark,0,sizeof(mark));
    ans=0;
}
int find(int x)
{
    if(x!=pr[x])
        return pr[x]=find(pr[x]);
        return x;
}
void join(int a,int b)
{
    int f1=find(a),f2=find(b);
    if(f1!=f2)
        pr[f1]=f2;
        return;
}
void ac()
{
    for(int i=1;i<=n;i++)
    {
        if(cnt[i]%2!=0)
        {
            int f=find(i);    //统计奇数度点数量。用mark[]数组来记录,如果i点奇度,那么i所在图不是欧拉回路,那么i的根节点标为1,代表此图不是欧拉回路。
            mark[f]=1;
            ans++;
        }
    }
    ans/=2;
    for(int i=1;i<=n;i++)  //统计欧拉回路图
    {
        if(cnt[i]>0)      //比如输入,9  3  9个点只给出了3个关系,肯定有点不算,cnt[i]=0,不能纳入计算。
        {                
            int f=find(i);    //找到i的根节点,如果没被标为1,说明i出度为偶数,而且满足pr[i]==i(i==f)(即搜到x==pr[x]时还是没被标记)说明此图是个欧拉回路,因为如果存在奇度点,i==pr[i]                    //处肯定被标记了。      ans++;
            if(mark[f]==0&&pr[i]==i)
            {
                ans++;
            }
        }
    }
}
int main()
{
    while(cin>>n>>m)
    {
        first();    //初始化
        for(int i=1;i<=m;i++)
        {
            int a,b;
            cin>>a>>b;
            join(a,b);
            cnt[a]++;  //加入并查集,统计入度出度
            cnt[b]++;
        }
        ac();
        cout<<ans<<endl;
    }
    return 0;
}

  



原文地址:https://www.cnblogs.com/liyexin/p/11704873.html

时间: 2025-01-18 01:03:15

HDU 3018 欧拉回路的相关文章

HDU 2894 欧拉回路

#include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define N 20020 struct node{ int from, to, dou, nex; }edge[N]; int head[N], edgenum; void add(int u, int v,int dou)

hdu 1878 欧拉回路 (欧拉回路)

//1.联通图 2.顶点度数都为偶数 ,则存在欧拉回路. # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int father[1010]; int vis[1010][1010],du[1010]; int find(int x) { if(father[x]==x) return x; return father[x]=find(father[x]

[欧拉回路] hdu 3018 Ant Trip

题目链接: 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): 1658    Accepted Submission(s): 641 Problem Description Ant Country consist of N to

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

Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 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

HDU 1878 欧拉回路 (并查集+欧拉回路)

题目地址:HDU 1878 这个题要注意欧拉回路与欧拉通路的区别.在都保证连通性的前提下,欧拉回路要求每个点的度数都是偶数,而欧拉通路允许两个点的度数是奇数.所以这题用并查集判断连通性后判断下度数就可以了. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib

hdu 3018 Ant Trip

并查集+欧拉回路 对于每个连通的集合,如果该集合只有一个元素 那么不用管,如果该集合大于一个元素,那么求出奇度的个数,如果奇度个数是0,那么ans+1,否则ans+sum/2,sum为该集合内奇度的个数. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; const int max

hdu 1878 欧拉回路

欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回路? Input 测试输入包含若干测试用例.每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M:随后的M行对应M条边,每行给出一对

HDU 1878.欧拉回路【欧拉路及欧拉回路初接触】【8月2】

欧拉回路 Problem Description 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回路? Input 测试输入包含若干测试用例.每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M:随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号).当N为0时输入结 束. Output 每个测试用例的输出占一行,若欧拉回路存在则输出1,否则输出0.

hdu 1878 欧拉回路 水题。测试数据貌似有点问题

欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10135    Accepted Submission(s): 3708 Problem Description 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回路? Input 测试输入包含若干测试用例.每个测试用例