程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVa 2038 - Strategic game(二分圖最小頂點覆蓋 or 樹形DP)

UVa 2038 - Strategic game(二分圖最小頂點覆蓋 or 樹形DP)

編輯:C++入門知識

UVa 2038 - Strategic game(二分圖最小頂點覆蓋 or 樹形DP)


Strategic game

Description

Download as PDF

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:

\

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

Input

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

Output

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

 

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)

 

題意:給定一棵樹,選擇盡量少的點,使得每個沒有選中的結點至少和一個已經選中的結點相鄰。輸出最少需要選擇的節點數。

思路:經典的二分圖最小頂點覆蓋, 也是經典的樹形 DP 。

最小頂點覆蓋 == 最大匹配(雙向圖)/2
數據較大,用鄰接表。不然會超時。
 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;

const int MAXN = 1510;
int nx, ny;
int used[MAXN];
int cx[MAXN], cy[MAXN];
vectorg[MAXN];

int Find(int u)
{
    for(int i = 0; i < g[u].size(); i++)
    {
        int v = g[u][i];
        if(!used[v])
        {
            used[v] = 1;
            if(cy[v]==-1 || Find(cy[v]))
            {
                cy[v] = u;
                cx[u] = v;
                return 1;
            }
        }
    }
    return 0;
}

int Hungary()
{
    int res = 0;
    memset(cx, -1, sizeof(cx));
    memset(cy, -1, sizeof(cy));
    for(int i = 0; i < nx; i++)
    {
        if(cx[i] == -1)
        {
            memset(used, 0, sizeof(used));
            if(Find(i))
                res++;
        }
    }
    return res;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int t, n, num;
    int x, y;
    while(cin>>n)
    {
        if(!n)
            break;
        ny = nx = n;
        t = n;
        for(int i = 0; i < n; i++)
            g[i].clear();
        while(t--)
        {
            scanf("%d:(%d)", &x, &num);
            while(num--)
            {
                scanf("%d", &y);
                g[x].push_back(y);
                g[y].push_back(x);
            }
        }
        printf("%d\n", Hungary()/2);
    }
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved