#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
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);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
39372 KB |
Output is correct |
2 |
Correct |
25 ms |
39344 KB |
Output is correct |
3 |
Correct |
25 ms |
39436 KB |
Output is correct |
4 |
Correct |
22 ms |
39452 KB |
Output is correct |
5 |
Correct |
23 ms |
39356 KB |
Output is correct |
6 |
Correct |
23 ms |
39436 KB |
Output is correct |
7 |
Correct |
23 ms |
39348 KB |
Output is correct |
8 |
Correct |
22 ms |
39372 KB |
Output is correct |
9 |
Correct |
22 ms |
39372 KB |
Output is correct |
10 |
Correct |
22 ms |
39452 KB |
Output is correct |
11 |
Correct |
22 ms |
39384 KB |
Output is correct |
12 |
Correct |
23 ms |
39364 KB |
Output is correct |
13 |
Correct |
26 ms |
39432 KB |
Output is correct |
14 |
Correct |
23 ms |
39396 KB |
Output is correct |
15 |
Correct |
22 ms |
39372 KB |
Output is correct |
16 |
Correct |
22 ms |
39372 KB |
Output is correct |
17 |
Correct |
22 ms |
39348 KB |
Output is correct |
18 |
Correct |
23 ms |
39364 KB |
Output is correct |
19 |
Correct |
23 ms |
39468 KB |
Output is correct |
20 |
Correct |
22 ms |
39372 KB |
Output is correct |
21 |
Correct |
21 ms |
39432 KB |
Output is correct |
22 |
Correct |
22 ms |
39372 KB |
Output is correct |
23 |
Correct |
22 ms |
39396 KB |
Output is correct |
24 |
Correct |
23 ms |
39396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
39372 KB |
Output is correct |
2 |
Correct |
25 ms |
39344 KB |
Output is correct |
3 |
Correct |
25 ms |
39436 KB |
Output is correct |
4 |
Correct |
22 ms |
39452 KB |
Output is correct |
5 |
Correct |
23 ms |
39356 KB |
Output is correct |
6 |
Correct |
23 ms |
39436 KB |
Output is correct |
7 |
Correct |
23 ms |
39348 KB |
Output is correct |
8 |
Correct |
22 ms |
39372 KB |
Output is correct |
9 |
Correct |
22 ms |
39372 KB |
Output is correct |
10 |
Correct |
22 ms |
39452 KB |
Output is correct |
11 |
Correct |
22 ms |
39384 KB |
Output is correct |
12 |
Correct |
23 ms |
39364 KB |
Output is correct |
13 |
Correct |
26 ms |
39432 KB |
Output is correct |
14 |
Correct |
23 ms |
39396 KB |
Output is correct |
15 |
Correct |
22 ms |
39372 KB |
Output is correct |
16 |
Correct |
22 ms |
39372 KB |
Output is correct |
17 |
Correct |
22 ms |
39348 KB |
Output is correct |
18 |
Correct |
23 ms |
39364 KB |
Output is correct |
19 |
Correct |
23 ms |
39468 KB |
Output is correct |
20 |
Correct |
22 ms |
39372 KB |
Output is correct |
21 |
Correct |
21 ms |
39432 KB |
Output is correct |
22 |
Correct |
22 ms |
39372 KB |
Output is correct |
23 |
Correct |
22 ms |
39396 KB |
Output is correct |
24 |
Correct |
23 ms |
39396 KB |
Output is correct |
25 |
Correct |
28 ms |
39852 KB |
Output is correct |
26 |
Correct |
25 ms |
39888 KB |
Output is correct |
27 |
Correct |
27 ms |
39832 KB |
Output is correct |
28 |
Correct |
27 ms |
39964 KB |
Output is correct |
29 |
Correct |
25 ms |
39824 KB |
Output is correct |
30 |
Correct |
29 ms |
39860 KB |
Output is correct |
31 |
Correct |
24 ms |
39848 KB |
Output is correct |
32 |
Correct |
25 ms |
39756 KB |
Output is correct |
33 |
Correct |
25 ms |
39848 KB |
Output is correct |
34 |
Correct |
25 ms |
39940 KB |
Output is correct |
35 |
Correct |
25 ms |
39936 KB |
Output is correct |
36 |
Correct |
25 ms |
39864 KB |
Output is correct |
37 |
Correct |
26 ms |
39980 KB |
Output is correct |
38 |
Correct |
25 ms |
40096 KB |
Output is correct |
39 |
Correct |
25 ms |
39744 KB |
Output is correct |
40 |
Correct |
29 ms |
39716 KB |
Output is correct |
41 |
Correct |
26 ms |
39660 KB |
Output is correct |
42 |
Correct |
26 ms |
39652 KB |
Output is correct |
43 |
Correct |
25 ms |
39628 KB |
Output is correct |
44 |
Correct |
26 ms |
39756 KB |
Output is correct |
45 |
Correct |
27 ms |
39656 KB |
Output is correct |
46 |
Correct |
32 ms |
39628 KB |
Output is correct |
47 |
Correct |
60 ms |
39588 KB |
Output is correct |
48 |
Correct |
179 ms |
39700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
39372 KB |
Output is correct |
2 |
Correct |
25 ms |
39344 KB |
Output is correct |
3 |
Correct |
25 ms |
39436 KB |
Output is correct |
4 |
Correct |
22 ms |
39452 KB |
Output is correct |
5 |
Correct |
23 ms |
39356 KB |
Output is correct |
6 |
Correct |
23 ms |
39436 KB |
Output is correct |
7 |
Correct |
23 ms |
39348 KB |
Output is correct |
8 |
Correct |
22 ms |
39372 KB |
Output is correct |
9 |
Correct |
22 ms |
39372 KB |
Output is correct |
10 |
Correct |
22 ms |
39452 KB |
Output is correct |
11 |
Correct |
22 ms |
39384 KB |
Output is correct |
12 |
Correct |
23 ms |
39364 KB |
Output is correct |
13 |
Correct |
26 ms |
39432 KB |
Output is correct |
14 |
Correct |
23 ms |
39396 KB |
Output is correct |
15 |
Correct |
22 ms |
39372 KB |
Output is correct |
16 |
Correct |
22 ms |
39372 KB |
Output is correct |
17 |
Correct |
22 ms |
39348 KB |
Output is correct |
18 |
Correct |
23 ms |
39364 KB |
Output is correct |
19 |
Correct |
23 ms |
39468 KB |
Output is correct |
20 |
Correct |
22 ms |
39372 KB |
Output is correct |
21 |
Correct |
21 ms |
39432 KB |
Output is correct |
22 |
Correct |
22 ms |
39372 KB |
Output is correct |
23 |
Correct |
22 ms |
39396 KB |
Output is correct |
24 |
Correct |
23 ms |
39396 KB |
Output is correct |
25 |
Correct |
28 ms |
39852 KB |
Output is correct |
26 |
Correct |
25 ms |
39888 KB |
Output is correct |
27 |
Correct |
27 ms |
39832 KB |
Output is correct |
28 |
Correct |
27 ms |
39964 KB |
Output is correct |
29 |
Correct |
25 ms |
39824 KB |
Output is correct |
30 |
Correct |
29 ms |
39860 KB |
Output is correct |
31 |
Correct |
24 ms |
39848 KB |
Output is correct |
32 |
Correct |
25 ms |
39756 KB |
Output is correct |
33 |
Correct |
25 ms |
39848 KB |
Output is correct |
34 |
Correct |
25 ms |
39940 KB |
Output is correct |
35 |
Correct |
25 ms |
39936 KB |
Output is correct |
36 |
Correct |
25 ms |
39864 KB |
Output is correct |
37 |
Correct |
26 ms |
39980 KB |
Output is correct |
38 |
Correct |
25 ms |
40096 KB |
Output is correct |
39 |
Correct |
25 ms |
39744 KB |
Output is correct |
40 |
Correct |
29 ms |
39716 KB |
Output is correct |
41 |
Correct |
26 ms |
39660 KB |
Output is correct |
42 |
Correct |
26 ms |
39652 KB |
Output is correct |
43 |
Correct |
25 ms |
39628 KB |
Output is correct |
44 |
Correct |
26 ms |
39756 KB |
Output is correct |
45 |
Correct |
27 ms |
39656 KB |
Output is correct |
46 |
Correct |
32 ms |
39628 KB |
Output is correct |
47 |
Correct |
60 ms |
39588 KB |
Output is correct |
48 |
Correct |
179 ms |
39700 KB |
Output is correct |
49 |
Correct |
827 ms |
84112 KB |
Output is correct |
50 |
Correct |
799 ms |
86848 KB |
Output is correct |
51 |
Correct |
806 ms |
89240 KB |
Output is correct |
52 |
Correct |
848 ms |
80952 KB |
Output is correct |
53 |
Correct |
643 ms |
88004 KB |
Output is correct |
54 |
Correct |
682 ms |
90368 KB |
Output is correct |
55 |
Correct |
665 ms |
84164 KB |
Output is correct |
56 |
Correct |
708 ms |
86984 KB |
Output is correct |
57 |
Correct |
680 ms |
92488 KB |
Output is correct |
58 |
Correct |
677 ms |
89304 KB |
Output is correct |
59 |
Correct |
667 ms |
89252 KB |
Output is correct |
60 |
Correct |
676 ms |
87804 KB |
Output is correct |
61 |
Correct |
999 ms |
112912 KB |
Output is correct |
62 |
Correct |
976 ms |
105824 KB |
Output is correct |
63 |
Correct |
984 ms |
78088 KB |
Output is correct |
64 |
Correct |
944 ms |
70888 KB |
Output is correct |
65 |
Correct |
895 ms |
66452 KB |
Output is correct |
66 |
Correct |
887 ms |
64288 KB |
Output is correct |
67 |
Correct |
874 ms |
63044 KB |
Output is correct |
68 |
Correct |
885 ms |
62584 KB |
Output is correct |
69 |
Correct |
866 ms |
62464 KB |
Output is correct |
70 |
Correct |
866 ms |
62352 KB |
Output is correct |
71 |
Correct |
917 ms |
62288 KB |
Output is correct |
72 |
Correct |
995 ms |
62288 KB |
Output is correct |
73 |
Execution timed out |
2083 ms |
62324 KB |
Time limit exceeded |
74 |
Halted |
0 ms |
0 KB |
- |