现在的位置: 首页 > 综合 > 正文

HDU 3018 Ant Trip 【欧拉图+并查集】

2018年01月21日 ⁄ 综合 ⁄ 共 2086字 ⁄ 字号 评论关闭

Ant Trip

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

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
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.
 /*
题解: 
很不错的一个题,需要判断有多少个欧拉图和连通图。
如果是欧拉图 ans++;
如果是连通图 ans+=图中度数为奇数的节点数/2
最后的ans就为所需组数 
*/
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int pre[100005],in[100005],count[100005];
bool vis[100005]; 

int find(int x)
{
    return x==pre[x]?x:pre[x]=find(pre[x]);
}

void join(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)
    {
        pre[fx]=fy;
    }
}

int main()
{
    int n,m,i,t,flag,fa,ans,a,b;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        memset(in,0,sizeof(in));
        memset(vis,0,sizeof(vis));
        memset(count,0,sizeof(count));
        for(i=1; i<=n; i++) pre[i]=i;
        for(i=1; i<=m; i++)
        {
            scanf("%d %d",&a,&b);
            in[a]++,in[b]++;//统计入度 
            join(a,b);
        }
        
        vector<int>q;//统计各个图的父节点 
        for(i=1,t=flag=0; i<=n; i++)
        {
             fa=find(i);
             if(!vis[fa])
             {
                q.push_back(fa);
                vis[fa]=1;
             }
             if(in[i]&1)
             {
                count[fa]++;
             }
        }
        for(i=0,ans=0; i<q.size(); i++)
        {
            if(!in[q[i]])//一个点 
                continue;  
            if(!count[q[i]])//欧拉图
            {
                ans++;
            }
            else//连通集 
                ans+=count[q[i]]/2; 
        }
        printf("%d\n",ans);
    }
    return 0;
}

抱歉!评论已关闭.