//
// 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][2];
ll mLoc, mDist, root;
ll dist[mn];
ll pa[mn];
ll ans, ansNum;
bool depthB;
ll dfs(ll l, ll p){
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);
if(!depthB) continue;
if(depC >= mD[l][0]){
mD[l][1] = mD[l][0];
mD[l][0] = depC;
} else mD[l][1] = max(mD[l][1], depC);
}
return depthB ? mD[l][0]+1 : 0;
}
ll dop=1, dPath;
void check(ll H){
if(H > ans) {ans = H; ansNum = 0;}
if(H == ans) {ansNum++;}
}
void retrace(ll loc){
if(dop == dPath) return;
if(graph[loc].size() < 3){
if(ans == 0) ansNum = 1;
dop++; retrace(pa[loc]);
return;
}
ll dist = mD[loc][0];
if(dist == dop) dist = mD[loc][1];
ll H = dPath * dist; check(H);
H = (dop+dist) * (dPath-dop); check(H);
H = (dist + dPath-dop) * dop; check(H);
dop++;
retrace(pa[loc]);
}
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); root = mLoc; dist[root] = 0; mDist = 0;
depthB = true;
dPath = dfs(root, -1)-1;
retrace(pa[mLoc]);
cout << ans << " " << ansNum;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
9 ms |
12140 KB |
Output is correct |
3 |
Correct |
8 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
8 ms |
12140 KB |
Output is correct |
6 |
Incorrect |
9 ms |
12140 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
9 ms |
12140 KB |
Output is correct |
3 |
Correct |
8 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
8 ms |
12140 KB |
Output is correct |
6 |
Incorrect |
9 ms |
12140 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
9 ms |
12140 KB |
Output is correct |
3 |
Correct |
8 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
8 ms |
12140 KB |
Output is correct |
6 |
Incorrect |
9 ms |
12140 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |