POJ 1463 Strategic game(二分图最大匹配)
2013-09-02 23:53
387 查看
Description
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
For example for the tree:
View Code
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
For example for the tree:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 1510; const int MAXE = MAXN << 1; int head[MAXN]; int to[MAXE], next[MAXE]; int n, ecnt; void init() { memset(head, 0, sizeof(head)); ecnt = 2; } void add_edge(int u, int v) { to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++; to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++; } int link[MAXN], dep[MAXN]; bool vis[MAXN]; bool dfs(int u) { for(int p = head[u]; p; p = next[p]) { int &v = to[p]; if(vis[v]) continue; vis[v] = true; if(!~link[v] || dfs(link[v])) { link[v] = u; return true; } } return false; } int main() { while(scanf("%d", &n) != EOF) { init(); memset(dep, 0, sizeof(dep)); for(int i = 0; i < n; ++i) { int a, b, c; scanf("%d:(%d)", &a, &b); while(b--) { scanf("%d", &c); dep[c] = dep[a] + 1; add_edge(a, c); } } int ans = 0; memset(link, 255, sizeof(link)); for(int i = 0; i < n; ++i) { if(dep[i] & 1) continue; memset(vis, 0, sizeof(vis)); if(dfs(i)) ++ans; } printf("%d\n", ans); } }
View Code
相关文章推荐
- POJ - 1274 The Perfect Stall 二分图 最大匹配
- POJ 3041 Asteroids 二分图最大匹配
- poj 1274(二分图最大匹配)
- poj 2226 Muddy Fields(二分图最大匹配)
- POJ 2195 - Going Home(二分图最大权匹配)
- Poj 2112 [最大流] [二分图的多重匹配]
- poj 2446 二分图 最大匹配
- POJ 1274 / POJ 1469 / POJ 2239 二分图最大匹配
- poj 2239 Selecting Courses(二分图最大匹配or最大流)
- POJ 1087 A Plug for UNIX【二分图最大匹配】
- POJ-3041 匈牙利算法 二分图最大匹配
- poj 3020 Antenna Placement(二分图最大匹配)
- POJ-1469-COURSES(二分图最大匹配)
- poj 3041 Asteroids(二分图最大匹配)
- POJ 3020二分图的最大匹配
- poj 1469 二分图最大匹配
- poj 1469 COURSES (二分图最大匹配)
- poj 3894 System Engineer (二分图最大匹配--匈牙利算法)
- poj 1469 COURSES 二分图最大匹配数
- POJ 1486 Sorting Slides(二分图最大匹配:关键边)