QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#302865 | #6307. Chase Game 2 | Itisa# | WA | 4ms | 4252kb | C++14 | 2.4kb | 2024-01-11 14:24:34 | 2024-01-11 14:24:35 |
Judging History
answer
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int read() {
int r=0,w=1;char ch=getchar();
while(ch < '0' || ch > '9'){if (ch=='-') {w=-1;}ch=getchar();}
while(ch >= '0' && ch <='9') {r = r*10+(ch-'0');ch=getchar();}
return r*w;
}
const int N = 100005;
int head[N];
int nxt[N];
int to[N];
int deg[N];
int cnt;
int lastfa;
int sons;
int ans;
int ava;
int maxlea;
int sumlea;
void add(int u,int v) {
cnt++;
nxt[cnt] = head[u];
to[cnt] = v;
head[u] = cnt;
}
void dfs1(int now,int f){
int c = 0;
for (int w = head[now]; w != -1; w = nxt[w]) {
c++;
int v = to[w];
if (v == f) continue;
dfs1(v,now);
}
deg[now] = c;
}
int dfs2(int now,int f){
int c = 0;
int leaves = 0;
for (int w = head[now]; w != -1; w = nxt[w]) {
c++;
int v = to[w];
if (v == f) continue;
int k = dfs2(v,now);
if (k == 1) {
leaves++;
}
}
if (leaves > 0) {
ava++;
sumlea += leaves;
maxlea = max(leaves,maxlea);
}
// if (leaves > 0){
// if (sons == 0) {
// sons += leaves;
// } else {
// if (sons >= leaves){
// sons -= leaves;
// ans += leaves;
// } else {
// ans += sons;
// sons = leaves - sons;
// }
// ava = true;
// }
// lastfa = now;
// }
return c;
}
int main(){
int T = read();
int n = 0;
int u,v;
cnt = 0;
memset(head,-1,sizeof(head));
while(T--) {
maxlea = -1;
sumlea = 0;
for (int i = 0; i <= n; ++i) {
head[i] = -1;
}
// for (int i = 0; i < cnt+1; ++i) {
// nxt[i] = -1;
// to[i] = -1;
// }
// memset(head,-1,sizeof(head));
cnt = 0;
n = read();
for (int i = 0; i < n-1; ++i) {
u = read();
v = read();
add(u,v);
add(v,u);
}
ans = 0;
sons = 0;
ava = 0;
dfs1(1,-1);
// return 0;
for (int i = 1; i <= n; ++i) {
if (deg[i] != 1) {
int tmp = dfs2(i,-1);
break;
}
}
if (sumlea % 2 == 0){
if (maxlea > sumlea / 2) {
ans = (sumlea-maxlea) + maxlea-(sumlea-maxlea);
} else {
ans = sumlea-maxlea*2 + maxlea;
}
} else {
if (maxlea > sumlea / 2) {
ans = (sumlea-maxlea) + maxlea-(sumlea-maxlea);
} else {
ans = sumlea-maxlea*2 + maxlea;
}
}
if (ava <= 1) {
printf("-1\n");
} else {
printf("%d\n", ans);
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 4252kb
input:
4 2 1 2 4 1 2 2 3 3 4 4 1 2 2 3 2 4 5 1 2 2 3 3 4 3 5
output:
-1 1 -1 2
result:
ok 4 number(s): "-1 1 -1 2"
Test #2:
score: 0
Accepted
time: 2ms
memory: 4196kb
input:
10000 4 1 2 1 3 3 4 4 1 2 1 3 1 4 4 1 2 2 3 1 4 5 1 2 2 3 1 4 4 5 5 1 2 2 3 3 4 4 5 4 1 2 2 3 2 4 5 1 2 1 3 2 4 2 5 4 1 2 2 3 1 4 5 1 2 1 3 2 4 1 5 5 1 2 2 3 3 4 2 5 5 1 2 1 3 2 4 2 5 4 1 2 1 3 3 4 5 1 2 1 3 3 4 1 5 4 1 2 1 3 1 4 5 1 2 1 3 3 4 3 5 5 1 2 2 3 3 4 3 5 4 1 2 1 3 2 4 5 1 2 2 3 2 4 3 5 5 ...
output:
1 -1 1 1 1 -1 2 1 2 2 2 1 2 -1 2 2 1 2 2 1 1 1 -1 2 2 2 1 -1 1 1 2 1 1 -1 1 2 1 1 1 -1 1 1 2 2 2 1 1 1 -1 1 2 1 1 2 1 2 1 1 2 -1 -1 -1 2 2 2 1 1 1 2 2 2 -1 1 2 -1 1 1 -1 2 -1 -1 1 2 2 2 1 1 1 1 1 1 1 1 1 2 -1 1 1 2 -1 2 1 1 1 -1 2 -1 1 -1 -1 2 -1 2 1 2 2 1 1 1 1 2 1 1 1 1 -1 2 1 2 1 1 1 1 1 1 1 2 -1...
result:
ok 10000 numbers
Test #3:
score: -100
Wrong Answer
time: 4ms
memory: 4224kb
input:
10000 9 1 2 2 3 3 4 4 5 1 6 6 7 5 8 7 9 9 1 2 2 3 2 4 1 5 2 6 4 7 6 8 1 9 9 1 2 2 3 1 4 4 5 5 6 4 7 2 8 1 9 10 1 2 2 3 1 4 3 5 3 6 2 7 6 8 6 9 6 10 10 1 2 1 3 3 4 2 5 1 6 5 7 4 8 2 9 7 10 10 1 2 2 3 2 4 1 5 3 6 6 7 5 8 4 9 9 10 9 1 2 2 3 2 4 1 5 3 6 2 7 1 8 2 9 9 1 2 1 3 2 4 1 5 3 6 3 7 7 8 8 9 10 1...
output:
1 3 3 3 3 2 3 3 4 3 3 3 3 2 3 3 3 2 3 2 3 3 3 3 2 2 3 3 3 3 3 3 2 4 3 4 4 3 4 3 3 4 4 3 3 3 2 4 3 3 3 3 3 2 4 2 3 3 2 3 3 4 3 4 3 3 3 3 3 2 3 2 4 3 3 3 3 2 4 3 3 4 2 4 2 2 3 3 3 3 3 3 3 2 3 4 3 3 3 3 3 3 2 3 5 3 3 2 2 4 3 3 4 3 5 3 2 3 3 3 3 3 3 4 3 3 2 2 3 3 3 3 3 3 3 3 4 4 3 3 3 2 3 4 3 4 3 3 3 3 ...
result:
wrong answer 5th numbers differ - expected: '2', found: '3'