NP-Hard Problem codeforces (二分图,dfs)

来源:互联网 发布:react pdf js 编辑:程序博客网 时间:2024/06/05 10:41

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
Note
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.

/*    二分图,dfs染色     用vector数组存每个点的相邻的点,    先将每个点的颜色标为-1,再逐个遍历,    每当找到一个点的颜色是-1时,dfs将其及其相邻点染色;    如果染色时发现该点已经染色了,则须进行判断    1,如果该点的颜色与想给它染的颜色相同,则直接返回(对于写法要注意,刚开始判断放在外面,造成了死循环)     2,如果该点的颜色与想染的颜色不同,则说明产生了矛盾,及该图不能成为二分图,标记并返回     最后打印就好 */#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<queue>#include<algorithm>#define ll long long#define inf 0x3f3f3f3f#define maxn 100007  // 1e5+7using namespace std;vector<int> edge[maxn],co[2];//edge[] 用来存边,co[]用来存每种颜色的点 int colour[maxn];int n,m;int flag;void dfs(int x,int c){    if(flag == 1) return;    if(colour[x] != -1 )        {            if(colour[x] != c)// 这个判断不能放在外面if中,会少判断要染的颜色正是想要的时候这种情况                 flag = 1;            return;        }    colour[x] = c;    co[c].push_back(x);    for(int i=0;i<edge[x].size();i++)        dfs(edge[x][i],c^1);}int main(){        scanf("%d%d",&n,&m);     for(int i=0;i<m;i++)        {               int a,b;            scanf("%d%d",&a,&b);            edge[a].push_back(b);            edge[b].push_back(a);        }    memset(colour,-1,sizeof(colour));    flag = 0;    for(int i=1;i<=n;i++)        if(colour[i] == -1)            {                dfs(i,0);            }    if(flag == 1)        printf("-1\n");    else {        int tmp = co[0].size();        printf("%d\n",tmp);        for(int i=0;i<tmp;i++)            printf("%d%c",co[0][i],i==tmp-1?'\n':' ');        tmp = co[1].size();        printf("%d\n",tmp);        for(int i=0;i<tmp;i++)            printf("%d%c",co[1][i],i==tmp-1?'\n':' ');    }    return 0;}
0 0