QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#207208#5157. High-quality TreeMinhhoWA 73ms46584kbC++203.0kb2023-10-08 11:12:332023-10-08 11:12:33

Judging History

你现在查看的是最新测评结果

  • [2023-10-08 11:12:33]
  • 评测
  • 测评结果:WA
  • 用时:73ms
  • 内存:46584kb
  • [2023-10-08 11:12:33]
  • 提交

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 = ((u == 1 && adj[u].size() == 1) || (u != 1 && adj[u].size() == 2));
    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];
//                if (u == 1)
//                    for (auto [x, y]: mp[u]) cerr<<"WWWWW: "<<x<<" "<<y<<"\n";
            }
            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 - ((mxv - 1) == 1);
                    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<<"WUT: "<<lz[u]<<" "<<mp[u].begin()->ff<<"\n";
//                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];
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 18280kb

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: 18436kb

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: 73ms
memory: 46584kb

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:

784465

result:

wrong answer 1st lines differ - expected: '199998', found: '784465'