题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Consider an undirected graph with $N$ nodes labeled $1\dots N$ and $M$ edges ($1\le N\le 2\cdot 10^5, 0\le M\le 4\cdot 10^5$). You're given a binary string $s_1s_2\dots s_N$. At time $t$ for each $t\in [1,N]$,
- If $s_t=0$, node $t$ is removed from the graph.
- If $s_t=1$, node $t$ is removed from the graph, and edges are added between every pair of neighbors that node $t$ had just before removal.
Note that in both cases, when a node is removed from the graph all of its incident edges are removed as well.
Count the number of pairs of nodes that can reach each other via some sequence of edges just before each of timesteps $1\ldots N$.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $N$ and $M$.The second line contains the bit string $s$ of length $N$.
The next $M$ lines each contain two integers denoting an edge of the graph.
OUTPUT FORMAT (print output to the terminal / stdout):
$N$ lines, the number of pairs before each timestep.SAMPLE INPUT:
3 2 111 1 2 1 3
SAMPLE OUTPUT:
3 1 0
Before any removals, all pairs of nodes are reachable from each other. After node $1$ is removed, an edge is added between $2$ and $3$, so they can still reach each other.
SAMPLE INPUT:
3 2 000 1 2 1 3
SAMPLE OUTPUT:
3 0 0
Before any removals, all pairs of nodes are reachable from each other. After node $1$ is removed, $2$ and $3$ can no longer reach each other.
SAMPLE INPUT:
7 8 1101101 6 2 1 2 2 3 6 3 1 3 1 7 4 5 2 7
SAMPLE OUTPUT:
11 7 4 2 1 1 0
SCORING:
- Inputs 4-6: $N\le 100$
- Inputs 7-8: All $s_i$ equal zero.
- Inputs 9-11: All $s_i$ equal one.
- Inputs 12-23: No additional constraints.
Problem credits: Benjamin Qi
结果
Kimi K3.1 ProKimi
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 6.9k平均输出 token(含思考) ↓
- 22.9输出速度 tokens/s ↑
- 291 秒延迟 ↓
- 0.94×CPU 时间 / 官方解 ↓
- 同阶 1、不确定 2时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
2/3
解出的采样 ↑
- 95.7采样 1 · 超时
- 100采样 2 · 解出
- 100采样 3 · 解出
- 74.1k平均输出 token(含思考) ↓
- 191.9输出速度 tokens/s ↑
- 481 秒延迟 ↓
- 2.52×CPU 时间 / 官方解 ↓
- 更差 2、同阶 1时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 28.6k平均输出 token(含思考) ↓
- 142.9输出速度 tokens/s ↑
- 203 秒延迟 ↓
- 1.10×CPU 时间 / 官方解 ↓
- 不确定 3时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; struct DSU { vector<int> parent, sz; vector<int> terminals; explicit DSU(int n) : parent(n + 1), sz(n + 1, 1), terminals(n + 1, 0) { iota(parent.begin(), parent.end(), 0); } int find(int x) { return parent[x] == x ? x : parent[x] = find(parent[x]); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; string s; cin >> s; vector<pair<int, int>> edges(M); vector<vector<int>> adj(N + 1); for (auto &[u, v] : edges) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } DSU dsu(N); vector<char> active(N + 1, false); // Nodes labeled 1 act as connector nodes until their own reverse timestep. for (int i = 1; i <= N; ++i) { if (s[i - 1] == '1') active[i] = true; } long long reachablePairs = 0; auto unite = [&](int a, int b) { a = dsu.find(a); b = dsu.find(b); if (a == b) return; if (dsu.sz[a] < dsu.sz[b]) swap(a, b); reachablePairs += 1LL * dsu.terminals[a] * dsu.terminals[b]; dsu.parent[b] = a; dsu.sz[a] += dsu.sz[b]; dsu.terminals[a] += dsu.terminals[b]; }; for (const auto &[u, v] : edges) { if (active[u] && active[v]) unite(u, v); } vector<long long> answer(N + 1); // Reverse the process. A terminal of weight 1 is a node still present; // connector nodes have weight 0 and do not contribute to the answer. for (int t = N; t >= 1; --t) { if (s[t - 1] == '1') { int r = dsu.find(t); reachablePairs += dsu.terminals[r]; ++dsu.terminals[r]; } else { active[t] = true; dsu.terminals[t] = 1; for (int v : adj[t]) { if (active[v]) unite(t, v); } } answer[t] = reachablePairs; } for (int t = 1; t <= N; ++t) { cout << answer[t] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct DSU { vector<int> parent, sz; vector<long long> active; long long pairs = 0; explicit DSU(int n) : parent(n + 1), sz(n + 1, 1), active(n + 1, 0) { iota(parent.begin(), parent.end(), 0); } int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; pairs += active[a] * active[b]; if (sz[a] < sz[b]) swap(a, b); parent[b] = a; sz[a] += sz[b]; active[a] += active[b]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; string s; cin >> s; vector<pair<int, int>> edges; edges.reserve(M); vector<vector<int>> adj(N + 1); for (int i = 0; i < M; ++i) { int a, b; cin >> a >> b; edges.push_back({a, b}); adj[a].push_back(b); adj[b].push_back(a); } DSU dsu(N); vector<char> present(N + 1, false); // Every node with s_i = 1 is present throughout the reverse process, // initially only as a zero-weight connector. for (int i = 1; i <= N; ++i) { if (s[i - 1] == '1') present[i] = true; } for (auto [a, b] : edges) { if (s[a - 1] == '1' && s[b - 1] == '1') { dsu.unite(a, b); } } vector<long long> answer(N + 1); for (int t = N; t >= 1; --t) { if (s[t - 1] == '1') { int r = dsu.find(t); dsu.pairs += dsu.active[r]; ++dsu.active[r]; } else { present[t] = true; dsu.active[t] = 1; for (int u : adj[t]) { if (present[u]) dsu.unite(t, u); } } answer[t] = dsu.pairs; } for (int t = 1; t <= N; ++t) { cout << answer[t] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct DSU { vector<int> parent, sz, counted; long long pairs = 0; explicit DSU(int n) : parent(n + 1), sz(n + 1, 1), counted(n + 1, 0) { iota(parent.begin(), parent.end(), 0); } int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; pairs += 1LL * counted[a] * counted[b]; if (sz[a] < sz[b]) swap(a, b); parent[b] = a; sz[a] += sz[b]; counted[a] += counted[b]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; string s; cin >> s; vector<pair<int, int>> edges(M); vector<vector<int>> adj(N + 1); for (auto &[u, v] : edges) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } DSU dsu(N); vector<char> active(N + 1, false); // In the reverse process, every node with s_i = 1 is always present. // Initially none of them is counted as an unprocessed node. for (int i = 1; i <= N; ++i) { if (s[i - 1] == '1') active[i] = true; } for (auto [u, v] : edges) { if (active[u] && active[v]) { dsu.unite(u, v); } } vector<long long> answer(N + 1); for (int t = N; t >= 1; --t) { if (s[t - 1] == '1') { // Node t changes from a connector to a counted node. int root = dsu.find(t); dsu.pairs += dsu.counted[root]; ++dsu.counted[root]; } else { // Reinsert node t and all edges to currently present nodes. active[t] = true; dsu.parent[t] = t; dsu.sz[t] = 1; dsu.counted[t] = 1; for (int v : adj[t]) { if (active[v]) { dsu.unite(t, v); } } } answer[t] = dsu.pairs; } for (int t = 1; t <= N; ++t) { cout << answer[t] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int THRESH = 20; struct Hyper { vector<int> mem; vector<int> st; int active_size; bool active; bool large; }; struct Change { int type; // 0 = union, 1 = activate int a, b; int old_cnt; int old_sz; long long delta; }; int N, M, S; vector<vector<pair<int, int>>> seg; vector<int> parent, cnt, sz; vector<Change> history; long long total_pairs = 0; vector<long long> ans; inline void add_interval(int l, int r, int a, int b) { if (l > r) return; if (l < 1) l = 1; if (r > N) r = N; if (l > r) return; l--; r--; l += S; r += S; while (l <= r) { if (l & 1) seg[l++].push_back({a, b}); if (!(r & 1)) seg[r--].push_back({a, b}); l >>= 1; r >>= 1; } } int find_set(int x) { while (parent[x] != x) x = parent[x]; return x; } void unite(int a, int b) { int ra = find_set(a), rb = find_set(b); if (ra == rb) return; if (sz[ra] < sz[rb]) swap(ra, rb); Change c; c.type = 0; c.a = ra; c.b = rb; c.old_cnt = cnt[ra]; c.old_sz = sz[ra]; c.delta = 1LL * cnt[ra] * cnt[rb]; history.push_back(c); total_pairs += c.delta; parent[rb] = ra; cnt[ra] += cnt[rb]; sz[ra] += sz[rb]; } void activate(int v) { int r = find_set(v); Change c; c.type = 1; c.a = r; c.b = 0; c.old_cnt = cnt[r]; c.old_sz = 0; c.delta = cnt[r]; history.push_back(c); total_pairs += c.delta; cnt[r]++; } void rollback(int cp) { while ((int)history.size() > cp) { Change c = history.back(); history.pop_back(); if (c.type == 0) { int ra = c.a, rb = c.b; parent[rb] = rb; cnt[ra] = c.old_cnt; sz[ra] = c.old_sz; total_pairs -= c.delta; } else { int r = c.a; cnt[r] = c.old_cnt; total_pairs -= c.delta; } } } void dfs(int node, int L, int R) { int cp = history.size(); for (auto &e : seg[node]) { if (e.second == 0) activate(e.first); else unite(e.first, e.second); } if (L == R) { if (L < N) ans[L + 1] = total_pairs; } else { int mid = (L + R) >> 1; dfs(node << 1, L, mid); dfs(node << 1 | 1, mid + 1, R); } rollback(cp); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> M; string s; cin >> s; S = 1; while (S < N) S <<= 1; seg.resize(S << 1); vector<vector<int>> adj(N + 1); for (int i = 0; i < M; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); add_interval(1, min(u, v), u, v); } // Vertex activation intervals: vertex v is active for times 1..v for (int v = 1; v <= N; ++v) { add_interval(1, v, v, 0); } vector<Hyper> hs; hs.reserve(N + 5); vector<vector<pair<int, int>>> incident(N + 1); vector<int> visH(N + 5, 0), seenTemp(N + 1, 0); unordered_set<long long> largeSet; largeSet.reserve(100000); auto key = [](int h, int v) -> long long { return ((long long)h << 20) | v; }; auto contains = [&](int h, int v) -> bool { Hyper &H = hs[h]; if (H.large) { return largeSet.find(key(h, v)) != largeSet.end(); } for (int u : H.mem) { if (u == v) return true; } return false; }; auto add_member = [&](int h, int v, int start) { if (contains(h, v)) return; Hyper &H = hs[h]; H.mem.push_back(v); H.st.push_back(start); H.active_size++; incident[v].push_back({h, start}); if (H.large) { largeSet.insert(key(h, v)); } else if ((int)H.mem.size() > THRESH) { H.large = true; for (int u : H.mem) largeSet.insert(key(h, u)); } }; auto destroy = [&](int h) { Hyper &H = hs[h]; if (!H.active) return; H.active = false; if (H.large) { for (int v : H.mem) largeSet.erase(key(h, v)); H.large = false; } vector<int>().swap(H.mem); vector<int>().swap(H.st); H.active_size = 0; }; vector<int> inc; for (int t = 1; t <= N; ++t) { inc.clear(); for (auto &pr : incident[t]) { int h = pr.first; if (h >= (int)hs.size()) continue; if (!hs[h].active) continue; int start = pr.second; add_interval(start, t, N + h + 1, t); hs[h].active_size--; if (visH[h] != t) { visH[h] = t; inc.push_back(h); } } if (s[t - 1] == '0') { for (int h : inc) { if (hs[h].active && hs[h].active_size == 0) { destroy(h); } } } else { int base = -1, maxsz = -1; for (int h : inc) { if (hs[h].active_size > maxsz) { maxsz = hs[h].active_size; base = h; } } for (int h : inc) { if (h == base) continue; Hyper &H = hs[h]; for (size_t i = 0; i < H.mem.size(); ++i) { int u = H.mem[i]; if (u <= t) continue; add_interval(H.st[i], t, N + h + 1, u); add_member(base, u, t + 1); } destroy(h); } if (base != -1) { for (int v : adj[t]) { if (v > t) add_member(base, v, t + 1); } if (hs[base].active && hs[base].active_size == 0) { destroy(base); } } else { vector<int> newMem; for (int v : adj[t]) { if (v > t && seenTemp[v] != t) { seenTemp[v] = t; newMem.push_back(v); } } if ((int)newMem.size() >= 2) { int h = hs.size(); hs.push_back({{}, {}, 0, true, false}); for (int v : newMem) { add_member(h, v, t + 1); } } } } } int totalNodes = N + hs.size() + 5; parent.resize(totalNodes + 1); cnt.assign(totalNodes + 1, 0); sz.assign(totalNodes + 1, 1); for (int i = 1; i <= totalNodes; ++i) parent[i] = i; ans.assign(N + 1, 0); history.reserve(1000000); // Free memory no longer needed vector<vector<int>>().swap(adj); vector<vector<pair<int, int>>>().swap(incident); vector<Hyper>().swap(hs); unordered_set<long long>().swap(largeSet); dfs(1, 0, S - 1); for (int i = 1; i <= N; ++i) { cout << ans[i] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct DSU { vector<int> parent; vector<long long> sz; // number of active vertices in component long long pairs; DSU(int n) : parent(n), sz(n, 0), pairs(0) { iota(parent.begin(), parent.end(), 0); } int find(int x) { return parent[x] == x ? x : parent[x] = find(parent[x]); } void add_vertex(int x) { int r = find(x); pairs -= sz[r] * (sz[r] - 1) / 2; sz[r] += 1; pairs += sz[r] * (sz[r] - 1) / 2; } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; pairs -= sz[a] * (sz[a] - 1) / 2; pairs -= sz[b] * (sz[b] - 1) / 2; if (sz[a] < sz[b]) swap(a, b); parent[b] = a; sz[a] += sz[b]; pairs += sz[a] * (sz[a] - 1) / 2; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; string s; cin >> s; vector<vector<int>> adj(N); for (int i = 0; i < M; ++i) { int u, v; cin >> u >> v; --u; --v; adj[u].push_back(v); adj[v].push_back(u); } vector<char> active_vertex(N, 1); vector<unordered_set<int>> active_members; vector<unordered_set<int>> hist; vector<char> alive; vector<vector<int>> inc_active(N); auto get_new_id = [&]() { int id = active_members.size(); active_members.emplace_back(); hist.emplace_back(); alive.push_back(1); return id; }; for (int t = 0; t < N; ++t) { if (s[t] == '0') { for (int id : inc_active[t]) { if (alive[id]) { active_members[id].erase(t); if (active_members[id].size() < 2) { alive[id] = 0; } } } active_vertex[t] = 0; } else { vector<int> H; for (int id : inc_active[t]) { if (alive[id] && active_members[id].count(t)) { H.push_back(id); } } sort(H.begin(), H.end()); H.erase(unique(H.begin(), H.end()), H.end()); int base = -1; if (!H.empty()) { for (int id : H) { if (base == -1 || active_members[id].size() > active_members[base].size()) { base = id; } } for (int id : H) { if (id == base) continue; // merge hist if (hist[id].size() > hist[base].size()) { hist[base].swap(hist[id]); } for (int v : hist[id]) { hist[base].insert(v); } // merge active for (int v : active_members[id]) { if (v == t) continue; if (active_members[base].insert(v).second) { inc_active[v].push_back(base); hist[base].insert(v); } } alive[id] = 0; active_members[id].clear(); hist[id].clear(); } // remove t from base active active_members[base].erase(t); // add active original neighbors for (int v : adj[t]) { if (active_vertex[v] && v != t) { if (active_members[base].insert(v).second) { inc_active[v].push_back(base); hist[base].insert(v); } } } if (active_members[base].size() < 2) { alive[base] = 0; } } else { unordered_set<int> C; for (int v : adj[t]) { if (active_vertex[v] && v != t) { C.insert(v); } } if (C.size() >= 2) { int id = get_new_id(); active_members[id] = std::move(C); for (int v : active_members[id]) { inc_active[v].push_back(id); hist[id].insert(v); } } } active_vertex[t] = 0; } } int num_ids = active_members.size(); vector<vector<int>> inc_hist(N); for (int id = 0; id < num_ids; ++id) { if (hist[id].size() < 2) continue; for (int v : hist[id]) { inc_hist[v].push_back(N + id); } } int total_nodes = N + num_ids; DSU dsu(total_nodes); vector<long long> ans(N); vector<char> active_rev(N, 0); for (int v = N - 1; v >= 0; --v) { active_rev[v] = 1; dsu.add_vertex(v); for (int u : adj[v]) { if (u > v) { dsu.unite(v, u); } } for (int hub : inc_hist[v]) { dsu.unite(v, hub); } ans[v] = dsu.pairs; } for (int i = 0; i < N; ++i) { cout << ans[i] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct DSU { vector<int> parent, cnt; long long total = 0; DSU(int n) : parent(n + 1), cnt(n + 1, 0) { iota(parent.begin(), parent.end(), 0); } long long C2(long long x) { return x * (x - 1) / 2; } int find(int x) { if (parent[x] != x) parent[x] = find(parent[x]); return parent[x]; } void increment(int x) { int r = find(x); total -= C2(cnt[r]); cnt[r]++; total += C2(cnt[r]); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; total -= C2(cnt[a]); total -= C2(cnt[b]); if (cnt[a] < cnt[b]) swap(a, b); parent[b] = a; cnt[a] += cnt[b]; total += C2(cnt[a]); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; string s; cin >> s; vector<vector<int>> adj(N + 1); vector<pair<int, int>> edges; edges.reserve(M); for (int i = 0; i < M; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); edges.push_back({u, v}); } DSU dsu(N); vector<char> active(N + 1, false); for (int i = 1; i <= N; i++) { if (s[i - 1] == '1') { active[i] = true; } } // Initially, after all removals, all type-1 vertices remain as connectors. for (auto [u, v] : edges) { if (s[u - 1] == '1' && s[v - 1] == '1') { dsu.unite(u, v); } } vector<long long> ans(N + 1); for (int t = N; t >= 1; t--) { if (s[t - 1] == '0') { active[t] = true; } dsu.increment(t); if (s[t - 1] == '0') { for (int v : adj[t]) { if (active[v]) { dsu.unite(t, v); } } } ans[t] = dsu.total; } for (int t = 1; t <= N; t++) { cout << ans[t] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct DSU { vector<int> parent, sz, activeCount; long long pairCount = 0; explicit DSU(int n) : parent(n), sz(n, 1), activeCount(n, 0) { iota(parent.begin(), parent.end(), 0); } int find(int x) { while (parent[x] != x) { parent[x] = parent[parent[x]]; x = parent[x]; } return x; } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; pairCount += 1LL * activeCount[a] * activeCount[b]; if (sz[a] < sz[b]) swap(a, b); parent[b] = a; sz[a] += sz[b]; activeCount[a] += activeCount[b]; } void activate(int x) { int root = find(x); pairCount += activeCount[root]; ++activeCount[root]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; string s; cin >> s; vector<vector<int>> adj(N); vector<char> included(N, false); // All '1' vertices exist from the beginning as zero-weight connectors. for (int i = 0; i < N; ++i) { included[i] = (s[i] == '1'); } DSU dsu(N); for (int i = 0; i < M; ++i) { int u, v; cin >> u >> v; --u; --v; adj[u].push_back(v); adj[v].push_back(u); // Edges between two initially included vertices are immediately present. if (included[u] && included[v]) { dsu.unite(u, v); } } vector<long long> answer(N); for (int t = N - 1; t >= 0; --t) { if (s[t] == '0') { // Restore this deleted vertex and all edges to included vertices. included[t] = true; for (int v : adj[t]) { if (included[v]) { dsu.unite(t, v); } } } // Vertex t is now one of the remaining vertices and is counted. dsu.activate(t); answer[t] = dsu.pairCount; } for (long long value : answer) { cout << value << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct DSU { vector<int> parent, sz; vector<long long> liveCount; long long totalPairs = 0; explicit DSU(int n) : parent(n), sz(n, 1), liveCount(n, 0) { iota(parent.begin(), parent.end(), 0); } int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (sz[a] < sz[b]) swap(a, b); // Every live vertex in one component can now reach every // live vertex in the other component. totalPairs += liveCount[a] * liveCount[b]; parent[b] = a; sz[a] += sz[b]; liveCount[a] += liveCount[b]; } void activate(int x) { int root = find(x); // The new live vertex forms a pair with every existing // live vertex in its component. totalPairs += liveCount[root]; ++liveCount[root]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; string s; cin >> s; vector<vector<int>> adj(N); for (int i = 0; i < M; ++i) { int u, v; cin >> u >> v; --u; --v; adj[u].push_back(v); adj[v].push_back(u); } DSU dsu(N); // "present" refers to membership in the auxiliary graph. // A present vertex is not necessarily live: removed type-1 // vertices remain as uncounted connectors. vector<char> present(N, false); for (int v = 0; v < N; ++v) { if (s[v] == '1') present[v] = true; } // Type-1 vertices are always structurally present. // Initially, unite all edges between them. for (int u = 0; u < N; ++u) { if (!present[u]) continue; for (int v : adj[u]) { if (present[v] && u < v) { dsu.unite(u, v); } } } vector<long long> answer(N); // Reverse time. At time t, the live vertices are exactly t, ..., N. for (int t = N - 1; t >= 0; --t) { if (s[t] == '0') { // A type-0 vertex is genuinely deleted, so in reverse time // it is inserted into the auxiliary graph. present[t] = true; dsu.activate(t); for (int v : adj[t]) { if (present[v]) { dsu.unite(t, v); } } } else { // A type-1 vertex was already structurally present. // It only becomes live now. dsu.activate(t); } answer[t] = dsu.totalPairs; } for (long long value : answer) { cout << value << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct DSU { vector<int> parent, sz, cnt; long long pairs = 0; explicit DSU(int n) : parent(n), sz(n, 1), cnt(n, 0) { iota(parent.begin(), parent.end(), 0); } int find(int x) { if (parent[x] == x) return x; return parent[x] = find(parent[x]); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (sz[a] < sz[b]) swap(a, b); pairs += 1LL * cnt[a] * cnt[b]; parent[b] = a; sz[a] += sz[b]; cnt[a] += cnt[b]; } // Make one previously uncounted vertex counted. void activate(int v) { int r = find(v); pairs += cnt[r]; ++cnt[r]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N, M; cin >> N >> M; string s; cin >> s; vector<vector<int>> adj(N); vector<char> present(N, false); // Type-1 vertices remain in the auxiliary graph as uncounted connectors. for (int i = 0; i < N; ++i) { if (s[i] == '1') present[i] = true; } DSU dsu(N); for (int i = 0; i < M; ++i) { int u, v; cin >> u >> v; --u; --v; adj[u].push_back(v); adj[v].push_back(u); // Both endpoints are present throughout the reverse process. if (s[u] == '1' && s[v] == '1') { dsu.unite(u, v); } } vector<long long> answer(N); // Start from the state after every vertex has been removed. // Type-1 vertices exist as uncounted connectors; type-0 vertices are absent. for (int t = N - 1; t >= 0; --t) { if (present[t]) { // A type-1 vertex was already present, but was uncounted. dsu.activate(t); } else { // Restore a type-0 vertex. dsu.activate(t); present[t] = true; for (int u : adj[t]) { if (present[u]) { dsu.unite(t, u); } } } answer[t] = dsu.pairs; } for (long long x : answer) { cout << x << '\n'; } return 0; }