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 < pii > v[N];
pll dis[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 calc_dis(int x, int lst, int ind){
if(dis[2 * ind + (x < lst)].X != -1) return dis[2 * ind + (x < lst)];
pll ret = {0, 1};
for(pii y : v[x])
if(y.X != lst)
ret = add(ret, calc_dis(y.X, x, y.Y));
ret.X++;
return dis[2 * ind + (x < lst)] = ret;
}
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, i}), v[y].PB({x, i});
}
pll sol = {0, 0};
for(int i = 1;i <= n;i++){
vector < int > sus;
for(pii nxt : v[i]){
pll nw = calc_dis(nxt.X, i, nxt.Y);
sus.PB(nw.X);
// printf("i = %d nxt = %d nw = %lld %lld\n", i, nxt.X, nw.X, nw.Y);
cnt[nw.X][2] += nw.Y * cnt[nw.X][1];
cnt[nw.X][1] += nw.Y;
}
sort(sus.rbegin(), sus.rend());
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]});
// printf("i = %d %lld %lld\n", i, sol.X, sol.Y);
}
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:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
46 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
road.cpp:48:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
48 | 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... |