This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
bool rev;
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);
}
void passDown(ll l, ll p){
for(ll j : graph[l]) if(j!=p){
ll pass = mD[l][0];
if(mD[j][0] == pass - 1) pass = mD[l][1];
upd(j, pass+1);
passDown(j,l);
}
}
ll dfs(ll l, ll 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;
upd(l,depC);
}
if(!depthB) return 0;
return mD[l][0]+1;
}
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); root = mLoc; dist[root] = 0; mDist = 0;
depthB = true;
dfs(root, -1);
passDown(root, -1);
for(ll i = 1; i <= N; i++){
// cout << i << " " << mD[i][0] << " " << mD[i][1] << " " << mD[i][2] << " " << endl;
hard(i);
}
cout << ans << " " << ansNum << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |