zoukankan      html  css  js  c++  java
  • C

    C - NP-Hard Problem
    Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

     

    Input

     

    Output

     

    Sample Input

     

    Sample Output

     

    Hint

     

    Description

    Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.

    Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both).

    Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.

    They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).

    Input

    The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.

    Each of the next m lines contains a pair of integers ui and vi (1  ≤  ui,  vi  ≤  n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.

    Output

    If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes).

    If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.

    Sample Input

    Input
    4 2
    1 2
    2 3
    Output
    1
    2
    2
    1 3
    Input
    3 3
    1 2
    2 3
    1 3
    Output
    -1

    Sample Output

     

    Hint

    In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).

    In the second sample, there is no way to satisfy both Pari and Arya.

    题意:给你m组边的俩端点,若能构成二分图输出左右俩点集和个数,若不能输出-1.(若能构成二分图则给出的边的俩端点分别在左右俩个集团,不能出现一条边的俩点在一边)

    思路:染色,给俩边的点染不同的颜色

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    const int MAX=1e5+5;
    using namespace std;
    vector <int>mp[MAX];
    int d[MAX];
    int vis[MAX];
    int n,m;
    int dfs(int x,int f)
    {
       vis[x]=1;
       d[x]=f;
       int flag;
       for(int i=0;i<mp[x].size();i++)
       {
           if(d[mp[x][i]]==d[x])
           return flag=0;
           if(d[mp[x][i]]==0)
           {
               d[mp[x][i]]=-1*f;
               if(!dfs(mp[x][i],-1*f))
               return flag=0;
           }
    
       }
       return flag=1;
    }
    int main()
    {
        while(cin>>n>>m)
        {
          int a,b,flag=1;
          for(int i=0;i<MAX;i++)
          mp[i].clear();
            for(int i=0;i<m;i++)
            {
                scanf("%d%d",&a,&b);
                mp[a].push_back(b);
                mp[b].push_back(a);
            }
            memset(d,0,sizeof(d));
            memset(vis,0,sizeof(vis));
            for(int i=1;i<=n;i++)
            {
                if(!vis[i])
                {
                    if(!dfs(i,1))
                    {flag=0;break;}
                }
            }
            if(!flag)
            cout<<-1<<endl;
            else
            {
                int q=0,p=0;
                for(int i=1;i<=n;i++)
                {
                    if(d[i]==1)
                    q++;
                    if(d[i]==-1)
                    p++;
                }
                cout<<q<<endl;
                for(int i=1;i<=n;i++)
                if(d[i]==1)
                printf("%d ",i);
                cout<<endl;
                cout<<p<<endl;
                for(int i=1;i<=n;i++)
                if(d[i]==-1)
                printf("%d ",i);
                cout<<endl;
            }
    
    
    
        }
    }
    代码改良:
    #include <iostream>
    #include <vector>
    #include <cstring>
    #include <cstdio>
    const int MAX=1e5+5;
    using namespace std;
    vector<int>mp[MAX],ans1,ans2;
    int d[MAX],n,m;
    int dfs(int x)
    {
        if(d[x]==0)
        d[x]=1;
        if(d[x]==1)
        ans1.push_back(x);
        if(d[x]==-1)
        ans2.push_back(x);
        for(int i=0;i<mp[x].size();i++)
        {
            if(d[x]==d[mp[x][i]])
            return 0;
            if(d[mp[x][i]]==0)
            {d[mp[x][i]]=-1*d[x];
            if(!dfs(mp[x][i]))
            return 0;}
        }
        return 1;
    }
    int main()
    { int a,b;
        while(cin>>n>>m)
        {   for(int i=1;i<=n;i++)
             mp[i].clear();
             ans1.clear();
             ans2.clear();
            for(int i=0;i<m;i++)
            {
                scanf("%d%d",&a,&b);
                mp[a].push_back(b);
                mp[b].push_back(a);
            }
            int flag=1;
            memset(d,0,sizeof(d));
            for(int i=1;i<=n;i++)
            {
                if(d[i]==0)
                {
                    if(!dfs(i))
                    {
                        flag=0;
                        break;
                    }
                }
    
            }
            if(!flag)
            cout<<-1<<endl;
            else
            {
                cout<<ans1.size()<<endl;
                for(int i=0;i<ans1.size();i++)
                 printf("%d ",ans1[i]);
               cout<<endl;
                cout<<ans2.size()<<endl;
                for(int i=0;i<ans2.size();i++)
                printf("%d ",ans2[i]);
                cout<<endl;
    
            }
    
        }
    }
    
     
  • 相关阅读:
    软件测试学习总结
    MySQL数据库中主键和索引的区别和联系
    什么是接口测试及其测试流程
    bug生命周期
    啊这...2-get/post请求区别,来给你看看post请求url中传参
    啊这...1-get/post请求区别,你还在认为get只能在url传参吗?传json格式会咋样?
    关于博客园全站内容审核中...如出现此问题:请移步xxx
    git-2-企业级gitlab的使用及管理工具Sourcetree
    fiddler-12-Proxifier+fiddler进行PC端抓包
    微信小程序弹出订阅消息确认弹窗的限制
  • 原文地址:https://www.cnblogs.com/llsq/p/5901453.html
Copyright © 2011-2022 走看看