Submission #677327

# Submission time Handle Problem Language Result Execution time Memory
677327 2023-01-02T21:10:08 Z YENGOYAN Inside information (BOI21_servers) C++17
0 / 100
37 ms 4816 KB
/*
	//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\
	\\                                    //
	//  271828___182845__904523__53602__  \\
	\\  87___47____13______52____66__24_  //
	//  97___75____72______47____09___36  \\
	\\  999595_____74______96____69___67  //
	//  62___77____24______07____66__30_  \\
	\\  35___35____47______59____45713__  //
	//                                    \\
	\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\//
											  */

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_map>
#include <cmath>
#include <climits>
#include <algorithm>
#include <random>
#include <queue>
#include <deque>
#include <iomanip>
#include <string>
#include <tuple>
#include <bitset>
#include <chrono>
#include <ctime>
#include <fstream>
#include <stack>
#include <cstdio>

using namespace std;
using ll = long long;
const int N = 1e5 + 5;
const ll mod = 1e9 + 7, inf = 1e12;

int n, k, timer = 0;
vector<vector<pair<int, int>>> gp;
vector<vector<int>> up;
vector<int> achox, nvazox, d, tin, tout, par;

void dfs(int u, int p, int h, int lst) {
	up[u][0] = p;
	par[u] = p;
	tin[u] = ++timer;
	for (int i = 1; i < 20; ++i) up[u][i] = up[up[u][i - 1]][i - 1];
	achox[u] = h;
	for (pair<int, int>& v : gp[u]) {
		if (v.first != p) {
			d[v.first] = d[u] + 1;
			if (v.second >= lst) dfs(v.first, u, h, v.second);
			else dfs(v.first, u, u, v.second);
		}
	}
	tout[u] = ++timer;
}

void dfs1(int u, int p, int h, int lst) {
	nvazox[u] = h;
	for (pair<int, int>& v : gp[u]) {
		if (v.first != p) {
			if (v.second <= lst) dfs1(v.first, u, h, v.second);
			else dfs1(v.first, u, u, v.second);
		}
	}
}

bool isAncestor(int u, int v) {
	return tin[u] > tin[v] && tout[u] < tout[v];
}

int lca(int u, int v) {
	if (isAncestor(u, v)) return v;
	if (isAncestor(v, u))return u;
	for (int i = 19; i >= 0; --i) {
		if (!isAncestor(v, up[u][i])) u = up[u][i];
	}
	return up[u][0];
}

void solve() {
	cin >> n >> k;
	vector<pair<pair<int, int>, int>> query;
	gp = vector<vector<pair<int, int>>>(n);
	vector<vector<int>> upd(n);
	up = vector<vector<int>>(n, vector<int>(20));
	nvazox = tin = tout = par = d = achox = vector<int>(n);
	vector<pair<pair<int, int>, int>> edg;
	map<pair<int, int>, int> mp;
	for (int i = 0; i < n + k - 1; ++i) {
		char c; cin >> c;
		if (c == 'S') {
			int u, v; cin >> u >> v;
			gp[--u].push_back({ --v, i + 1 });
			gp[v].push_back({ u, i + 1 });
			mp[{u, v}] = mp[{v, u}] = i + 1;
			//ws[{u, v }] = 1;
		}
		else {
			int a, d; cin >> a >> d;
			query.push_back({ {--a, --d}, i + 1 });
		}
	}
	dfs(0, 0, 0, 0);
	dfs1(0, 0, 0, 1e9);
	for (pair<pair<int, int>, int> x : query) {
		int A = x.first.first, D = x.first.second, w = x.second;
		int l = A;
		if (d[D] < d[A]) l = D;
		int wa = 0, wd = 0;
		for (pair<int, int>& v : gp[l]) {
			if (v.first == par[l]) continue;
			if (tin[A] >= tin[v.first] && tout[A] <= tout[v.first]) wa = v.second;
			if (tin[D] >= tin[v.first] && tout[D] <= tout[v.first]) wd = v.second;
		}
		if (A == D) cout << "yes\n";
		else if (d[achox[A]] > d[l] || d[nvazox[D]] > d[l]) cout << "no\n";
		else if (tin[A] > tin[D] && tout[A] < tout[D]) {
			if (mp[{A, par[A]}] <= w) cout << "yes\n";
			else cout << "no\n";
		}
		else if (tin[A] < tin[D] && tout[A] > tout[D]) {
			bool f = 1;
			for (pair<int, int>& v : gp[A]) {
				if (tin[v.first] <= tin[D] && tout[v.first] >= tout[D] && v.second > w)  f = 0;
			}
			if (f) cout << "yes\n";
			else cout << "no\n";
		}
		else if (d[achox[A]] <= d[l] && d[nvazox[D]] <= d[l] && wa >= wd) {
			if (mp[{A, par[A]}] < w) cout << "yes\n";
			else cout << "no\n";
		}
		else cout << "no\n";
	}
	while (1) {
		int A, D; cin >> A >> D;
		--A, --D;
		int l = lca(A, D);
		//continue;
		int wa = 0, wd = 0, w = 1e8;
		for (pair<int, int>& v : gp[l]) {
			if (v.first == par[l]) continue;
			if (tin[A] >= tin[v.first] && tout[A] <= tout[v.first]) wa = v.second;
			if (tin[D] >= tin[v.first] && tout[D] <= tout[v.first]) wd = v.second;
		}
		if (A == D) cout << "yes\n";
		else if (d[achox[A]] > d[l] || d[nvazox[D]] > d[l]) cout << "no1\n";
		else if (tin[A] > tin[D] && tout[A] < tout[D]) {
			if (mp[{A, par[A]}] <= w) cout << "yes\n";
			else cout << "no2\n";
		}
		/*
		        9 0
S 5 9
S 4 6
S 3 5
S 1 2
S 5 8
S 5 7
S 1 4
S 3 1

 1 3
		*/
		else if (tin[A] < tin[D] && tout[A] > tout[D]) {
			bool f = 1;
			for (pair<int, int>& v : gp[A]) {
				if (v.first != par[A] && tin[v.first] <= tin[D] && tout[v.first] >= tout[D] && v.second > w)  f = 0;
			}
			if (f) cout << "yes\n";
			else cout << "no3\n";
		}
		else if (d[achox[A]] <= d[l] && d[nvazox[D]] <= d[l] && wa >= wd) {
			if (mp[{A, par[A]}] < w) cout << "yes\n";
			else cout << "no4\n";
		}
		else cout << "no5\n";
	}
}

/*
TEST
 6 9
S 1 2
S 1 3
S 3 4
Q 5 1
S 4 5
S 1 6
Q  5 1
Q 1 5
Q 1 5
Q 1 5
Q 1 5
Q 1 5
Q 1 5
Q 1 5

*/

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	//int _; cin >> _; while (_--)
		solve();
}
# Verdict Execution time Memory Grader output
1 Runtime error 24 ms 4788 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 24 ms 4788 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 28 ms 4808 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 28 ms 4808 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 24 ms 4760 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 24 ms 4760 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 24 ms 4816 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 24 ms 4816 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 25 ms 4752 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 25 ms 4752 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 37 ms 4804 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 37 ms 4804 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -