标签:
题目:
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;
}
版权声明:转载请注明出处。
标签:
原文地址:http://blog.csdn.net/u014427196/article/details/47993793