P - Ant Trip

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

题意:n个村子间有m条路,每条路只能走一次,问最少需要多少队人能走完所有的路

解法:典型的欧拉图,先判断图中有多少联通分量,再判断每个联通分量度为奇数的点的个数,则每个联通分量所需的队伍数量为1或者为奇数点的个数/2中的最大值

<span style="font-size:18px;">#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
vector<int>edge[100010];
int dis[100010],vis[100010],odd[100010];//dis记录每个点的联通分量,vis记录每个点出现的次数,记录每个联通分量里度数为奇数的个数
int main()
{
    int m,n,i;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            edge[i].clear();
            dis[i]=0;
            vis[i]=0;
            odd[i]=0;
        }
        for(i=1;i<=m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            vis[a]++;
            vis[b]++;
            edge[a].push_back(b);//注意,这里两边都要进表,因为如果不这样,若a>b就要出问题
            edge[b].push_back(a);
        }
        queue<int>q;
        int k=0;
        for(i=1;i<=n;i++)
        {
            if(dis[i]==0)
            {
                if(vis[i]==0)//孤立的点不管
                    continue;
                dis[i]=++k;
            }
            else
                continue;
            q.push(i);
            while(!q.empty())
            {
                int num=q.front();
                q.pop();
                for(int j=0;j<edge[num].size();j++)//遍历在一条联通分量上的点
                {
                    int val=edge[num][j];
                    if(dis[val]==0)//此点还未标记联通分量
                    {
                        dis[val]=k;
                        q.push(val);
                    }
                }
            }
        }
        for(i=1;i<=n;i++)
        {
            if(vis[i]%2==1)
                odd[dis[i]]++;//记录各联通分量的度为奇数点的个数
        }
        int ans=0;
        for(i=1;i<=k;i++)
        {
            ans+=max(1,odd[i]/2);//求每天联通分量需要的队伍数量
        }
        printf("%d\n",ans);
    }
    return 0;
}
</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值