#include <bits/stdc++.h>
using namespace std;
const int N = 5003;
int n;
vector<int> a[N];
int ans[N][N];
int d[N], maxd[N];;
void dfs0(int x, int p)
{
if (x == p)
d[x] = 0;
else
d[x] = d[p] + 1;
maxd[x] = d[x];
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i];
if (h == p)
continue;
dfs0(h, x);
maxd[x] = max(maxd[x], maxd[h]);
}
}
void dfs(int x, int p, int dd, int pp)
{
int max1 = 0, max2 = 0;
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i];
if (h == p)
continue;
int t = (maxd[h] - d[x]);
if (t > max1)
{
max2 = max1;
max1 = t;
}
else if (t > max2)
max2 = t;
}
int q1 = 0, q2 = 0;
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i];
if (h == p)
continue;
int t = (maxd[h] - d[x]);
if (t == max1)
q1++;
if (t == max2)
q2++;
}
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i];
if (h == p)
continue;
int t = (maxd[h] - d[x]);
if (max1 == max2)
dfs(h, x, max(dd, max1), pp);
else
{
if (t == max1)
dfs(h, x, max(dd, max2), pp);
else
dfs(h, x, max(dd, max1), pp);
}
}
ans[pp][x] = dd * d[x];
}
int main()
{
//freopen("input2.txt", "r", stdin);
cin >> n;
for (int i = 0; i < n - 1; ++i)
{
int x, y;
cin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
for (int x = 1; x <= n; ++x)
{
if (a[x].size() == 1)
{
dfs0(x, x);
dfs(x, x, 0, x);
}
}
int maxu = -1;
for (int x = 1; x <= n; ++x)
{
if (a[x].size() > 1)
continue;
for (int y = 1; y <= n; ++y)
{
if (a[y].size() > 1)
continue;
maxu = max(maxu, ans[x][y]);
}
}
int q = 0;
for (int x = 1; x <= n; ++x)
{
if (a[x].size() > 1)
continue;
for (int y = 1; y <= n; ++y)
{
if (a[y].size() > 1 || x == y)
continue;
if (ans[x][y] == maxu)
++q;
}
}
cout << maxu << ' ' << q / 2 << endl;
return 0;
}
Compilation message
road.cpp: In function 'void dfs0(int, int)':
road.cpp:18:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[x].size(); ++i)
~~^~~~~~~~~~~~~
road.cpp: In function 'void dfs(int, int, int, int)':
road.cpp:31:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[x].size(); ++i)
~~^~~~~~~~~~~~~
road.cpp:46:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[x].size(); ++i)
~~^~~~~~~~~~~~~
road.cpp:57:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[x].size(); ++i)
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
632 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
632 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
2 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
504 KB |
Output is correct |
24 |
Correct |
2 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
632 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
632 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
2 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
504 KB |
Output is correct |
24 |
Correct |
2 ms |
888 KB |
Output is correct |
25 |
Correct |
373 ms |
29088 KB |
Output is correct |
26 |
Correct |
381 ms |
29048 KB |
Output is correct |
27 |
Correct |
384 ms |
29188 KB |
Output is correct |
28 |
Correct |
391 ms |
29020 KB |
Output is correct |
29 |
Correct |
584 ms |
55100 KB |
Output is correct |
30 |
Correct |
583 ms |
55032 KB |
Output is correct |
31 |
Correct |
583 ms |
54924 KB |
Output is correct |
32 |
Correct |
603 ms |
55028 KB |
Output is correct |
33 |
Correct |
8 ms |
1272 KB |
Output is correct |
34 |
Correct |
8 ms |
1272 KB |
Output is correct |
35 |
Correct |
8 ms |
1272 KB |
Output is correct |
36 |
Correct |
8 ms |
1272 KB |
Output is correct |
37 |
Correct |
7 ms |
1144 KB |
Output is correct |
38 |
Correct |
6 ms |
1144 KB |
Output is correct |
39 |
Correct |
7 ms |
888 KB |
Output is correct |
40 |
Correct |
10 ms |
1016 KB |
Output is correct |
41 |
Correct |
15 ms |
1272 KB |
Output is correct |
42 |
Correct |
26 ms |
1912 KB |
Output is correct |
43 |
Correct |
44 ms |
3192 KB |
Output is correct |
44 |
Correct |
83 ms |
5368 KB |
Output is correct |
45 |
Correct |
173 ms |
11640 KB |
Output is correct |
46 |
Correct |
213 ms |
20284 KB |
Output is correct |
47 |
Correct |
592 ms |
51600 KB |
Output is correct |
48 |
Correct |
691 ms |
98848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
632 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
632 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
21 |
Correct |
2 ms |
504 KB |
Output is correct |
22 |
Correct |
2 ms |
504 KB |
Output is correct |
23 |
Correct |
2 ms |
504 KB |
Output is correct |
24 |
Correct |
2 ms |
888 KB |
Output is correct |
25 |
Correct |
373 ms |
29088 KB |
Output is correct |
26 |
Correct |
381 ms |
29048 KB |
Output is correct |
27 |
Correct |
384 ms |
29188 KB |
Output is correct |
28 |
Correct |
391 ms |
29020 KB |
Output is correct |
29 |
Correct |
584 ms |
55100 KB |
Output is correct |
30 |
Correct |
583 ms |
55032 KB |
Output is correct |
31 |
Correct |
583 ms |
54924 KB |
Output is correct |
32 |
Correct |
603 ms |
55028 KB |
Output is correct |
33 |
Correct |
8 ms |
1272 KB |
Output is correct |
34 |
Correct |
8 ms |
1272 KB |
Output is correct |
35 |
Correct |
8 ms |
1272 KB |
Output is correct |
36 |
Correct |
8 ms |
1272 KB |
Output is correct |
37 |
Correct |
7 ms |
1144 KB |
Output is correct |
38 |
Correct |
6 ms |
1144 KB |
Output is correct |
39 |
Correct |
7 ms |
888 KB |
Output is correct |
40 |
Correct |
10 ms |
1016 KB |
Output is correct |
41 |
Correct |
15 ms |
1272 KB |
Output is correct |
42 |
Correct |
26 ms |
1912 KB |
Output is correct |
43 |
Correct |
44 ms |
3192 KB |
Output is correct |
44 |
Correct |
83 ms |
5368 KB |
Output is correct |
45 |
Correct |
173 ms |
11640 KB |
Output is correct |
46 |
Correct |
213 ms |
20284 KB |
Output is correct |
47 |
Correct |
592 ms |
51600 KB |
Output is correct |
48 |
Correct |
691 ms |
98848 KB |
Output is correct |
49 |
Runtime error |
4 ms |
632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
50 |
Halted |
0 ms |
0 KB |
- |