QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#844393 | #5937. Full Binary Tree | abc12321 | 30 ✓ | 3083ms | 3912kb | C++14 | 3.6kb | 2025-01-05 20:49:58 | 2025-01-05 20:49:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ld long double
#define lld long long double
#define endl "\n"
#define fastio() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL); \
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x)
{
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x)
{
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
vector<ll> dp;
vector<ll> sz;
vector<ll> *adj;
void dfs(ll node, ll parent)
{
ll sz_node = sz[node] - 1;
vector<ll> children_sz;
vector<ll> children_dp;
for (auto x : adj[node])
{
if (x != parent)
{
dfs(x, node);
children_sz.pb(sz[x]);
children_dp.pb(dp[x]);
}
}
if (children_sz.size() == 0)
{
dp[node] = 0;
return;
}
if (children_sz.size() == 1)
{
dp[node] = children_sz[0];
return;
}
if (children_sz.size() == 2)
{
dp[node] = children_dp[0] + children_dp[1];
return;
}
vector<ll> minus;
ll sum = 0;
for (ll i = 0; i < children_dp.size(); i++)
{
minus.pb(children_dp[i] - children_sz[i]);
sum += children_sz[i];
}
sort(minus.begin(), minus.end());
dp[node] = sum + minus[0] + minus[1];
return;
}
void dfs1(ll node, ll par)
{
sz[node] = 1;
for (auto x : adj[node])
{
if (x != par)
{
dfs1(x, node);
sz[node] += sz[x];
}
}
}
void solve(ll cc)
{
ll n;
cin >> n;
adj = new vector<ll>[n];
for (ll i = 0; i < n - 1; i++)
{
ll u, v;
cin >> u >> v;
u--, v--;
adj[u].pb(v);
adj[v].pb(u);
}
ll ans = 1e9;
dp.resize(n);
sz.resize(n);
for (ll i = 0; i < n; i++)
{
fill(dp.begin(), dp.end(), 0);
fill(sz.begin(), sz.end(), 0);
dfs1(i, -1);
dfs(i, -1);
ans = min(ans, dp[i]);
}
cout << "Case #" << cc << ": ";
cout << ans << endl;
delete[] adj;
}
signed main()
{
fastio();
int t;
cin >> t;
int T = t;
for (int i = 1; i <= T; i++)
solve(i);
return 0;
}
詳細信息
Subtask #1:
score: 9
Accepted
Test #1:
score: 9
Accepted
time: 2ms
memory: 3636kb
input:
100 15 1 3 2 10 2 12 3 10 4 5 5 6 5 8 5 14 6 7 9 14 10 14 11 12 12 13 13 15 15 1 3 2 3 2 4 2 7 2 8 4 14 5 6 5 7 5 11 5 13 6 9 10 11 11 12 13 15 12 1 4 2 5 3 12 4 10 5 6 5 7 7 12 8 9 8 11 8 12 9 10 15 1 8 2 12 2 13 3 10 4 8 4 14 5 9 5 12 5 15 6 12 7 10 7 11 7 15 8 10 15 1 3 1 14 2 14 3 15 4 6 4 8 4 1...
output:
Case #1: 4 Case #2: 6 Case #3: 3 Case #4: 2 Case #5: 6 Case #6: 6 Case #7: 8 Case #8: 8 Case #9: 4 Case #10: 4 Case #11: 6 Case #12: 0 Case #13: 6 Case #14: 8 Case #15: 6 Case #16: 6 Case #17: 10 Case #18: 6 Case #19: 8 Case #20: 6 Case #21: 6 Case #22: 8 Case #23: 2 Case #24: 7 Case #25: 6 Case #26...
result:
ok 100 lines
Subtask #2:
score: 21
Accepted
Test #2:
score: 21
Accepted
time: 3083ms
memory: 3912kb
input:
100 18 1 5 1 10 1 16 2 17 3 4 3 18 4 12 4 14 6 16 7 14 8 11 8 16 9 11 12 17 13 14 13 16 15 18 29 1 15 1 16 2 12 3 11 3 21 4 6 4 28 5 27 6 8 7 17 8 25 9 19 10 15 10 20 10 26 12 20 12 23 13 28 14 23 16 22 17 20 18 19 18 28 21 28 21 29 22 27 22 28 24 27 1000 1 254 1 403 2 97 2 891 3 282 3 807 4 529 5 5...
output:
Case #1: 7 Case #2: 18 Case #3: 971 Case #4: 20 Case #5: 969 Case #6: 12 Case #7: 1 Case #8: 963 Case #9: 3 Case #10: 4 Case #11: 953 Case #12: 925 Case #13: 943 Case #14: 951 Case #15: 953 Case #16: 943 Case #17: 20 Case #18: 0 Case #19: 961 Case #20: 21 Case #21: 27 Case #22: 17 Case #23: 5 Case #...
result:
ok 100 lines
Extra Test:
score: 0
Extra Test Passed