# 【ZJOI2012】灾难 - LCA+拓扑排序

## 思路

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int n,deg[maxn],dep[maxn],father[maxn][25],size[maxn];
vector<int> edge1[maxn],edge2[maxn],tree[maxn];
queue<int> Q;
inline int lca(int a,int b) {
if (dep[a] < dep[b]) swap(a,b);
for (int i = 20;i >= 0;i--)
if (dep[father[a][i]] >= dep[b]) a = father[a][i];
if (a == b) return a;
for (int i = 20;i >= 0;i--)
if (father[a][i] != father[b][i]) {
a = father[a][i];
b = father[b][i];
}
return father[a][0];
}
inline void dfs(int now,int fa) {
size[now] = 1;
for (size_t i = 0;i < tree[now].size();i++)
if (tree[now][i] != fa) {
dfs(tree[now][i],now);
size[now] += size[tree[now][i]];
}
}
int main() {
scanf("%d",&n);
for (int i = 1,x;i <= n;i++)
for (scanf("%d",&x);x;scanf("%d",&x)) {
edge1[x].push_back(i);
edge2[i].push_back(x);
deg[i]++;

}
dep[n+1] = 1;
for (int i = 1;i <= n;i++)
if (!deg[i]) {
Q.push(i);
edge2[i].push_back(n+1);
}
while (Q.size()) {
int now = Q.front(),LCA = edge2[now][0]; Q.pop();
for (size_t i = 1;i < edge2[now].size();i++) LCA = lca(LCA,edge2[now][i]);
dep[now] = dep[LCA]+1;
father[now][0] = LCA;
tree[LCA].push_back(now);
for (int i = 1;i <= 20;i++) father[now][i] = father[father[now][i-1]][i-1];
for (size_t i = 0;i < edge1[now].size();i++) {
deg[edge1[now][i]]--;
if (!deg[edge1[now][i]]) Q.push(edge1[now][i]);
}
}
dfs(n+1,0);
for (int i = 1;i <= n;i++) printf("%d\n",size[i]-1);
return 0;
}
posted @ 2018-06-10 11:47 lrj124 阅读(...) 评论(...) 编辑 收藏