QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#84425#5651. Parmigiana With SeafoodlnyxWA 44ms13768kbC++142.2kb2023-03-06 14:51:482023-03-06 14:51:50

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-06 14:51:50]
  • 评测
  • 测评结果:WA
  • 用时:44ms
  • 内存:13768kb
  • [2023-03-06 14:51:48]
  • 提交

answer

#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>

#define pb push_back

namespace IO {
	template<typename T> inline void rd(T& x) {
		x = 0; bool f = 0; char s = getchar();
		while (s < '0' || s > '9') f |= s == '-', s = getchar();
		while ('0' <= s && s <= '9') x = x * 10 + (s - '0'), s = getchar();
		x = f ? -x : x;
	}
	template<typename T, typename ...Args> inline void rd(T& x, Args& ...args) { rd(x), rd(args...); } 
	template<typename T> inline void wt(T x, char s) {
		int stk[114], top = 0;
		if (x < 0) putchar('-'), x = -x;
		do stk[ ++ top] = x % 10, x /= 10; while (x);
		while (top) putchar(stk[top -- ] + '0');
		putchar(s);
	}
}
using namespace IO;
using namespace std;

typedef pair<int, int> PII;
const int INF = 1e9;
const int N = 1e5 + 7;
vector<int> g[N];
int deg[N], dep[N];
int n;
int res = 0;

inline PII dfs(int u, int fa) {
	priority_queue<int, vector<int>, greater<int> > odd, even;
	dep[u] = dep[fa] + 1;
	if (!(dep[u] & 1)) res = max(res, u);
	for (int v : g[u]) {
		if (v == fa) continue;
		PII A = dfs(v, u);
		odd.push(A.first), even.push(A.second);
	}
	if (u == n) {
		while (even.size() > 1) even.pop();
		while (odd.size() > 2) odd.pop();
		if (odd.size() == 2 && even.size() == 1) res = max(res, min(odd.top(), even.top()));
	}
	else {
		if (dep[u] & 1) {
			while (even.size() > 2) even.pop();
			if (even.size() == 2) res = max(res, even.top());
		}
		else {
			while (even.size() > 1) even.pop();
			while (odd.size() > 1) odd.pop();
			if (odd.size() == 1 && even.size() == 1) res = max(res, min(odd.top(), even.top()));
		}
	}
	if (dep[u] & 1) odd.push(u);
	else even.push(u);
	while (odd.size() > 1) odd.pop();
	while (even.size() > 1) even.pop();
	int x = -INF, y = -INF;
	if (odd.size()) x = odd.top();
	if (even.size()) y = even.top();
	return {x, y};
}

int main() {
	#ifndef ONLINE_JUDGE 
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
	#endif
	rd(n);
	if (!(n & 1)) return wt(n, '\n'), 0;
	for (int i = 1; i < n; i ++ ) {
		int u, v; rd(u, v);
		g[u].pb(v), g[v].pb(u);
		deg[u] ++ , deg[v] ++ ;
	}
	for (int i = 1; i <= n; i ++ ) {
		if (deg[i] == 1) res = i;
	}
	dfs(n, 0);
	wt(res, '\n');
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 5960kb

input:

4
1 2
1 3
1 4

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 3ms
memory: 5904kb

input:

5
1 5
5 3
3 4
4 2

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 38ms
memory: 10108kb

input:

99999
81856 39633
81856 94012
99999 43062
99946 220
81856 46131
99933 36505
99939 35662
99952 70971
99999 3275
99938 58416
99976 66658
99991 87922
81856 80992
99933 6392
99951 41047
99970 54115
81856 38150
99934 73554
81856 64578
81856 18576
99951 67996
99938 84479
81856 39617
99999 18664
99946 2505...

output:

99925

result:

ok single line: '99925'

Test #4:

score: 0
Accepted
time: 44ms
memory: 13768kb

input:

99997
90325 59106
22545 8765
88871 37709
14739 95233
8778 29659
48110 57549
91258 76066
15724 65144
48244 87291
12076 94378
41946 96707
93645 12812
53817 34343
72097 94062
81212 263
78713 78150
6754 94906
20957 97539
59293 5018
77961 78090
57262 95225
79349 47902
99024 7869
10613 13728
61757 41090
4...

output:

85398

result:

ok single line: '85398'

Test #5:

score: -100
Wrong Answer
time: 41ms
memory: 9812kb

input:

97687
5206 6282
79497 65247
26426 93558
88096 86680
12934 32573
14514 39078
1619 40141
52678 92737
31478 91858
85427 62603
83477 53003
38500 72325
62910 10306
97005 13325
38472 67023
39728 18368
78232 5993
20560 1752
22173 38357
97114 10935
4680 13734
45188 13484
58025 44787
70778 20
11932 28511
416...

output:

96848

result:

wrong answer 1st lines differ - expected: '96849', found: '96848'