Submission #342363

# Submission time Handle Problem Language Result Execution time Memory
342363 2021-01-02T01:03:11 Z cow Hard route (IZhO17_road) C++14
0 / 100
8 ms 12140 KB
//
//  main.cpp
//  Hard Paths
//
//  Created by Isaac Noel on 1/1/21.
//

#include <iostream>
#include <vector>
#define ll long long
using namespace std;
#define mn (ll) 5e5+5

ll N;
vector<ll> graph[mn];
ll mD[mn][3];
ll mLoc, mDist, root;
ll dist[mn];
ll pa[mn];

ll ans, ansNum = 1;
bool depthB;

void upd(ll l, ll v){
    if(v >= mD[l][0]){
        mD[l][2] = mD[l][1];
        mD[l][1] = mD[l][0];
        mD[l][0] = v;
    } else if (v >= mD[l][1]){
        mD[l][2] = mD[l][1];
        mD[l][1] = v;
    } else mD[l][2] = max(mD[l][2], v);
}

ll dfs(ll l, ll p, ll d){
    pa[l] = p;
    for(ll j: graph[l]) if(j!=p) {
        dist[j] = dist[l]+1;
        if(dist[j] > mDist){
            mDist = dist[j];
            mLoc = j;
        }
        ll depC = dfs(j, l,d+1);
        if(!depthB) continue;
        upd(l,depC);
    }
    int ret = mD[l][0]+1;
    if(depthB) upd(l,d);
    return depthB ? ret: 0;
}

void check(ll H){
    if(H > ans) {ans = H; ansNum = 0;}
    if(H == ans) {ansNum++;}
}
void hard(ll l){
    if(graph[l].size() < 3){
        if(ans==0) ansNum = 1;
        return;
    }
    ll H = (mD[l][1] + mD[l][0])*mD[l][2]; check(H);
    H = mD[l][1] * (mD[l][0] + mD[l][2]); check(H);
    H = mD[l][0] * (mD[l][1] + mD[l][2]); check(H);
}
int main() {
    cin >> N;
    for(ll i = 0; i < N-1; i++){
        ll a, b; cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    dfs(1,-1,0); root = mLoc; dist[root] = 0; mDist = 0;
    depthB = true;
    dfs(root, -1,0);
    for(int i = 1; i <= N; i++){
        cout << mD[i][0] << " " << mD[i][1] << " " << mD[i][2] << " " << pa[i] << endl;
        hard(i);
    }
    cout << ans << " " << ansNum;
}
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 12140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 12140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 12140 KB Output isn't correct
2 Halted 0 ms 0 KB -