#include <bits/stdc++.h>
using namespace std;
#define int ll
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;
const int maxn = 5010;
int n, sol = 0, cnt = 0;
vi e[maxn];
vp dete[maxn];
int maxd = 0;
void calc(int x, int st, int dist = 1){
maxd = max(maxd, dist);
for (int i : e[x]){
if (i == st) continue;
calc(i, x, dist+1);
}
}
void dfs(int x, int st, int d = 0){
int len = dete[x].size();
if (len > 2){
pi prvi = {-1, -1}, drugi = {-1, -1};
for (int i = 0; i < 3; ++i){
if (dete[x][i].second == st) continue;
if (prvi.first == -1)
prvi = dete[x][i];
else
drugi = dete[x][i];
}
int res = prvi.first + drugi.first; res *= d;
if (res > sol){
sol = res;
cnt = 1;
} else if (res == sol){
++cnt;
}
}
for (int i : e[x]){
if (i == st) continue;
dfs(i, x, d+1);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
cin >> n;
if (n > 5000) throw SIGSEGV;
for (int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
int k = 0;
for (int i = 1; i <= n; ++i){
int len = e[i].size();
k += (len==1);
}
if (k == 2) return cout << "0 1\n", 0;
for (int i = 1; i <= n; ++i){
for (int x : e[i]){
maxd = 0;
calc(x, i);
dete[i].push_back({maxd, x});
}
sort(dete[i].rbegin(), dete[i].rend());
}
for (int x = 1; x <= n; ++x){
int len = e[x].size();
if (len > 1) continue;
dfs(x, x);
}
cout << sol << ' ' << cnt << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |