Submission #403873

# Submission time Handle Problem Language Result Execution time Memory
403873 2021-05-13T14:29:26 Z wind_reaper Inside information (BOI21_servers) C++17
0 / 100
43 ms 836 KB
#include <bits/stdc++.h>

using namespace std;

int INF = 1000000000;

struct DSU{
	vector<int> head, par, sz;
	vector<map<int, int>> data;

	DSU(int n){
		data.resize(n);
		head.resize(n);
		par.resize(n);
		sz.resize(n);
		for(int i = 0; i < n; i++){
			par[i] = i;
			sz[i] = 1;
			data[i][i] = -INF;
			head[i] = INF;
		}
	}

	int get(int x){
		return (par[x] == x ? x : get(par[x]));
	}

	void unite(int x, int y, int timer){
		if(sz[x] < sz[y])
			swap(x, y);
		par[y] = x;
		head[y] = timer;
		sz[x] += sz[y];
		for(auto& [chunk, t] : data[y]){
			data[x][chunk] = timer;
		}
	}

	bool query(int node, int og, int chunk){
		// cout << "query" << " " << node << ' ' << og << ' ' << chunk << endl;
		if(head[node] > head[og] || par[node] == node){
			return (data[node][chunk] == 0 ? 0 : data[node][chunk] <= head[og]);
		}
		else return query(par[node], og, chunk);
	}
};

int32_t main(){
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); 
	
	int N, Q;
	cin >> N >> Q;

	DSU D(N);
	int timer = 1;
	for(int i = 1; i <= N+Q-1; i++){
		char type;
		cin >> type;
		if(type == 'S'){
			int u, v;
			cin >> u >> v;
			--u, --v;
			D.unite(u, v, timer);
			timer++;
		}
		else if(type == 'Q'){
			int u, chunk;
			cin >> u >> chunk;
			--u, --chunk;
			cout << (D.query(u, u, chunk) == 0 ? "no" : "yes") << '\n';
		}
	}

	return 0; 
}
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 752 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 752 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 780 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 780 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 836 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 36 ms 836 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 43 ms 740 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 43 ms 740 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 39 ms 816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 39 ms 816 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 35 ms 724 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 35 ms 724 KB Output isn't correct
2 Halted 0 ms 0 KB -