#include<bits/stdc++.h>
#pragma optimize ("g",on)
#pragma GCC optimize ("inline")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("03")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
//01001101 01100001 01101011 01101000 01100001 01100111 01100001 01111001
using namespace std;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define pb push_back
#define sz(a) a.size()
#define nl '\n'
#define popb pop_back()
#define ld double
#define ull unsigned long long
#define ff first
#define ss second
#define fix fixed << setprecision
#define pii pair<int, int>
#define E exit (0)
#define int long long
const int inf = 1e9, N = 5e5 + 1, mod = 998244353;
vector<pii> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<int> adj[N];
map<int, int> cn;
multiset<int> s;
int pr[N], d[N], d2[N];
int mx, cc;
void dfs(int u, int p, int cur = -1) {
pr[u] = cur;
d[u] = d[p] + 1;
for (auto e: adj[u]) {
if (e != p) {
dfs(e, u, (cur == -1 ? e : cur));
d2[u] = max(d2[u], d2[e] + 1);
}
}
}
void calc(int u, int p) {
multiset<int> cur;
map<int, int> c;
int x = d[u] + (s.empty() ? -1 : *--s.end());
cur.insert(inf - x);
int y = 1;
if (!s.empty()) y = cn[*--s.end()];
c[inf - x] += y;
for (auto e: adj[u]) {
if (e != p) {
calc(e, u);
cur.insert(inf - d2[e] - 1);
c[inf - d2[e] - 1]++;
}
}
for (auto e: adj[u]) {
if (e == p) continue;
cur.erase(cur.find(inf - d2[e] - 1));
c[inf - d2[e] - 1]--;
int sum = 0, cnt = 0;
vector<int> h;
for (auto e: cur) {
sum += inf - e;
h.pb(e);
cnt++;
if (cnt == 2) break;
}
if (cnt == 2) {
int val = (d2[e] + 1) * sum;
if (val > mx) mx = val, cc = 0;
if (val == mx) {
if (h[0] == h[1]) cc += c[h[0]] * (c[h[0]] - 1) / 2;
else cc += c[h[0]] * c[h[1]];
}
}
c[inf - d2[e] - 1]++;
cur.insert(inf - d2[e] - 1);
}
cur.erase(cur.find(inf - x));
c[inf - x] -= y;
int sum = 0, cnt = 0;
vector<int> h;
for (auto e: cur) {
sum += inf - e;
cnt++;
h.pb(e);
if (cnt == 2) break;
}
if (cnt == 2) {
int val = x * sum;
if (val > mx) mx = val, cc = 0;
if (val == mx) {
if (h[0] == h[1]) cc += c[h[0]] * (c[h[0]] - 1) / 2;
else cc += c[h[0]] * c[h[1]];
}
}
if (sz(adj[u]) == 1) {
if (mx == 0) cc += y;
}
}
main() {
//freopen("cowrect.in", "r", stdin);
//freopen("cowrect.out", "w", stdout);
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1, 1);
for (auto e: adj[1]) {
s.insert(d2[e]);
cn[d2[e]]++;
}
for (auto e: adj[1]) {
s.erase(s.find(d2[e]));
cn[d2[e]]--;
calc(e, 1);
s.insert(d2[e]);
cn[d2[e]]++;
}
cout << mx << ' ' << cc;
}
Compilation message
road.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
3 | #pragma optimize ("g",on)
|
road.cpp:9: warning: ignoring '#pragma comment ' [-Wunknown-pragmas]
9 | #pragma comment(linker, "/stack:200000000")
|
road.cpp:117:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
117 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
11980 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Incorrect |
6 ms |
11980 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
11980 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Incorrect |
6 ms |
11980 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
11980 KB |
Output is correct |
2 |
Correct |
6 ms |
11980 KB |
Output is correct |
3 |
Correct |
6 ms |
11980 KB |
Output is correct |
4 |
Incorrect |
6 ms |
11980 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |