zoukankan      html  css  js  c++  java
  • codeforces 688C C. NP-Hard Problem(bfs判断奇数长度环)

    题目链接:

    C. NP-Hard Problem

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 kintegers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.

    Examples
    input
    4 2
    1 2
    2 3
    output
    1
    2
    2
    1 3
    input
    3 3
    1 2
    2 3
    1 3
    output
    -1

    题意:

    给一个森林,问能否找到这样的两个集合,使每条边的至少一个点在这样的集合里;有的话输出这两个集合;

    思路:

    每一条边的两个点分别是这两个集合里的;如果出现奇数长度的环就怎么也无法满足了;

    AC代码:

    //#include <bits/stdc++.h>
    #include <vector>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <map>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef  long long LL;
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=1e5+20;
    const int maxn=1005;
    const double eps=1e-10;
    
    int n,m;
    vector<int>ve[N];
    int dis[N],vis[N],ansa[N],ansb[N];
    queue<int>qu;
    
    int bfs(int x)
    {
        vis[x]=1;
        while(!qu.empty())qu.pop();
        qu.push(x);
        while(!qu.empty())
        {
            int fr=qu.front();
            qu.pop();
            int len=ve[fr].size();
            for(int i=0;i<len;i++)
            {
                int y=ve[fr][i];
                if(!vis[y])
                {
                    dis[y]=dis[fr]+1;
                    qu.push(y);
                    vis[y]=1;
                }
                else
                {
                    if((dis[y]+dis[fr])%2==0)return 0;
                }
            }
        }
        return 1;
    }
    int check()
    {
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                dis[i]=0;
               if( bfs(i)==0)return 0;
            }
        }
        return 1;
    }
    int main()
    {
    
            read(n);read(m);
            int u,v;
            for(int i=0;i<m;i++)
            {
                read(u);read(v);
                ve[u].push_back(v);
                ve[v].push_back(u);
            }
            if(!check())cout<<"-1"<<"
    ";
            else
            {
                int A=0,B=0;
                for(int i=1;i<=n;i++)
                {
                    if(dis[i]&1)ansa[A++]=i;
                    else ansb[B++]=i;
                }
                cout<<A<<"
    ";
                for(int i=0;i<A-1;i++)printf("%d ",ansa[i]);
                printf("%d
    ",ansa[A-1]);
                cout<<B<<"
    ";
                for(int i=0;i<B-1;i++)printf("%d ",ansb[i]);
                printf("%d
    ",ansb[B-1]);
    
            }
    
    
            return 0;
    }
  • 相关阅读:
    Android基本功:Socket
    Javascript模板及其中的数据逻辑分离思想
    MFC,QT与WinForm,WPF简介
    GPIB、USB、PCI、PCI Express和以太网/LAN/LXI
    Office批量授权(VL)版本和激活方法
    Labview 错误1400-打包库封装类时将对类重命名导致
    console.log()
    a标签中href=""的几种用法
    正斜杠"/"与反斜杠""
    提问的艺术
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5629366.html
Copyright © 2011-2022 走看看