답안 #972959

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
972959 2024-05-01T10:51:19 Z THXuan Tourism (JOI23_tourism) C++14
28 / 100
5000 ms 23120 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 1e9
using namespace std;
typedef long long ll;

int timer = 0, ans = 0;
vector<int>adj[100005], g[100005];
int tin[100005], tout[100005], d[100005], p[100005][20], c[100005];

void dfs(int s, int e) {
	for (int i = 1; i <= 18; i++) {
		p[s][i] = p[p[s][i - 1]][i - 1];
	}
	++timer;
	tin[s] = timer;
	for (auto u : adj[s]) {
		if (u == e) continue;
		d[u] = d[s] + 1;
		p[u][0] = s;
		dfs(u, s);
	}
	tout[s] = timer;
}

int lca(int a, int b) {
	if (d[a] > d[b]) swap(a, b);
	int dis = d[b] - d[a];
	for (int i = 0; i <= 18; i++) {
		if (dis & (1 << i)) b = p[b][i];
	}
	if (a == b) return a;
	for (int i = 18; i >= 0; i--) {
		if (p[a][i] != p[b][i]) {
			a = p[a][i];
			b = p[b][i];
		}
	}
	return p[a][0];
}

bool ancestor(int x, int y) {
	return tin[x] <= tin[y] && tout[y] <= tout[x];
}

bool comp(int x, int y) {
	return tin[x] < tin[y];
}

void dfs1(int s) {
	for (auto u : g[s]) {
		ans += d[u] - d[s];
		dfs1(u);
	}
}

void build(vector<int>& a) {
	sort(a.begin(), a.end(), comp);
	int sz = a.size();
	for (int i = 0; i < sz - 1; i++) {
		a.push_back(lca(a[i], a[i + 1]));
	}
	sort(a.begin(), a.end(), comp);
	a.resize(unique(a.begin(), a.end()) - a.begin());
	stack<int>stk;
	for (auto u : a) {
		while (stk.size() && !ancestor(stk.top(), u))stk.pop();
		if (stk.size()) g[stk.top()].push_back(u);
		stk.push(u);
	}
	//for (int i = 0; i < a.size(); i++) cout << a[i] << " ";
	dfs1(a[0]);
	for (auto u : a) g[u].clear();
}

void solve()
{
	int n, m, q; cin >> n >> m >> q;
	for (int i = 1; i <= n - 1; i++) {
		int u, v; cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1, 0);
	for (int i = 1; i <= m; i++) cin >> c[i];
	while (q--) {
		int l, r; cin >> l >> r;
		vector<int>a;
		ans = 0;
		for (int i = l; i <= r; i++) a.push_back(c[i]);
		build(a);
		cout << ans + 1 << "\n";
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;// cin>>t;
	while (t--) solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 6 ms 6236 KB Output is correct
5 Correct 4 ms 5976 KB Output is correct
6 Correct 7 ms 6164 KB Output is correct
7 Correct 6 ms 6212 KB Output is correct
8 Correct 4 ms 5976 KB Output is correct
9 Correct 8 ms 6236 KB Output is correct
10 Correct 9 ms 6168 KB Output is correct
11 Correct 9 ms 6236 KB Output is correct
12 Correct 18 ms 6236 KB Output is correct
13 Correct 17 ms 6232 KB Output is correct
14 Correct 19 ms 6488 KB Output is correct
15 Correct 7 ms 6232 KB Output is correct
16 Correct 7 ms 6232 KB Output is correct
17 Correct 7 ms 6236 KB Output is correct
18 Correct 7 ms 6236 KB Output is correct
19 Correct 8 ms 6232 KB Output is correct
20 Correct 8 ms 6232 KB Output is correct
21 Correct 6 ms 6236 KB Output is correct
22 Correct 6 ms 5980 KB Output is correct
23 Correct 6 ms 6176 KB Output is correct
24 Correct 6 ms 6232 KB Output is correct
25 Correct 6 ms 6232 KB Output is correct
26 Correct 7 ms 5980 KB Output is correct
27 Correct 3 ms 6164 KB Output is correct
28 Correct 2 ms 5980 KB Output is correct
29 Correct 2 ms 5976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 6 ms 6236 KB Output is correct
5 Correct 4 ms 5976 KB Output is correct
6 Correct 7 ms 6164 KB Output is correct
7 Correct 6 ms 6212 KB Output is correct
8 Correct 4 ms 5976 KB Output is correct
9 Correct 8 ms 6236 KB Output is correct
10 Correct 9 ms 6168 KB Output is correct
11 Correct 9 ms 6236 KB Output is correct
12 Correct 18 ms 6236 KB Output is correct
13 Correct 17 ms 6232 KB Output is correct
14 Correct 19 ms 6488 KB Output is correct
15 Correct 7 ms 6232 KB Output is correct
16 Correct 7 ms 6232 KB Output is correct
17 Correct 7 ms 6236 KB Output is correct
18 Correct 7 ms 6236 KB Output is correct
19 Correct 8 ms 6232 KB Output is correct
20 Correct 8 ms 6232 KB Output is correct
21 Correct 6 ms 6236 KB Output is correct
22 Correct 6 ms 5980 KB Output is correct
23 Correct 6 ms 6176 KB Output is correct
24 Correct 6 ms 6232 KB Output is correct
25 Correct 6 ms 6232 KB Output is correct
26 Correct 7 ms 5980 KB Output is correct
27 Correct 3 ms 6164 KB Output is correct
28 Correct 2 ms 5980 KB Output is correct
29 Correct 2 ms 5976 KB Output is correct
30 Correct 155 ms 6488 KB Output is correct
31 Correct 240 ms 6432 KB Output is correct
32 Correct 329 ms 6952 KB Output is correct
33 Correct 329 ms 6516 KB Output is correct
34 Correct 326 ms 6500 KB Output is correct
35 Correct 1026 ms 6500 KB Output is correct
36 Correct 956 ms 6496 KB Output is correct
37 Correct 963 ms 6756 KB Output is correct
38 Correct 311 ms 6492 KB Output is correct
39 Correct 309 ms 6448 KB Output is correct
40 Correct 296 ms 6632 KB Output is correct
41 Correct 1004 ms 6740 KB Output is correct
42 Correct 998 ms 6672 KB Output is correct
43 Correct 1014 ms 6860 KB Output is correct
44 Correct 307 ms 6588 KB Output is correct
45 Correct 303 ms 6544 KB Output is correct
46 Correct 313 ms 6832 KB Output is correct
47 Correct 983 ms 6808 KB Output is correct
48 Correct 961 ms 6540 KB Output is correct
49 Correct 1030 ms 6560 KB Output is correct
50 Correct 230 ms 6500 KB Output is correct
51 Correct 244 ms 6492 KB Output is correct
52 Correct 242 ms 6492 KB Output is correct
53 Correct 229 ms 6496 KB Output is correct
54 Correct 231 ms 6492 KB Output is correct
55 Correct 250 ms 6492 KB Output is correct
56 Correct 81 ms 6216 KB Output is correct
57 Correct 4 ms 6232 KB Output is correct
58 Correct 3 ms 6236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5976 KB Output is correct
2 Correct 3 ms 6188 KB Output is correct
3 Correct 120 ms 6224 KB Output is correct
4 Execution timed out 5036 ms 23120 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5976 KB Output is correct
2 Correct 37 ms 15196 KB Output is correct
3 Correct 60 ms 15700 KB Output is correct
4 Correct 47 ms 15708 KB Output is correct
5 Correct 97 ms 21324 KB Output is correct
6 Correct 102 ms 21280 KB Output is correct
7 Correct 95 ms 20216 KB Output is correct
8 Correct 101 ms 19796 KB Output is correct
9 Correct 93 ms 19540 KB Output is correct
10 Correct 87 ms 19560 KB Output is correct
11 Correct 86 ms 19564 KB Output is correct
12 Correct 93 ms 19452 KB Output is correct
13 Correct 77 ms 19600 KB Output is correct
14 Correct 79 ms 19816 KB Output is correct
15 Correct 82 ms 20640 KB Output is correct
16 Correct 95 ms 20404 KB Output is correct
17 Correct 83 ms 20428 KB Output is correct
18 Correct 87 ms 20424 KB Output is correct
19 Correct 101 ms 22156 KB Output is correct
20 Correct 103 ms 21828 KB Output is correct
21 Correct 92 ms 21528 KB Output is correct
22 Correct 113 ms 21316 KB Output is correct
23 Correct 107 ms 21496 KB Output is correct
24 Correct 91 ms 21016 KB Output is correct
25 Correct 88 ms 20816 KB Output is correct
26 Correct 90 ms 20484 KB Output is correct
27 Correct 91 ms 20240 KB Output is correct
28 Correct 90 ms 19796 KB Output is correct
29 Correct 107 ms 19896 KB Output is correct
30 Correct 96 ms 19680 KB Output is correct
31 Correct 85 ms 19800 KB Output is correct
32 Correct 81 ms 19796 KB Output is correct
33 Correct 80 ms 20052 KB Output is correct
34 Correct 97 ms 20824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 3 ms 5980 KB Output is correct
3 Correct 83 ms 6232 KB Output is correct
4 Execution timed out 5099 ms 18616 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 6 ms 6236 KB Output is correct
5 Correct 4 ms 5976 KB Output is correct
6 Correct 7 ms 6164 KB Output is correct
7 Correct 6 ms 6212 KB Output is correct
8 Correct 4 ms 5976 KB Output is correct
9 Correct 8 ms 6236 KB Output is correct
10 Correct 9 ms 6168 KB Output is correct
11 Correct 9 ms 6236 KB Output is correct
12 Correct 18 ms 6236 KB Output is correct
13 Correct 17 ms 6232 KB Output is correct
14 Correct 19 ms 6488 KB Output is correct
15 Correct 7 ms 6232 KB Output is correct
16 Correct 7 ms 6232 KB Output is correct
17 Correct 7 ms 6236 KB Output is correct
18 Correct 7 ms 6236 KB Output is correct
19 Correct 8 ms 6232 KB Output is correct
20 Correct 8 ms 6232 KB Output is correct
21 Correct 6 ms 6236 KB Output is correct
22 Correct 6 ms 5980 KB Output is correct
23 Correct 6 ms 6176 KB Output is correct
24 Correct 6 ms 6232 KB Output is correct
25 Correct 6 ms 6232 KB Output is correct
26 Correct 7 ms 5980 KB Output is correct
27 Correct 3 ms 6164 KB Output is correct
28 Correct 2 ms 5980 KB Output is correct
29 Correct 2 ms 5976 KB Output is correct
30 Correct 155 ms 6488 KB Output is correct
31 Correct 240 ms 6432 KB Output is correct
32 Correct 329 ms 6952 KB Output is correct
33 Correct 329 ms 6516 KB Output is correct
34 Correct 326 ms 6500 KB Output is correct
35 Correct 1026 ms 6500 KB Output is correct
36 Correct 956 ms 6496 KB Output is correct
37 Correct 963 ms 6756 KB Output is correct
38 Correct 311 ms 6492 KB Output is correct
39 Correct 309 ms 6448 KB Output is correct
40 Correct 296 ms 6632 KB Output is correct
41 Correct 1004 ms 6740 KB Output is correct
42 Correct 998 ms 6672 KB Output is correct
43 Correct 1014 ms 6860 KB Output is correct
44 Correct 307 ms 6588 KB Output is correct
45 Correct 303 ms 6544 KB Output is correct
46 Correct 313 ms 6832 KB Output is correct
47 Correct 983 ms 6808 KB Output is correct
48 Correct 961 ms 6540 KB Output is correct
49 Correct 1030 ms 6560 KB Output is correct
50 Correct 230 ms 6500 KB Output is correct
51 Correct 244 ms 6492 KB Output is correct
52 Correct 242 ms 6492 KB Output is correct
53 Correct 229 ms 6496 KB Output is correct
54 Correct 231 ms 6492 KB Output is correct
55 Correct 250 ms 6492 KB Output is correct
56 Correct 81 ms 6216 KB Output is correct
57 Correct 4 ms 6232 KB Output is correct
58 Correct 3 ms 6236 KB Output is correct
59 Correct 2 ms 5976 KB Output is correct
60 Correct 3 ms 6188 KB Output is correct
61 Correct 120 ms 6224 KB Output is correct
62 Execution timed out 5036 ms 23120 KB Time limit exceeded
63 Halted 0 ms 0 KB -