题目 › usaco-1474

Reachable Pairs

USACO 2025 January · 金组 · 2025-01 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

来源: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

auto(max)

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

auto(high)

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

auto(max)

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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 89 行
#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;
}