QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#111240 | #6307. Chase Game 2 | xaphoenix# | WA | 4ms | 6040kb | C++14 | 2.1kb | 2023-06-06 14:26:08 | 2023-06-06 14:26:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define LC k<<1
#define RC k<<1|1
#define IO cin.sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define rep(i,a,n) for (int i = a; i < n; i++)
#define repn(i,a,n) for (int i = a; i <= n; i++)
#define per(i,a,n) for (int i = (n) - 1; i >= a; i--)
#define pern(i,a,n) for (int i = n; i >= a; i--)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<LL, int> PLI;
typedef pair<double, double> PDD;
typedef pair<ull, ull> PUU;
typedef pair<LL, LL> PLL;
const int N = 110000;
const int M = 1100000;
const int mod = 1e9+7;
const int inf = (int)1e9;
const LL INF = 1e18;
const double eps = 1e-9;
mt19937_64 Rand((unsigned long long)new char);
#define rand Rand
int T, n, sz[N], dp[N], rt, du[N], mx, ans;
PII v[N];
vector<int> g[N];
void dfs(int x, int fa) {
if (du[x] == 1) sz[x] = 1;
else sz[x] = 0;
v[x] = mp(0, 0);
int num = 0;
for (auto y: g[x]) {
if (y != fa) {
dfs(y, x), sz[x] += sz[y];
if (du[y] == 1) num++;
else v[x] = max(v[x], mp(sz[y], y));
}
}
if (num) v[x] = max(v[x], mp(num, x));
}
int work(int x, int fa, int delta) {
if (v[x].fi * 2 <= sz[x] + delta * 2) {
return (sz[x] - delta) / 2 + ((sz[x] - delta) % 2);
}
if (v[x].se == x) return max(v[x].fi - delta, 0);
return work(v[x].se, x, delta + sz[x] - v[x].fi);
}
int main() {
IO;
cin >> T;
while (T--) {
cin >> n;
repn(i, 1, n) g[i].clear(), du[i] = 0;
rep(i, 1, n) {
int x, y;
cin >> x >> y;
g[x].pb(y), g[y].pb(x);
du[x]++, du[y]++;
}
if (n <= 3) {
cout << "-1\n";
continue;
}
mx = 0;
repn(i, 1, n) {
if (du[i] > 1) rt = i;
mx = max(mx, du[i]);
}
if (mx == n - 1) {
cout << "-1\n";
continue;
}
ans = 0;
dfs(rt, 0);
cout << work(rt, 0, 0) << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 6040kb
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: -100
Wrong Answer
time: 3ms
memory: 6004kb
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 1 1 2 1 1 -1 2 2 1 1 1 1 1 1 -1 1 1 2 1 -1 1 1 2 1 1 -1 1 1 1 1 1 -1 1 1 2 1 1 1 1 1 -1 1 1 1 1 1 1 2 1 1 1 -1 -1 -1 2 2 2 1 1 1 1 2 1 -1 1 1 -1 1 1 -1 2 -1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 -1 1 1 1 -1 1 1 1 1 -1 1 -1 1 -1 -1 2 -1 1 1 2 1 1 1 1 1 1 1 1 1 1 -1 1 1 1 1 1 1 1 1 1 1 1 -1...
result:
wrong answer 9th numbers differ - expected: '2', found: '1'