QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#207252#5157. High-quality TreeMinhhoWA 211ms69312kbC++203.2kb2023-10-08 12:47:162023-10-08 12:47:16

Judging History

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

  • [2023-10-08 12:47:16]
  • 评测
  • 测评结果:WA
  • 用时:211ms
  • 内存:69312kb
  • [2023-10-08 12:47:16]
  • 提交

answer

#define taskname "H"
#include <bits/stdc++.h>
#define ii pair<int,int>
#define ff first
#define ss second

using namespace std;
const int maxn = 2e5 + 10;
vector<int> adj[maxn];
set<ii> mp[maxn];
int n, m, dp[maxn], dep[maxn], lz[maxn], pa[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)
    {
        dep[v] = dep[u] + 1, pa[v] = u;
        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)
                {
                    ii ed = *(--mp[v].end());
    //                cerr<<v<<" "<<mxv<<" AND: "<<u<<" "<<mnu<<"\n";
                    dp[u]++;
                    mp[v].erase(ed);
                    ed.ss = pa[ed.ss];
                    ed.ff--;
                    mp[v].emplace(ed);
                    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)
            {
                ii ed = *(--mp[v].end());
//                cerr<<v<<" "<<mxv<<" AND: "<<u<<" "<<mnu<<"\n";
                dp[u]++;
                mp[v].erase(ed);
                ed.ss = pa[ed.ss];
                ed.ff--;
                mp[v].emplace(ed);
                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);
                ii ed = *(--mp[u].end());
                dp[u]++;
                mp[u].erase(ed);
                ed.ss = pa[ed.ss];
                ed.ff--;
                mp[u].emplace(ed);
                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].emplace(x+lz[v]-lz[u], y);
            else
            {
                for (auto [x, y]: mp[u]) mp[v].emplace(x+lz[u]-lz[v], y);
                lz[u] = lz[v];
                swap(mp[u], mp[v]);
            }
        }
    }
    if (mp[u].empty()) mp[u].emplace(0, u), 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: 0ms
memory: 18500kb

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

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: 0
Accepted
time: 146ms
memory: 64592kb

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:

199998

result:

ok single line: '199998'

Test #4:

score: 0
Accepted
time: 87ms
memory: 61400kb

input:

200000
144434 24107
75087 108465
38670 156657
31235 30143
40544 44213
51188 21788
170574 164351
14169 155909
120876 119956
196361 140453
197958 142813
23944 62568
12098 71652
162226 122184
123783 86178
70076 115586
74439 94246
83296 36713
182500 16937
174946 154091
97484 194764
179943 61793
114439 1...

output:

199998

result:

ok single line: '199998'

Test #5:

score: 0
Accepted
time: 211ms
memory: 69312kb

input:

200000
42469 8516
3910 143673
129125 150433
170053 160404
147325 66173
130784 195620
183508 43943
90940 88012
187183 803
139576 36677
190280 71191
107959 177664
14308 20402
93449 130555
80315 75413
178265 104526
4428 8875
151397 91172
181321 47276
105060 81973
196326 19584
44364 56143
187070 195424
...

output:

199998

result:

ok single line: '199998'

Test #6:

score: 0
Accepted
time: 91ms
memory: 52360kb

input:

131071
94531 87688
119005 53065
70725 126770
61026 82294
114384 270
98205 38915
61461 14652
123122 36872
37639 52311
17774 89648
79899 59785
6033 52465
15449 93250
43849 18174
2665 82543
26740 15199
71645 14339
45549 119270
22896 70677
126250 23614
5796 85715
92715 25280
119740 8911
17923 5547
47703...

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 0ms
memory: 19076kb

input:

7
1 3
3 4
1 7
7 2
7 6
3 5

output:

0

result:

ok single line: '0'

Test #8:

score: -100
Wrong Answer
time: 41ms
memory: 27884kb

input:

75026
12155 64806
40053 74785
70103 1220
72989 33966
74199 66365
52024 24358
54545 52118
52572 28566
68873 41146
10161 67848
41221 63589
72291 44013
51515 14784
12150 33009
3919 23413
61773 13741
21172 17759
27774 65766
58702 13619
11690 19263
45469 30662
33296 45184
51641 13235
11413 52734
74437 57...

output:

66848

result:

wrong answer 1st lines differ - expected: '2', found: '66848'