Submission #502561

# Submission time Handle Problem Language Result Execution time Memory
502561 2022-01-06T09:00:12 Z Gurban Inside information (BOI21_servers) C++17
0 / 100
29 ms 7980 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int B = 21;
const int maxn=2e5+5;
int n,k;
int p[maxn];
int L[maxn];
int par[B][maxn];
pair<int,int> sp[2][B][maxn];
vector<pair<int,int>>E[maxn];

int ata(int nd){
	if(p[nd] == nd) return nd;
	return p[nd] = ata(p[nd]);
}

void dfs(int nd,int pr){
	par[0][nd] = pr;
	L[nd] = L[pr] + 1;
	for(auto i : E[nd]){
		if(i.first != pr){
			sp[0][0][i.first] = {1,i.second};
			sp[1][0][i.first] = {1,i.second};
			dfs(i.first,nd);
		}
	}
}

bool LCA(int a,int b){
	bool tr = 1;
	int cep = 1e9;
	int sag = 0;
	if(L[a] > L[b]){
		for(int i = B-1;i >= 0;i--){
			if(par[i][a] and L[a] - (1<<i) >= L[b]){
				tr &= sp[0][i][a].first;
				tr &= (cep >= sp[0][0][a].second);
				
				// assert(sp[0][i][a].second <= sp[0][i][a].second);
				cep = sp[0][i][a].second;
				a = par[i][a];
			}
			if(!tr) return 0;
		}
	}
	else if(L[b] > L[a]){
		for(int i = B-1;i >= 0;i--){
			if(par[i][b] and L[b] - (1<<i) >= L[a]){
				tr &= sp[1][i][b].first;
				tr &= (sag <= sp[1][0][b].second);

				sag = sp[1][i][b].second;
				b = par[i][b];
			}
			
			if(!tr) return 0;
		}
	}
	if(a == b) return tr;
	for(int i = B-1;i >= 0;i--){
		if(par[i][a] and par[i][b] and par[i][a] != par[i][b]){
			tr &= sp[0][i][a].first;
			tr &= sp[1][i][b].first;

			tr &= (cep >= sp[0][0][a].second);
			tr &= (sag <= sp[1][0][b].second);
			
			if(!tr) return 0;

			assert(sp[0][0][a].second >= sp[0][i][a].second);
			assert(sp[1][0][b].second <= sp[1][i][b].second);

			cep = sp[0][i][a].second;
			sag = sp[1][i][b].second;
			a = par[i][a];
			b = par[i][b];
			
			if(!tr) return 0;
		}
	}
	tr &= (cep >= sp[0][0][a].second);
	tr &= (sag <= sp[1][0][b].second);
	tr &= (sp[0][0][a].second >= sp[1][0][b].second);
	return tr;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> k;
	for(int i = 1;i <= n;i++) p[i] = i;
	vector<tuple<int,int,int>>quer;
	for(int i = 1;i < n + k;i++){
		char tp; cin >> tp;
		if(tp == 'S'){
			int x,y; cin >> x >> y;
			E[x].push_back({y,i});
			E[y].push_back({x,i});
			
			quer.push_back({0,x,y});
		}
		else if(tp == 'Q'){
			int x,y; cin >> x >> y;
			quer.push_back({1,x,y});
		}
		else {
			int x; cin >> x;
			quer.push_back({2,x,-1});
		}
	}
	dfs(1,0);
	for(int i = 1;i < B;i++){
		for(int j = 1;j <= n;j++){
			par[i][j] = par[i-1][par[i-1][j]];
			if(sp[0][i-1][j].first and sp[0][i-1][par[i-1][j]].first and sp[0][i-1][j].second >= sp[0][0][par[i-1][j]].second){
				sp[0][i][j] = make_pair(1,sp[0][i-1][par[i-1][j]].second);
			}
			if(sp[1][i-1][j].first and sp[1][i-1][par[i-1][j]].first and sp[1][i-1][j].second <= sp[1][0][par[i-1][j]].second){
				sp[1][i][j] = {1,sp[1][i-1][par[i-1][j]].second};
			}
		}
	}
	// for(int i = 0;i < B;i++){
	// 	for(int j = 1;j <= n;j++){
	// 		if(par[i][j]) cout<<i<<' '<<j<<' '<<par[i][j]<<' '<<sp[0][i][j].first<<' '<<sp[0][i][j].second<<' '<<sp[1][i][j].first<<' '<<sp[1][i][j].second<<'\n';
	// 	}
	// }
	for(auto i : quer){
		int tp = get<0>(i);
		int x = get<1>(i);
		int y = get<2>(i);
		if(tp == 0) p[y] = x;
		else if(tp == 1){
			int aa = ata(x);
			int bb = ata(y);
			if(aa != bb){
				cout<<"no\n";
				continue;
			}
			bool jg = LCA(x,y);
			if(jg) cout<<"yes\n";
			else cout<<"no\n";
		}
		else cout<<"0\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 22 ms 7152 KB Output is correct
2 Correct 27 ms 7728 KB Output is correct
3 Incorrect 29 ms 7912 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 22 ms 7152 KB Output is correct
2 Correct 27 ms 7728 KB Output is correct
3 Incorrect 29 ms 7912 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 7132 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 7132 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 7092 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 7092 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 7128 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 7128 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 7152 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 23 ms 7152 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 24 ms 7112 KB Output is correct
2 Correct 28 ms 7760 KB Output is correct
3 Incorrect 28 ms 7980 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 24 ms 7112 KB Output is correct
2 Correct 28 ms 7760 KB Output is correct
3 Incorrect 28 ms 7980 KB Output isn't correct
4 Halted 0 ms 0 KB -