版本比较
P3233 [HNOI2014] 世界树
正文
增加 9 行删除 9 行
24 行未改动2526272829293031323331 行未改动656667686969707172737475767777787980818283848585868788899091929393949595969798999910010110210317 行未改动1211221231241251251261271281295 行未改动1351361371381391391401411421435 行未改动24 行未改动#define il inlineusing namespace std;int n, Q, m;int father[N], dfn[N], siz[N], cnt, son[N], dep[N], top[N], fa[N][20];vector<int>p[N], G[N]; //p是原来的树,G是虚树 vector<int>p[N], G[N]; //p是原来的树,G是虚树il void dfs(int x, int Fa) { father[x] = Fa; fa[x][0] = Fa; siz[x] = 1;31 行未改动bool vis[N]; //是否为议事处il bool cmp(int a, int b) { return dfn[a] < dfn[b];}il void dp1(int x) { //求出在x子树内最近的议事处 il void dp1(int x) { //求出在x子树内最近的议事处 if(vis[x]) f[x] = 0, g[x] = x; for(auto y : G[x]) { dp1(y); if(f[y] + (dep[y] - dep[x]) < f[x]) f[x] = f[y] + (dep[y] - dep[x]), g[x] = g[y]; else if(f[y] + (dep[y] - dep[x]) == f[x]) g[x] = min(g[x], g[y]); }}il void dp2(int x) { //求出在x子树外最近的议事处 il void dp2(int x) { //求出在x子树外最近的议事处 for(auto y : G[x]) { if(f[x] + (dep[y] - dep[x]) < f[y]) f[y] = f[x] + (dep[y] - dep[x]), g[y] = g[x]; else if(f[x] + (dep[y] - dep[x]) == f[y]) g[y] = min(g[y], g[x]); dp2(y); }}il int Get_kth(int x, int k) { for(int i = 18; i >= 0; i--) for(int i = 18; i >= 0; i--) if(k >= (1 << i)) x = fa[x][i], k -= (1 << i); return x;}il void Get(int x) { int res = siz[x] - 1; for(auto y : G[x]) { int d = dep[y] - dep[x]; if(d - 1 > 0) { //x,y之间有点 if(d - 1 > 0) { //x,y之间有点 int X = (f[x] + d - f[y]) / 2; X = max(X, 0); X = max(X, 0); if(X > 0 && f[y] + X == f[x] + d - X && g[x] < g[y]) X--; if(X == d) X--; if(X > 0) ans[g[y]] += siz[Get_kth(y, X)] - siz[y]; if(d - 1 - X > 0) ans[g[x]] += siz[Get_kth(y, d - 1)] - siz[Get_kth(y, X)]; if(d - 1 - X > 0) ans[g[x]] += siz[Get_kth(y, d - 1)] - siz[Get_kth(y, X)]; } int num = Get_kth(y, d - 1); res -= siz[num]; Get(y);17 行未改动 cin >> Q; while(Q--) { cin >> m; for(int i = 1, x; i <= m; i++) { cin >> h[i]; cin >> h[i]; uu[i] = h[i]; vis[h[i]] = 1; ans[h[i]] = 0; }5 行未改动 sort(z + 1, z + tot + 1, cmp); tot = unique(z + 1, z + tot + 1) - z - 1; for(int i = 1; i <= tot; i++) G[z[i]].clear(), f[z[i]] = g[z[i]] = 1e9; for(int i = 1; i < tot; i++) G[lca(z[i], z[i + 1])].push_back(z[i + 1]); dp1(1); dp2(1); dp1(1); dp2(1); for(int i = 1; i <= tot; i++) ans[g[z[i]]]++; Get(1); for(int i = 1; i <= m - 1; i++) cout << ans[uu[i]] << " ", vis[uu[i]] = 0; cout << endl;5 行未改动