답안 #502558

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
502558 2022-01-06T08:53:17 Z Gurban Inside information (BOI21_servers) C++17
0 / 100
30 ms 9192 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];
			}
		}
	}
	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(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);
			
			cep = sp[0][i][a].second;
			sag = sp[1][i][b].second;
			a = par[i][a];
			b = par[i][b];
			
		}
	}
	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";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 7612 KB Output is correct
2 Correct 30 ms 9128 KB Output is correct
3 Incorrect 28 ms 9192 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 7612 KB Output is correct
2 Correct 30 ms 9128 KB Output is correct
3 Incorrect 28 ms 9192 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 24 ms 7564 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 24 ms 7564 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 23 ms 7684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 23 ms 7684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 7568 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 25 ms 7568 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 26 ms 7600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 26 ms 7600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 7696 KB Output is correct
2 Correct 30 ms 9128 KB Output is correct
3 Incorrect 29 ms 9172 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 7696 KB Output is correct
2 Correct 30 ms 9128 KB Output is correct
3 Incorrect 29 ms 9172 KB Output isn't correct
4 Halted 0 ms 0 KB -