tjut 3018

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值