QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#332125 | #6307. Chase Game 2 | automac# | WA | 7ms | 3788kb | C++20 | 2.2kb | 2024-02-19 09:27:57 | 2024-02-19 09:27:58 |
Judging History
answer
#include <bits/stdc++.h>
#define forn(i, n) for(int i = 0; i < (int)n; ++i)
#define for1(i, n) for(int i = 1; i <= (int)n; ++i)
#define fore(i, l, r) for(int i = (int)l; i <= (int)r; ++i)
#define all(x) x.begin(), x.end()
#define el '\n'
#define d(x) cerr << #x << ' ' << x << el
#define sz(a) (int) a.size()
#define pb push_back
using namespace std;
typedef vector<int> vi;
typedef array<int, 4> a4;
const int nax = 2e5;
// const int nax = 2e3;
vi g[nax];
int sobra[nax], dist[2][nax];
int ans = 0;
void dfs(int u, int p = -1){
// d(u),d(p);
vi pasar;
int cnt = 0;
for(int v : g[u]){
if(v == p)continue;
dfs(v,u);
pasar.pb(sobra[v]);
cnt+=sobra[v];
}
if(sz(g[u]) == 1){
sobra[u] = 1;
++ans;
return;
}
sort(all(pasar), greater<int> ());
int grand = pasar[0];
int left = 0;
if(grand <= cnt - grand){
left = cnt & 1;
ans -= cnt / 2;
}else{
left = grand - (cnt - grand);
ans -= (cnt - grand);
}
sobra[u] = left;
}
int curMax = -1, mxDist = -1;
void diam(int u, int &idx, int p = -1){
for(int v : g[u]){
if(v == p)continue;
dist[idx][v] = dist[idx][u] + 1;
if(dist[idx][v] > mxDist){
mxDist = dist[idx][v];
curMax = v;
}
diam(v, idx, u);
}
}
void solve(){
int n;
cin>>n;
ans = 0;
forn(i,n) g[i].clear(), sobra[i] = 0, dist[0][i] = dist[1][i] = 0;
forn(i,n-1){
int u,v;
cin>>u>>v;
--u,--v;
g[u].pb(v);
g[v].pb(u);
}
if(n == 2){
cout<<"-1"<<el;
return;
}
int root = 0;
forn(i,n) if(sz(g[i]) > 1) root = i;
dfs(root);
curMax = 0, mxDist = 0;
int idx = 0;
diam(curMax, idx);
mxDist = 0;
// d(curMax);
diam(curMax, ++idx);
// d(curMax);
// d(mxDist);
if(mxDist <= 2){
cout<<"-1"<<el;
}else
cout<<ans<<el;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt;
cin>>tt;
while(tt--){
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3620kb
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: 5ms
memory: 3556kb
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: 7ms
memory: 3788kb
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 2 2 3 2 3 2 3 2 3 2 3 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 3 2 3 3 3 3 3 3 3 2 3 3 3 3 3 2 3 3 3 3 3 3 2 3 2 3 2 2 3 3 4 3 3 3 3 2 2 3 2 2 2 3 3 2 3 3 2 3 3 3 3 2 3 2 2 3 3 3 3 2 3 3 2 3 3 3 3 3 2 3 2 2 3 3 3 3 2 2 3 2 3 4 2 4 3 2 3 3 2 3 2 3 3 3 3 2 2 3 3 3 2 3 3 3 3 3 3 2 3 3 2 2 4 3 3 3 3 2 3 ...
result:
wrong answer 37th numbers differ - expected: '4', found: '3'