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 maxn=100010;
int father[maxn],summ[maxn],ff[maxn],tong[maxn];
int n,m,i,u,v;
vector<int>b;

int findd(int x)
{
    if(x!=father[x]) father[x]=findd(father[x]);
    return father[x];
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
    for(i=0;i<=n;i++) father[i]=i;
    memset(summ,0,sizeof(summ));
    memset(tong,0,sizeof(tong));
    memset(ff,0,sizeof(ff));
    b.clear();

    for(i=0;i<m;i++)
    {
        scanf("%d%d",&u,&v);
        int fu=findd(u);
        int fv=findd(v);
        summ[u]++;summ[v]++;
        if(fu!=fv) father[fu]=fv;
    }

    for(i=1;i<=n;i++)
    {
        int xx=findd(i);
        if(ff[xx]==0)
        {
            b.push_back(xx);
            ff[xx]=1;
        }
        if(summ[i]%2==1) tong[xx]++;
    }
    int anss=0;
    for(i=0;i<b.size();i++)
    {
        if(summ[b[i]]==0) continue;//这个集合只有一个点 忽略
        if(tong[b[i]]==0)//这个集合无奇度节点
            anss++;
        else anss=anss+tong[b[i]]/2;
    }
    printf("%d\n",anss);
    }
    return 0;
}
时间: 2024-12-05 15:12:44

hdu 3018 Ant Trip的相关文章

[欧拉回路] 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 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)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,

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

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

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

HDU3018:Ant Trip(欧拉回路)

Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2461    Accepted Submission(s): 965 Problem Description Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,t

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 ever

Ant Trip(欧拉回路+并查集)

Ant Trip 题目描述 原题来自:2009 Multi-University Training Contest 12 - Host by FZU 给你无向图的 N 个点和 M 条边,保证这 M 条边都不同且不会存在同一点的自环边,现在问你至少要几笔才能所有边都画一遍.(一笔画的时候笔不离开纸) 输入格式 多组数据,每组数据用空行隔开. 对于每组数据,第一行两个整数 N,M表示点数和边数.接下去 M 行每行两个整数 a,b,表示 a,b 之间有一条边. 输出格式 对于每组数据,输出答案. 样例