Hippskill

Untitled

Jan 29th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include<stdio.h>
  3. #include<iostream>
  4. #include<vector>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<map>
  8. #include<set>
  9. #include<sstream>
  10. #include<cstring>
  11. #include<numeric>
  12. #include<limits.h>
  13. #include<time.h>
  14. using namespace std;
  15.  
  16. const int N = 102;
  17.  
  18. int n, m;
  19. vector<int> g[N];
  20.  
  21. bool used[N];
  22. int color[N];
  23. bool fail;
  24.  
  25. vector<int> path;
  26. vector<vector<int>> comps;
  27. pair<int, int> val[N];
  28.  
  29.  
  30. void dfs(int v, int c, int prev) {
  31.     used[v] = true;
  32.     color[v] = c;
  33.     for (auto to : g[v]) {
  34.         if (fail)
  35.             return;
  36.         if (!used[to]) {
  37.             path.push_back(to);
  38.             dfs(to, c ^ 1, v);
  39.         }
  40.         else {
  41.             if (to != prev) {
  42.                 if (color[v] == color[to]) {
  43.                     fail = true;
  44.                     return;
  45.                 }
  46.             }
  47.         }
  48.     }
  49. }
  50.  
  51. bool u[N];
  52. bool succes = false;
  53. int curColor[N];
  54. bool dp[2][N][N];
  55. void brute(int value){
  56.     if (value == n) {
  57.         for (int i = 0; i < comps.size(); i++) {
  58.             if (!u[i]) {
  59.                 if (val[i].first > 0 && val[i].second > 0)
  60.                     return;
  61.             }
  62.         }
  63.         succes = true;
  64.         return;
  65.     }
  66.     for (int i = 0; i < comps.size(); i++) {
  67.         if (!u[i]) {
  68.             if (val[i].first + value <= n) {
  69.                 if (!dp[0][i][val[i].first + value]) {
  70.                     dp[0][i][val[i].first + value] = true;
  71.                     curColor[i] = 0;
  72.                     u[i] = true;
  73.                     brute(val[i].first + value);
  74.                     u[i] = false;
  75.                 }
  76.             }
  77.             if (succes)
  78.                 return;
  79.             if (val[i].second + value <= n) {
  80.                 if (!dp[1][i][val[i].second + value]) {
  81.                     dp[1][i][val[i].second + value] = true;
  82.                     curColor[i] = 1;
  83.                     u[i] = true;
  84.                     brute(val[i].second + value);
  85.                     u[i] = false;
  86.                 }
  87.             }
  88.             if (succes)
  89.                 return;
  90.         }
  91.     }
  92. }
  93.  
  94.  
  95. int main() {
  96.  
  97. #ifndef ONLINE_JUDGE
  98.     freopen("input.txt", "r", stdin);
  99. #endif
  100.     scanf("%d%d", &n, &m);
  101.     for (int i = 0; i < m; i++) {
  102.         int a, b;
  103.         scanf("%d %d", &a, &b);
  104.         g[a - 1].push_back(b - 1);
  105.         g[b - 1].push_back(a - 1);
  106.     }
  107.     int id = 0;
  108.     for (int i = 0; i < 2 * n; i++) {
  109.         if (!used[i]) {
  110.             fail = false;
  111.             path.clear();
  112.             path.push_back(i);
  113.             dfs(i, 0, -1);
  114.             if (fail) {
  115.                 printf("IMPOSSIBLE");
  116.                 return 0;
  117.             }
  118.             int cur[2]{ 0, 0 };
  119.             for (auto j : path) {
  120.                 cur[color[j]]++;
  121.             }
  122.             comps.push_back(path);
  123.             val[id++] = make_pair(cur[0], cur[1]);
  124.         }
  125.     }
  126.  
  127.     fill_n(curColor, N, -1);
  128.     brute(0);
  129.  
  130.     if (succes) {
  131.         int cnt[2]{ 0, 0 };
  132.         for (int i = 0; i < comps.size(); i++) {
  133.             if (curColor[i] == 1) {
  134.                 for (auto j : comps[i]) {
  135.                     color[j] ^= 1;
  136.                 }
  137.             }
  138.         }
  139.         for (int i = 0; i < 2 * n; i++) {
  140.             cnt[color[i]]++;
  141.         }
  142.  
  143.         for (int i = 0; i < comps.size(); i++) {
  144.             if (curColor[i] == -1) {
  145.                 if (cnt[0] < cnt[1]) {
  146.                     cnt[0]++;
  147.                     color[comps[i][0]] = 0;
  148.                 }
  149.                 else {
  150.                     cnt[1]++;
  151.                     color[comps[i][0]] = 1;
  152.                 }
  153.             }
  154.         }
  155.  
  156.         for (int j = 0; j < 2; j++) {
  157.             for (int i = 0; i < 2 * n; i++) {
  158.                 if (color[i] == j) {
  159.                     printf("%d ", i + 1);
  160.                 }
  161.             }
  162.             printf("\n");
  163.         }
  164.     }
  165.     else {
  166.         printf("IMPOSSIBLE");
  167.     }
  168.  
  169.  
  170.  
  171.        
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment