UVA 10968 - KuPellaKeS 【BFS】

题目:
In ancient times, many territories were under the control of a powerful king called Basm. Basm is
well-known in history because of his strange works and as a result, there are many history-lovers who
wish to know more about him. Koorosh is one of them and he has worked hard to find a way to know
more about Basms works.
Recently, he managed to invent a Time MachineTM and traveled to the past to Basm time in order
to be able to see and study his weird works thoroughly. Unfortunately, he has been caught by royal
guard soldiers of Basm and is now in his prison. Basm ordered him to solve a problem if he wants to stay
alive. King Basm wants to change the structure of roads of his newly captured territory, KuPellaKes
in such a way that each city has an even number of neighboring cities. Now, he wants to know the
minimum number of roads that should be destroyed in order to satisfy this condition. Note that each
city must have at least one neighbor city after the road destruction process. Also, It should be noted
that in the given territory at most two cites of KuPellaKes have an odd number of neighboring cities
and there is at most one road between two cities. Also, there is no road from a city to itself.

题意:
给定一个图,要求把这个图删掉一些边后,使得他所以有点度数为不等于0的偶数,保证图一开始最多只有最多2个奇数度数结点。求最小删边数

解法:求奇数点间的距离。奇数点为 0 个,答案为0。

代码:

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#include <queue>

using namespace std;

int n, m;
vector<int> g[1010];
int du[1010];
int cnt[1010];
int st, ed;

bool solve()
{
    st = ed = 0;
    for (int i = 1; i <= n; i++)
    {
        if (du[i] == 0 || du[i] == 1) return false;
        if (du[i] % 2 == 1)
        {
            if (st == 0) st = i;
            else ed = i;
        }
    }

    if (st == 0)
    {
        printf("0\n");
        return true;
    }

    memset(cnt, -1, sizeof(cnt));
    queue<int> q;
    while (!q.empty()) q.pop();
    q.push(st);
    cnt[st] = 0;

    while (!q.empty())
    {
        int tp = q.front();q.pop();
        if (tp == ed)
        {
            printf("%d\n",cnt[tp]);
            return true;
        }
        for (int i = 0; i < g[tp].size(); i++)
        {
            int tt = g[tp][i];
            if (du[tt] == 2) continue;
            if (cnt[tt] != -1) continue;
            cnt[tt] = cnt[tp] + 1;
            q.push(tt);
        }
    }
    return false;
}

int main()
{
    while (~scanf("%d%d", &n, &m) && n)
    {
        memset(du,0,sizeof(du));
        for (int i = 0; i <= n; i++) g[i].clear();
        int a, b;
        while (m--)
        {
            scanf("%d%d", &a, &b);
            du[a]++; du[b]++;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if (!solve()) printf("Poor Koorosh\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值