#include <bits/stdc++.h>
#define MAXN 500007
using namespace std;
vector<int> g[MAXN];
int b,mb,u,v,in[MAXN],out[MAXN],c1,c2,n,p[MAXN];
long long pot,cnt,d[MAXN],k[MAXN],ds[2][MAXN];
void dfsD(int s,int f,int di)
{
d[s]=di;
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfsD(g[s][i],s,di+1);
}
void dfsd(int s,int f,int ka,long long di)
{
ds[ka][s]=di;
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfsd(g[s][i],s,ka,di+1);
}
int dfsp(int s,int f)
{
int mx=0;
in[c1++]=s;
p[s]=f;
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) mx=max(mx,dfsp(g[s][i],s));
d[s]=mx+1;
out[c2++]=s;
return d[s];
}
void calc(int s,int f,long long mx,long long du)
{
if(s!=f && g[s].size()==1)
{
if(s!=mb && du*mx==pot) cnt++;
if(s!=mb && du*mx>pot) {pot=du*mx; cnt=1;}
return;
}
int mxi=0;
long long mx1=0,mx2=0;
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(d[g[s][i]]>mx1) {mx1=d[g[s][i]]; mxi=i;}
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(i!=mxi) mx2=max(mx2,(long long)d[g[s][i]]);
calc(g[s][mxi],s,max(mx,mx2),du+1);
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && i!=mxi) calc(g[s][i],s,max(mx,mx1),du+1);
}
inline void dfs(int ka)
{
for(int i=0;i<n;i++)
{
int s=out[i],f=p[s];
long long du=ds[ka][s];
if(s==v || s==u) {d[s]=-MAXN; continue;}
if(g[s].size()==1) {k[s]=1; d[s]=1; continue;}
int mxi=0;
long long mx1=0,mx2=0;
k[s]=0;
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(d[g[s][i]]>mx1) {mx1=d[g[s][i]]; mxi=i;}
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(d[g[s][i]]==mx1) k[s]+=k[g[s][i]];
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(i!=mxi) mx2=max(mx2,(long long)d[g[s][i]]);
d[s]=mx1+1;
long long tc=0;
if(mx1==mx2)
{
tc=k[s]*k[s];
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(d[g[s][i]]==mx1) tc-=k[g[s][i]]*k[g[s][i]];
tc/=2;
}
else
{
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(d[g[s][i]]==mx2) tc+=k[g[s][i]];
tc*=k[s];
}
if(mx2==0 || (ka==1 && ds[0][s]==ds[1][s])) continue;
if((mx1+mx2)*du>pot) {pot=(mx1+mx2)*du; cnt=0;}
if((mx1+mx2)*du==pot) cnt+=tc;
}
}
int main()
{
u=1; v=1;
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
int t1,t2;
scanf("%d%d",&t1,&t2);
g[t1].push_back(t2);
g[t2].push_back(t1);
}
dfsD(1,1,0);
for(int i=1;i<=n;i++) if(d[i]>d[u]) u=i;
dfsD(u,u,0);
for(int i=1;i<=n;i++) if(d[i]>d[v]) v=i;
dfsd(u,u,0,0);
dfsd(v,v,1,0);
dfsp(u,u);
calc(u,u,0,0);
dfs(0);
mb=u; c1=0; c2=0;
dfsp(v,v);
calc(v,v,0,0);
dfs(1);
printf("%lld %lld",pot,cnt);
}
Compilation message
road.cpp: In function 'void dfsD(int, int, int)':
road.cpp:10:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfsD(g[s][i],s,di+1);
~^~~~~~~~~~~~
road.cpp: In function 'void dfsd(int, int, int, long long int)':
road.cpp:15:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) dfsd(g[s][i],s,ka,di+1);
~^~~~~~~~~~~~
road.cpp: In function 'int dfsp(int, int)':
road.cpp:22:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) mx=max(mx,dfsp(g[s][i],s));
~^~~~~~~~~~~~
road.cpp: In function 'void calc(int, int, long long int, long long int)':
road.cpp:37:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(d[g[s][i]]>mx1) {mx1=d[g[s][i]]; mxi=i;}
~^~~~~~~~~~~~
road.cpp:38:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(i!=mxi) mx2=max(mx2,(long long)d[g[s][i]]);
~^~~~~~~~~~~~
road.cpp:40:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && i!=mxi) calc(g[s][i],s,max(mx,mx1),du+1);
~^~~~~~~~~~~~
road.cpp: In function 'void dfs(int)':
road.cpp:53:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(d[g[s][i]]>mx1) {mx1=d[g[s][i]]; mxi=i;}
~^~~~~~~~~~~~
road.cpp:54:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(d[g[s][i]]==mx1) k[s]+=k[g[s][i]];
~^~~~~~~~~~~~
road.cpp:55:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(i!=mxi) mx2=max(mx2,(long long)d[g[s][i]]);
~^~~~~~~~~~~~
road.cpp:61:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(d[g[s][i]]==mx1) tc-=k[g[s][i]]*k[g[s][i]];
~^~~~~~~~~~~~
road.cpp:66:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) if(d[g[s][i]]==mx2) tc+=k[g[s][i]];
~^~~~~~~~~~~~
road.cpp: In function 'int main()':
road.cpp:77:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
road.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&t1,&t2);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
14 ms |
12244 KB |
Output is correct |
3 |
Correct |
13 ms |
12252 KB |
Output is correct |
4 |
Correct |
12 ms |
12252 KB |
Output is correct |
5 |
Correct |
18 ms |
12252 KB |
Output is correct |
6 |
Correct |
12 ms |
12252 KB |
Output is correct |
7 |
Correct |
13 ms |
12280 KB |
Output is correct |
8 |
Correct |
12 ms |
12280 KB |
Output is correct |
9 |
Correct |
12 ms |
12292 KB |
Output is correct |
10 |
Correct |
12 ms |
12292 KB |
Output is correct |
11 |
Correct |
13 ms |
12368 KB |
Output is correct |
12 |
Correct |
13 ms |
12384 KB |
Output is correct |
13 |
Correct |
13 ms |
12384 KB |
Output is correct |
14 |
Correct |
12 ms |
12384 KB |
Output is correct |
15 |
Correct |
12 ms |
12384 KB |
Output is correct |
16 |
Correct |
12 ms |
12384 KB |
Output is correct |
17 |
Correct |
12 ms |
12384 KB |
Output is correct |
18 |
Correct |
12 ms |
12400 KB |
Output is correct |
19 |
Correct |
17 ms |
12412 KB |
Output is correct |
20 |
Correct |
12 ms |
12412 KB |
Output is correct |
21 |
Correct |
12 ms |
12412 KB |
Output is correct |
22 |
Correct |
13 ms |
12412 KB |
Output is correct |
23 |
Correct |
14 ms |
12412 KB |
Output is correct |
24 |
Correct |
12 ms |
12412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
14 ms |
12244 KB |
Output is correct |
3 |
Correct |
13 ms |
12252 KB |
Output is correct |
4 |
Correct |
12 ms |
12252 KB |
Output is correct |
5 |
Correct |
18 ms |
12252 KB |
Output is correct |
6 |
Correct |
12 ms |
12252 KB |
Output is correct |
7 |
Correct |
13 ms |
12280 KB |
Output is correct |
8 |
Correct |
12 ms |
12280 KB |
Output is correct |
9 |
Correct |
12 ms |
12292 KB |
Output is correct |
10 |
Correct |
12 ms |
12292 KB |
Output is correct |
11 |
Correct |
13 ms |
12368 KB |
Output is correct |
12 |
Correct |
13 ms |
12384 KB |
Output is correct |
13 |
Correct |
13 ms |
12384 KB |
Output is correct |
14 |
Correct |
12 ms |
12384 KB |
Output is correct |
15 |
Correct |
12 ms |
12384 KB |
Output is correct |
16 |
Correct |
12 ms |
12384 KB |
Output is correct |
17 |
Correct |
12 ms |
12384 KB |
Output is correct |
18 |
Correct |
12 ms |
12400 KB |
Output is correct |
19 |
Correct |
17 ms |
12412 KB |
Output is correct |
20 |
Correct |
12 ms |
12412 KB |
Output is correct |
21 |
Correct |
12 ms |
12412 KB |
Output is correct |
22 |
Correct |
13 ms |
12412 KB |
Output is correct |
23 |
Correct |
14 ms |
12412 KB |
Output is correct |
24 |
Correct |
12 ms |
12412 KB |
Output is correct |
25 |
Correct |
16 ms |
12924 KB |
Output is correct |
26 |
Correct |
16 ms |
12948 KB |
Output is correct |
27 |
Correct |
16 ms |
12948 KB |
Output is correct |
28 |
Correct |
16 ms |
12948 KB |
Output is correct |
29 |
Correct |
16 ms |
12948 KB |
Output is correct |
30 |
Correct |
17 ms |
13076 KB |
Output is correct |
31 |
Correct |
15 ms |
13076 KB |
Output is correct |
32 |
Correct |
16 ms |
13076 KB |
Output is correct |
33 |
Correct |
16 ms |
13076 KB |
Output is correct |
34 |
Correct |
17 ms |
13076 KB |
Output is correct |
35 |
Correct |
17 ms |
13076 KB |
Output is correct |
36 |
Correct |
17 ms |
13076 KB |
Output is correct |
37 |
Correct |
18 ms |
13148 KB |
Output is correct |
38 |
Correct |
17 ms |
13180 KB |
Output is correct |
39 |
Correct |
19 ms |
13180 KB |
Output is correct |
40 |
Correct |
18 ms |
13180 KB |
Output is correct |
41 |
Correct |
18 ms |
13180 KB |
Output is correct |
42 |
Correct |
17 ms |
13180 KB |
Output is correct |
43 |
Correct |
17 ms |
13180 KB |
Output is correct |
44 |
Correct |
17 ms |
13180 KB |
Output is correct |
45 |
Correct |
16 ms |
13180 KB |
Output is correct |
46 |
Correct |
16 ms |
13180 KB |
Output is correct |
47 |
Correct |
16 ms |
13180 KB |
Output is correct |
48 |
Correct |
17 ms |
13180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
14 ms |
12244 KB |
Output is correct |
3 |
Correct |
13 ms |
12252 KB |
Output is correct |
4 |
Correct |
12 ms |
12252 KB |
Output is correct |
5 |
Correct |
18 ms |
12252 KB |
Output is correct |
6 |
Correct |
12 ms |
12252 KB |
Output is correct |
7 |
Correct |
13 ms |
12280 KB |
Output is correct |
8 |
Correct |
12 ms |
12280 KB |
Output is correct |
9 |
Correct |
12 ms |
12292 KB |
Output is correct |
10 |
Correct |
12 ms |
12292 KB |
Output is correct |
11 |
Correct |
13 ms |
12368 KB |
Output is correct |
12 |
Correct |
13 ms |
12384 KB |
Output is correct |
13 |
Correct |
13 ms |
12384 KB |
Output is correct |
14 |
Correct |
12 ms |
12384 KB |
Output is correct |
15 |
Correct |
12 ms |
12384 KB |
Output is correct |
16 |
Correct |
12 ms |
12384 KB |
Output is correct |
17 |
Correct |
12 ms |
12384 KB |
Output is correct |
18 |
Correct |
12 ms |
12400 KB |
Output is correct |
19 |
Correct |
17 ms |
12412 KB |
Output is correct |
20 |
Correct |
12 ms |
12412 KB |
Output is correct |
21 |
Correct |
12 ms |
12412 KB |
Output is correct |
22 |
Correct |
13 ms |
12412 KB |
Output is correct |
23 |
Correct |
14 ms |
12412 KB |
Output is correct |
24 |
Correct |
12 ms |
12412 KB |
Output is correct |
25 |
Correct |
16 ms |
12924 KB |
Output is correct |
26 |
Correct |
16 ms |
12948 KB |
Output is correct |
27 |
Correct |
16 ms |
12948 KB |
Output is correct |
28 |
Correct |
16 ms |
12948 KB |
Output is correct |
29 |
Correct |
16 ms |
12948 KB |
Output is correct |
30 |
Correct |
17 ms |
13076 KB |
Output is correct |
31 |
Correct |
15 ms |
13076 KB |
Output is correct |
32 |
Correct |
16 ms |
13076 KB |
Output is correct |
33 |
Correct |
16 ms |
13076 KB |
Output is correct |
34 |
Correct |
17 ms |
13076 KB |
Output is correct |
35 |
Correct |
17 ms |
13076 KB |
Output is correct |
36 |
Correct |
17 ms |
13076 KB |
Output is correct |
37 |
Correct |
18 ms |
13148 KB |
Output is correct |
38 |
Correct |
17 ms |
13180 KB |
Output is correct |
39 |
Correct |
19 ms |
13180 KB |
Output is correct |
40 |
Correct |
18 ms |
13180 KB |
Output is correct |
41 |
Correct |
18 ms |
13180 KB |
Output is correct |
42 |
Correct |
17 ms |
13180 KB |
Output is correct |
43 |
Correct |
17 ms |
13180 KB |
Output is correct |
44 |
Correct |
17 ms |
13180 KB |
Output is correct |
45 |
Correct |
16 ms |
13180 KB |
Output is correct |
46 |
Correct |
16 ms |
13180 KB |
Output is correct |
47 |
Correct |
16 ms |
13180 KB |
Output is correct |
48 |
Correct |
17 ms |
13180 KB |
Output is correct |
49 |
Correct |
1414 ms |
69444 KB |
Output is correct |
50 |
Correct |
1295 ms |
69540 KB |
Output is correct |
51 |
Correct |
1268 ms |
69540 KB |
Output is correct |
52 |
Correct |
1259 ms |
69540 KB |
Output is correct |
53 |
Correct |
1133 ms |
69540 KB |
Output is correct |
54 |
Correct |
1113 ms |
69540 KB |
Output is correct |
55 |
Correct |
1064 ms |
69540 KB |
Output is correct |
56 |
Correct |
1061 ms |
69540 KB |
Output is correct |
57 |
Correct |
1210 ms |
78972 KB |
Output is correct |
58 |
Correct |
1221 ms |
78972 KB |
Output is correct |
59 |
Correct |
1250 ms |
78972 KB |
Output is correct |
60 |
Correct |
1246 ms |
78972 KB |
Output is correct |
61 |
Correct |
1926 ms |
88808 KB |
Output is correct |
62 |
Correct |
1872 ms |
95912 KB |
Output is correct |
63 |
Correct |
1951 ms |
95912 KB |
Output is correct |
64 |
Correct |
1846 ms |
95912 KB |
Output is correct |
65 |
Correct |
1880 ms |
95912 KB |
Output is correct |
66 |
Correct |
1803 ms |
95912 KB |
Output is correct |
67 |
Correct |
1766 ms |
95912 KB |
Output is correct |
68 |
Correct |
1804 ms |
99872 KB |
Output is correct |
69 |
Correct |
1751 ms |
106952 KB |
Output is correct |
70 |
Correct |
1720 ms |
113812 KB |
Output is correct |
71 |
Correct |
1831 ms |
120880 KB |
Output is correct |
72 |
Correct |
1737 ms |
128076 KB |
Output is correct |
73 |
Correct |
1756 ms |
135284 KB |
Output is correct |
74 |
Correct |
1787 ms |
142604 KB |
Output is correct |
75 |
Correct |
1670 ms |
149708 KB |
Output is correct |
76 |
Correct |
1587 ms |
157016 KB |
Output is correct |
77 |
Correct |
1250 ms |
164336 KB |
Output is correct |
78 |
Correct |
713 ms |
172468 KB |
Output is correct |