Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. /*
  2. ID: dzhouwave
  3. PROG: closing
  4. LANG: C++11
  5. */
  6.  
  7. // @BEGIN_OF_SOURCE_CODE
  8.  
  9. #include <bits/stdc++.h>
  10.  
  11. #define pi acos(-1.0)
  12. #define LL unsigned long long
  13. using namespace std;
  14.  
  15. //#define cin fin
  16. //ifstream fin ("closing.in");
  17. //#define cout fout
  18. //ofstream fout ("closing.out");
  19.  
  20. vector<int> dsu;
  21. vector<pair<int, int>> edges;
  22. vector<int> positions;
  23. vector<vector<int>> nodes;
  24.  
  25. int dsuFind(int input)
  26. {
  27.     if(dsu[input] == -1)
  28.     {
  29.         return input;
  30.     }
  31.     else
  32.     {
  33. //        dsu[input] = dsuFind(dsu[input]);
  34.         return dsuFind(dsu[input]);
  35.     }
  36. }
  37.  
  38. int main()
  39. {
  40.     ios_base::sync_with_stdio(false);
  41.     cin.tie(0);
  42.     int N, M;
  43.     cin >> N >> M;
  44. //    cout << N << " " << M << endl;
  45.     positions.resize(N);
  46.     dsu.resize(N);
  47.     fill(dsu.begin(), dsu.end(), -1);
  48.     nodes.resize(N);
  49.     for(int i = 0; i < M; i++)
  50.     {
  51.         int first, second;
  52.         cin >> first >> second;
  53. //        cout << first << " " << second << endl;
  54.         edges.emplace_back(first-1, second-1);
  55.     }
  56.     for(int i = 0; i < N; i++)
  57.     {
  58.         int input;
  59.         cin >> input;
  60. //        cout << input << endl;
  61.         positions[N-i-1] = input-1;
  62.     }
  63.     for(int i = 0; i < edges.size(); i++)
  64.     {
  65.         int first = positions[edges[i].first];
  66.         int second = positions[edges[i].second];
  67. //        cout << first << " " << second << endl;
  68.         if(first < second)
  69.         {
  70.             nodes[edges[i].second].push_back(edges[i].first);
  71.         }
  72.         else
  73.         {
  74.             nodes[edges[i].first].push_back(edges[i].second);
  75.         }
  76.     }
  77. //    for(int i = 0; i < nodes.size(); i++)
  78. //    {
  79. //        cout << i << endl;
  80. //        for(int j = 0; j < nodes[i].size(); j++)
  81. //        {
  82. //            cout << nodes[i][j] << " ";
  83. //        }
  84. //        cout << endl;
  85. //    }
  86. //    cout << endl;
  87. //    cout << endl;
  88. //    cout << endl;
  89.  
  90.     queue<int> unconnected;
  91.     vector<string> result;
  92.     int comps = 0;
  93.     for(int i = 0; i < positions.size(); i++)
  94.     {
  95. //        cout << positions[i] << endl;
  96.         comps++;
  97.         for(int j = 0; j < nodes[positions[i]].size(); j++)
  98.         {
  99.             int x = dsuFind(positions[i]);
  100.             int y = dsuFind(nodes[positions[i]][j]);
  101.             if(x == y)
  102.             {
  103.                 //do nothing
  104.             }
  105.             else
  106.             {
  107.                 dsu[x] = y;
  108.                 comps--;
  109. //                dsuFind(positions[i]);
  110.             }
  111. //            cout << "UNIONIZING" << endl;
  112. //            cout << positions[i] << " " << nodes[positions[i]][j] << endl;
  113. //            cout << dsu[positions[i]] << " " << dsuFind(nodes[positions[i]][j]) << endl;
  114.         }
  115.         if(comps <= 1)
  116.         {
  117.             result.push_back("YES");
  118.         }
  119.         else
  120.         {
  121.             result.push_back("NO");
  122.         }
  123.     }
  124.     for(int i = 0; i < result.size(); i++)
  125.     {
  126.         cout << result[result.size()-i-1] << endl;
  127.     }
  128. //    for(int i = 0; i < dsu.size(); i++)
  129. //    {
  130. //        cout << dsu[i] << endl;
  131. //    }
  132.     return 0;
  133. }
  134.  
  135. // @END_OF_SOURCE_CODE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement