Advertisement
Guest User

Untitled

a guest
Aug 17th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. vector<int> v[100005];
  4. map<pair<int,int>,int> idx;
  5. int p[100005],ch[2][100005];
  6. bool flip[100005],del[300005];
  7. int sz[100005],ans[300005];
  8. void push(int node)
  9. {
  10.     if (flip[node])
  11.     {
  12.         flip[node]=0;
  13.         swap(ch[0][node],ch[1][node]);
  14.         if (ch[0][node])
  15.         flip[ch[0][node]]^=1;
  16.         if (ch[1][node])
  17.         flip[ch[1][node]]^=1;
  18.     }
  19. }
  20. void recalc(int node)
  21. {
  22.     push(ch[0][node]);
  23.     push(ch[1][node]);
  24.     sz[node]=1+sz[ch[0][node]]+sz[ch[1][node]];
  25. }
  26. int side(int node)
  27. {
  28.     if (!p[node])
  29.     return -2;
  30.     if (ch[0][p[node]]==node)
  31.     return 0;
  32.     if (ch[1][p[node]]==node)
  33.     return 1;
  34.     return -1;
  35. }
  36. void attach(int par,int node,int s)
  37. {
  38.     if (node)
  39.     p[node]=par;
  40.     if (s>=0)
  41.     ch[s][par]=node;
  42. }
  43. void rotate(int node)
  44. {
  45.     int s=side(node),par=p[node];
  46.     attach(p[par],node,side(par));
  47.     attach(par,ch[!s][node],s);
  48.     attach(node,par,!s);
  49.     recalc(par);
  50.     recalc(node);
  51. }
  52. void splay(int node)
  53. {
  54.     while (side(node)>=0 && side(p[node])>=0)
  55.     {
  56.         push(p[p[node]]);
  57.         push(p[node]);
  58.         push(node);
  59.         rotate((side(node)==side(p[node]))? p[node]:node);
  60.         rotate(node);
  61.     }
  62.     if (side(node)>=0)
  63.     {
  64.         push(p[node]);
  65.         push(node);
  66.         rotate(node);
  67.     }
  68.     push(node);
  69. }
  70. void access(int node)
  71. {
  72.     int o=node,cur=0;
  73.     while (node)
  74.     {
  75.         splay(node);
  76.         ch[1][node]=cur;
  77.         recalc(node);
  78.         cur=node;
  79.         node=p[node];
  80.     }
  81.     splay(o);
  82. }
  83. void reroot(int node)
  84. {
  85.     access(node);
  86.     flip[node]^=1;
  87.     access(node);
  88. }
  89. int dep(int node)
  90. {
  91.     access(node);
  92.     return sz[ch[0][node]];
  93. }
  94. int find(int node)
  95. {
  96.     access(node);
  97.     while (ch[0][node])
  98.     {
  99.         node=ch[0][node];
  100.         push(node);
  101.     }
  102.     access(node);
  103.     return node;
  104. }
  105. void link(int x,int y)
  106. {
  107.     reroot(x);
  108.     access(y);
  109.     attach(x,y,0);
  110.     recalc(x);
  111. }
  112. void cut(int x,int y)
  113. {
  114.     reroot(x);
  115.     access(y);
  116.     p[ch[0][y]]=0;
  117.     ch[0][y]=0;
  118.     recalc(y);
  119. }
  120. int parent(int node)
  121. {
  122.     access(node);
  123.     node=ch[0][node];
  124.     push(node);
  125.     while (ch[1][node])
  126.     {
  127.         node=ch[1][node];
  128.         push(node);
  129.     }
  130.     access(node);
  131.     return node;
  132. }
  133. int go(int a,int b,int c2)
  134. {
  135.     int c1=1,d1=dep(a),d2=dep(b);
  136.     vector<pair<int,int> > c;
  137.     while (a!=b)
  138.     {
  139.         if (d1<d2)
  140.         {
  141.             swap(a,b);
  142.             swap(c1,c2);
  143.             swap(d1,d2);
  144.         }
  145.         int p=parent(a);
  146.         ans[idx[{p,a}]]=c1;
  147.         c1*=-1;
  148.         c.push_back({a,p});
  149.         a=p;
  150.         d1--;
  151.     }
  152.     for (auto e:c)
  153.     cut(e.first,e.second);
  154. }
  155. int main()
  156. {
  157.     int t;
  158.     scanf("%d",&t);
  159.     while (t--)
  160.     {
  161.         int n,m;
  162.         scanf("%d%d",&n,&m);
  163.         idx.clear();
  164.         for (int i=1;i<=n;i++)
  165.         {
  166.             v[i].clear();
  167.             p[i]=0;
  168.             ch[0][i]=ch[1][i]=0;
  169.             flip[i]=0;
  170.             sz[i]=1;
  171.         }
  172.         for (int i=0;i<m;i++)
  173.         {
  174.             int a,b;
  175.             scanf("%d%d",&a,&b);
  176.             v[a].push_back(b);
  177.             v[b].push_back(a);
  178.             idx[{a,b}]=i;
  179.             idx[{b,a}]=i;
  180.             ans[i]=0;
  181.             del[i]=0;
  182.         }
  183.         for (int a=1;a<=n;a++)
  184.         {
  185.             int b=-1;
  186.             for (int c:v[a])
  187.             {
  188.                 int i=idx[{a,c}];
  189.                 if (!del[i] && !ans[i])
  190.                 {
  191.                     if (find(a)!=find(c))
  192.                     {
  193.                         link(a,c);
  194.                         del[i]=1;
  195.                     }
  196.                     else if (dep(a)%2!=dep(c)%2)
  197.                     {
  198.                         ans[i]=-1;
  199.                         go(a,c,1);
  200.                         if (b!=-1 && find(a)!=find(b))
  201.                         {
  202.                             link(a,b);
  203.                             del[idx[{a,b}]]=1;
  204.                             b=-1;
  205.                         }
  206.                     }
  207.                     else if (b==-1)
  208.                     b=c;
  209.                     else
  210.                     {
  211.                         ans[idx[{a,b}]]=-1;
  212.                         ans[i]=1;
  213.                         go(b,c,-1);
  214.                         b=-1;
  215.                     }
  216.                 }
  217.             }
  218.         }
  219.         for (int i=0;i<m;i++)
  220.         printf("%d\n",ans[i]);
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement