Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include<stdio.h>
- #include<iostream>
- #include<vector>
- #include<cmath>
- #include<algorithm>
- #include<map>
- #include<set>
- #include<sstream>
- #include<cstring>
- #include<numeric>
- #include<limits.h>
- #include<time.h>
- using namespace std;
- const int N = 102;
- int n, m;
- vector<int> g[N];
- bool used[N];
- int color[N];
- bool fail;
- vector<int> path;
- vector<vector<int>> comps;
- pair<int, int> val[N];
- void dfs(int v, int c, int prev) {
- used[v] = true;
- color[v] = c;
- for (auto to : g[v]) {
- if (fail)
- return;
- if (!used[to]) {
- path.push_back(to);
- dfs(to, c ^ 1, v);
- }
- else {
- if (to != prev) {
- if (color[v] == color[to]) {
- fail = true;
- return;
- }
- }
- }
- }
- }
- bool u[N];
- bool succes = false;
- int curColor[N];
- bool dp[2][N][N];
- void brute(int value){
- if (value == n) {
- for (int i = 0; i < comps.size(); i++) {
- if (!u[i]) {
- if (val[i].first > 0 && val[i].second > 0)
- return;
- }
- }
- succes = true;
- return;
- }
- for (int i = 0; i < comps.size(); i++) {
- if (!u[i]) {
- if (val[i].first + value <= n) {
- if (!dp[0][i][val[i].first + value]) {
- dp[0][i][val[i].first + value] = true;
- curColor[i] = 0;
- u[i] = true;
- brute(val[i].first + value);
- u[i] = false;
- }
- }
- if (succes)
- return;
- if (val[i].second + value <= n) {
- if (!dp[1][i][val[i].second + value]) {
- dp[1][i][val[i].second + value] = true;
- curColor[i] = 1;
- u[i] = true;
- brute(val[i].second + value);
- u[i] = false;
- }
- }
- if (succes)
- return;
- }
- }
- }
- int main() {
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- #endif
- scanf("%d%d", &n, &m);
- for (int i = 0; i < m; i++) {
- int a, b;
- scanf("%d %d", &a, &b);
- g[a - 1].push_back(b - 1);
- g[b - 1].push_back(a - 1);
- }
- int id = 0;
- for (int i = 0; i < 2 * n; i++) {
- if (!used[i]) {
- fail = false;
- path.clear();
- path.push_back(i);
- dfs(i, 0, -1);
- if (fail) {
- printf("IMPOSSIBLE");
- return 0;
- }
- int cur[2]{ 0, 0 };
- for (auto j : path) {
- cur[color[j]]++;
- }
- comps.push_back(path);
- val[id++] = make_pair(cur[0], cur[1]);
- }
- }
- fill_n(curColor, N, -1);
- brute(0);
- if (succes) {
- int cnt[2]{ 0, 0 };
- for (int i = 0; i < comps.size(); i++) {
- if (curColor[i] == 1) {
- for (auto j : comps[i]) {
- color[j] ^= 1;
- }
- }
- }
- for (int i = 0; i < 2 * n; i++) {
- cnt[color[i]]++;
- }
- for (int i = 0; i < comps.size(); i++) {
- if (curColor[i] == -1) {
- if (cnt[0] < cnt[1]) {
- cnt[0]++;
- color[comps[i][0]] = 0;
- }
- else {
- cnt[1]++;
- color[comps[i][0]] = 1;
- }
- }
- }
- for (int j = 0; j < 2; j++) {
- for (int i = 0; i < 2 * n; i++) {
- if (color[i] == j) {
- printf("%d ", i + 1);
- }
- }
- printf("\n");
- }
- }
- else {
- printf("IMPOSSIBLE");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment