#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e6 + 5;
const int oo = 1e9, mod = 1e9 + 7;
int par[N], in[N], cnt[N], n, ans, res;
vector<int> adj[N];
bool maximize(int &a, const int b) {
if (a < b) {
a = b;
return true;
} return false;
}
void dfs(int u) {
cnt[u] = 1;
in[u] = 1;
for (auto v : adj[u]) {
if (v == par[u]) continue;
par[v] = u;
dfs(v);
if (maximize(in[u], in[v] + 1)) cnt[u] = 0;
if (in[u] == in[v] + 1) {
cnt[u] += cnt[v];
}
}
}
void dp(int u, int out, int outc) {
vector<ii> vt;
if (u != 1)
vt.emplace_back(out, outc);
for (auto v : adj[u]) {
if (v == par[u]) continue;
vt.emplace_back(in[v], cnt[v]);
}
sort(vt.begin(), vt.end(), greater<ii>());
if (vt.size() >= 3 && (vt[0].fi * (vt[1].fi + vt[2].fi)) >= res) {
int curres = (vt[0].fi * (vt[1].fi + vt[2].fi)), curcnt = 0;
int ba = 0;
for (auto v : vt) {
if (v.fi == vt[2].fi) ba += v.se;
}
if (vt[1].fi == vt[2].fi) {
int cnt = 0;
for (auto tmp : vt) {
int x = tmp.fi, num = tmp.se;
if (x == vt[1].fi) {
curcnt += cnt * num;
cnt += num;
}
}
}
else if (vt[0].fi == vt[1].fi) {
curcnt = (vt[0].se + vt[1].se) * ba;
}
else {
curcnt = vt[1].se * ba;
}
if (maximize(res, curres)) ans = 0;
ans += curcnt;
}
int mx1 = vt[0].fi, mx1c = 0, mx2 = 0, mx2c = 1;
if (vt.size() >= 2) mx2 = vt[1].fi, mx2c = 0;
for (auto tmp : vt) {
int v = tmp.fi, num = tmp.se;
if (v == mx1) mx1c += num;
if (v == mx2) mx2c += num;
}
for (auto v : adj[u]) {
if (v == par[u]) continue;
if (in[v] == mx1) {
if (cnt[v] == mx1c) dp(v, mx2 + 1, mx2c);
else dp(v, mx1 + 1, mx1c - cnt[v]);
}
else dp(v, mx1 + 1, mx1c);
}
}
void process() {
cin >> n;
ans = 1;
foru(i,1,n-1) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int flag = 1, cnt = 0;
foru(i,1,n) {
if (adj[i].size() > 2) flag = 0;
if (adj[i].size() == 1) cnt++;
}
dfs(1);
dp(1, 0, 1);
cout << res << ' ' << ans << '\n';
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
// dont stop
Compilation message
road.cpp: In function 'void process()':
road.cpp:106:9: warning: variable 'flag' set but not used [-Wunused-but-set-variable]
106 | int flag = 1, cnt = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27304 KB |
Output is correct |
3 |
Correct |
7 ms |
27740 KB |
Output is correct |
4 |
Correct |
6 ms |
27264 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27264 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27228 KB |
Output is correct |
9 |
Correct |
6 ms |
27300 KB |
Output is correct |
10 |
Correct |
6 ms |
27304 KB |
Output is correct |
11 |
Correct |
6 ms |
27224 KB |
Output is correct |
12 |
Correct |
6 ms |
27224 KB |
Output is correct |
13 |
Correct |
7 ms |
27740 KB |
Output is correct |
14 |
Correct |
6 ms |
27308 KB |
Output is correct |
15 |
Correct |
6 ms |
27228 KB |
Output is correct |
16 |
Correct |
6 ms |
27228 KB |
Output is correct |
17 |
Correct |
6 ms |
27224 KB |
Output is correct |
18 |
Correct |
6 ms |
27228 KB |
Output is correct |
19 |
Correct |
6 ms |
27228 KB |
Output is correct |
20 |
Correct |
6 ms |
27224 KB |
Output is correct |
21 |
Correct |
5 ms |
27228 KB |
Output is correct |
22 |
Correct |
6 ms |
27308 KB |
Output is correct |
23 |
Correct |
6 ms |
27228 KB |
Output is correct |
24 |
Correct |
7 ms |
27228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27304 KB |
Output is correct |
3 |
Correct |
7 ms |
27740 KB |
Output is correct |
4 |
Correct |
6 ms |
27264 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27264 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27228 KB |
Output is correct |
9 |
Correct |
6 ms |
27300 KB |
Output is correct |
10 |
Correct |
6 ms |
27304 KB |
Output is correct |
11 |
Correct |
6 ms |
27224 KB |
Output is correct |
12 |
Correct |
6 ms |
27224 KB |
Output is correct |
13 |
Correct |
7 ms |
27740 KB |
Output is correct |
14 |
Correct |
6 ms |
27308 KB |
Output is correct |
15 |
Correct |
6 ms |
27228 KB |
Output is correct |
16 |
Correct |
6 ms |
27228 KB |
Output is correct |
17 |
Correct |
6 ms |
27224 KB |
Output is correct |
18 |
Correct |
6 ms |
27228 KB |
Output is correct |
19 |
Correct |
6 ms |
27228 KB |
Output is correct |
20 |
Correct |
6 ms |
27224 KB |
Output is correct |
21 |
Correct |
5 ms |
27228 KB |
Output is correct |
22 |
Correct |
6 ms |
27308 KB |
Output is correct |
23 |
Correct |
6 ms |
27228 KB |
Output is correct |
24 |
Correct |
7 ms |
27228 KB |
Output is correct |
25 |
Correct |
7 ms |
27740 KB |
Output is correct |
26 |
Correct |
7 ms |
27748 KB |
Output is correct |
27 |
Correct |
7 ms |
27992 KB |
Output is correct |
28 |
Correct |
7 ms |
27740 KB |
Output is correct |
29 |
Correct |
7 ms |
27832 KB |
Output is correct |
30 |
Correct |
7 ms |
27740 KB |
Output is correct |
31 |
Correct |
8 ms |
27740 KB |
Output is correct |
32 |
Correct |
8 ms |
27732 KB |
Output is correct |
33 |
Correct |
7 ms |
27740 KB |
Output is correct |
34 |
Correct |
7 ms |
27740 KB |
Output is correct |
35 |
Correct |
7 ms |
27740 KB |
Output is correct |
36 |
Correct |
7 ms |
27740 KB |
Output is correct |
37 |
Correct |
7 ms |
27996 KB |
Output is correct |
38 |
Correct |
8 ms |
28252 KB |
Output is correct |
39 |
Correct |
9 ms |
27740 KB |
Output is correct |
40 |
Correct |
8 ms |
27600 KB |
Output is correct |
41 |
Correct |
7 ms |
27484 KB |
Output is correct |
42 |
Correct |
8 ms |
27508 KB |
Output is correct |
43 |
Correct |
7 ms |
27484 KB |
Output is correct |
44 |
Correct |
9 ms |
27512 KB |
Output is correct |
45 |
Correct |
7 ms |
27484 KB |
Output is correct |
46 |
Correct |
7 ms |
27484 KB |
Output is correct |
47 |
Correct |
7 ms |
27484 KB |
Output is correct |
48 |
Correct |
8 ms |
27480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27304 KB |
Output is correct |
3 |
Correct |
7 ms |
27740 KB |
Output is correct |
4 |
Correct |
6 ms |
27264 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27264 KB |
Output is correct |
7 |
Correct |
6 ms |
27228 KB |
Output is correct |
8 |
Correct |
6 ms |
27228 KB |
Output is correct |
9 |
Correct |
6 ms |
27300 KB |
Output is correct |
10 |
Correct |
6 ms |
27304 KB |
Output is correct |
11 |
Correct |
6 ms |
27224 KB |
Output is correct |
12 |
Correct |
6 ms |
27224 KB |
Output is correct |
13 |
Correct |
7 ms |
27740 KB |
Output is correct |
14 |
Correct |
6 ms |
27308 KB |
Output is correct |
15 |
Correct |
6 ms |
27228 KB |
Output is correct |
16 |
Correct |
6 ms |
27228 KB |
Output is correct |
17 |
Correct |
6 ms |
27224 KB |
Output is correct |
18 |
Correct |
6 ms |
27228 KB |
Output is correct |
19 |
Correct |
6 ms |
27228 KB |
Output is correct |
20 |
Correct |
6 ms |
27224 KB |
Output is correct |
21 |
Correct |
5 ms |
27228 KB |
Output is correct |
22 |
Correct |
6 ms |
27308 KB |
Output is correct |
23 |
Correct |
6 ms |
27228 KB |
Output is correct |
24 |
Correct |
7 ms |
27228 KB |
Output is correct |
25 |
Correct |
7 ms |
27740 KB |
Output is correct |
26 |
Correct |
7 ms |
27748 KB |
Output is correct |
27 |
Correct |
7 ms |
27992 KB |
Output is correct |
28 |
Correct |
7 ms |
27740 KB |
Output is correct |
29 |
Correct |
7 ms |
27832 KB |
Output is correct |
30 |
Correct |
7 ms |
27740 KB |
Output is correct |
31 |
Correct |
8 ms |
27740 KB |
Output is correct |
32 |
Correct |
8 ms |
27732 KB |
Output is correct |
33 |
Correct |
7 ms |
27740 KB |
Output is correct |
34 |
Correct |
7 ms |
27740 KB |
Output is correct |
35 |
Correct |
7 ms |
27740 KB |
Output is correct |
36 |
Correct |
7 ms |
27740 KB |
Output is correct |
37 |
Correct |
7 ms |
27996 KB |
Output is correct |
38 |
Correct |
8 ms |
28252 KB |
Output is correct |
39 |
Correct |
9 ms |
27740 KB |
Output is correct |
40 |
Correct |
8 ms |
27600 KB |
Output is correct |
41 |
Correct |
7 ms |
27484 KB |
Output is correct |
42 |
Correct |
8 ms |
27508 KB |
Output is correct |
43 |
Correct |
7 ms |
27484 KB |
Output is correct |
44 |
Correct |
9 ms |
27512 KB |
Output is correct |
45 |
Correct |
7 ms |
27484 KB |
Output is correct |
46 |
Correct |
7 ms |
27484 KB |
Output is correct |
47 |
Correct |
7 ms |
27484 KB |
Output is correct |
48 |
Correct |
8 ms |
27480 KB |
Output is correct |
49 |
Correct |
382 ms |
87272 KB |
Output is correct |
50 |
Correct |
324 ms |
92496 KB |
Output is correct |
51 |
Correct |
374 ms |
97364 KB |
Output is correct |
52 |
Correct |
358 ms |
80752 KB |
Output is correct |
53 |
Incorrect |
276 ms |
92964 KB |
Output isn't correct |
54 |
Halted |
0 ms |
0 KB |
- |