QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#302870 | #6307. Chase Game 2 | Itisa# | WA | 4ms | 4708kb | C++14 | 2.3kb | 2024-01-11 14:29:29 | 2024-01-11 14:29:30 |
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 = 200005;
int head[N];
int nxt[N];
int to[N];
int deg[N];
int cnt;
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;
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4708kb
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: 4496kb
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: 4520kb
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'