Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <map>
- #include <stack>
- #include <queue>
- #include <deque>
- #include <unordered_map>
- #include <numeric>
- #include <iomanip>
- using namespace std;
- #define pii pair<int , int>
- #define ll long long
- #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
- const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
- const long long dl[2] = {1, -1};
- const long long MOD = 1000000007;
- const long long MAXN = 200005;
- int N, M;
- int v[MAXN][3];
- int sep[MAXN][3];
- int t[MAXN];
- vector<pii> q;
- int p[MAXN], sz[MAXN];
- set<int> group[MAXN];
- int Find(int x){
- if(x == p[x]) return x;
- return p[x] = Find(p[x]);
- }
- void Union(int a, int b, int cur){
- a = Find(a);
- b = Find(b);
- if(a == b) return;
- if(group[a].find(1) != group[a].end()){
- for(auto x : group[b]){
- if(t[x] == -2){
- t[x] = cur;
- }
- }
- }
- if(group[b].find(1) != group[b].end()){
- for(auto x : group[a]){
- if(t[x] == -2){
- t[x] = cur;
- }
- }
- }
- if(sz[a] > sz[b]) swap(a, b);
- group[b].merge(group[a]);
- p[a] = b;
- sz[b] += sz[a];
- }
- void init(){
- for(int i = 1; i <= N; i++){
- p[i] = i;
- sz[i] = 1;
- t[i] = -2;
- group[i].insert(i);
- }
- }
- int main() {
- FAST;
- cin >> N >> M;
- init();
- for(int x, y, i = 1; i <= N; i++){
- cin >> x >> y;
- v[i][1] = x;
- v[i][2] = y;
- }
- for(int x, y, i = 0; i < M; i++){
- cin >> x >> y;
- q.push_back({x, y});
- sep[x][y] = 1;
- }
- for(int i = 1; i <= N; i++){
- if(sep[i][1] == 0 and v[i][1] != -1){
- Union(i, v[i][1], 0);
- }
- if(sep[i][2] == 0 and v[i][2] != -1){
- Union(i, v[i][2], 0);
- }
- }
- for(int i = 1; i <= N; i++){
- if(Find(i) == Find(1)){
- t[i] = -1;
- }
- }
- for(int i = M - 1; i >= 0; i--){
- Union(q[i].first, v[q[i].first][q[i].second], i);
- }
- for(int i = 1; i <= N; i++){
- cout << t[i] << "\n";
- }
- }
- /*
- 3 2
- -1 3
- 3 -1
- 1 2
- 1 2
- 3 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement