#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
using namespace std;
const int N=5e5+5;
ll n;
vector < ll > v[N];
pair < ll , ll > D[N],T[N],ans;
void Dfs(int x,int p) {
for (int i=0; i<v[x].size(); i++)
if (v[x][i]==p) { swap(v[x][i],v[x][v[x].size()-1]); v[x].pop_back(); break; }
for (int i=0; i<v[x].size(); i++) {
int to=v[x][i];
Dfs(to,x);
if (D[to].F>D[x].F) D[x]=D[to];
else
if (D[to].F==D[x].F) D[x].S+=D[to].S;
}
D[x].F++;
if (!v[x].size()) D[x].S=1;
}
void Ufs(int x) {
pair < ll , ll > M1,M2;
M1.F=M1.S=M2.F=M2.S=0;
M1=D[x],M1.F--;
for (int i=0; i<v[x].size(); i++) {
int to=v[x][i];
if (M1.F==D[to].F) continue;
if (M2.F<D[to].F) M2=D[to];
else
if (M2.F==D[to].F) M2.S+=D[to].S;
}
if (x==1) T[x].S=1;
for (int i=0; i<v[x].size(); i++) {
int to=v[x][i];
if (M1.F==D[to].F && M1.S==D[to].S) T[to]=M2;
else
if (M1.F==D[to].F && M1.S>D[to].S) T[to].F=M1.F,T[to].S=M1.S-D[to].S;
else T[to]=M1;
if (T[x].F>T[to].F) T[to]=T[x];
else
if (T[x].F==T[to].F) T[to].S+=T[x].S;
T[to].F++;
Ufs(to);
}
}
void Df(int x) {
vector < pair < ll , ll > > s;
s.push_back(T[x]);
for (int i=0; i<v[x].size(); i++)
s.push_back(D[v[x][i]]);
sort(s.begin(),s.end());
reverse(s.begin(),s.end());
if (s.size()>=3 && s[2].F>0) {
ll g=s[0].F*(s[1].F+s[2].F),tot=0,sum=0,S3=0;
for (int i=2; i<s.size(); i++)
if (s[2].F==s[i].F) S3+=s[i].S;
if (s[0].F==s[1].F && s[1].F==s[2].F) {
for (int i=0; i<s.size(); i++)
if (s[0].F==s[i].F) tot+=s[i].S*sum,sum+=s[i].S;
}
else
if (s[0].F==s[1].F) {
tot=s[0].S*S3+s[1].S*S3;
}
else
if (s[1].F==s[2].F) {
for (int i=1; i<s.size(); i++)
if (s[1].F==s[i].F) tot+=s[i].S*sum,sum+=s[i].S;
}
else tot=s[1].S*S3;
if (ans.F<g) ans.F=g,ans.S=tot;
else
if (ans.F==g) ans.S+=tot;
}
for (int i=0; i<v[x].size(); i++)
Df(v[x][i]);
}
main () {
scanf("%d",&n);
for (int i=1; i<n; i++) {
int a,b;
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
Dfs(1,1);
Ufs(1);
Df(1);
if (!ans.F) ans.S=1;
printf("%lld %lld \n",ans.F,ans.S);
}
Compilation message
road.cpp: In function 'void Dfs(int, int)':
road.cpp:11:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++)
~^~~~~~~~~~~~
road.cpp:14:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++) {
~^~~~~~~~~~~~
road.cpp: In function 'void Ufs(int)':
road.cpp:31:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++) {
~^~~~~~~~~~~~
road.cpp:41:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++) {
~^~~~~~~~~~~~
road.cpp: In function 'void Df(int)':
road.cpp:59:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++)
~^~~~~~~~~~~~
road.cpp:68:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=2; i<s.size(); i++)
~^~~~~~~~~
road.cpp:72:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<s.size(); i++)
~^~~~~~~~~
road.cpp:81:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=1; i<s.size(); i++)
~^~~~~~~~~
road.cpp:91:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<v[x].size(); i++)
~^~~~~~~~~~~~
road.cpp: At global scope:
road.cpp:95:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
road.cpp: In function 'int main()':
road.cpp:96:15: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
scanf("%d",&n);
~~^
road.cpp:96:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
road.cpp:100:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&a,&b);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
12 ms |
12024 KB |
Output is correct |
4 |
Correct |
12 ms |
12024 KB |
Output is correct |
5 |
Correct |
13 ms |
12024 KB |
Output is correct |
6 |
Correct |
13 ms |
12156 KB |
Output is correct |
7 |
Correct |
13 ms |
12152 KB |
Output is correct |
8 |
Correct |
13 ms |
12024 KB |
Output is correct |
9 |
Correct |
13 ms |
12152 KB |
Output is correct |
10 |
Correct |
12 ms |
12028 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12024 KB |
Output is correct |
14 |
Correct |
13 ms |
12024 KB |
Output is correct |
15 |
Correct |
13 ms |
12152 KB |
Output is correct |
16 |
Correct |
13 ms |
12024 KB |
Output is correct |
17 |
Correct |
13 ms |
12152 KB |
Output is correct |
18 |
Correct |
12 ms |
12024 KB |
Output is correct |
19 |
Correct |
13 ms |
12024 KB |
Output is correct |
20 |
Correct |
12 ms |
12024 KB |
Output is correct |
21 |
Correct |
12 ms |
12024 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
12 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
12 ms |
12024 KB |
Output is correct |
4 |
Correct |
12 ms |
12024 KB |
Output is correct |
5 |
Correct |
13 ms |
12024 KB |
Output is correct |
6 |
Correct |
13 ms |
12156 KB |
Output is correct |
7 |
Correct |
13 ms |
12152 KB |
Output is correct |
8 |
Correct |
13 ms |
12024 KB |
Output is correct |
9 |
Correct |
13 ms |
12152 KB |
Output is correct |
10 |
Correct |
12 ms |
12028 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12024 KB |
Output is correct |
14 |
Correct |
13 ms |
12024 KB |
Output is correct |
15 |
Correct |
13 ms |
12152 KB |
Output is correct |
16 |
Correct |
13 ms |
12024 KB |
Output is correct |
17 |
Correct |
13 ms |
12152 KB |
Output is correct |
18 |
Correct |
12 ms |
12024 KB |
Output is correct |
19 |
Correct |
13 ms |
12024 KB |
Output is correct |
20 |
Correct |
12 ms |
12024 KB |
Output is correct |
21 |
Correct |
12 ms |
12024 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
12 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12024 KB |
Output is correct |
25 |
Correct |
16 ms |
12664 KB |
Output is correct |
26 |
Correct |
16 ms |
12792 KB |
Output is correct |
27 |
Correct |
16 ms |
12792 KB |
Output is correct |
28 |
Correct |
16 ms |
12792 KB |
Output is correct |
29 |
Correct |
16 ms |
12712 KB |
Output is correct |
30 |
Correct |
17 ms |
12792 KB |
Output is correct |
31 |
Correct |
16 ms |
12792 KB |
Output is correct |
32 |
Correct |
16 ms |
12792 KB |
Output is correct |
33 |
Correct |
16 ms |
12664 KB |
Output is correct |
34 |
Correct |
16 ms |
12792 KB |
Output is correct |
35 |
Correct |
16 ms |
12792 KB |
Output is correct |
36 |
Correct |
16 ms |
12792 KB |
Output is correct |
37 |
Correct |
17 ms |
12792 KB |
Output is correct |
38 |
Correct |
17 ms |
13048 KB |
Output is correct |
39 |
Correct |
17 ms |
12664 KB |
Output is correct |
40 |
Correct |
16 ms |
12536 KB |
Output is correct |
41 |
Correct |
16 ms |
12408 KB |
Output is correct |
42 |
Correct |
16 ms |
12480 KB |
Output is correct |
43 |
Correct |
16 ms |
12408 KB |
Output is correct |
44 |
Correct |
15 ms |
12408 KB |
Output is correct |
45 |
Correct |
15 ms |
12408 KB |
Output is correct |
46 |
Correct |
15 ms |
12408 KB |
Output is correct |
47 |
Correct |
16 ms |
12536 KB |
Output is correct |
48 |
Correct |
16 ms |
12664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
12 ms |
12024 KB |
Output is correct |
4 |
Correct |
12 ms |
12024 KB |
Output is correct |
5 |
Correct |
13 ms |
12024 KB |
Output is correct |
6 |
Correct |
13 ms |
12156 KB |
Output is correct |
7 |
Correct |
13 ms |
12152 KB |
Output is correct |
8 |
Correct |
13 ms |
12024 KB |
Output is correct |
9 |
Correct |
13 ms |
12152 KB |
Output is correct |
10 |
Correct |
12 ms |
12028 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12024 KB |
Output is correct |
14 |
Correct |
13 ms |
12024 KB |
Output is correct |
15 |
Correct |
13 ms |
12152 KB |
Output is correct |
16 |
Correct |
13 ms |
12024 KB |
Output is correct |
17 |
Correct |
13 ms |
12152 KB |
Output is correct |
18 |
Correct |
12 ms |
12024 KB |
Output is correct |
19 |
Correct |
13 ms |
12024 KB |
Output is correct |
20 |
Correct |
12 ms |
12024 KB |
Output is correct |
21 |
Correct |
12 ms |
12024 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
12 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12024 KB |
Output is correct |
25 |
Correct |
16 ms |
12664 KB |
Output is correct |
26 |
Correct |
16 ms |
12792 KB |
Output is correct |
27 |
Correct |
16 ms |
12792 KB |
Output is correct |
28 |
Correct |
16 ms |
12792 KB |
Output is correct |
29 |
Correct |
16 ms |
12712 KB |
Output is correct |
30 |
Correct |
17 ms |
12792 KB |
Output is correct |
31 |
Correct |
16 ms |
12792 KB |
Output is correct |
32 |
Correct |
16 ms |
12792 KB |
Output is correct |
33 |
Correct |
16 ms |
12664 KB |
Output is correct |
34 |
Correct |
16 ms |
12792 KB |
Output is correct |
35 |
Correct |
16 ms |
12792 KB |
Output is correct |
36 |
Correct |
16 ms |
12792 KB |
Output is correct |
37 |
Correct |
17 ms |
12792 KB |
Output is correct |
38 |
Correct |
17 ms |
13048 KB |
Output is correct |
39 |
Correct |
17 ms |
12664 KB |
Output is correct |
40 |
Correct |
16 ms |
12536 KB |
Output is correct |
41 |
Correct |
16 ms |
12408 KB |
Output is correct |
42 |
Correct |
16 ms |
12480 KB |
Output is correct |
43 |
Correct |
16 ms |
12408 KB |
Output is correct |
44 |
Correct |
15 ms |
12408 KB |
Output is correct |
45 |
Correct |
15 ms |
12408 KB |
Output is correct |
46 |
Correct |
15 ms |
12408 KB |
Output is correct |
47 |
Correct |
16 ms |
12536 KB |
Output is correct |
48 |
Correct |
16 ms |
12664 KB |
Output is correct |
49 |
Correct |
756 ms |
75288 KB |
Output is correct |
50 |
Correct |
769 ms |
79564 KB |
Output is correct |
51 |
Correct |
751 ms |
83364 KB |
Output is correct |
52 |
Correct |
743 ms |
70220 KB |
Output is correct |
53 |
Correct |
784 ms |
86364 KB |
Output is correct |
54 |
Correct |
779 ms |
91516 KB |
Output is correct |
55 |
Correct |
771 ms |
78720 KB |
Output is correct |
56 |
Correct |
779 ms |
84348 KB |
Output is correct |
57 |
Correct |
728 ms |
84728 KB |
Output is correct |
58 |
Correct |
709 ms |
80120 KB |
Output is correct |
59 |
Correct |
762 ms |
79772 KB |
Output is correct |
60 |
Correct |
720 ms |
77440 KB |
Output is correct |
61 |
Correct |
1031 ms |
112860 KB |
Output is correct |
62 |
Correct |
1195 ms |
101732 KB |
Output is correct |
63 |
Correct |
1012 ms |
68448 KB |
Output is correct |
64 |
Correct |
978 ms |
60732 KB |
Output is correct |
65 |
Correct |
960 ms |
55772 KB |
Output is correct |
66 |
Correct |
933 ms |
53240 KB |
Output is correct |
67 |
Correct |
949 ms |
51448 KB |
Output is correct |
68 |
Correct |
947 ms |
51104 KB |
Output is correct |
69 |
Correct |
928 ms |
50808 KB |
Output is correct |
70 |
Correct |
935 ms |
50680 KB |
Output is correct |
71 |
Correct |
913 ms |
50424 KB |
Output is correct |
72 |
Correct |
978 ms |
50680 KB |
Output is correct |
73 |
Correct |
932 ms |
51056 KB |
Output is correct |
74 |
Correct |
916 ms |
51488 KB |
Output is correct |
75 |
Correct |
891 ms |
52268 KB |
Output is correct |
76 |
Correct |
850 ms |
53480 KB |
Output is correct |
77 |
Correct |
726 ms |
56544 KB |
Output is correct |
78 |
Correct |
520 ms |
62880 KB |
Output is correct |