QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#207183 | #5157. High-quality Tree | Minhho | WA | 20ms | 23304kb | C++20 | 3.0kb | 2023-10-08 10:53:11 | 2023-10-08 10:53:12 |
Judging History
answer
#define taskname "H"
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
const int maxn = 2e5 + 10;
vector<int> adj[maxn];
map<int,int> mp[maxn];
int n, m, dp[maxn], lz[maxn];
void DFS(int u = 1)
{
bool f = 0;
if (adj[u].size() < 2) f = 1;
for (int v: adj[u])
{
DFS(v);
dp[u] += dp[v];
assert(!mp[v].empty());
if (mp[u].empty())
{
if (!f) swap(mp[u], mp[v]), lz[u]=lz[v];
else
{
int mnu = 0, mxv = (--mp[v].end())->ff + lz[v];
while ((mxv - mnu) > 1)
{
// cerr<<v<<" "<<mxv<<" AND: "<<u<<" "<<mnu<<"\n";
int ri = mp[v][mxv-lz[v]];
mp[v][mxv-lz[v]-1] += ri;
dp[u] += ri;
mp[v].erase(mxv-lz[v]);
mxv = (mp[v].empty() ? 0 : (--mp[v].end())->ff + lz[v]);
}
swap(mp[u], mp[v]), lz[u] = lz[v];
}
}
else
{
// if (u == 3)
// {
// cerr<<"IN: "<<lz[u]<<"\n";
// for (auto [x, y]: mp[u]) cerr<<x<<" "<<y<<"\n";
// cerr<<"OUT: "<<lz[v]<<"\n";
// for (auto [x, y]: mp[v]) cerr<<x<<" "<<y<<"\n";
// }
int mnu = f ? 0 : mp[u].begin()->ff + lz[u], mxv = (--mp[v].end())->ff + lz[v];
while ((mxv - mnu) > 1)
{
// cerr<<v<<" "<<mxv<<" AND: "<<u<<" "<<mnu<<"\n";
int ri = mp[v][mxv-lz[v]];
mp[v][mxv-lz[v]-1] += ri;
dp[u] += ri;
mp[v].erase(mxv-lz[v]);
mxv = (mp[v].empty() ? 0 : (--mp[v].end())->ff + lz[v]);
}
int mxu = (mp[u].empty() ? 0 : (--mp[u].end())->ff + lz[u]), mnv = (mp[v].empty() ? 1e9 : mp[v].begin()->ff + lz[v]);
while ((mxu - mnv) > 1)
{
assert(false);
int ri = mp[u][mxu-lz[u]];
mp[u][mxu-lz[u]-1] += ri;
dp[u] += ri;
mp[u].erase(mxu-lz[u]);
mxu = (mp[u].empty() ? 0 : (--mp[u].end())->ff + lz[u]);
}
if (mp[u].size() > mp[v].size())
for (auto [x, y]: mp[v]) mp[u][x+lz[v]-lz[u]] += y;
else
{
for (auto [x, y]: mp[u]) mp[v][x+lz[u]-lz[v]] += y;
lz[u] = lz[v];
swap(mp[u], mp[v]);
}
}
}
if (mp[u].empty()) mp[u][0] = 1, lz[u] = 0;
lz[u]++;
// cerr<<"DP: "<<u<<" "<<lz[u]<<" "<<dp[u]<<"\n";
// cerr<<"CUR: \n";
// for (auto [x, y]: mp[u]) cerr<<x<<" "<<y<<"\n";
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin>>n;
for (int i=1, u, v; i<n; i++) cin>>u>>v, adj[u].emplace_back(v);
DFS();
cout<<dp[1];
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 18944kb
input:
6 1 2 1 3 3 4 3 5 5 6
output:
1
result:
ok single line: '1'
Test #2:
score: 0
Accepted
time: 0ms
memory: 17804kb
input:
12 1 2 2 3 3 4 3 5 1 6 6 7 7 8 7 9 9 10 6 11 11 12
output:
3
result:
ok single line: '3'
Test #3:
score: -100
Wrong Answer
time: 20ms
memory: 23304kb
input:
200000 167246 158246 40931 40296 178588 27974 35171 899 4204 163250 101422 9230 55420 93371 16012 140142 28866 154497 33519 180725 50361 52348 46923 175364 126599 169575 15138 34958 164256 64770 63123 130169 154172 168301 127476 54744 199964 81879 173765 69220 178225 73653 59861 46415 138112 17507 8...
output:
0
result:
wrong answer 1st lines differ - expected: '199998', found: '0'