QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#207185#5157. High-quality TreeMinhhoWA 4ms19180kbC++202.7kb2023-10-08 10:56:132023-10-08 10:56:14

Judging History

This is the latest submission verdict.

  • [2023-10-08 10:56:14]
  • Judged
  • Verdict: WA
  • Time: 4ms
  • Memory: 19180kb
  • [2023-10-08 10:56:13]
  • Submitted

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, int p = 0)
{
    bool f = 0;
    if (adj[u].size() < 2) f = 1;
    for (int v: adj[u]) if (v != p)
    {
        DFS(v, u);
        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
        {
            int mnu = 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), adj[v].emplace_back(u);
    DFS();
    cout<<dp[1];
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 19064kb

input:

6
1 2
1 3
3 4
3 5
5 6

output:

1

result:

ok single line: '1'

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 19180kb

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:

0

result:

wrong answer 1st lines differ - expected: '3', found: '0'