HDU 3018 Ant Trip (欧拉路的个数 并查集)

Ant Trip

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

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

题目大意与分析

一笔画问题,求将所有的边都只走一次所需要的最少笔画数,首先用并查集维护连通图,对于每个连通图,如果所有的点度都为偶数,代表有一个欧拉路,如果只有一个点,则不需要笔画,对于有度为奇数的点,需要的笔画数为奇度点个数/2

#include<bits/stdc++.h>

using namespace std;

int f[100005],deg[100005],g[100005],j[100005],anss,cnt,n,m;

int findf(int x)
{
    return x==f[x]?x:f[x]=findf(f[x]);
}

void hebing(int a,int b)
{
    int fa=findf(a);
    int fb=findf(b);
    if(fa!=fb)
    {
        f[fa]=fb;
    }
}

int main()
{
    while(cin>>n>>m)
    {
        anss=0;
        memset(deg,0,sizeof(deg));
        memset(g,0,sizeof(g));
        memset(j,0,sizeof(j));
        for(int i=1;i<=n;i++)
        f[i]=i;
        cnt=0;
        while(m--)
        {
            int a,b;
            cin>>a>>b;
            hebing(a,b);
            deg[a]++;
            deg[b]++;
         }
        for(int i=1;i<=n;i++)
        {
            if(findf(i)==i)
            {
                g[cnt++]=i;
            }
            if(deg[i]%2==1)
            {
                j[findf(i)]++;
            }
        }
        for(int i=0;i<cnt;i++)
        {
            if(deg[g[i]]==0)
            continue;
            if(j[g[i]]==0)
            anss++;
            else
            anss+=j[g[i]]/2;
        }
        cout<<anss<<endl;
    }
 } 

原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/12635208.html

时间: 2025-01-09 08:54:59

HDU 3018 Ant Trip (欧拉路的个数 并查集)的相关文章

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

题意: Ant Tony和他的朋友们想游览蚂蚁国各地. 给你蚂蚁国的N个点和M条边,现在问你至少要几笔才能所有边都画一遍.(一笔画的时候笔不离开纸) 保证这M条边都不同且不会存在同一点的自环边. 也就是蚂蚁分组遍历整个无向图,他们试图把所有的人分成几个小组,每个小组可以从不同的城镇开始. Tony想知道最少需要几组.  Input输入包含多组测试用例,由多个空行分隔. 每个测试用例的第一行是两个整数N(1<=N<=100000).M(0<=M<=200000),表明蚂蚁国有N个城镇

[欧拉回路] 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) Total Submission(s): 1826    Accepted Submission(s): 702 Problem Description Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,

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 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

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 思路分析:题目可以看做一笔画问题,求最少画多少笔可以把所有的边画一次并且只画一次: 首先可以求出该无向图中连通图的个数,在每个无向连通图中求出需要画的笔数再相加即为所求.在一个无向连通图中,如果所有的点的度数为偶数则存在一个欧拉回路, 则只需要画一笔即可:如果图中存在度数为奇数的点,则需要画的笔数为度数为奇数的点的个数 /2:需要注意的孤立的点不需要画: 代码如下: #include <cst

[ACM] POJ 2513 Colored Sticks (Trie树,欧拉通路,并查集)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 29736   Accepted: 7843 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a st

hdu 1829 A Bug&#39;s Life(分组并查集(偏移量))

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9204    Accepted Submission(s): 2961 Problem Description Background Professor Hopper is researching the sexual behavior of a rare sp

HDU 3038 How Many Answers Are Wrong(种类并查集)

题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[x] y - x = s 可以推出b - a = rank[y] - rank[x] + s; 并查集 延迟更新什么的,都忘了啊. 还有这题,如果是x--的话,记得更新0的根. #include <cstring> #include <cstdio> #include <stri