P2860
for (int i = 1; i <= m; i++) {
int x = U[i], y = V[i];
if (bel[x] != bel[y]) {
t[bel[x]].push_back(bel[y]);
t[bel[y]].push_back(bel[x]);
deg[bel[x]]++, deg[bel[y]]++;
}
}
int res = 0;
for (int i = 1; i <= idx; i++) res += (deg[i] == 1);
cout << (res + 1) / 2;P2783
P4652
注意原图不一定连通。
for (int i = 1; i <= m; i++) {
int x = bel[u[i]], y = bel[v[i]];
if (x != y) {
G[x].push_back(y);
G[y].push_back(x);
} else ans[i] = 'B';
}
for (int i = 1; i <= idx; i++)
if (!siz[i]) dfs1(i, 0), dfs2(i, i);
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
x = bel[x], y = bel[y];
int anc = LCA(x, y);
diff1[x]++, diff1[anc]--;
diff2[y]++, diff2[anc]--;
}
for (int i = 1; i <= idx; i++)
if (!fa[i]) get_ans(i, 0);
for (int i = 1; i <= m; i++) {
int x = bel[u[i]], y = bel[v[i]];
if (ans[i]) cout << ans[i];
else {
if (fa[x] == y) cout << ((diff1[x] > 0 && diff2[x] > 0) ? 'B' : ((diff1[x] > 0 && diff2[x] == 0) ? 'R' : ((diff2[x] > 0 && diff1[x] == 0) ? 'L' : 'B')));
else cout << ((diff1[y] > 0 && diff2[y] > 0) ? 'B' : ((diff1[y] > 0 && diff2[y] == 0) ? 'L' : ((diff2[y] > 0 && diff1[y] == 0) ? 'R' : 'B')));
}
}P8867
void dfs1(int u, int fa) {
s[u] = E[u];
for (auto v : G[u]) {
if (v == fa) continue;
dfs1(v, u);
s[u] += s[v] + 1;
}
}
void dfs(int u, int fa) {
dp[u][0] = qpow(2, E[u]);
dp[u][1] = (qpow(2, E[u] + V[u]) - qpow(2, E[u]) dsa szh) % szh;
for (auto v : G[u]) {
if (v == fa) continue;
dfs(v, u);
dp[u][1] = (dp[u][0] * dp[v][1] % szh + dp[u][1] * (2 * dp[v][0] + dp[v][1]) % szh) % szh;
dp[u][0] = dp[u][0] * 2 * dp[v][0] % szh;
}
if (u == 1) ans = (ans + dp[u][1]) % szh;
else ans = (ans + qpow(2, s[1] - s[u] - 1) * dp[u][1] % szh) % szh;
}P2542
while (cin >> op[++totot]) {
if (op[totot] == -1) break;
cin >> U[totot] >> V[totot];
if (op[totot] == 0) {
mp[{U[totot], V[totot]}]++;
mp[{V[totot], U[totot]}]++;
}
}
int now = 0;
for (int i = 1; i <= m; i++) {
if (mp.find({u[i], v[i]}) != mp.end() && mp[{u[i], v[i]}] > 0) {
mp[{u[i], v[i]}]--;
mp[{v[i], u[i]}]--;
} else {
vis[i] = 1;
p[u[i]].push_back({v[i], i * 2});
p[v[i]].push_back({u[i], i * 2 + 1});
}
}
for (int i = 1; i <= n; i++)
if (!Dfn[i]) dfs(i, 0);
for (int i = 1; i <= m; i++) {
if (!vis[i]) continue;
int x = bel[u[i]], y = bel[v[i]];
if (x != y) {
G[x].push_back({y, 1});
G[y].push_back({x, 1});
}
}
dfs1(bel[1], 0);
dfs2(bel[1], bel[1]);
build(1, 1, idx);
for (int i = totot; i >= 1; i--) {
if (op[i] == -1) continue;
if (op[i] == 1) ans[i] = qry_sum(bel[U[i]], bel[V[i]]);
else upd(bel[U[i]], bel[V[i]], 0);
}
for (int i = 1; i <= totot; i++)
if (op[i] == 1) cout << ans[i] << '\n';P3225
P4320
P4606
while (q--) {
int len;
cin >> len;
for (int i = 1; i <= len; i++) cin >> S[i];
sort(S + 1, S + len + 1, cmp);
int ans = dist(S[1], S[len]);
for (int i = 1; i < len; i++) ans += dist(S[i], S[i + 1]);
ans >>= 1;
cout << (ans + (LCA(S[1], S[len]) <= n)) - len << '\n';
}P4334
bool solve(int u, int v, int w) {
if (dist(u, w) + dist(w, v) == dist(u, v)) return 1;
else return 0;
}
while (q--) {
int op;
cin >> op;
if (op == 1) {
int a, b, g1, g2;
cin >> a >> b >> g1 >> g2;
if (!vis[{g1, g2}]) {
cout << "yes\n";
continue;
}
if (solve(a, g2, g1) == solve(b, g2, g1)) cout << "yes\n";
else cout << "no\n";
} else {
int a, b, c;
cin >> a >> b >> c;
if (solve(a, b, c)) cout << "no\n";
else cout << "yes\n";
}
}P4630
void dfs1(int u, int fa) {
siz[u] = (u <= n);
for (auto v : g[u]) {
if (v == fa) continue;
dfs1(v, u);
ans += 2 * val[u] * siz[u] * siz[v];
siz[u] += siz[v];
}
ans += 2 * val[u] * siz[u] * (nnn - siz[u]);
}