zoukankan      html  css  js  c++  java
  • hdu 1054(最小点覆盖集)

    Strategic Game

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


    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
     
    现在学了二分图直接可以用最小点覆盖集解,这题所有点都在同一集合里面,所以建造二分图的时候要连双向边,结果要除二。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include <algorithm>
    #include <math.h>
    using namespace std;
    const int N = 1505;
    struct Edge{
        int v,next;
    }edge[N*N];
    int head[N],tot;
    void addEdge(int u,int v,int &k){
        edge[k].v = v,edge[k].next = head[u],head[u] = k++;
    }
    void init(){
        memset(head,-1,sizeof(head));
        tot = 0;
    }
    int linker[N];
    bool vis[N];
    int n;
    bool dfs(int u){
        for(int k=head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v;
            if(!vis[v]){
                vis[v] = true;
                if(linker[v]==-1||dfs(linker[v])){
                    linker[v] = u;
                    return true;
                }
            }
        }
        return false;
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF){
            init();
            for(int i=1;i<=n;i++){
                int u,v,num;
                scanf("%d:(%d)",&u,&num);
                for(int j=1;j<=num;j++){
                    scanf("%d",&v);
                    addEdge(u,v,tot);
                    addEdge(v,u,tot);
                }
            }
            int res = 0;
            memset(linker,-1,sizeof(linker));
            for(int i=0;i<n;i++){
                memset(vis,0,sizeof(vis));
                if(dfs(i)) res++;
            }
            printf("%d
    ",res/2);
        }
        return 0;
    }
  • 相关阅读:
    FIND-S:寻找极大特殊假设
    XPath实例教程
    XML:四种解析器(dom,sax,jdom,dom4j)原理及性能比较
    XML文件解析之--DOM与SAX
    JAVA中内部类(匿名内部类)访问的局部变量为什么要用final修饰?
    java中的匿名内部类总结
    java中的new BufferedReader(new InputStreamReader(System.in))
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    区分XML中CDATA和#PCDATA
    浅谈get 和post的区别
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5712797.html
Copyright © 2011-2022 走看看