GastonFontenla

Two Rounds

May 30th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #define ll long long
  5.  
  6. using namespace std;
  7.  
  8. bool GB(ll m, ll x)
  9. {
  10.     return ((m) & (1LL << (x)));
  11. }
  12.  
  13. void SB(ll &m, ll x)
  14. {
  15.     m = ((m) | (1LL <<(x)));
  16. }
  17.  
  18. vector <int> a, b;
  19. vector <string> aa, bb; ///Bitmasks correspondientes con a y b
  20. int f, tam;
  21. long long solSet; ///Bitmask solucion
  22. int sola, solb;
  23.  
  24. void solve(int i, long long s, int sa, int sb)
  25. {
  26.     if(i >= tam)
  27.     {
  28.         ///Ver si los puedo acomodar bien
  29.  
  30.         if(sola == -1)
  31.         {
  32.             if(sa > sb)
  33.                 swap(sa, sb);
  34.  
  35.             if(sa == sb && f%2 == 0)
  36.                 solSet = s, sola = sa, solb = sb;
  37.  
  38.             if((f - sb + sa) >= 0 && (f - sb + sa)%2 == 0)
  39.                 solSet = s, sola = sa, solb = sb;
  40.         }
  41.         return;
  42.     }
  43.  
  44.     solve(i+1, s, sa+a[i], sb+b[i]);
  45.     SB(s, i);
  46.     solve(i+1, s, sa+b[i], sb+a[i]);
  47. }
  48.  
  49. struct Grafo
  50. {
  51.     vector <vector <int> > Adj;
  52.     vector <bool> u, color;
  53.  
  54.     bool bipartible;
  55.     int c[2];
  56.  
  57.     string setA, setB;
  58.  
  59.     void dfs(int n)
  60.     {
  61.         if(color[n])
  62.             setA[n] = '1';//SB(setA, n);
  63.         else
  64.             setB[n] = '1';//SB(setB, n);
  65.  
  66.         c[color[n]]++;
  67.         u[n] = true;
  68.         for(int i=0; i<Adj[n].size(); i++)
  69.         {
  70.             if(u[Adj[n][i]] && color[n] == color[Adj[n][i]])
  71.                 bipartible = false;
  72.             else if(!u[Adj[n][i]])
  73.             {
  74.                 color[Adj[n][i]] = 1-color[n];
  75.                 dfs(Adj[n][i]);
  76.             }
  77.         }
  78.     }
  79.  
  80.     void leer()
  81.     {
  82.         int n, m, x,y;
  83.         cin >> n >> m;
  84.  
  85.         n *= 2;
  86.  
  87.         Adj = vector <vector <int> > (n+1, vector <int> ());
  88.         u = vector <bool> (n+1, false);
  89.         color = vector <bool> (n+1, false);
  90.  
  91.         bipartible = true;
  92.  
  93.         for(int i=0; i<m; i++)
  94.         {
  95.             cin >> x >> y;
  96.             Adj[x].push_back(y);
  97.             Adj[y].push_back(x);
  98.         }
  99.  
  100.         string patron(102, '0');
  101.         for(int i=1; i<n+1; i++)
  102.             if(Adj[i].size() && !u[i])
  103.             {
  104.                 color[i] = 0;
  105.                 c[0] = 0, c[1] = 0;
  106.  
  107.                 setA = patron, setB = patron;
  108.                 dfs(i);
  109.  
  110.                 a.push_back(c[0]);
  111.                 b.push_back(c[1]);
  112.  
  113.                 aa.push_back(setA);
  114.                 bb.push_back(setB);
  115.             }
  116.         if(!bipartible)
  117.         {
  118.             cout << "IMPOSSIBLE" << endl;
  119.             return;
  120.         }
  121.         f = 0;
  122.  
  123.         ///Ver cuántos nodos están sin conflictos
  124.  
  125.         vector <int> sobra;
  126.  
  127.         for(int i=1; i<=n; i++)
  128.             if(Adj[i].size() == 0)
  129.                 sobra.push_back(i), f++;
  130.  
  131.         sola = -1, solb = -1;
  132.         tam = a.size();
  133.         solve(0, 0, 0, 0);
  134.  
  135.         if(sola == -1)
  136.         {
  137.             cout << "IMPOSSIBLE" << endl;
  138.             return;
  139.         }
  140.  
  141.         vector <int> ra, rb;
  142.  
  143.         for(int i=0; i<tam; i++)
  144.         {
  145.             bool z = GB(solSet, i);
  146.             if(z == 0) ///Queda como está
  147.             {
  148.                 for(int j=0; j<101; j++)
  149.                     if(aa[i][j] == '1')
  150.                         ra.push_back(j);
  151.                 for(int j=0; j<101; j++)
  152.                     if(bb[i][j] == '1')
  153.                         rb.push_back(j);
  154.             }
  155.             else
  156.             {
  157.                 for(int j=0; j<101; j++)
  158.                     if(aa[i][j] == '1')
  159.                         rb.push_back(j);
  160.                 for(int j=0; j<101; j++)
  161.                     if(bb[i][j] == '1')
  162.                         ra.push_back(j);
  163.             }
  164.         }
  165.  
  166.         ///Ahora tengo que dividir los problemas que son neutros
  167.  
  168.         for(int i=0; i<sobra.size(); i++)
  169.         {
  170.             if(ra.size() < rb.size())
  171.                 ra.push_back(sobra[i]);
  172.             else
  173.                 rb.push_back(sobra[i]);
  174.         }
  175.  
  176.         for(int i=0; i<rb.size(); i++)
  177.             cout << rb[i] << " ";
  178.         cout << endl;
  179.  
  180.         for(int i=0; i<ra.size(); i++)
  181.             cout << ra[i] << " ";
  182.         cout << endl;
  183.     }
  184. };
  185.  
  186. int main()
  187. {
  188.     ///Mi código es un asco
  189.     ///Pobre de vos si venís a ver
  190.     ///cómo se resuelve este problema
  191.     ///Jajaja
  192.     Grafo g;
  193.     g.leer();
  194.  
  195.     return 0;
  196. }
Add Comment
Please, Sign In to add comment