QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#242830#7338. Education NightmarePetroTarnavskyi#WA 656ms32288kbC++201.6kb2023-11-07 17:40:532023-11-07 17:40:53

Judging History

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

  • [2023-11-07 17:40:53]
  • 评测
  • 测评结果:WA
  • 用时:656ms
  • 内存:32288kb
  • [2023-11-07 17:40:53]
  • 提交

answer

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

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); i--)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int N = 1 << 18;

int n, s, m;
VI g[N];
int sz[N], d[N], h[N];
bool isAnc[N];
int sum[N], mx[N];
int ans;

void dfs1(int v, int p)
{
	sz[v] = 1;
	h[v] = 1;
	isAnc[v] = v == m;
	for (int to : g[v])
	{
		if (to == p)
			continue;
		d[to] = d[v] + 1;
		dfs1(to, v);
		sz[v] += sz[to];
		h[v] = max(h[v], h[to] + 1);
		isAnc[v] |= isAnc[to];
	}
}

void dfs2(int v, int p)
{
	int son = -1;
	for (int to : g[v])
	{
		if (to == p)
			continue;
		if (isAnc[to])
			son = to;
		else
		{
			sum[v] += 2 * sz[to];
			mx[v] = max(mx[v], h[to] + d[m] - d[v]);
		}
	}
	if (son != -1)
	{
		sum[son] = sum[v] + 1;
		dfs2(son, v);
		mx[v] = max(mx[v], mx[son]);
	}
	ans = min(ans, sum[v] + d[m] - d[v] + mx[v]);
}

void solve()
{
	cin >> n >> s >> m;
	s--;
	m--;
	FOR(i, 0, n - 1)
	{
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		g[u].PB(v);
		g[v].PB(u);
	}
	d[s] = 0;
	dfs1(s, -1);
	ans = 4 * n;
	dfs2(s, -1);
	cout << ans << "\n";
	FOR(i, 0, n)
	{
		g[i].clear();
		sum[i] = 0;
		mx[i] = 0;
	}
}

int main()
{
    ios::sync_with_stdio(0); 
    cin.tie(0);
	cout << fixed << setprecision(15);
	int t;
	cin >> t;
	while (t--)
		solve();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 14096kb

input:

1
4 2 3
1 2
1 3
1 4

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: -100
Wrong Answer
time: 656ms
memory: 32288kb

input:

14966
15 13 8
7 14
7 10
5 3
1 3
15 3
1 13
9 5
7 2
6 13
10 11
13 8
5 10
5 12
14 4
16 14 9
16 11
14 9
8 15
14 15
13 10
6 11
3 2
9 5
6 7
10 6
6 8
1 5
15 4
10 2
11 12
100 49 58
67 43
55 34
84 42
3 74
84 54
20 6
86 83
88 51
2 99
4 78
91 64
14 59
82 38
91 44
24 12
12 2
39 19
43 46
5 80
41 35
80 97
79 8
47...

output:

27
31
146
22
3
9
131
8
119
12
13
180
8
122
121
7
113
8
111
19
164
151
107
144
7
117
0
102
107
6
108
135
7
25
7
174
139
104
13
3
3
12
12
26
22
5
103
118
111
15
114
155
11
6
1
114
13
137
117
10
5
132
99
8
3
11
6
13
7
209
187
123
141
13
30
166
113
12
155
11
6
4
8
5
11
18
9
134
29
188
130
8
161
146
3
10...

result:

wrong answer 1st numbers differ - expected: '9', found: '27'