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 <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#define X first
#define Y second
#define PB push_back
using namespace std;
typedef long long ll;
typedef pair < int , int > pii;
typedef pair < ll , ll > pll;
const int N = 1e6 + 500;
int n;
vector < int > v[N];
pll dis[N], gor[N];
int dep[N];
ll cnt[N][3];
inline pll add(const pll &A, const pll &B){
return {(A.X > B.X ? A.X : B.X), A.Y * (A.X >= B.X) + B.Y * (B.X >= A.X)};
}
pll dolje(int x, int lst){
dep[x] = dep[lst] + 1;
dis[x] = {0, 1};
for(int y : v[x]){
if(y == lst) continue;
pll nxt = dolje(y, x);
nxt.X++; dis[x] = add(dis[x], nxt);
}
pll cur = {1, 1};
for(int y : v[x]){
if(y == lst) continue;
gor[y] = add(gor[y], cur);
pll tmp = dis[y]; tmp.X += 2;
cur = add(cur, tmp);
}
reverse(v[x].begin(), v[x].end());
cur = {0, 0};
for(int y : v[x]){
if(y == lst) continue;
gor[y] = add(gor[y], cur);
pll tmp = dis[y]; tmp.X += 2;
cur = add(cur, tmp);
}
reverse(v[x].begin(), v[x].end());
return dis[x];
}
void gore(int x, int lst){
for(int y : v[x]){
if(y == lst) continue;
gor[y] = add(gor[y], {gor[x].X + 1, gor[x].Y});
gore(y, x);
}
}
pll calc_dis(int x, int y){
if(dep[x] > dep[y])
return {dis[x].X + 1, dis[x].Y};
return {gor[y].X, gor[y].Y};
}
ll ch(int n, int k){
int ret = 1;
for(int i = 0;i < k;i++)
ret *= (n - i), ret /= i + 1;
return ret;
}
int main(){
for(int i = 0;i < N;i++) dis[i].X = -1;
scanf("%d", &n);
for(int i = 1;i < n;i++){
int x, y; scanf("%d%d", &x, &y);
v[x].PB(y), v[y].PB(x);
}
dolje(1, 1); gore(1, 1);
pll sol = {0, 0};
for(int i = 1;i <= n;i++){
vector < int > sus;
for(int nxt : v[i]){
pll nw = calc_dis(nxt, i);
sus.PB(nw.X);
cnt[nw.X][2] += nw.Y * cnt[nw.X][1];
cnt[nw.X][1] += nw.Y;
}
for(int j = 0;j < 3;j++)
for(int i = (int)sus.size() - 1; i ;i--)
if(sus[i - 1] < sus[i]) swap(sus[i], sus[i - 1]);
if((int)sus.size() > 2){
if(sus[0] > sus[1] && sus[1] > sus[2])
sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[1]][1] * cnt[sus[2]][1]});
else if(sus[0] == sus[1] && sus[1] == sus[2])
sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[0]][2]});
else if(sus[0] > sus[1])
sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[1]][2]});
else
sol = add(sol, {(ll)(sus[1] + sus[2]) * sus[0], cnt[sus[1]][2] & cnt[sus[2]][1]});
}
for(int x : sus){
cnt[x][1] = 0, cnt[x][2] = 0;
}
}
if(!sol.X) sol.Y++;
printf("%lld %lld\n", sol.X, sol.Y);
return 0;
}
Compilation message (stderr)
road.cpp: In function 'int main()':
road.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
78 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
road.cpp:80:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
80 | int x, y; scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |