Submission #4353019


Source Code Expand

Copy
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

int solve1(const vector<vector<int> >& edges, int curr, int prev, vector<int>& ans)
{
    for(int next : edges[curr]){
        if(next == prev)
            continue;
        int len = solve1(edges, next, curr, ans);
        ans[curr] = max(ans[curr], len + 1);
    }
    return ans[curr];
}

void solve2(const vector<vector<int> >& edges, int curr, int prev, int len, vector<int>& ans)
{
    ans[curr] = max(ans[curr], len + 1);

    multiset<int> ms;
    for(int next : edges[curr]){
        if(next == prev)
            continue;
        ms.insert(ans[next]);
    }

    for(int next : edges[curr]){
        if(next == prev)
            continue;
        int tmp = ans[next];
        ms.erase(ms.find(tmp));
        int len2 = len + 1;
        if(!ms.empty())
            len2 = max(len2, *ms.rbegin() + 1);
        solve2(edges, next, curr, len2, ans);
        ms.insert(tmp);
    }
}

int main()
{
    vector<int> n(2);
    vector<vector<vector<int> > > edges(2);
    vector<vector<int> > len(2);
    for(int i=0; i<2; ++i){
        cin >> n[i];
        edges[i].assign(n[i], vector<int>());
        for(int j=0; j<n[i]-1; ++j){
            int p, q;
            cin >> p >> q;
            -- p;
            -- q;
            edges[i][p].push_back(q);
            edges[i][q].push_back(p);
        }
        len[i].assign(n[i], 0);
        solve1(edges[i], 0, -1, len[i]);
        solve2(edges[i], 0, -1, -1, len[i]);
        sort(len[i].begin(), len[i].end());
    }

    int maxLen = max(*max_element(len[0].begin(), len[0].end()),
                     *max_element(len[1].begin(), len[1].end()));
    long long ans = 0;
    long long sum = accumulate(len[1].begin(), len[1].end(), 0LL);
    int j = 0;
    for(int i=n[0]-1; i>=0; --i){
        while(j < n[1] && len[0][i] + len[1][j] < maxLen){
            sum -= len[1][j];
            ++ j;
        }
        ans += sum + (len[0][i] + 1) * (long long)(n[1] - j) + maxLen * (long long)j;
    }
    cout << ans << endl;

    return 0;
}

Submission Info

Submission Time
Task B - Bonsai Grafting
User mamekin
Language C++14 (GCC 5.4.1)
Score 700
Code Size 2560 Byte
Status AC
Exec Time 207 ms
Memory 23808 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 2
AC × 42
Set Name Test Cases
Sample 01.txt, 02.txt
All 01.txt, 02.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt
Case Name Status Exec Time Memory
01.txt AC 1 ms 256 KB
02.txt AC 1 ms 256 KB
11.txt AC 1 ms 256 KB
12.txt AC 1 ms 256 KB
13.txt AC 1 ms 256 KB
14.txt AC 1 ms 256 KB
15.txt AC 1 ms 256 KB
16.txt AC 1 ms 256 KB
17.txt AC 1 ms 256 KB
18.txt AC 1 ms 256 KB
19.txt AC 1 ms 256 KB
20.txt AC 1 ms 256 KB
21.txt AC 191 ms 12160 KB
22.txt AC 197 ms 13952 KB
23.txt AC 191 ms 12160 KB
24.txt AC 196 ms 21376 KB
25.txt AC 195 ms 14464 KB
26.txt AC 191 ms 12416 KB
27.txt AC 202 ms 21376 KB
28.txt AC 195 ms 12032 KB
29.txt AC 204 ms 22640 KB
30.txt AC 207 ms 23424 KB
31.txt AC 194 ms 12160 KB
32.txt AC 194 ms 14080 KB
33.txt AC 193 ms 12160 KB
34.txt AC 203 ms 23808 KB
35.txt AC 195 ms 14080 KB
36.txt AC 194 ms 12416 KB
37.txt AC 200 ms 18688 KB
38.txt AC 193 ms 12032 KB
39.txt AC 202 ms 20224 KB
40.txt AC 203 ms 20864 KB
41.txt AC 94 ms 6272 KB
42.txt AC 96 ms 6272 KB
43.txt AC 98 ms 6144 KB
44.txt AC 97 ms 6272 KB
45.txt AC 99 ms 7808 KB
46.txt AC 98 ms 7552 KB
47.txt AC 105 ms 17408 KB
48.txt AC 97 ms 6144 KB
49.txt AC 102 ms 12544 KB
50.txt AC 99 ms 12544 KB