hdu 1054 Strategic Game (二分匹配)

Strategic Game


Time Limit: 20000/10000 MS
(Java/Others)    Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 4697    Accepted
Submission(s): 2125

Problem 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.

The input file contains several data sets in
text format. Each data set represents a tree with the following
description:

the number of nodes
the description of each node in the
following format
node_identifier:(number_of_roads) node_identifier1
node_identifier2 ... node_identifier
or
node_identifier:(0)

The
node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n
<= 1500). Every edge appears only once in the input data.

For example
for the tree: 

 

the
solution is one soldier ( at the node 1).

The output should be printed on
the standard output. For each given input data set, print one integer number in
a single line that gives the result (the minimum number of soldiers). An example
is given in the following table:

Sample Input

4

0:(1) 1

1:(2) 2 3

2:(0)

3:(0)

5

3:(3) 1 4 2

1:(1) 0

2:(0)

0:(0)

4:(0)

Sample Output

1

2

Source

Southeastern
Europe 2000

Recommend

JGShining   |   We have
carefully selected several similar problems for you:  1083 1507 1269 1528 1052

题意:求最少要多少个士兵站岗可使全部点都能观察得到。

因为是树形结构,每个点都会有一条边,最大匹配数即为题解(不证明了..)。

和 hdu1068
差不多,一样的模板,不过这题是求最大匹配数,匈牙利后结果除二即为解。


 1 //234MS    280K    1166 B    C++
2 #include<stdio.h>
3 #include<string.h>
4 #define N 1505
5 struct node{
6 int v;
7 int next;
8 }edge[10*N];
9 int match[N];
10 int vis[N];
11 int head[N];
12 int n,edgenum;
13 void addedge(int u,int v)
14 {
15 edge[edgenum].v=v;
16 edge[edgenum].next=head[u];
17 head[u]=edgenum++;
18 }
19 int dfs(int x)
20 {
21 for(int i=head[x];i!=-1;i=edge[i].next){
22 int v=edge[i].v;
23 if(!vis[v]){
24 vis[v]=1;
25 if(match[v]==-1 || dfs(match[v])){
26 match[v]=x;
27 return 1;
28 }
29 }
30 }
31 return 0;
32 }
33 int hungary()
34 {
35 int ret=0;
36 memset(match,-1,sizeof(match));
37 for(int i=0;i<n;i++){
38 memset(vis,0,sizeof(vis));
39 ret+=dfs(i);
40 }
41 return ret;
42 }
43 int main(void)
44 {
45 int a,b,m;
46 while(scanf("%d",&n)!=EOF)
47 {
48 edgenum=0;
49 memset(head,-1,sizeof(head));
50 for(int i=0;i<n;i++){
51 scanf("%d:(%d)",&a,&m);
52 while(m--){
53 scanf("%d",&b);
54 addedge(a,b);
55 addedge(b,a);
56 }
57 }
58 printf("%d\n",hungary()/2);
59 }
60 return 0;
61 }

hdu 1054 Strategic Game (二分匹配)

时间: 2024-11-03 03:24:39

hdu 1054 Strategic Game (二分匹配)的相关文章

hdu 4185 Oil Skimming(二分匹配)

Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 883    Accepted Submission(s): 374 Problem Description Thanks to a certain "green" resources company, there is a new profitable

HDU 1281 棋盘游戏(二分匹配 与 删边)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 根据题目描述,什么是重要点?在求出最大匹配后,进行枚举,依次删边,看最大匹配数会不会发生改变,改变的话,那么该点就是重要点. #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <

HDU 2119 Matrix 简单二分匹配

行做x集,列做y集,1就给该行该列连一条边,输出最大匹配边即可 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<set> using namespace std; #define N 105 int lef[N], pn;//lef[v]表示Y集的点v 当前连接的点 , pn为x点集的

HDU 1083 裸的二分匹配

Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3424    Accepted Submission(s): 1650 Problem Description Consider a group of N students and P courses. Each student visits zero, one or

hdu 1054 Strategic Game (最小顶点覆盖+稀疏图)

Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4889    Accepted Submission(s): 2225 Problem Description Bob enjoys playing computer games, especially strategic games, but some

hdu 1281 棋盘游戏 (二分匹配)

//是象棋里的车 符合二分匹配 # include<stdio.h> # include<algorithm> # include<string.h> using namespace std; int n,m,pp[110][110],map[110],vis[110]; int bfs(int x) { for(int i=1;i<=m;i++) { if(!vis[i]&&pp[x][i]) { vis[i]=1; if(!map[i]||bf

hdu 1281 棋盘游戏(二分匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2905    Accepted Submission(s): 1702 Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽

HDU 1150 Machine Schedule (二分匹配)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

HDU 1045 Fire Net (二分匹配)

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street