Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.97 KB | None | 0 0
  1. ///Bismillahir Rahmanir Rahim
  2.  
  3. #include<cstdio>
  4. #include<iomanip>
  5. #include<cstring>
  6. #include<cmath>
  7. #include<cstdlib>
  8. #include<cctype>
  9. #include<algorithm>
  10. #include<string>
  11. #include<vector>
  12. #include<queue>
  13. #include<map>
  14. #include<set>
  15. #include<sstream>
  16. #include<stack>
  17. #include<list>
  18. #include<iostream>
  19. #include<assert.h>
  20.  
  21. /**Define file I/O **/
  22. #define f_input freopen("input.txt","r",stdin)
  23. #define f_output freopen("output.txt","w",stdout)
  24.  
  25. /**Define memory set function**/
  26. #define mem(x,y) memset(x,y,sizeof(x))
  27. #define CLEAR(x) memset(x,0,sizeof(x))
  28.  
  29. /**Define function and object**/
  30. #define pb push_back
  31. #define Sort(v) sort(v.begin(),v.end())
  32. #define RSort(v) sort(v.rbegin(),v.rend())
  33. #define CSort(v,C) sort(v.begin(),v.end(),C)
  34. #define all(v) (v).begin(),(v).end()
  35. #define sqr(x) ((x)*(x))
  36. #define find_dist(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y))
  37.  
  38. /**Define constant value**/
  39. #define ERR 1e-9
  40. #define pi (2*acos(0))
  41. #define PI 3.141592653589793
  42.  
  43. /**Define input**/
  44. #define scanint(a) scanf("%d",&a)
  45. #define scanLLD(a) scanf("%lld",&a)
  46. #define scanstr(s) scanf("%s",s)
  47. #define scanline(l) scanf(" %[^\n]",l);
  48.  
  49. /**Define Bitwise operation**/
  50. #define check(n, pos) (n & (1ll<<(pos)))
  51. #define biton(n, pos) (n | (1ll<<(pos)))
  52. #define bitoff(n, pos) (n & ~(1ll<<(pos)))
  53.  
  54. /**Define color**/
  55. enum {WHITE,GREY,BLACK};
  56.  
  57. /**Sync off with stdio**/
  58. #define __ cin.sync_with_stdio(false);\
  59.            cin.tie();
  60.  
  61. /**Debug tools**/
  62. #define what_is(x) cerr<<(#x)<<" is "<<x<<endl
  63. using namespace std;
  64.  
  65. /**Typedef**/
  66. typedef vector<int> vint;
  67. typedef vector< vint > vint2D;
  68. typedef vector<string> vstr;
  69. typedef vector<char>vchar;
  70. typedef vector< vchar >vchar2D;
  71. typedef queue<int> Qi;
  72. typedef queue< Qi > Qii;
  73. typedef map<int,int> Mii;
  74. typedef map<string,int> Msi;
  75. typedef map<int,string> Mis;
  76. typedef stack<int> stk;
  77. typedef pair<int,int> pp;
  78. typedef pair<int, pp > ppp;
  79. typedef long long int ll;
  80. ll inf=1e18;
  81.  
  82. /**Template & structure**/
  83.  
  84. template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
  85.  
  86. template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
  87.  
  88. template<typename T>T last_bit(T n) { return n & 1; }
  89.  
  90. template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
  91.  
  92. template<class T>T modInv(T a, T m){T x, y; extgcd(a, m, x, y); x %= m; while (x < 0){x += m;} return x;}
  93.  
  94. template<class T> T extgcd(T a,T b,T& x,T& y){if(b==0){x=1;y=0;return a;}else{int g=extgcd(b,a%b,y,x);y-=a/b*x;return g;}}
  95.  
  96. template<class T>T multiplication(T n,T p,T m){if(p==0)return (T)0;T x=multiplication(n,p/2,m);x=(x+x)%m;if(p&1)x=(x+n)%m;return x;}
  97.  
  98. template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
  99.  
  100. template <class T> double getdist(T a, T b){return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));}/// distance between a & b
  101.  
  102. template <class T> T extract(string s, T ret) {stringstream ss(s); ss >> ret; return ret;}/// extract words or numbers from a line
  103.  
  104. template <class T> string tostring(T n) {stringstream ss; ss << n; return ss.str();}/// convert a number to string
  105.  
  106. template<class T> T Mod(T n,T m) {return (n%m+m)%m;} ///For Positive Negative No.
  107.  
  108. template<class T> T MIN3(T a,T b,T c) {return min(a,min(b,c));} /// minimum of 3 number
  109.  
  110. template<class T> T MAX3(T a,T b,T c) {return max(a,max(b,c));} ///maximum of 3 number
  111.  
  112. template <class T> void print_vector(T &v){int sz=v.size();if(sz)cout<<v[0];for(int i = 1; i < sz; i++)cout << ' '<<v[i];cout<<"\n";}/// prints all elements in a vector
  113.  
  114. bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
  115.  
  116. bool isConsonant(char ch){if (isalpha(ch) && !isVowel(ch)) return true; return false;}
  117.  
  118. template <class R> R Josephus(R n,R k){R ans=1;for(R i=2;i<=n;i++)ans=(ans+k-1)%i+1;return ans;}
  119.  
  120. template <class R> R toitent_Phi2(R a){R result = a;for(R i=2;i*i<=a;i++){if(a%i==0) result=result-result/i;while(a%i==0) a=a/i;}if(a>1) result=result-result/a;return result;}
  121.  
  122. template <typename T> T Angle(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
  123.  
  124.  
  125. //namespace debug{
  126. //    int sum(){return 0;}
  127. //    template<typename T,typename... Args> T sum(T a,Args... args) {return a+sum(args...);}
  128. //    void print(){cout<<"\n";return;}template<typename T, typename... Args>void print(T a,Args... args){cout<<a<<" ";print(args...);}
  129. //}
  130.  
  131.  
  132. /**Direction**/
  133. ///int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; ///8 Direction
  134. ///int dx[4] = {1, 0, -1, 0};int dy[4] = {0, 1, 0, -1}; ///4 Direction
  135. ///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};///Knight Direction
  136. ///int dx[]={-1,-1,+0,+1,+1,+0};int dy[]={-1,+1,+2,+1,-1,-2}; ///Hexagonal Direction
  137.  
  138.  
  139. /******************************End******************************/
  140.  
  141. #define mxn 10009
  142.  
  143. ll par[mxn];
  144. ll vis[mxn];
  145. ll inp[mxn][4];
  146. ll ans[10*mxn];
  147. ll cum[mxn];
  148. vector<ll>graph[mxn],cost[mxn];
  149.  
  150. ll find_par(ll u){
  151.     if(par[u]==u) return u;
  152.     return par[u]=find_par(par[u]);
  153. }
  154.  
  155. void dfs(ll u,ll x){
  156.     vis[u]=1;
  157.     cum[u]=x;
  158.     ll sz=graph[u].size();
  159.     for(ll i=0;i<sz;i++){
  160.         ll v=graph[u][i];
  161.         ll c=cost[u][i]; //cout<<"in dfs:"<<u<<" "<<v<<" "<<c<<"\n";
  162.         if(vis[v]==0){
  163.             dfs(v,x^c);
  164.         }
  165.     }
  166. }
  167.  
  168. int main()
  169. { //freopen("10158 output","w",stdout);
  170.     //__;
  171.  
  172.     ll n,i,j,k;
  173.     ll c,u,v;
  174.     scanf("%lld",&n);
  175.     if(n){
  176.         for(i=0;i<=n;i++){
  177.             par[i]=i;
  178.             vis[i]=0;
  179.             ans[i]=-1;
  180.         }
  181.  
  182.         k=0;
  183.         while(1){
  184.             scanf("%lld %lld %lld",&c,&u,&v);
  185.             //cout<<c<<" "<<u<<" "<<v<<"\n";
  186.             //cin>>c>>u>>v;
  187.             if(c==0) break;
  188.             k++;
  189.             ans[k]=-1;
  190.             inp[k][1]=c;
  191.             inp[k][2]=u;
  192.             inp[k][3]=v;
  193.             if(c==1 || c==2){
  194.                 ll x=find_par(u); //cout<<"par of:"<<u<<" is:"<<x<<"\n";
  195.                 ll y=find_par(v); //cout<<"par of:"<<v<<" is:"<<y<<"\n";
  196.                 if(x!=y){
  197.                     par[y]=x;
  198.                     if(c==1){
  199.                         graph[u].push_back(v);
  200.                         graph[v].push_back(u);
  201.                         cost[u].push_back(0);
  202.                         cost[v].push_back(0);
  203.                         ans[k]=2;
  204.                     }
  205.                     else if(c==2){
  206.                         graph[u].push_back(v);
  207.                         graph[v].push_back(u);
  208.                         cost[u].push_back(1);
  209.                         cost[v].push_back(1);
  210.                         ans[k]=2;
  211.                     }
  212.                 }
  213.                 else{
  214.                     if(c==1 && u==v) ans[k]=2;
  215.                     else if(c==2 && u==v) ans[k]=-1;
  216.                 }
  217.             }
  218.             else if(c==3 || c==4){
  219.                 ll x=find_par(u); //cout<<"par of:"<<u<<" is:"<<x<<"\n";
  220.                 ll y=find_par(v); //cout<<"par of:"<<v<<" is:"<<y<<"\n";
  221.                 if(x!=y){
  222.                     ans[k]=0;
  223.                 }
  224.             }
  225.         } //cout<<"val k:"<<k<<"\n";
  226.  
  227.         for(i=0;i<n;i++){
  228.             if(vis[i]==0){ //cout<<"dfs of:"<<i<<"\n";
  229.                 cum[i]=0;
  230.                 dfs(i,0);
  231.             }
  232.         }
  233. //        cout<<"cumulative sum\n";
  234. //        for(i=0;i<n;i++) cout<<i<<" "<<cum[i]<<"\n";
  235.  
  236.         for(i=1;i<=k;i++){
  237.             if(ans[i]==-1){ //cout<<"wtf:"<<i<<"\n";
  238.                 c=inp[i][1];
  239.                 u=inp[i][2];
  240.                 v=inp[i][3]; //cout<<"checking:"<<c<<" "<<u<<" "<<v<<"\n";
  241.                 //ll x=find_par(u);
  242.                 //ll y=find_par(v);
  243.  
  244.                 if(c==1 || c==2){
  245.                     if(c==1){
  246.                         if(cum[u]==cum[v]){
  247.                             ans[i]=2;
  248.                         }
  249.                         else cum[i]=-1;
  250.                     }
  251.                     else if(c==2){
  252.                         if(cum[u]!=cum[v]){
  253.                             ans[i]=2;
  254.                         }
  255.                         else ans[i]=-1;
  256.                     }
  257.                 }
  258.                 else if(c==3 || c==4){
  259.                     if(c==3){
  260.                         if(cum[u]==cum[v]){
  261.                             ans[i]=1;
  262.                         }
  263.                         else ans[i]=0;
  264.                     }
  265.                     else if(c==4){
  266.                         if(cum[u]!=cum[v]){
  267.                             ans[i]=1;
  268.                         }
  269.                         else ans[i]=0;
  270.                     }
  271.                 }
  272.             }
  273.         }
  274.  
  275.         for(i=1;i<=k;i++){
  276.             if(ans[i]!=2){//cout<<inp[i][1]<<" "<<inp[i][2]<<" "<<inp[i][3]<<" ";
  277. //                cout<<ans[i]<<"\n";
  278.                 printf("%lld\n",ans[i]);
  279.             }
  280.         }
  281.  
  282. //        for(i=0;i<n;i++){
  283. //            graph[i].clear();
  284. //            cost[i].clear();
  285.         }
  286.     }
  287.  
  288.     return 0;
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement