uva 10968 - KuPellaKeS

KuPellaKes

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 Basm’s works.

Recently, he managed to invent a Time Machine™ 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.

 

The Input

Input consists of several test-cases. Each test-case starts with a line containing three numbers m indicating the number of cities and roads in KuPellaKes respectively. Next m lines, each containing two numbers  indicating that there is a road between the th and the th city. Note that all the roads are bidirectional. Input will be terminated with a line containing three zeros.

 

The Output

For each test-case, your program should output the minimum number of roads that should be destroyed. In the case that this task is impossible the phrase “Poor Koorosh” should be printed.

 

Sample Input

4 5

1 2

2 3

3 4

4 1

1 3

0 0

 

Sample Output

1


Amirkabir University of Technology - Local Contest - Round #2


容易看出原图中要么没有奇点,要么有2个。减少一条边,会使总度数减2,而最终的图,所有点的度数都必须是偶数,这个2度只能减在原图中的2个奇点,其他的点被减的度数都是2,这么看来,就是找一条路从一个奇点到另一个奇点。而要边数最少,所以是求最短路。注意,题目要求剩下的点的度数不能为0。

#include<cstdio>
#include<map>
#include<queue>
#include<cstring>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 1000 + 5;
const int INF = 1000000000;
typedef long long LL;
typedef pair<int, int> P;

vector<int> G[maxn];
int degree[maxn];
int S, T;
int vis[maxn];

int bfs(){
    queue<P> q;
    while(!q.empty()) q.pop();
    q.push(P(S, 0));
    memset(vis, 0, sizeof(vis));
    vis[S] = 1;
    while(!q.empty()){
        P p = q.front();
        q.pop();
        int pos = p.first;
        int step = p.second;
        if(G[pos].size()<=2) continue;
        if(pos == T) return step;
        for(int i = 0;i < G[pos].size();i++){
            int to = G[pos][i];
            if(!vis[to]){
                vis[to] = 1;
                q.push(P(to, step+1));
            }
        }
    }
    return -1;
}

int main(){
    int n ,m;
    while(scanf("%d%d", &n, &m)){
        if(n == 0 && m == 0) break;
        for(int i = 1;i <= n;i++){
            G[i].clear();
        }
        memset(degree, 0, sizeof degree);
        while(m--){
            int u, v;
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
            degree[u]++;
            degree[v]++;
        }
        S = T = -1;
        int ans = INF;
        for(int i = 1;i <= n;i++){
            if(degree[i]==0){
                ans = -1;
            }
        }
        for(int i = 1;i <= n;i++){
            if(degree[i]%2==1){
                S = i;
                break;
            }
        }
        for(int i = n;i >= 1;i--){
            if(degree[i]%2==1){
                T = i;
                break;
            }
        }
        if(S == T){
            ans = min(ans, 0);
        }
        else
            ans = min(ans, bfs());
        if(ans == -1)
            printf("Poor Koorosh\n");
        else
            printf("%d\n", ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值