#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
ll readint(){
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,tot;
int v[1000005],nxt[1000005],h[500005],mx[500005],cnt[500005];
void addedge(int x,int y){
v[++tot]=y; nxt[tot]=h[x]; h[x]=tot;
v[++tot]=x; nxt[tot]=h[y]; h[y]=tot;
}
void dfs(int x,int px,int d){
mx[x]=d;
cnt[x]=1;
for(int p=h[x];p;p=nxt[p]){
if(v[p]==px) continue;
dfs(v[p],x,d+1);
if(mx[v[p]]==mx[x]) cnt[x]+=cnt[v[p]];
else if(mx[x]<mx[v[p]]) cnt[x]=cnt[v[p]];
mx[x]=max(mx[x],mx[v[p]]);
}
}
int main(){
n=readint();
for(int i=1;i<n;i++) addedge(readint(),readint());
ll res=0,c=1;
for(int i=1;i<=n;i++){
dfs(i,i,0);
vector<pii> f;
for(int p=h[i];p;p=nxt[p]) f.pb(mp(mx[v[p]],cnt[v[p]]));
sort(f.rbegin(),f.rend());
if((int)f.size()<=2) continue;
ll cur=0,cc=0;
cur=f[0].fi*(f[1].fi+f[2].fi);
for(int x=0;x<f.size();x++){
for(int y=x+1;y<f.size();y++){
int fir=0;
for(int z=0;z<f.size();z++) if(x!=z&&y!=z) fir=max(fir,f[z].fi);
if(fir*(f[x].fi+f[y].fi)==cur) cc+=f[x].se*f[y].se;
}
}
if(res<cur) res=cur,c=cc;
else if(res==cur) c+=cc;
}
printf("%lld %lld\n",res,c);
return 0;
}
Compilation message
road.cpp: In function 'int main()':
road.cpp:58:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int x=0;x<f.size();x++){
| ~^~~~~~~~~
road.cpp:59:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int y=x+1;y<f.size();y++){
| ~^~~~~~~~~
road.cpp:61:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int z=0;z<f.size();z++) if(x!=z&&y!=z) fir=max(fir,f[z].fi);
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6900 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6588 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6488 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6704 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6900 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6588 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6488 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6704 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
277 ms |
7020 KB |
Output is correct |
26 |
Correct |
257 ms |
6748 KB |
Output is correct |
27 |
Correct |
249 ms |
6904 KB |
Output is correct |
28 |
Correct |
280 ms |
6860 KB |
Output is correct |
29 |
Correct |
231 ms |
6860 KB |
Output is correct |
30 |
Correct |
225 ms |
7004 KB |
Output is correct |
31 |
Correct |
218 ms |
6864 KB |
Output is correct |
32 |
Correct |
220 ms |
6748 KB |
Output is correct |
33 |
Correct |
295 ms |
6956 KB |
Output is correct |
34 |
Correct |
248 ms |
6960 KB |
Output is correct |
35 |
Correct |
287 ms |
6956 KB |
Output is correct |
36 |
Correct |
281 ms |
6956 KB |
Output is correct |
37 |
Correct |
445 ms |
7056 KB |
Output is correct |
38 |
Correct |
477 ms |
7004 KB |
Output is correct |
39 |
Correct |
450 ms |
6824 KB |
Output is correct |
40 |
Correct |
442 ms |
6744 KB |
Output is correct |
41 |
Correct |
467 ms |
6708 KB |
Output is correct |
42 |
Correct |
407 ms |
6692 KB |
Output is correct |
43 |
Correct |
410 ms |
6680 KB |
Output is correct |
44 |
Correct |
387 ms |
6676 KB |
Output is correct |
45 |
Correct |
379 ms |
6668 KB |
Output is correct |
46 |
Correct |
717 ms |
6672 KB |
Output is correct |
47 |
Execution timed out |
2021 ms |
6676 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6900 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6588 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6488 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6488 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6704 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
277 ms |
7020 KB |
Output is correct |
26 |
Correct |
257 ms |
6748 KB |
Output is correct |
27 |
Correct |
249 ms |
6904 KB |
Output is correct |
28 |
Correct |
280 ms |
6860 KB |
Output is correct |
29 |
Correct |
231 ms |
6860 KB |
Output is correct |
30 |
Correct |
225 ms |
7004 KB |
Output is correct |
31 |
Correct |
218 ms |
6864 KB |
Output is correct |
32 |
Correct |
220 ms |
6748 KB |
Output is correct |
33 |
Correct |
295 ms |
6956 KB |
Output is correct |
34 |
Correct |
248 ms |
6960 KB |
Output is correct |
35 |
Correct |
287 ms |
6956 KB |
Output is correct |
36 |
Correct |
281 ms |
6956 KB |
Output is correct |
37 |
Correct |
445 ms |
7056 KB |
Output is correct |
38 |
Correct |
477 ms |
7004 KB |
Output is correct |
39 |
Correct |
450 ms |
6824 KB |
Output is correct |
40 |
Correct |
442 ms |
6744 KB |
Output is correct |
41 |
Correct |
467 ms |
6708 KB |
Output is correct |
42 |
Correct |
407 ms |
6692 KB |
Output is correct |
43 |
Correct |
410 ms |
6680 KB |
Output is correct |
44 |
Correct |
387 ms |
6676 KB |
Output is correct |
45 |
Correct |
379 ms |
6668 KB |
Output is correct |
46 |
Correct |
717 ms |
6672 KB |
Output is correct |
47 |
Execution timed out |
2021 ms |
6676 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |