scu oj 4439 : Vertex Cover(2015年四川省程序ACM设计竞赛D题 )

一般图的最小点覆盖问题是是一个npc问题,目前哈没有比较好的多项式的算法。但是这题有一点特殊的地方,每条边必定包含前面30个点的的一个,所以这题可以枚举钱30个点的选和不选的状态,后面的点对应的状态就唯一了。    所以这题就是  dfs+可行性减枝和答案最优减枝。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<algorithm>
#include<vector>
#include<bitset>
using namespace std;
const int mmax = 510;
const int inf =0x3fffffff;
int n,m;
struct node
{
    int en;
    int next;
}E[mmax*mmax];
int p[mmax];
int num;
void add(int st,int en)
{
    E[num].en=en;
    E[num].next=p[st];
    p[st]=num++;
}
void init()
{
    memset(p,-1,sizeof p);
    num=0;
}
int fg[510];
int Cnt[510];
void build(int x)
{
    int tmp=0;
    Cnt[x]=0;
    for(int i=p[x];i+1;i=E[i].next)
    {
        int v=E[i].en;
        if(v<30)
            tmp|=(1<<v);
        if(v>=30)
            Cnt[x]++;
    }
    fg[x]=tmp;
}
int solve(int falg)
{
    int cnt=0;
    for(int i=30;i<n;i++)
    {
        if((falg&fg[i])!=fg[i])
            cnt++;
    }
    return cnt;
}

int ans;
bool ok(int u,int falg)
{
    for(int i=u;i<30;i++)
        falg|=(1<<i);

    for(int i=0;i<30;i++)
    {
        if( !(falg&(1<<i)) &&(falg&fg[i])!=fg[i])
            return 0;
    }
    return 1;
}
void dfs(int u,int falg,int cnt)
{
    if(!ok(u,falg))
        return ;
    int tmp=falg;
    for(int i=u;i<30;i++)
        tmp|=(1<<i);
    if(cnt+solve(tmp)>=ans)
        return ;
    if(u>=30)
    {
        ans=min(ans,cnt+solve(falg));
        return ;
    }
    if((falg&fg[u])==fg[u] && !Cnt[u])
    {
        dfs(u+1,falg,cnt);
        return ;
    }
    dfs(u+1,falg,cnt);
    dfs(u+1,falg|(1<<u),cnt+1);
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        init();
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            u--,v--;
            add(u,v);
            add(v,u);
        }
        memset(fg,0,sizeof fg);
        for(int i=0;i<n;i++)
            build(i);
        ans=30;
        dfs(0,0,0);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-09-29 13:16:02

scu oj 4439 : Vertex Cover(2015年四川省程序ACM设计竞赛D题 )的相关文章

scu oj 4437: Carries (2015年四川省程序ACM设计竞赛B题目 )

其实这题只要想到这个结论就简单了.如果2个数a,b的第k位相加要进位,那么必须满足(a%10^k+b%10^k)>=10^k  .有了这个结论就很简单了,枚举没一位就好了. #include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<queue> #include<cmath> #include<map> #inc

scu oj 4445 Right turn 2015年四川省赛J题(模拟题)

一般的模拟题.对于出现过的每一个x建立一个set 集合,对于y也如此.然后查找要走到哪个点即可,主要要对状态记录,判断是否无线循环,否者出现无线循环的情况,就tle了. #include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<queue> #include<cmath> #include<map> #include&

SCU - 4439 Vertex Cover (图的最小点覆盖集)

Vertex Cover frog has a graph with \(n\) vertices \(v(1), v(2), \dots, v(n)\) and \(m\) edges \((v(a_1), v(b_1)), (v(a_2), v(b_2)), \dots, (v(a_m), v(b_m))\). She would like to color some vertices so that each edge has at least one colored vertex. Fi

二分图匹配 + 最小点覆盖 - Vertex Cover

Vertex Cover Problem's Link Mean: 给你一个无向图,让你给图中的结点染色,使得:每条边的两个顶点至少有一个顶点被染色.求最少的染色顶点数. analyse: 裸的最小点覆盖问题,二分图的最大匹配,直接套模版即可. Time complexity: O(N^2) view code

URAL 2038 Minimum Vertex Cover

2038. Minimum Vertex Cover Time limit: 1.0 secondMemory limit: 64 MB A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at least one vertex of the set. A minimum vertex cover is a vertex cover with minimal

四川第七届 D Vertex Cover(二分图最小点覆盖,二分匹配模板)

Vertex Cover frog has a graph with nn vertices v(1),v(2),-,v(n)v(1),v(2),-,v(n) and mm edges (v(a1),v(b1)),(v(a2),v(b2)),-,(v(am),v(bm))(v(a1),v(b1)),(v(a2),v(b2)),-,(v(am),v(bm)). She would like to color some vertices so that each edge has at least

PAT 1134 Vertex Cover

A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at least one vertex of the set. Now given a graph with several vertex sets, you are supposed to tell if each of them is a vertex cover or not. Input Specif

1134 Vertex Cover (25 分)

1134 Vertex Cover (25 分) A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at least one vertex of the set. Now given a graph with several vertex sets, you are supposed to tell if each of them is a vertex c

2015南阳CCPC L - Huatuo&#39;s Medicine 水题

L - Huatuo's Medicine Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Huatuo was a famous doctor. He use identical bottles to carry the medicine. There are different types of medicine. Huatuo put medicines into the bottles and chain these b