inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 100007
vector<int> e[N];
int sz[N], mxs[N], cnt[N], co[N], n, mx; // cnt 统计颜色 mx 全局的众数出现的次数 ll ans[N], res; // res 众数和
voiddfs(int u, int fa){ sz[u] = 1; for (auto v : e[u]) { if (v != fa) { dfs(v, u); sz[u] += sz[v]; if (sz[v] > sz[mxs[u]]) mxs[u] = v; } } }
voidadd(int u, int fa){ cnt[co[u]]++; if (cnt[co[u]] == mx) res += co[u]; elseif (cnt[co[u]] > mx) {res = co[u]; mx = cnt[co[u]];} for (auto v : e[u]) { if (v != fa) add(v, u); } }
voiddel(int u, int fa){ cnt[co[u]] = 0; for (auto v : e[u]) { if (v != fa) del(v, u); } }
voiddfs1(int u, int fa){ for (auto v : e[u]) { if (v != fa && v != mxs[u]) {dfs1(v, u); del(v, u); mx = res = 0;} } if (mxs[u]) dfs1(mxs[u], u); for (auto v : e[u]) if (v != fa && v != mxs[u]) add(v, u); cnt[co[u]]++; if (cnt[co[u]] == mx) res += co[u]; elseif (cnt[co[u]] > mx) {res = co[u]; mx = cnt[co[u]];} ans[u] = res; }
intmain(){ n = rd(); for (int i = 1; i <= n; ++i) co[i] = rd(); for (int i = 1; i < n; ++i) { int u = rd(), v = rd(); e[u].pb(v); e[v].pb(u); } dfs(1, 1); dfs1(1, 1); for (int i = 1; i <= n; ++i) printf("%lld ", ans[i]); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 300007 #define mod 1000000007
int c[N], n;
inlineintlowbit(int x){return x & -x;}
inlinevoidadd(int p){ for (; p <= n; p += lowbit(p)) c[p]++; }
inlineintsum(int p){ int tmp = 0; for (; p; p -= lowbit(p)) tmp += c[p]; return tmp; }
voiddfs(int u, int fa){ sz[u] = 1; for (auto v : e[u]) { if (v != fa) { dfs(v, u); sz[u] += sz[v]; if (sz[v] > sz[mxs[u]]) mxs[u] = v; } } cnt = (cnt + sz[u] - 1) % mod; dfscnt = (1ll * dfscnt * f[e[u].size() - (u != r)]) % mod; }
voidadd(int u, int fa){ add(u); for (auto v : e[u]) { if (v != fa) add(v, u); } }
voidclear(int p){ for (; p <= n; p += lowbit(p)) c[p] = 0; }
voiddel(int u, int fa){ clear(u); for (auto v : e[u]) { if (v != fa) del(v, u); } }
voiddfs1(int u, int fa){ for (auto v : e[u]) { if (v != fa && v != mxs[u]) {dfs1(v, u); del(v, u);} } if (mxs[u]) dfs1(mxs[u], u); for (auto v : e[u]) if (v != fa && v != mxs[u]) add(v, u); res = (res + sum(u)) % mod; add(u); }
inline ll qpow(ll x, int p){ ll res = 1; for (; p; p >>= 1, x = 1ll * x * x % mod) if (p & 1) res = 1ll * res * x % mod; return res; }
intmain(){ n = rd(); r = rd(); f[0] = 1; for (int i = 1; i <= n; ++i) f[i] = (1ll * f[i - 1] * i) % mod; for (int i = 1; i < n; ++i) { int u = rd(), v = rd(); e[u].pb(v); e[v].pb(u); } dfs(r, r); dfs1(r, r); printf("%lld\n", ((res + qpow(2, mod - 2) * ((1ll * n * (n - 1) / 2 - cnt) % mod) % mod) * dfscnt) % mod); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 500007
vector<int> e[N]; int n;
int sz[N], mxs[N], dep[N];
voiddfs1(int u){ sz[u] = 1; for (auto v : e[u]) { dep[v] = dep[u] + 1; dfs1(v); sz[u] += sz[v]; if (sz[v] > sz[mxs[u]]) mxs[u] = v; } }
structque {int k, id;};
vector<que> q[N];
int cnt[N][27]; bool ans[N];
string s;
voidadd(int u){ cnt[dep[u]][s[u - 1] - 'a']++; for (auto v : e[u]) add(v); }
voiddel(int u){ cnt[dep[u]][s[u - 1] - 'a']--; for (auto v : e[u]) del(v); }
voiddfs2(int u){ for (auto v : e[u]) { if (v != mxs[u]) {dfs2(v); del(v);} } if (mxs[u]) dfs2(mxs[u]); for (auto v : e[u]) if (v != mxs[u]) add(v); cnt[dep[u]][s[u - 1] - 'a']++; for (auto cur : q[u]) { int tmp = 0; for (int i = 0; i < 26; ++i) if (cnt[cur.k][i] % 2 == 1) tmp++; if (tmp <= 1) ans[cur.id] = 1; } }
intmain(){ n = rd(); int m = rd(); for (int i = 2; i <= n; ++i) { int u = rd(); e[u].pb(i); } cin >> s; for (int i = 1 ; i <= m; ++i) { int u = rd(), k = rd(); q[u].pb(que{k, i}); } dep[1] = 1; dfs1(1); dfs2(1); for (int i = 1; i <= m; ++i) puts(ans[i] ? "Yes" : "No"); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 100007
vector<int> e[N]; int n, fa[N][21], t, dep[N];
voiddfs(int u){ for (int i = 1; i <= t; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1]; for (auto v : e[u]) { fa[v][0] = u; dep[v] = dep[u] + 1; dfs(v); } }
int sz[N], mxs[N];
voiddfs1(int u){ sz[u] = 1; for (auto v : e[u]) { dfs1(v); sz[u] += sz[v]; if (sz[v] > sz[mxs[u]]) mxs[u] = v; } }
structque {int k, u, id;};
vector<que> q[N];
int cnt[N], ans[N];
voidadd(int u, int fa){ cnt[dep[u]]++; for (auto v : e[u]) if (v != fa) add(v, u); }
voiddel(int u, int fa){ cnt[dep[u]]--; for (auto v : e[u]) if (v != fa) del(v, u); }
voiddfs2(int u){ for (auto v : e[u]) { if (v != mxs[u]) {dfs2(v); del(v, u);} } if (mxs[u]) dfs2(mxs[u]); for (auto v : e[u]) if (v != mxs[u]) add(v, u); for (auto cur : q[u]) ans[cur.id] = cnt[dep[u] + cur.k] - 1; cnt[dep[u]]++; }
intmain(){ n = rd(); t = __lg(n); for (int i = 1; i <= n; ++i) { fa[i + 1][0] = rd() + 1; e[fa[i + 1][0]].pb(i + 1); } fa[1][0] = 1; dfs(1); for (int i = 1; i <= t; ++i) for (int j = 1; j <= n + 1; ++j) { fa[j][i] = fa[fa[j][i - 1]][i - 1]; } int m = rd(); for (int i = 1 ; i <= m; ++i) { int v = rd() + 1, k = rd(); if (dep[v] <= k) continue; int u = v; for (int j = t; j >= 0; --j) if (k & (1 << j)) u = fa[u][j]; q[u].pb(que{k, u, i}); } dfs1(1); dfs2(1); for (int i = 1; i <= m; ++i) printf("%d ", ans[i]); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 100007
vector<int> e[N]; int n;
int sz[N], mxs[N], dep[N];
voiddfs1(int u){ sz[u] = 1; for (auto v : e[u]) { dep[v] = dep[u] + 1; dfs1(v); sz[u] += sz[v]; if (sz[v] > sz[mxs[u]]) mxs[u] = v; } }
structque {int k, id;};
vector<que> q[N];
int cnt[N][27], ans[N];
char name[N][21]; map<string, int> S[N];
voidadd(int u){ S[dep[u]][name[u]]++; for (auto v : e[u]) add(v); }
voiddel(int u){ S[dep[u]][name[u]]--; if (S[dep[u]][name[u]] == 0) S[dep[u]].erase(name[u]); for (auto v : e[u]) del(v); }
voiddfs2(int u){ for (auto v : e[u]) { if (v != mxs[u]) {dfs2(v); del(v);} } if (mxs[u]) dfs2(mxs[u]); for (auto v : e[u]) if (v != mxs[u]) add(v); S[dep[u]][name[u]]++; for (auto cur : q[u]) { if (dep[u] + cur.k > n) continue; ans[cur.id] = S[dep[u] + cur.k].size(); } }
intmain(){ n = rd(); for (int i = 1; i <= n; ++i) { cin >> name[i]; int u = rd(); if (u) e[u].pb(i); } int m = rd(); for (int i = 1 ; i <= m; ++i) { int u = rd(), k = rd(); q[u].pb(que{k, i}); } for (int i = 1; i <= n; ++i) if (!dep[i]) { dep[i] = 1; dfs1(i); dfs2(i); del(i); } for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 1000007
vector<int> e[N]; int n;
int sz[N], mxs[N], dep[N];
voiddfs1(int u, int fa){ sz[u] = 1; for (auto v : e[u]) { if (v != fa) { dep[v] = dep[u] + 1; dfs1(v, u); sz[u] += sz[v]; if (sz[v] > sz[mxs[u]]) mxs[u] = v; } } }
int cnt[N], ans[N], mx, mxid;
voidadd(int u, int fa){ cnt[dep[u]]++; if (cnt[dep[u]] > mx) {mxid = dep[u]; mx = cnt[dep[u]];} elseif (cnt[dep[u]] == mx) mxid = min(mxid, dep[u]); for (auto v : e[u]) if (v != fa) add(v, u); }
voiddel(int u, int fa){ cnt[dep[u]]--; for (auto v : e[u]) if (v != fa) del(v, u); }
voiddfs2(int u, int fa){ for (auto v : e[u]) { if (v != fa && v != mxs[u]) {dfs2(v, u); del(v, u); mx = mxid = 0;} } if (mxs[u]) dfs2(mxs[u], u); for (auto v : e[u]) if (v != fa && v != mxs[u]) add(v, u); cnt[dep[u]]++; if (cnt[dep[u]] > mx) {mxid = dep[u]; mx = cnt[dep[u]];} elseif (cnt[dep[u]] == mx) mxid = min(mxid, dep[u]); ans[u] = mxid - dep[u]; }
intmain(){ n = rd(); for (int i = 1; i < n; ++i) { int u = rd(), v = rd(); e[u].pb(v); e[v].pb(u); } dep[1] = 1; dfs1(1, 1); dfs2(1, 1); for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]); return0; }
Post title:dsu on tree
Post author:Eva.Q
Create time:2022-08-30 16:07:16
Post link:https://qyy/2022/08/30/Algorithm/dsu-on-tree/
Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.