Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <array>
  4. using namespace std;
  5.  
  6. int father[200000+5];
  7. bool odwiedzone[200000+5];
  8. bool pocz_sasiedzi[2000+2][2000+2];
  9. bool aktu_sasiedzi[2000+2][2000+2];
  10.  
  11.  
  12. int n;
  13.  
  14.  
  15. void Divide(int w)
  16. {
  17.     for(int i=0; i<=n; i++)
  18.     {
  19.         if(aktu_sasiedzi[w][i] == 1)
  20.         {aktu_sasiedzi[w][i] = 0; aktu_sasiedzi[i][w] = 0;}
  21.     }
  22. }
  23.  
  24. void Add(int w)
  25. {
  26.     for(int i=0; i<=n; i++)
  27.     {
  28.         if(pocz_sasiedzi[w][i] == 1)
  29.         {aktu_sasiedzi[w][i] = 1; aktu_sasiedzi[i][w] = 1;}
  30.     }
  31. }
  32.  
  33. void DFS(int w)
  34. {
  35.     odwiedzone[w] = true;
  36.     for(int i=0; i<=n; i++)
  37.     {
  38.         if(aktu_sasiedzi[w][i] == 1 && odwiedzone[i] == false)
  39.             DFS(i);
  40.     }
  41. }
  42.  
  43. void Ans(int A, int B)
  44. {
  45.  
  46.     for(int i=0; i<=n; i++)
  47.         odwiedzone[i] = false;
  48.  
  49.     DFS(A);
  50.  
  51.     if(odwiedzone[B] == true)
  52.         cout<<"TAK"<<endl;
  53.     else
  54.         cout<<"NIE"<<endl;
  55. }
  56.  
  57. int main()
  58. {
  59.     ios_base::sync_with_stdio(0);
  60.     cin.tie(0);
  61.  
  62.     cin>>n;
  63.  
  64.     int t1;
  65.     int t2;
  66.     for(int i=1; i<n; i++)
  67.     {
  68.         cin>>t1;
  69.         cin>>t2;
  70.         pocz_sasiedzi[t1][t2] = 1;
  71.         pocz_sasiedzi[t2][t1] = 1;
  72.  
  73.         aktu_sasiedzi[t1][t2] = 1;
  74.         aktu_sasiedzi[t2][t1] = 1;
  75.     }
  76.  
  77.     for(int i=1; i<=n; i++)
  78.     {
  79.         pocz_sasiedzi[i][i] = 1;
  80.         aktu_sasiedzi[i][i] = 1;
  81.  
  82.     }
  83.     int q;
  84.     cin>>q;
  85.  
  86.  
  87.     char temp1;
  88.     int temp2;
  89.     int temp3;
  90.     for(int i=0; i<q; i++)
  91.     {
  92.         cin>>temp1;
  93.         cin>>temp2;
  94.  
  95.         if(temp1 == '?')
  96.         {
  97.             cin>>temp3;
  98.  
  99.             if(temp2 == temp3 && aktu_sasiedzi[temp2][temp2] == 0)
  100.                 {cout<<"NIE"<<endl;}
  101.             else
  102.                 Ans(temp2, temp3);
  103.         }
  104.  
  105.         else if(temp1 == '-')
  106.             Divide(temp2);
  107.         else
  108.             Add(temp2);
  109.     }
  110.  
  111.  
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement