QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#84859#5651. Parmigiana With Seafoodsinsop90WA 25ms12120kbC++141.1kb2023-03-06 20:20:152023-03-06 20:20:17

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 20:20:17]
  • 评测
  • 测评结果:WA
  • 用时:25ms
  • 内存:12120kb
  • [2023-03-06 20:20:15]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int head[maxn], fa[maxn], dis[maxn], ans, t1, t2, tot, n, t[maxn], vis[maxn], out[maxn];
struct node {
	int v, pre;
}e[maxn << 1];
void add(int u, int v) {
	e[++ tot].v = v;
	e[tot].pre = head[u];
	head[u] = tot;
}
void dfs(int now, int f) {
	fa[now] = f;
	for(int i = head[now];i;i = e[i].pre) {
		int v = e[i].v;
		if(v == f) continue;
		dis[v] = dis[now] ^ 1;
		dfs(v, now);
	}
}
int main() {
	scanf("%d", &n);
	if(n % 2 == 0) {
		printf("%d\n", n);
		return 0;
	}
	for(int i = 1, u, v;i < n;i++) {
		scanf("%d%d", &u, &v);
		add(u, v), add(v, u);
		out[u] ++, out[v] ++;
	}
	dfs(n, 0);
	vis[n] = 1;
	for(int i = n - 1;i >= 1;i--) {
		if(dis[i] || out[i] == 1) {
			printf("%d\n", i);
			return 0;
		}
		int now = i;
		while(1) {
//			cout << now << " " << t[now] << endl; 
			t[now] ++;
			if(vis[now] || !now) break;
			vis[now] = 1;
			now = fa[now];
			t[now] ++; 
		}
//		cout << now << " " << t[now] << endl;
		if(t[now] > 2 && !dis[now]) {
			printf("%d\n", i);
			return 0;
		}
	}
	printf("%d\n", max(ans, min(t1, t2)));
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
1 2
1 3
1 4

output:

4

result:

ok single line: '4'

Test #2:

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

input:

5
1 5
5 3
3 4
4 2

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 25ms
memory: 11800kb

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: -100
Wrong Answer
time: 12ms
memory: 12120kb

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:

99995

result:

wrong answer 1st lines differ - expected: '85398', found: '99995'