QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#84399#5651. Parmigiana With SeafoodwusuoweiwohuichushouWA 12ms9976kbC++141.1kb2023-03-06 14:34:362023-03-06 14:34:37

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:34:37]
  • 评测
  • 测评结果:WA
  • 用时:12ms
  • 内存:9976kb
  • [2023-03-06 14:34:36]
  • 提交

answer

#include <bits/stdc++.h>
#define N 100010

using namespace std;

int n, dep[N];

vector <int> g[N], a, b;

inline int read() {
	char ch = getchar(); int x = 0, f = 1;
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while('0' <= ch && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
	return x * f;
}

void dfs(int u, int fa) {
	if(dep[u] % 2)
		b.push_back(u);
	else
		a.push_back(u);
	for(int v : g[u]) {
		if(v == fa)
			continue;
		dep[v] = dep[u] + 1;
		dfs(v, u);
	}
}

int main() {
	n = read();
	
	for(int i = 1; i < n; i++) {
		int u = read(), v = read();
		
		g[u].push_back(v);
		g[v].push_back(u);
	}
	
	if(n % 2 == 0) {
		cout << n << endl;
		return 0;
	}
	
	dfs(1, 0);
	
	int res = 0;
	for(int i = 1; i <= n; i++)
		if(g[i].size() == 1)
			res = max(res, i);
	
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	reverse(a.begin(), a.end());
	reverse(b.begin(), b.end());
	if(b.size() >= 3)
		res = max(res, b[2]);
	if(a.size() && b.size())
		cout << max(res, min(a[0], b[0])) << endl;
	else
		cout << res << endl;
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 6028kb

input:

4
1 2
1 3
1 4

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 2ms
memory: 5668kb

input:

5
1 5
5 3
3 4
4 2

output:

3

result:

ok single line: '3'

Test #3:

score: -100
Wrong Answer
time: 12ms
memory: 9976kb

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:

99997

result:

wrong answer 1st lines differ - expected: '99925', found: '99997'