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;
    }
  • 相关阅读:
    Sliverlight之 矢量绘图
    Silverlight之 xaml布局
    七天学会ASP.NET MVC(七)——创建单页应用
    MVC视图之间调用方法总结
    C#取得程序的根目录以及判断文件是否存在
    七天学会ASP.NET MVC (六)——线程问题、异常处理、自定义URL
    [C#] .NET4.0中使用4.5中的 async/await 功能实现异步
    C#中StreamReader读取中文文本出现乱码的解决方法
    七天学会ASP.NET MVC (五)——Layout页面使用和用户角色管理
    常用正则表达式
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5712797.html
Copyright © 2011-2022 走看看