11#pragma once
2- void edge_cd_asserts (const vector<vi>& adj, int cent,
3- int split) {
2+ void edge_cd_asserts (const vector<vi>& adj, int cent, int split) {
43 assert (0 < split && split < sz (adj[cent]));
54 auto dfs = [&](auto && self, int u, int p) -> int {
65 int siz = 1 ;
@@ -11,12 +10,37 @@ void edge_cd_asserts(const vector<vi>& adj, int cent,
1110 int sz_all = dfs (dfs, cent, -1 );
1211 assert (sz_all >= 3 );
1312 array<int , 2 > cnts = {0 , 0 };
13+ array<int , 2 > max_cnt = {0 , 0 };
1414 for (int i = 0 ; i < sz (adj[cent]); i++) {
1515 int sz_subtree = dfs (dfs, adj[cent][i], cent);
1616 assert (2 * sz_subtree <= sz_all);
1717 cnts[i < split] += sz_subtree;
18+ max_cnt[i < split] = max (max_cnt[i < split], sz_subtree);
1819 }
1920 assert (cnts[0 ] + cnts[1 ] + 1 == sz_all);
20- for (int i = 0 ; i < 2 ; i++)
21- assert (0 < cnts[i] && cnts[i] <= 2 * cnts[!i]);
21+
22+ if (sz_all == 4 ) return ;
23+
24+ // a is the number of edges in the smaller edge set
25+ // b is the number of edges in the larger edge set
26+ // so we know 1/2 <= b/(a+b)
27+ // returns true iff b/(a+b) <= 1/phi
28+ auto is_balanced = [&](ll a, ll b) -> bool {
29+ assert (a <= b);
30+ return b * b <= a * (a + b);
31+ };
32+
33+ if (cnts[0 ] > cnts[1 ]) {
34+ swap (cnts[0 ], cnts[1 ]);
35+ swap (max_cnt[0 ], max_cnt[1 ]);
36+ }
37+
38+ if (!is_balanced (cnts[0 ], cnts[1 ])) {
39+ int a = max_cnt[1 ];
40+ int b = cnts[1 ] - max_cnt[1 ];
41+ assert (a > 0 );
42+ assert (b > 0 );
43+ if (a > b) swap (a, b);
44+ assert (is_balanced (a, b));
45+ }
2246}
0 commit comments