UVA 10968 KuPellaKes(贪心、最短路)

题意:

$N\le 2000个点的图,无重边自环,现要删去一些边$
$使得所有的点都是正偶度,保证图最多有2个奇度点$
$输出满足要求的要删去的最少的边,不能输出Poor Koorosh$

分析:

$首先奇度点只有0个或2个,因为total degree = 2|E|,是偶度,必有偶数个奇度点$
$首先check图是不是合法,有没有孤立节点,或者1度的奇度点(删了就变了0了)$
$如果0个奇度点,答案显然是0,不然就是2个奇度点的最短路$
$因为删去2个奇度点的一条边,如果路上经过偶度点,显然会删去2条边(1入1出)$
$最终肯定是个合法的图,最短路显然是最优答案咯$
$但是不能经过2度的偶度点(去掉2度就为0了)$
$时间复杂度为O(n+m)$

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// Created by TaoSama on 2016-03-27
// Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#include <cassert>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e3 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, m;
vector<int> G[N];
int d[N];
int bfs(int s, int t) {
memset(d, -1, sizeof d);
queue<int> q; q.push(s);
d[s] = 0;
while(q.size()) {
int u = q.front(); q.pop();
for(int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if(G[v].size() == 2) continue;
if(d[v] == -1) {
d[v] = d[u] + 1;
q.push(v);
}
}
}
return d[t];
}
int solve() {
for(int i = 1; i <= n; ++i) G[i].clear();
for(int i = 1; i <= m; ++i) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
int s, t; s = t = -1;
for(int i = 1; i <= n; ++i) {
if(G[i].size() <= 1) return -1;
if(G[i].size() & 1) {
if(s == -1) s = i;
else t = i;
}
}
//deg = 2E, so vertices of odd degree = 2
if(s == -1) return 0;
assert(t != -1);
return bfs(s, t);
}
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
while(scanf("%d%d", &n, &m) == 2 && (n || m)) {
int ans = solve();
if(~ans) printf("%d\n", ans);
else puts("Poor Koorosh");
}
return 0;
}


1. 除非注明,本博文即为原创,转载请注明链接地址
2. 本博文只代表博主当时的观点或结论,请不要恶意攻击
3. 如果本文帮到了您,不妨点一下 下面分享到 按钮,让更多的人看到