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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054

    思路:最小顶点覆盖 == 最大匹配(双向图)/2。。。不过一开始是用邻接矩阵,傻傻的TLE。。。然后搞了个vector,建立邻接表。。。

    View Code
     1 #include<iostream>
     2 #include<vector>
     3 const int MAXN=1507;
     4 using namespace std;
     5 int n;
     6 bool mark[MAXN];
     7 int lx[MAXN],ly[MAXN];
     8 vector<int>map[MAXN];
     9 
    10 int dfs(int u){
    11     for(int v=0;v<map[u].size();v++){
    12         int k=map[u][v];
    13         if(!mark[k]){
    14             mark[k]=true;
    15             if(ly[k]==-1||dfs(ly[k])){
    16                 ly[k]=u;
    17                 lx[u]=k;
    18                 return true;
    19             }
    20         }
    21     }
    22     return false;
    23 }
    24 
    25 int MaxMatch(){
    26     int res=0;
    27     memset(lx,-1,sizeof(lx));
    28     memset(ly,-1,sizeof(ly));
    29     for(int i=0;i<n;i++){
    30         if(lx[i]==-1){
    31             memset(mark,false,sizeof(mark));
    32             res+=dfs(i);
    33         }
    34     }
    35     return res;
    36 }
    37 
    38 
    39 int main(){
    40     while(~scanf("%d",&n)){
    41         int x,y,count;
    42         for(int i=0;i<n;i++)map[i].clear();
    43         for(int i=1;i<=n;i++){
    44             scanf("%d:(%d)",&x,&count);
    45             for(int j=1;j<=count;j++){
    46                 scanf("%d",&y);
    47                 //map[x][y]=map[y][x]=true;//超时了
    48                 //建立邻接表
    49                 map[x].push_back(y);
    50                 map[y].push_back(x);
    51             }
    52         }
    53         int ans=MaxMatch();
    54         printf("%d\n",ans/2);
    55     }
    56     return 0;
    57 }
    58 
    59 
    60                 
  • 相关阅读:
    localStroage 和sessionStorage的区别
    audio 在ios无法播放问题解决
    判断是否在微信浏览器中打开
    使用CSS样式的方式
    Django总结
    Django框架初步
    HTML表单设计(下)
    HTML表单设计(上)
    HTML框架
    nohup和&后台运行,查看占用端口进程
  • 原文地址:https://www.cnblogs.com/wally/p/2999455.html
Copyright © 2011-2022 走看看