HDU3018 Ant Trip

题意:每条边过且只过一次,问至少要画几笔才能全部边都经过。孤立的点忽视。

#include <iostream>
using namespace std;
const int M=100000+10;
int gree[M];
int father[M];
int rank1[M];
int save[M];
bool used[M];
bool mark[M];
void Make_Set(int x)
{
    father[x]=x;
    rank1[x]=0;
}
int Find(int x)
{
    int k=0;
    while(x!=father[x])
    {
        save[k++]=x;
        x=father[x];
    }
    for(int j=0; j<k; j++)
        father[save[j]]=x;
    return x;
}
void Union(int a,int b)
{

    int x=Find(a);
    int y=Find(b);
    if(x==y) return;
    if(rank1[x]<rank1[y]) father[x]=y;
    else
    {
        father[y]=x;
        if(rank1[x]==rank1[y]) rank1[x]++;
    }
}
int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m))
    {
        memset(gree,0,sizeof(gree));
        memset(used,0,sizeof(used));
        memset(mark,0,sizeof(mark));
        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if (!used[a])
            {
                used[a]=1;
                Make_Set(a);
            }
            if (!used[b])
            {
                used[b]=1;
                Make_Set(b);
            }
            gree[a]++;
            gree[b]++;
            Union(a,b);
        }
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            if(used[i]&&gree[i]%2==1)
            {
                if(mark[Find(i)]==0) mark[Find(i)]=1;//标记该块已经有奇数点了
                ans++;//计算总奇数点
            }
        }
        ans/=2;//累加,看有多少块没奇数点。。。mark[老大]==0没有奇数点
        for(int i = 1; i <= n; i++)
            if(used[i]&&father[i]==i&&mark[i]==0) ans++;
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-11-10 12:09:05

HDU3018 Ant Trip的相关文章

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

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

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

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

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,

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

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

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