Advertisement
Guest User

Task 6

a guest
Dec 15th, 2012
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. /*
  2.  * File:     main.cpp
  3.  * Author:   Hrayr [HarHro94]
  4.  * Problem:  COCI contest#3
  5.  * IDE:      Visual C++
  6.  */
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <cstring>
  10. #include <cstdlib>
  11. #include <string>
  12. #include <vector>
  13. using namespace std;
  14.  
  15. int n,m,bit[100007][32],val[32*100007],pos[32];
  16. vector<vector<pair<int,int> > > G;
  17. bool used[32*100007];
  18.  
  19. void dfs(int u,int b);
  20.  
  21. int main()
  22. {
  23.     //freopen("input.txt","r",stdin);
  24.     //freopen("output.txt","w",stdout);
  25.     int i,j,k,i1,i2,cnt,type;
  26.     long long res,num;
  27.     scanf("%d %d", &n, &m);
  28.     for(i=0;i<n;++i)
  29.     {
  30.         for(j=0;j<32;++j)
  31.         {
  32.             bit[i][j]=i*32+j;
  33.         }
  34.     }
  35.     G.resize(32*n);
  36.     for(i=0;i<m;++i)
  37.     {
  38.         scanf("%d", &type);
  39.         if (type==1)
  40.         {
  41.             scanf("%d%d", &k, &cnt);
  42.             --k;
  43.             cnt%=32;
  44.             if (cnt==0)
  45.             {
  46.                 continue;
  47.             }
  48.             if (cnt==2)
  49.             {
  50.                 cnt=2;
  51.             }
  52.             int p=0,tmp[32];
  53.             for(j=0;j<32;++j)
  54.             {
  55.                 tmp[j]=bit[k][(j+cnt)%32];
  56.             }
  57.             for(j=0;j<32;++j)
  58.             {
  59.                 bit[k][j]=tmp[j];
  60.             }
  61.         }
  62.         else
  63.         {
  64.             scanf("%d%d%lld", &i1, &i2, &res);
  65.             --i1;--i2;
  66.             int tmp[32];
  67.             for(j=0;j<32;++j)
  68.             {
  69.                 tmp[j]=((res>>j)&1);
  70.             }
  71.             for(j=0;j<32;++j)
  72.             {
  73.                 G[bit[i1][j]].push_back(make_pair(bit[i2][j],tmp[j]));
  74.                 G[bit[i2][j]].push_back(make_pair(bit[i1][j],tmp[j]));
  75.             }
  76.         }
  77.     }
  78.     for(i=0;i<n;++i)
  79.     {
  80.         for(j=0;j<32;++j)
  81.         {
  82.             pos[bit[i][j]%32]=j;
  83.         }
  84.         for(j=31;j>=0;--j)
  85.         {
  86.             if (!used[bit[i][pos[j]]])
  87.             {
  88.                 dfs(bit[i][pos[j]],0);
  89.             }
  90.         }
  91.     }
  92.     for(i=0;i<n;++i)
  93.     {
  94.         num=0;
  95.         for(j=0;j<32;++j)
  96.         {
  97.             if (val[bit[i][j]]==1)
  98.             {
  99.                 num+=(1LL<<(bit[i][j]%32));
  100.             }
  101.         }
  102.         printf("%lld ", num);
  103.     }
  104.     printf("\n");
  105.     return 0;
  106. }
  107.  
  108. void dfs(int u,int b)
  109. {
  110.     int i,willBe,to,type;
  111.     val[u]=b;
  112.     used[u]=1;
  113.     for(i=0;i<G[u].size();++i)
  114.     {
  115.         to=G[u][i].first;
  116.         type=G[u][i].second;
  117.         if (type==0)
  118.         {
  119.             willBe=b;
  120.         }
  121.         else
  122.         {
  123.             willBe=1-b;
  124.         }
  125.         if (!used[to])
  126.         {
  127.             dfs(to,willBe);
  128.         }
  129.         else if (willBe!=val[to])
  130.         {
  131.             printf("-1\n");
  132.             exit(0);
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement