Submission #4353546


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+1LL+i);
                --idxB;
            }
            res += part * distA[i];
            if(idxB >= 0) res += distA[i] * sumB[idxB];
        }
        cout << res << endl;
    }
}

Submission Info

Submission Time
Task B - Bonsai Grafting
User pes
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2762 Byte
Status
Exec Time 178 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 173 ms 11896 KB
22.txt 162 ms 12152 KB
23.txt 174 ms 11776 KB
24.txt 170 ms 12024 KB
25.txt 155 ms 12416 KB
26.txt 168 ms 12032 KB
27.txt 169 ms 12280 KB
28.txt 171 ms 11648 KB
29.txt 171 ms 11896 KB
30.txt 178 ms 12664 KB
31.txt 173 ms 11904 KB
32.txt 160 ms 12152 KB
33.txt 174 ms 11776 KB
34.txt 175 ms 12024 KB
35.txt 155 ms 12416 KB
36.txt 170 ms 12032 KB
37.txt 169 ms 12152 KB
38.txt 177 ms 11648 KB
39.txt 173 ms 11896 KB
40.txt 178 ms 12664 KB
41.txt 87 ms 6272 KB
42.txt 87 ms 6272 KB
43.txt 89 ms 6144 KB
44.txt 87 ms 6272 KB
45.txt 77 ms 6528 KB
46.txt 79 ms 6528 KB
47.txt 91 ms 6400 KB
48.txt 89 ms 6144 KB
49.txt 90 ms 6400 KB
50.txt 90 ms 6400 KB