Submission #4353489


Source Code Expand

Copy
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

vector<int> dist(const vector<vector<int>>& g){
    const int N = g.size();
    queue<int> qu;
    qu.push(0);
    vector<int> dist(N, 100000000);
    dist[0] = 0;
    int last = 0;
    while(!qu.empty()){
        int p = qu.front(); qu.pop();
        last = p;
        for(auto t : g[p]){
            if(dist[p]+1 < dist[t]){
                dist[t] = dist[p]+1;
                qu.push(t);
            }
        }
    }
    fill(dist.begin(), dist.end(), 1000000000);
    qu.push(last);
    dist[last] = 0;
    int maxDist = 0;
    while(!qu.empty()){
        int p = qu.front(); qu.pop();
        maxDist = max(maxDist, dist[p]);
        for(auto t : g[p]){
            if(dist[p]+1 < dist[t]){
                dist[t] = dist[p]+1;
                qu.push(t);
            }
        }
    }
    vector<int> start;
    for(int i=0;i<N;i++){
        if(dist[i] == 0 || dist[i] == maxDist) start.push_back(i);
    }
    fill(dist.begin(), dist.end(), 1000000000);
    for(auto& t : start){qu.push(t); dist[t] = 0;}
    while(!qu.empty()){
        int p = qu.front(); qu.pop();
        for(auto t : g[p]){
            if(dist[p]+1 < dist[t]){
                dist[t] = dist[p]+1;
                qu.push(t);
            }
        }
    }
    vector<int> res(maxDist+1, 0);
    for(int i=0;i<N;i++) res[maxDist-dist[i]]++;
    return res;
}

int main(){
    int N;
    while(cin >> N){
        vector<vector<int>> A(N);
        for(int i=0;i<N-1;i++){
            int a, b; cin >> a >> b;
            --a; --b;
            A[a].push_back(b);
            A[b].push_back(a);
        }
        int M; cin >> M;
        vector<vector<int>> B(M);
        for(int i=0;i<M-1;i++){
            int a, b; cin >> a >> b;
            --a; --b;
            B[a].push_back(b);
            B[b].push_back(a);
        }
        vector<int> distA = dist(A);
        vector<int> distB = dist(B);
        if(distA.size() < distB.size()) swap(distA, distB);
        long long res = 0;
        int idxB = distB.size()-1;
        vector<long long> sumB(distB.size()+1, 0);
        for(int i=0;i<distB.size();i++){
            sumB[i+1] = sumB[i] + distB[i];
        }
        long long part = 0;
        for(int i=0;i<distA.size();i++){
            long long lower = distA.size() - 1;
            part += sumB.back() - sumB[idxB+1];
            while(idxB >= 0 && idxB+1+i > lower){
                part += distB[idxB] * (idxB+1+i);
                --idxB;
            }
            res += part * distA[i];
            if(idxB > 0) res += distA[i] * sumB[idxB];
        }

        // vector<long long> sumM(distB.size()+1, 0);
        // vector<long long> sumB(distB.size()+1, 0);
        // for(int i=0;i<distB.size();i++){
        //     sumM[i+1] = sumM[i] + i*distB[i];
        //     sumB[i+1] = sumB[i] + distB[i];
        // }
        // for(auto& t : distA) cout << t << " "; cout << endl;
        // for(auto& t : distB) cout << t << " "; cout << endl;
        // for(int i=0;i<distA.size();i++){
        //     int lower = distA.size()-1;
        //     int thr = min(max(0, lower-i-1), (int)distB.size());
        //     cout << thr << ":";
        //     res += distA[i] * (i+1) * (sumM.back()-sumM[thr]);
        //     cout << res << " " ;
        //     res += distA[i] * lower * sumB[thr];           
        //     cout << res << endl;
        // }
        cout << res << endl;
    }
}

Submission Info

Submission Time
Task B - Bonsai Grafting
User pes
Language C++14 (GCC 5.4.1)
Score 0
Code Size 3571 Byte
Status
Exec Time 179 ms
Memory 12664 KB

Judge Result

Set Name Score / Max Score Test Cases
Sample 0 / 0 01.txt, 02.txt
All 0 / 700 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 1 ms 256 KB
02.txt 1 ms 256 KB
11.txt 1 ms 256 KB
12.txt 1 ms 256 KB
13.txt 1 ms 256 KB
14.txt 1 ms 256 KB
15.txt 1 ms 256 KB
16.txt 1 ms 256 KB
17.txt 1 ms 256 KB
18.txt 1 ms 256 KB
19.txt 1 ms 256 KB
20.txt 1 ms 256 KB
21.txt 172 ms 11896 KB
22.txt 159 ms 12152 KB
23.txt 168 ms 11776 KB
24.txt 175 ms 12024 KB
25.txt 160 ms 12416 KB
26.txt 164 ms 12032 KB
27.txt 172 ms 12280 KB
28.txt 176 ms 11648 KB
29.txt 178 ms 11896 KB
30.txt 179 ms 12664 KB
31.txt 177 ms 11904 KB
32.txt 163 ms 12152 KB
33.txt 174 ms 11776 KB
34.txt 178 ms 12024 KB
35.txt 157 ms 12416 KB
36.txt 167 ms 12032 KB
37.txt 172 ms 12152 KB
38.txt 176 ms 11648 KB
39.txt 177 ms 11896 KB
40.txt 179 ms 12664 KB
41.txt 87 ms 6272 KB
42.txt 87 ms 6272 KB
43.txt 90 ms 6144 KB
44.txt 87 ms 6272 KB
45.txt 76 ms 6528 KB
46.txt 76 ms 6528 KB
47.txt 90 ms 6400 KB
48.txt 89 ms 6144 KB
49.txt 90 ms 6400 KB
50.txt 90 ms 6400 KB