#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)
{
multiset<int> s;
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i];
if (h == p)
continue;
s.insert(maxd[h] - d[x]);
}
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i];
if (h == p)
continue;
s.erase(s.find(maxd[h] - d[x]));
if (s.empty())
dfs(h, x, dd, pp);
else
dfs(h, x, max(dd, *(--s.end())), pp);
s.insert(maxd[h] - d[x]);
}
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)
{
if (x == 6)
cout << "";
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)
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:38: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 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |