#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
vector<vector<int>> graph;
vector<pii> depth;
pii sol = {0,0};
void add(pii p, pii &s)
{
if (p.first > s.first)
{
s = p;
}
else if (p.first == s.first)
{
s.second += p.second;
}
}
void dfsup(int i, int p, pii d)
{
vector<pii> conn;
vector<pii> conns;
for (int ele:graph[i])
{
if (ele == p) continue;
pii m = depth[ele];
conn.push_back(m);
conns.push_back(m);
}
int b = 0;
if (conns.empty())
{
b = 1;
conns.push_back({0, 1});
conn.push_back({0, 1});
}
if (d.second) conns.push_back(d);
if (d.second) conn.push_back(d);
sort(conns.begin(), conns.end(), greater<pii>());
#define arr array<int, 4>
vector<arr> conns2;
arr curr = {conns[0].first, 1, conns[0].second, 0};
for (int j = 1; j < conns.size(); j++)
{
pii ele = conns[j];
if (ele.first == curr[0])
{
curr[3] += ele.second*curr[2];
curr[2] += ele.second;
curr[1]++;
}
else
{
conns2.push_back(curr);
curr = {{ele.first, 1, ele.second, 0}};
}
}
conns2.push_back(curr);
int j = 0;
for (int ele:graph[i])
{
if (ele==p) continue;
pii pp = {conns2[0][0], conns2[0][2]};
if (pp.first == conn[j].first) pp.second -= conn[j].second;
if (pp.second == 0) pp = {conns2[1][0], conns2[1][2]};
pp.first++;
dfsup(ele, i, pp);
j++;
}
for (pii ele:conn)
{
int k = 0;
arr p1 = conns2[k];
int y = (p1[1]>2);
if (p1[0] == ele.first && p1[1]<=2)
{
p1[1]--;
p1[2]-=ele.second;
p1[3]-= ele.second*p1[2];
}
if (p1[1] == 0)
{
k++;
p1 = conns2[k];
}
if (p1[1]>1)
{
add({p1[0]*2*ele.first, p1[3]}, sol);
if (y) break;
continue;
}
k++;
if (k==conns2.size())
{
if (b && p1[0]) add({p1[0]*ele.first, p1[2]}, sol);
continue;
}
arr p2 = conns2[k];
if (p2[0] == ele.first)
{
p2[1]--;
p2[2]-=ele.second;
p2[3]-= ele.second*p2[2];
}
if (p2[1] == 0)
{
k++;
p2 = conns2[k];
}
if (k==conns2.size())
{
if (b && p1[0]) add({p1[0]*ele.first, p1[2]}, sol);
continue;
}
add({ele.first*(p1[0]+p2[0]), p1[2]*p2[2]}, sol);
}
}
pii dfsdown(int i, int p)
{
pii mx = {0,0};
if (graph[i].size()==1 && p >= 0) mx = {0, 1};
for (int ele:graph[i])
{
if (ele == p) continue;
pii m = dfsdown(ele, i);
add(m, mx);
}
mx.first++;
depth[i] = mx;
return mx;
}
int main()
{
int n;
cin >> n;
if (n==2)
{
cout << 0 << " " << 1;
return 0;
}
graph.assign(n, vector<int>());
depth.assign(n, {0,0});
for (int i = 1; i < n; i++)
{
int a, b;
cin >> a >> b;
a--; b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
int start = 0;
for (int i = 0; i < n; i++)
{
if (graph[i].size()>1) start = i;
}
dfsdown(start, -1);
dfsup(start, -1, {0,0});
//get rid of double counting!
if (sol.first==0) cout << 0 << " " << 1;
else cout << sol.first << " " << sol.second;
return 0;
}
Compilation message
road.cpp: In function 'void dfsup(int, int, std::pair<int, int>)':
road.cpp:47:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int j = 1; j < conns.size(); j++)
| ~~^~~~~~~~~~~~~~
road.cpp:97:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | if (k==conns2.size())
| ~^~~~~~~~~~~~~~~
road.cpp:114:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | if (k==conns2.size())
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
420 KB |
Output is correct |
11 |
Correct |
1 ms |
436 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
432 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
420 KB |
Output is correct |
11 |
Correct |
1 ms |
436 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
432 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1372 KB |
Output is correct |
27 |
Correct |
3 ms |
1368 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1116 KB |
Output is correct |
30 |
Correct |
3 ms |
1228 KB |
Output is correct |
31 |
Correct |
3 ms |
1372 KB |
Output is correct |
32 |
Correct |
3 ms |
1368 KB |
Output is correct |
33 |
Correct |
4 ms |
1628 KB |
Output is correct |
34 |
Correct |
3 ms |
1884 KB |
Output is correct |
35 |
Correct |
3 ms |
1628 KB |
Output is correct |
36 |
Correct |
3 ms |
1372 KB |
Output is correct |
37 |
Correct |
4 ms |
1884 KB |
Output is correct |
38 |
Correct |
3 ms |
2188 KB |
Output is correct |
39 |
Correct |
3 ms |
1116 KB |
Output is correct |
40 |
Correct |
3 ms |
860 KB |
Output is correct |
41 |
Correct |
5 ms |
860 KB |
Output is correct |
42 |
Correct |
5 ms |
844 KB |
Output is correct |
43 |
Correct |
5 ms |
604 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
604 KB |
Output is correct |
47 |
Correct |
3 ms |
860 KB |
Output is correct |
48 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
420 KB |
Output is correct |
11 |
Correct |
1 ms |
436 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
432 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
1372 KB |
Output is correct |
26 |
Correct |
3 ms |
1372 KB |
Output is correct |
27 |
Correct |
3 ms |
1368 KB |
Output is correct |
28 |
Correct |
3 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1116 KB |
Output is correct |
30 |
Correct |
3 ms |
1228 KB |
Output is correct |
31 |
Correct |
3 ms |
1372 KB |
Output is correct |
32 |
Correct |
3 ms |
1368 KB |
Output is correct |
33 |
Correct |
4 ms |
1628 KB |
Output is correct |
34 |
Correct |
3 ms |
1884 KB |
Output is correct |
35 |
Correct |
3 ms |
1628 KB |
Output is correct |
36 |
Correct |
3 ms |
1372 KB |
Output is correct |
37 |
Correct |
4 ms |
1884 KB |
Output is correct |
38 |
Correct |
3 ms |
2188 KB |
Output is correct |
39 |
Correct |
3 ms |
1116 KB |
Output is correct |
40 |
Correct |
3 ms |
860 KB |
Output is correct |
41 |
Correct |
5 ms |
860 KB |
Output is correct |
42 |
Correct |
5 ms |
844 KB |
Output is correct |
43 |
Correct |
5 ms |
604 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
604 KB |
Output is correct |
47 |
Correct |
3 ms |
860 KB |
Output is correct |
48 |
Correct |
2 ms |
860 KB |
Output is correct |
49 |
Correct |
579 ms |
121172 KB |
Output is correct |
50 |
Correct |
480 ms |
121204 KB |
Output is correct |
51 |
Correct |
554 ms |
119380 KB |
Output is correct |
52 |
Correct |
520 ms |
92696 KB |
Output is correct |
53 |
Incorrect |
500 ms |
101708 KB |
Output isn't correct |
54 |
Halted |
0 ms |
0 KB |
- |