답안 #342365

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
342365 2021-01-02T01:04:54 Z cow Hard route (IZhO17_road) C++14
0 / 100
10 ms 12160 KB
#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 << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 10 ms 12040 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 9 ms 12160 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Incorrect 8 ms 12140 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 10 ms 12040 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 9 ms 12160 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Incorrect 8 ms 12140 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 10 ms 12040 KB Output is correct
3 Correct 9 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 9 ms 12160 KB Output is correct
6 Correct 9 ms 12140 KB Output is correct
7 Incorrect 8 ms 12140 KB Output isn't correct
8 Halted 0 ms 0 KB -