题面
LOJ
题解
感性理解一下,榕树之心最后要停在一个节点就是要使得它的不同子树作用效果互相抵消,
而要想使其最后停在一个点\(x\)的最大困难就是如何消除重儿子的影响最好办法就是微笑着去面对它
我们要想办法量化这一个过程。
令\(cnt_i\)表示\(i\)子树能自行消化的对数,\(siz_i\)表示\(i\)子树的大小,\(son_i\)表示节点\(i\)的重儿子,那么一个点\(i\)能被消掉当且仅当
\[ \begin{cases} siz_i\bmod 2 = 1\\ siz_i-siz_{son_i}\geq siz_{son_i}-cnt_{siz_{son_i}} \end{cases} \]
这个是怎么来的呢,
就是说当一个重儿子的\(siz\)较小时,因为它的\(siz\)还是较其他儿子最大的,所以总可以通过其他子树来互相消掉;
而一个重儿子的\(siz\)较大时,其他儿子一起搞不过它,那么只有它内部打架别人才有可能打得过,而这显然也有一个限度。
如何求\(cnt\)可以向上面一样思考求出。
因为要求每个点所以要换根,对于一个点\(x\),把根到\(x\)的路径看作一个点就可以了,具体实现细节详见代码。
代码
#include
#include
#include
#include
#include
#include
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 1e5 + 5;
struct Graph { int to, next; } e[MAX_N << 1];
int fir[MAX_N], e_cnt;
void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; }
void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}, fir[u] = e_cnt++; }
int N;
int siz[MAX_N], s1[MAX_N], s2[MAX_N], cnt[MAX_N];
bool ans[MAX_N];
void dfs1(int x, int fa) {
siz[x] = 1, s1[x] = s2[x] = cnt[x] = 0;
for (int i = fir[x]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa) continue;
dfs1(v, x), siz[x] += siz[v];
if (siz[s1[x]] < siz[v]) s2[x] = s1[x], s1[x] = v;
else if (siz[v] > siz[s2[x]]) s2[x] = v;
}
if (siz[s1[x]] - (cnt[s1[x]] << 1) <= siz[x] - siz[s1[x]] - 1) cnt[x] = (siz[x] - 1) / 2;
else cnt[x] = cnt[s1[x]] + siz[x] - siz[s1[x]] - 1;
}
void dfs2(int x, int fa, int scnt, int pcnt, int ssiz) {
//scnt : hson's size
//pcnt : hson's cnt
//ssiz : previous siz
if (!s1[x]) {
if (ssiz >= scnt - (pcnt << 1) && !((ssiz + scnt) & 1)) ans[x] = 1;
else ans[x] = 0;
return ;
}
int sz, ct, tmp = ssiz + siz[x] - siz[s1[x]] - 1;
if (siz[s1[x]] > scnt) sz = siz[s1[x]], ct = cnt[s1[x]], tmp += scnt;
else sz = scnt, ct = pcnt, tmp += siz[s1[x]];
if (tmp >= sz - (ct << 1) && !((sz + tmp) & 1)) ans[x] = 1;
else ans[x] = 0;
for (int i = fir[x]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa) continue;
int tscnt, tpcnt, tssiz = ssiz;
if (v == s1[x]) {
tssiz += siz[x] - siz[v] - 1 - siz[s2[x]];
if (scnt > siz[s2[x]]) tscnt = scnt, tpcnt = pcnt, tssiz += siz[s2[x]];
else tscnt = siz[s2[x]], tpcnt = cnt[s2[x]], tssiz += scnt;
dfs2(v, x, tscnt, tpcnt, tssiz);
} else {
tssiz += siz[x] - siz[v] - 1 - siz[s1[x]];
if (scnt > siz[s1[x]]) tscnt = scnt, tpcnt = pcnt, tssiz += siz[s1[x]];
else tscnt = siz[s1[x]], tpcnt = cnt[s1[x]], tssiz += scnt;
dfs2(v, x, tscnt, tpcnt, tssiz);
}
}
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
int type = gi(), Q = gi();
while (Q--) {
clearGraph();
N = gi();
for (int i = 1; i < N; i++) {
int u = gi(), v = gi();
Add_Edge(u, v), Add_Edge(v, u);
}
dfs1(1, 0);
dfs2(1, 0, 0, 0, 0);
if (type == 3) printf("%d\n", ans[1]);
else {
for (int i = 1; i <= N; i++) putchar((int)ans[i] + '0');
putchar('\n');
}
}
return 0;
}