codeforce 360 div1 a
来源:互联网 发布:win32编程是什么 编辑:程序博客网 时间:2024/06/05 12:06
A. 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 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.
题意:
给你一个无向图,问你可不可以分成一个二分
思路:
DFS模拟…….
CODE:
#include<stdio.h>#include<iostream>#include<string.h>#include<queue>#include<algorithm>#include<vector>#include<stdlib.h>#include<conio.h>using namespace std;vector<int>MAP[100100],ans1,ans2;int n, m,book[100100];bool dfs(int x){ if (!book[x]) book[x] = 1; if (book[x] == 1) ans1.push_back(x); else if (book[x] == -1) ans2.push_back(x); for (int i = 0; i < MAP[x].size(); i++) { if (book[x] == book[MAP[x][i]]) return 0; else if(book[MAP[x][i]]==0) { book[MAP[x][i]] = -1 * book[x]; if (dfs(MAP[x][i]) == 0) return 0; } } return 1;}int main(){ int flag = 1; cin >> n >> m; for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; MAP[a].push_back(b); MAP[b].push_back(a); } for (int i = 1; i <= n; i++) { if (!book[i]) { if (!dfs(i)) { flag = 0; break; } } } if (flag) { cout << ans1.size() << endl; for (int i = 0; i < ans1.size(); i++) cout << ans1[i] << ' '; cout << endl; cout << ans2.size() << endl; for (int i = 0; i < ans2.size(); i++) cout << ans2[i] << ' '; cout << endl; } else { cout << -1 << endl; } return 0;}
- codeforce 360 div1 a
- codeforce 399 div1 a
- CODEFORCE #405 div1 a
- codeforce 385 div1 a
- codeforce contest 713 #371 Div1 A
- CodeForce#190 Div1
- CodeForce Round 257 div1
- Codeforce A
- codeforce contest 713 #371 Div1 B
- codeforce contest 713 #371 Div1 C
- Codeforces #176 div1 A
- CF 148 div1 A
- CF 127 div1 A
- TC SRM664 DIV1 A
- Codeforces #383 div1 A
- cf #336 div1 a
- codeforce Round201 div1 B. Lucky Common Subsequence KMP+DP
- Codeforce # A Plug-in
- ubuntu 执行apt-get update报错Failed to fetch
- sql语句大全
- [PHP] Mac下homebrew安装及php.mysql.nginx环境安装及配置
- Find Duplicate Subtrees 解题心得
- 关于Python2和Python3
- codeforce 360 div1 a
- linux下安装think-captcha和加密解密的类
- iOS开发网络篇 一一 SDWebImage框架的基本使用
- 链表的基本操作
- springboot实现mqtt broker启动
- Android | 应用框架
- PAT 乙级 1080. MOOC期终成绩 (25)
- 子数组之和
- Java+Kotlin混合调用实现时光轴(ExpandableListView可扩展列表)