【VK Cup 2012】Codeforces 209C Trails and Glades

177 篇文章 1 订阅
87 篇文章 1 订阅

Vasya went for a walk in the park. The park has n glades, numbered
from 1 to n. There are m trails between the glades. The trails are
numbered from 1 to m, where the i-th trail connects glades xi and yi.
The numbers of the connected glades may be the same (xi = yi), which
means that a trail connects a glade to itself. Also, two glades may
have several non-intersecting trails between them.

Vasya is on glade 1, he wants to walk on all trails of the park
exactly once, so that he can eventually return to glade 1.
Unfortunately, Vasya does not know whether this walk is possible or
not. Help Vasya, determine whether the walk is possible or not. If
such walk is impossible, find the minimum number of trails the
authorities need to add to the park in order to make the described
walk possible.

Vasya can shift from one trail to another one only on glades. He can
move on the trails in both directions. If Vasya started going on the
trail that connects glades a and b, from glade a, then he must finish
this trail on glade b.

Input The first line contains two integers n and m (1 ≤ n ≤ 106;
0 ≤ m ≤ 106) — the number of glades in the park and the number of
trails in the park, respectively. Next m lines specify the trails. The
i-th line specifies the i-th trail as two space-separated numbers, xi,
yi (1 ≤ xi, yi ≤ n) — the numbers of the glades connected by this
trail.

Output Print the single integer — the answer to the problem. If
Vasya’s walk is possible without adding extra trails, print 0,
otherwise print the minimum number of trails the authorities need to
add to the park in order to make Vasya’s walk possible.

目标是使原图联通并且所有点的度数都为偶数。每次贪心的选取奇度数点最多的两块连接,复杂度O(nlogn)。
注意处理孤立点,因为只要求覆盖所有边,除了1点以外【强制经过1点】其他孤立点都不必连接。

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
priority_queue<int> que;
int fa[1000010],du[1000010],cnt[1000010];
int rd()
{
    int x=0;
    char c=getchar();
    while (c<'0'||c>'9') c=getchar();
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return x;
}
int find(int x)
{
    return x==fa[x]?x:fa[x]=find(fa[x]);
}
int main()
{
    int i,j,k,m,n,p,q,x,y,z,ans=0;
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++)
      fa[i]=i;
    for (i=1;i<=m;i++)
    {
        x=rd();
        y=rd();
        du[x]++;
        du[y]++;
        fa[find(x)]=find(y);
    }
    for (i=1;i<=n;i++)
      if (du[i]&1)
        cnt[find(i)]++;
    for (i=1;i<=n;i++)
      if (fa[i]==i&&(du[i]||i==1))
        que.push(cnt[i]);
    while (que.size()>1)
    {
        x=que.top();
        que.pop();
        y=que.top();
        que.pop();
        ans++;
        if (x) x--;
        else x++;
        if (y) y--;
        else y++;
        que.push(x+y);
    }
    printf("%d\n",ans+que.top()/2);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值