QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#864537#9678. 网友小 Z 的树KaXdd_#0 0ms0kbC++141.6kb2025-01-20 18:31:062025-01-20 18:31:18

Judging History

你现在查看的是最新测评结果

  • [2025-01-20 18:31:18]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2025-01-20 18:31:06]
  • 提交

answer

#include"diameter.h"
#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
int fa[N], dep[N], cnt, p[N], pos[N], l[N], r[N];
vector<int> g[N];
void dfs(int u) {
	p[++cnt] = u, pos[u] = cnt;
	dep[u] = dep[fa[u]] + 1;
	l[u] = cnt;
	for (auto v : g[u])
		if (v != fa[u]) {
			fa[v] = u, dfs(v);
			p[++cnt] = u;
		}
	r[u] = cnt;
}

int lg[N];
std::pair<int, int> mn[N][20];
void init() {
	cnt = 0, dfs(1), lg[0] = -1;
	for (int i = 1; i <= cnt; i++) {
		mn[i][0] = std::make_pair(dep[p[i]], p[i]);
		lg[i] = lg[i >> 1] + 1;
	}
	for (int j = 1; j <= lg[cnt]; j++) {
		for (int i = 1; i <= cnt - (1 << j) + 1; i++) {
			mn[i][j] = std::min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
		}
	}
}
int lca(int x, int y) {
	int l = pos[x], r = pos[y];
	if (l > r) std::swap(l, r);
	int tmp = lg[r - l + 1];
	return std::min(mn[l][tmp], mn[r - (1 << tmp) + 1][tmp]).second;
}

void add(int x, int y){
	g[x].push_back(y);
}

int dis(int x, int y){
	return dep[x] + dep[y] - 2 * dep[lca(x, y)];
}

pair<int, int> find_diameter(int subid, int n){
	for (int i = 1; i <= n; i++) g[i].clear();
	for (int x = 1; x <= n; x++){
		for (int y = x + 1; y <= n; y++){
			vector<int> cnt;
			for (int z = y + 1; z <= n; z++){
				int tp = query(x, y, z);
				if (tp == 4) cnt.push_back(z);
			}
			if (cnt.size()) add(x, y), add(y, x);
		}
	}
	init();
	int x = 1, y = 1;
	for (int i = 1; i <= n; i++)
		if (dis(x, i) > dis(x, y)) y = i;
	x = y, y = 1;
	for (int i = 1; i <= n; i++)
		if (dis(x, i) > dis(x, y)) y = i;
	return {x, y};
}

詳細信息

Subtask #1:

score: 0
Memory Limit Exceeded

Test #1:

score: 0
Memory Limit Exceeded

input:

1 100
25
1 3
2 18
3 8
4 18
5 14
6 22
7 18
8 10
9 11
10 12
11 25
12 16
13 11
14 17
15 17
16 25
17 2
18 20
19 18
20 12
21 1
22 17
23 14
24 1
50
1 37
2 27
3 10
4 25
5 16
6 17
7 10
8 36
9 16
10 6
11 48
12 2
13 28
14 30
15 10
16 44
17 31
18 1
19 6
20 7
21 30
22 42
23 45
24 23
25 27
26 39
27 45
28 48
29 4...

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #5:

0%

Subtask #7:

score: 0
Skipped

Dependency #6:

0%

Subtask #8:

score: 0
Skipped

Dependency #7:

0%

Subtask #9:

score: 0
Skipped

Dependency #8:

0%

Subtask #10:

score: 0
Skipped

Dependency #9:

0%

Subtask #11:

score: 0
Skipped

Dependency #1:

0%