Hdu1054 strategic game (binary matching)

Source: Internet
Author: User

Question:

Give you a graph. When each vertex is overwritten, the adjacent vertex is also overwritten.

Minimum number to overwrite all vertices

Ideas:

I did this question when I learned the tree-like DP, which is definitely faster than the Binary Graph ..

Click the bipartite graph. n = 1500. Use Hungary and HK to calculate it.

Go to Hungary first .. 624 Ms

/* ***********************************************Author        :devilCreated Time  :2016/5/17 11:42:42************************************************ */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <stdlib.h>using namespace std;const int N=1510;int link[N];bool vis[N];vector<int>eg[N];bool dfs(int u){    for(int i=0;i<eg[u].size();i++)    {        int v=eg[u][i];        if(!vis[v])        {            vis[v]=1;            if(link[v]==-1||dfs(link[v]))            {                link[v]=u;                return 1;            }        }    }    return 0;}int main(){    //freopen("in.txt","r",stdin);    int n,x,y,k;    while(~scanf("%d",&n))    {        memset(link,-1,sizeof(link));        for(int i=0;i<n;i++)            eg[i].clear();        for(int i=0;i<n;i++)        {            scanf("%d:(%d)",&x,&k);            while(k--)            {                scanf("%d",&y);                eg[x].push_back(y);                eg[y].push_back(x);            }        }        int ans=0;        for(int i=0;i<n;i++)        {            memset(vis,0,sizeof(vis));            ans+=dfs(i);        }        printf("%d\n",ans/2);    }    return 0;}

Then my big HK--, 327 Ms

/* ***********************************************Author        :devilCreated Time  :2016/5/17 11:46:38************************************************ */#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <stdlib.h>using namespace std;const int N=1510;const int inf=0x3f3f3f3f;vector<int>eg[N];int n;int mx[N],my[N];int dx[N],dy[N],dis;bool vis[N];bool bfs(){    queue<int>q;    dis=inf;    memset(dx,-1,sizeof(dx));    memset(dy,-1,sizeof(dy));    for(int i=0;i<n;i++)        if(mx[i]==-1)        {            q.push(i);            dx[i]=0;        }    while(!q.empty())    {        int u=q.front();        q.pop();        if(dx[u]>dis) break;        for(int i=0;i<eg[u].size();i++)        {            int v=eg[u][i];            if(dy[v]==-1)            {                dy[v]=dx[u]+1;                if(my[v]==-1) dis=dy[v];                else                {                    dx[my[v]]=dy[v]+1;                    q.push(my[v]);                }            }        }    }    return dis!=inf;}bool dfs(int u){    for(int i=0;i<eg[u].size();i++)    {        int v=eg[u][i];        if(!vis[v]&&dy[v]==dx[u]+1)        {            vis[v]=1;            if(my[v]!=-1&&dy[v]==dis) continue;            if(my[v]==-1||dfs(my[v]))            {                my[v]=u;                mx[u]=v;                return 1;            }        }    }    return 0;}int MaxMatch(){    int ans=0;    memset(mx,-1,sizeof(mx));    memset(my,-1,sizeof(my));    while(bfs())    {        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++)            if(mx[i]==-1&&dfs(i))                ans++;    }    return ans;}int main(){    //freopen("in.txt","r",stdin);    int x,y,k;    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)            eg[i].clear();        for(int i=0;i<n;i++)        {            scanf("%d:(%d)",&x,&k);            while(k--)            {                scanf("%d",&y);                eg[x].push_back(y);                eg[y].push_back(x);            }        }        printf("%d\n",MaxMatch()/2);    }    return 0;}

 

Hdu1054 strategic game (binary matching)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.