Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 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.     int q;
  78.     cin>>q;
  79.  
  80.  
  81.     char temp1;
  82.     int temp2;
  83.     int temp3;
  84.     for(int i=0; i<q; i++)
  85.     {
  86.         cin>>temp1;
  87.         cin>>temp2;
  88.  
  89.         if(temp1 == '?')
  90.         {cin>>temp3; Ans(temp2, temp3);}
  91.  
  92.         else if(temp1 == '-')
  93.             Divide(temp2);
  94.         else
  95.             Add(temp2);
  96.     }
  97.  
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement