Codeforces Round #360 (Div. 2) C DFS判斷二分圖
連結:戳這裡
C. NP-Hard Problem
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 k integers — 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.
題意:
給定一個無向圖(存在環),要求判斷是否是二分圖
二分圖定義:設G=(V,E)是一個無向圖,如果頂點V可分割為兩個互不相交的子集(A,B),並且圖中的每條邊(i,j)所關聯的兩個頂點i和j分別屬於這兩個不同的頂點集(i
in A,j in B),則稱圖G為一個二分圖。
思路:
直接用DFS判斷是否是二分圖,染色就行了。
程式碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m,tot=0;
struct edge{
int v,next;
}e[400100];
int head[100100],vis[100100];
void Add(int u,int v){
e[tot].v=v;
e[tot].next=head[u];
head[u]=tot++;
}
int flag=0;
void DFS(int u,int fa){
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].v;
if(v==fa) continue;
if(vis[v]!=0 && vis[v]==-vis[u]) continue;
if(vis[v]!=0 && vis[v]==vis[u]){
flag=1;
continue;
}
if(vis[v]==0) {
vis[v]=-vis[u];
DFS(v,u);
}
}
}
int main(){
mst(head,-1);
mst(vis,0);
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
Add(u,v);
Add(v,u);
}
for(int i=1;i<=n;i++){
if(flag) break;
if(!vis[i]){
vis[i]=1;
DFS(i,0);
}
}
if(flag){
cout<<-1<<endl;
return 0;
}
int num1=0,num2=0;
for(int i=1;i<=n;i++){
if(vis[i]==1) ++num1;
if(vis[i]==-1) ++num2;
}
printf("%d\n",num1);
for(int i=1;i<=n;i++) {
if(vis[i]==1) printf("%d ",i);
}
printf("\n%d\n",num2);
for(int i=1;i<=n;i++){
if(vis[i]==-1) printf("%d ",i);
}
printf("\n");
return 0;
}
相關文章
- Codeforces Round #321 (Div. 2) C DFS
- Codeforces Round #359 (Div. 2) C DFS
- Codeforces Round #361 (Div. 2) C 二分
- Codeforces Round #316 (Div. 2) D DFS+vector+二分
- Codeforces Round #359 (Div. 2) D DFS
- Codeforces Round #336 (Div. 2) C 二分+dp
- Codeforces Round #224 (Div. 2)(數學、dfs)
- Codeforces Round #321 (Div. 2) B 二分
- Codeforces Round #453 (Div. 2) C
- Codeforces Round #452 (Div. 2) C
- Codeforces Round 973 (Div. 2) C
- Codeforces Round 972 (Div. 2) C
- Codeforces Round 982 (Div. 2)(A~C)
- Codeforces Round #360 (Div. 2) D 數學題
- Codeforces Round #360 (Div. 1) D 並查集判奇環並查集
- Codeforces Round #361 (Div. 2) D RMQ+二分MQ
- Educational Codeforces Round 33 (Rated for Div. 2) C
- Codeforces Round #323 (Div. 2) C gcdGC
- Codeforces Round #325 (Div. 2) C 模擬
- Codeforces Round #290 (Div. 2) A,B,C,D
- codeforces Round #252 (Div. 2) C - Valera and Tubes
- Codeforces Round #250 (Div. 2) C、The Child and Toy
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) E DFS
- Codeforces Round 977 (Div. 2)(B-C2)
- Codeforces Round #360 (Div. 2) E dp 類似01揹包
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A
- Codeforces Round #217 (Div. 2)
- Codeforces Round #256 (Div. 2)
- Codeforces Round #259 (Div. 2)
- Codeforces Round #257 (Div. 2)
- Codeforces Round #258 (Div. 2)