给定树和一些树上有向边,每次删去叶子,询问每个结点能否最后一个删去
加入一条边 (u,v) 可以发现以v为根时u的子树都没法最后删,借鉴这道题思路
注意判无解情况,可以对于一个方向的加边(钦定为从上往下)则在新图上连接整条路径,反向加边时(从下往上)就找到比较短的能代表这条路径的边(长度为1)加边,最后判是否有环即可
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 1e5 + 10;
int n, m, dfn[N], idx[N], sz[N];
int fa[N][20], d[N];
vector<int> G[N], g[N];
bool ans[N];
void dfs(int u, int f = 0) {
fa[u][0] = f; d[u] = d[f] + 1;
for(int i = 1; i < 20; i ++)
fa[u][i] = fa[fa[u][i - 1]][i - 1];
dfn[u] = ++ dfn[0]; idx[dfn[u]] = u; sz[u] = 1;
for(int i = 0; i < G[u].size(); i ++) if(G[u][i] != f)
dfs(G[u][i], u), sz[u] += sz[G[u][i]];
}
int c[N];
inline void cover(int l, int r) {
if(l <= r) c[l] ++, c[r + 1] --;
}
void kthfa(int & u, int k) {
if(k > 0) for(int i = 0; i < 20; i ++)
if(k >> i & 1) u = fa[u][i];
}
bool vis[N];
void fixdir(int u, int f) {
for(int & v : G[u]) if(v != f) {
if(!vis[v]) {
g[v].push_back(u);
vis[v] = 1; fixdir(v, u);
}
}
}
bool vis2[N], ins[N];
bool cir(int u) {
if(ins[u]) return 1;
if(vis2[u]) return 0;
vis2[u] = ins[u] = 1;
for(const int & v : g[u])
if(cir(v)) return 1;
return ins[u] = 0;
}
int main() {
scanf("%d%d", &n, &m);
int u, v;
for(int i = 1; i < n; i ++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1);
for(int i = 1; i <= m; i ++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
int l = dfn[u], r = l + sz[u] - 1;
if(dfn[v] < l || dfn[v] > r) {
cover(l, r), fixdir(u, fa[u][0]);
} else {
kthfa(v, d[v] - d[u] - 1);
int l2 = dfn[v], r2 = l2 + sz[v] - 1;
cover(1, l2 - 1), cover(r2 + 1, n);
for(const int & j : G[v]) if(!vis[j]) {
if(dfn[j] < l2 || dfn[j] > r2) {
vis[j] = 1;
fixdir(j, v);
}
}
}
}
bool tag = true;
for(int i = 1; i <= n; i ++) if(!vis2[i])
if(cir(i)) tag = false, i = n;
if(!tag) {
for(int i = 1; i <= n; i ++) puts("0");
return 0;
}
int val = 0;
for(int i = 1; i <= n; i ++)
val += c[i], ans[idx[i]] = (val > 0) ? 0 : 1;
for(int i = 1; i <= n; i ++)
printf("%d\n", ans[i]);
return 0;
}