Submission #655645

# Submission time Handle Problem Language Result Execution time Memory
655645 2022-11-05T04:49:31 Z GusterGoose27 Inside information (BOI21_servers) C++11
50 / 100
422 ms 65992 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
const int inf = 1e9;

class Query {
public:
	bool type; // 0 = query, 1 = count
	int x, y, time;
	Query(bool t, int time, int x, int y=-1) : type(t), time(time), x(x), y(y) {}
	Query() {}
};

const int MAXN = 12e4;
const int MX_jump = 17;
vector<pii> edges[MAXN]; // node, time
pii lca[MAXN][MX_jump];
pii lca_rev[MAXN][MX_jump];
int lca_node[MAXN][MX_jump];
pii edge_list[MAXN];
Query queries[MAXN];
int depth[MAXN];
vector<int> cent_graph[MAXN];
int cent_par[MAXN];
bool vis[MAXN];
int sz[MAXN];
int rt;

int n, q, t = 0, p = 0;

void dfs_lca(int cur = 0, int p = -1) {
	for (pii e: edges[cur]) {
		if (e.first == p) continue;
		depth[e.first] = 1+depth[cur];
		dfs_lca(e.first, cur);
		lca_node[e.first][0] = cur;
		lca[e.first][0] = pii(e.second, e.second);
		lca_rev[e.first][0] = pii(e.second, e.second);
	} // confirmed
}

pii combine(pii a, pii b) {
	if (b == pii(-2, -2)) return a;
	if (a == pii(-2, -2)) return b;
	if (b.first <= a.second) return pii(-1, inf);
	return pii(a.first, b.second); // confirmed
}

void make_lca() {
	for (int i = 1; i < MX_jump; i++) {
		for (int j = 0; j < n; j++) {
			lca_node[j][i] = lca_node[lca_node[j][i-1]][i-1];
			lca[j][i] = combine(lca[j][i-1], lca[lca_node[j][i-1]][i-1]);
			lca_rev[j][i] = combine(lca_rev[lca_node[j][i-1]][i-1], lca_rev[j][i-1]);
		}
	} // confirmed
}

pii get_path(int a, int b) { // a != b
	pii a_path = pii(-2, -2);
	pii b_path = pii(-2, -2);
	int d = depth[a]-depth[b];
	int pow = 0;
	while (d > 0) {
		if (d & 1) {
			a_path = combine(a_path, lca[a][pow]);
			a = lca_node[a][pow];
		}
		pow++;
		d /= 2;
	}
	d = depth[b]-depth[a];
	pow = 0;
	while (d > 0) {
		if (d & 1) {
			b_path = combine(lca_rev[b][pow], b_path);
			b = lca_node[b][pow];
		}
		pow++;
		d /= 2;
	}
	for (int i = MX_jump-1; i >= 0; i--) {
		if (lca_node[a][i] != lca_node[b][i]) {
			a_path = combine(a_path, lca[a][i]);
			b_path = combine(lca_rev[b][i], b_path);
			a = lca_node[a][i];
			b = lca_node[b][i];
		}
	}
	if (a != b) {
		a_path = combine(a_path, lca[a][0]);
		b_path = combine(lca_rev[b][0], b_path);
	}
	return combine(a_path, b_path);
}

bool is_path(int a, int b, int t) {
	return get_path(a, b).second < t;
}

void make_sz(int cur, int p = -1) {
	sz[cur] = 1;
	for (pii e: edges[cur]) {
		int nxt = e.first;
		if (nxt == p || vis[nxt]) continue;
		make_sz(nxt, cur);
		sz[cur] += sz[nxt];
	}
}

int cent_dec(int cur) {
	make_sz(cur);
	int tot = sz[cur];
	bool next_found = 1;
	int p = -1;
	while (next_found) {
		next_found = 0;
		for (pii e: edges[cur]) {
			int nxt = e.first;
			if (nxt == p || vis[nxt]) continue;
			if (sz[nxt] > tot/2) {
				p = cur;
				cur = nxt;
				next_found = 1;
				break;
			}
		}
	}
	vis[cur] = 1;
	for (pii e: edges[cur]) {
		if (!vis[e.first]) {
			int c = cent_dec(e.first);
			cent_graph[cur].push_back(c);
			cent_par[c] = cur;
		}
	}
	return cur;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n >> q;
	for (int i = 0; i < n+q-1; i++) {
		int a, b;
		char c; cin >> c >> a;
		a--;
		if (c == 'S') {
			cin >> b; b--;
			edge_list[t] = pii(a, b);
			edges[a].push_back(pii(b, t));
			edges[b].push_back(pii(a, t));
			t++;
		}
		else if (c == 'Q') {
			cin >> b; b--;
			queries[p++] = Query(0, i-p, a, b);
		}
		else queries[p++] = Query(1, i-p, a);
	}
	dfs_lca();
	make_lca();
	rt = cent_dec(0);
	for (int i = 0; i < q; i++) {
		if (queries[i].type) {
			cout << "0\n";
		}
		else {
			if (is_path(queries[i].y, queries[i].x, queries[i].time)) cout << "yes\n";
			else cout << "no\n";
		}
	}
}

Compilation message

servers.cpp: In constructor 'Query::Query(bool, int, int, int)':
servers.cpp:11:12: warning: 'Query::time' will be initialized after [-Wreorder]
   11 |  int x, y, time;
      |            ^~~~
servers.cpp:11:6: warning:   'int Query::x' [-Wreorder]
   11 |  int x, y, time;
      |      ^
servers.cpp:12:2: warning:   when initialized here [-Wreorder]
   12 |  Query(bool t, int time, int x, int y=-1) : type(t), time(time), x(x), y(y) {}
      |  ^~~~~
servers.cpp: In function 'int main()':
servers.cpp:158:13: warning: operation on 'p' may be undefined [-Wsequence-point]
  158 |    queries[p++] = Query(0, i-p, a, b);
      |            ~^~
servers.cpp:158:13: warning: operation on 'p' may be undefined [-Wsequence-point]
servers.cpp:160:17: warning: operation on 'p' may be undefined [-Wsequence-point]
  160 |   else queries[p++] = Query(1, i-p, a);
      |                ~^~
servers.cpp:160:17: warning: operation on 'p' may be undefined [-Wsequence-point]
# Verdict Execution time Memory Grader output
1 Correct 29 ms 8176 KB Output is correct
2 Correct 40 ms 9768 KB Output is correct
3 Correct 34 ms 9904 KB Output is correct
4 Correct 62 ms 9932 KB Output is correct
5 Correct 43 ms 10116 KB Output is correct
6 Correct 35 ms 9896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 8176 KB Output is correct
2 Correct 40 ms 9768 KB Output is correct
3 Correct 34 ms 9904 KB Output is correct
4 Correct 62 ms 9932 KB Output is correct
5 Correct 43 ms 10116 KB Output is correct
6 Correct 35 ms 9896 KB Output is correct
7 Incorrect 29 ms 8176 KB Extra information in the output file
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 37 ms 8280 KB Output is correct
2 Correct 168 ms 56328 KB Output is correct
3 Correct 170 ms 56224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 8280 KB Output is correct
2 Correct 168 ms 56328 KB Output is correct
3 Correct 170 ms 56224 KB Output is correct
4 Incorrect 31 ms 8240 KB Extra information in the output file
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 8160 KB Output is correct
2 Correct 235 ms 65756 KB Output is correct
3 Correct 247 ms 65744 KB Output is correct
4 Correct 205 ms 65716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 8160 KB Output is correct
2 Correct 235 ms 65756 KB Output is correct
3 Correct 247 ms 65744 KB Output is correct
4 Correct 205 ms 65716 KB Output is correct
5 Incorrect 28 ms 8148 KB Extra information in the output file
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 29 ms 8152 KB Output is correct
2 Correct 207 ms 57104 KB Output is correct
3 Correct 226 ms 57252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 8152 KB Output is correct
2 Correct 207 ms 57104 KB Output is correct
3 Correct 226 ms 57252 KB Output is correct
4 Incorrect 28 ms 8272 KB Extra information in the output file
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 8280 KB Output is correct
2 Correct 244 ms 65780 KB Output is correct
3 Correct 245 ms 65812 KB Output is correct
4 Correct 205 ms 65992 KB Output is correct
5 Correct 33 ms 8216 KB Output is correct
6 Correct 198 ms 57088 KB Output is correct
7 Correct 211 ms 57272 KB Output is correct
8 Correct 270 ms 57060 KB Output is correct
9 Correct 228 ms 57092 KB Output is correct
10 Correct 310 ms 60652 KB Output is correct
11 Correct 422 ms 59928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 8280 KB Output is correct
2 Correct 244 ms 65780 KB Output is correct
3 Correct 245 ms 65812 KB Output is correct
4 Correct 205 ms 65992 KB Output is correct
5 Correct 33 ms 8216 KB Output is correct
6 Correct 198 ms 57088 KB Output is correct
7 Correct 211 ms 57272 KB Output is correct
8 Correct 270 ms 57060 KB Output is correct
9 Correct 228 ms 57092 KB Output is correct
10 Correct 310 ms 60652 KB Output is correct
11 Correct 422 ms 59928 KB Output is correct
12 Incorrect 28 ms 8148 KB Extra information in the output file
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 29 ms 8220 KB Output is correct
2 Correct 42 ms 9804 KB Output is correct
3 Correct 51 ms 9784 KB Output is correct
4 Correct 47 ms 9856 KB Output is correct
5 Correct 50 ms 10052 KB Output is correct
6 Correct 33 ms 9796 KB Output is correct
7 Correct 26 ms 8316 KB Output is correct
8 Correct 163 ms 56300 KB Output is correct
9 Correct 190 ms 56240 KB Output is correct
10 Correct 29 ms 8244 KB Output is correct
11 Correct 224 ms 65804 KB Output is correct
12 Correct 228 ms 65860 KB Output is correct
13 Correct 219 ms 65740 KB Output is correct
14 Correct 36 ms 8232 KB Output is correct
15 Correct 175 ms 57176 KB Output is correct
16 Correct 197 ms 57260 KB Output is correct
17 Correct 298 ms 57116 KB Output is correct
18 Correct 227 ms 57192 KB Output is correct
19 Correct 317 ms 60588 KB Output is correct
20 Correct 329 ms 60072 KB Output is correct
21 Correct 162 ms 56160 KB Output is correct
22 Correct 193 ms 56120 KB Output is correct
23 Correct 182 ms 56376 KB Output is correct
24 Correct 197 ms 56388 KB Output is correct
25 Correct 291 ms 60816 KB Output is correct
26 Correct 238 ms 56840 KB Output is correct
27 Correct 225 ms 56788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 8220 KB Output is correct
2 Correct 42 ms 9804 KB Output is correct
3 Correct 51 ms 9784 KB Output is correct
4 Correct 47 ms 9856 KB Output is correct
5 Correct 50 ms 10052 KB Output is correct
6 Correct 33 ms 9796 KB Output is correct
7 Correct 26 ms 8316 KB Output is correct
8 Correct 163 ms 56300 KB Output is correct
9 Correct 190 ms 56240 KB Output is correct
10 Correct 29 ms 8244 KB Output is correct
11 Correct 224 ms 65804 KB Output is correct
12 Correct 228 ms 65860 KB Output is correct
13 Correct 219 ms 65740 KB Output is correct
14 Correct 36 ms 8232 KB Output is correct
15 Correct 175 ms 57176 KB Output is correct
16 Correct 197 ms 57260 KB Output is correct
17 Correct 298 ms 57116 KB Output is correct
18 Correct 227 ms 57192 KB Output is correct
19 Correct 317 ms 60588 KB Output is correct
20 Correct 329 ms 60072 KB Output is correct
21 Correct 162 ms 56160 KB Output is correct
22 Correct 193 ms 56120 KB Output is correct
23 Correct 182 ms 56376 KB Output is correct
24 Correct 197 ms 56388 KB Output is correct
25 Correct 291 ms 60816 KB Output is correct
26 Correct 238 ms 56840 KB Output is correct
27 Correct 225 ms 56788 KB Output is correct
28 Incorrect 33 ms 8208 KB Extra information in the output file
29 Halted 0 ms 0 KB -