Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ID: dzhouwave
- PROG: closing
- LANG: C++11
- */
- // @BEGIN_OF_SOURCE_CODE
- #include <bits/stdc++.h>
- #define pi acos(-1.0)
- #define LL unsigned long long
- using namespace std;
- //#define cin fin
- //ifstream fin ("closing.in");
- //#define cout fout
- //ofstream fout ("closing.out");
- vector<int> dsu;
- vector<pair<int, int>> edges;
- vector<int> positions;
- vector<vector<int>> nodes;
- int dsuFind(int input)
- {
- if(dsu[input] == -1)
- {
- return input;
- }
- else
- {
- // dsu[input] = dsuFind(dsu[input]);
- return dsuFind(dsu[input]);
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int N, M;
- cin >> N >> M;
- // cout << N << " " << M << endl;
- positions.resize(N);
- dsu.resize(N);
- fill(dsu.begin(), dsu.end(), -1);
- nodes.resize(N);
- for(int i = 0; i < M; i++)
- {
- int first, second;
- cin >> first >> second;
- // cout << first << " " << second << endl;
- edges.emplace_back(first-1, second-1);
- }
- for(int i = 0; i < N; i++)
- {
- int input;
- cin >> input;
- // cout << input << endl;
- positions[N-i-1] = input-1;
- }
- for(int i = 0; i < edges.size(); i++)
- {
- int first = positions[edges[i].first];
- int second = positions[edges[i].second];
- // cout << first << " " << second << endl;
- if(first < second)
- {
- nodes[edges[i].second].push_back(edges[i].first);
- }
- else
- {
- nodes[edges[i].first].push_back(edges[i].second);
- }
- }
- // for(int i = 0; i < nodes.size(); i++)
- // {
- // cout << i << endl;
- // for(int j = 0; j < nodes[i].size(); j++)
- // {
- // cout << nodes[i][j] << " ";
- // }
- // cout << endl;
- // }
- // cout << endl;
- // cout << endl;
- // cout << endl;
- queue<int> unconnected;
- vector<string> result;
- int comps = 0;
- for(int i = 0; i < positions.size(); i++)
- {
- // cout << positions[i] << endl;
- comps++;
- for(int j = 0; j < nodes[positions[i]].size(); j++)
- {
- int x = dsuFind(positions[i]);
- int y = dsuFind(nodes[positions[i]][j]);
- if(x == y)
- {
- //do nothing
- }
- else
- {
- dsu[x] = y;
- comps--;
- // dsuFind(positions[i]);
- }
- // cout << "UNIONIZING" << endl;
- // cout << positions[i] << " " << nodes[positions[i]][j] << endl;
- // cout << dsu[positions[i]] << " " << dsuFind(nodes[positions[i]][j]) << endl;
- }
- if(comps <= 1)
- {
- result.push_back("YES");
- }
- else
- {
- result.push_back("NO");
- }
- }
- for(int i = 0; i < result.size(); i++)
- {
- cout << result[result.size()-i-1] << endl;
- }
- // for(int i = 0; i < dsu.size(); i++)
- // {
- // cout << dsu[i] << endl;
- // }
- return 0;
- }
- // @END_OF_SOURCE_CODE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement