题目 › usaco-1594

Picking Flowers

USACO 2026 Third · 金组 · 2026-02 · L3 困难 · C++17 · 时限 3 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

**Note: The time limit for this problem is 3s, 1.5x the default.**

Farmer John's farm structure can be represented as a connected undirected graph with $N$ vertices and $M$ unweighted edges ($2 \leq N \leq 2 \cdot 10^5, N - 1 \leq M \leq 2 \cdot 10^5$). Initially, Farmer John is at his barn, represented by farm $1$.

Initially, farms $s_1, s_2, \ldots, s_K$ contain flower fields and farms $d_1, d_2, \ldots, d_L$ are destination farms. FJ calls a path pretty if:

  • It starts at farm $1$.
  • It ends at some destination farm $x$.
  • There is no shorter path starting at farm $1$ and ending at farm $x$.
  • FJ visits all flower fields along the way.

FJ can wave his magic wand and make up to one more farm contain a flower field (if it doesn't already). However, FJ isn't very decisive. For farms $f$ numbered $2$ through $N$, after FJ temporarily makes farm $f$ contain a flower field, determine if there exists a pretty path.

Note that there are multiple test cases, and each case must be treated independently.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $T$ ($1 \leq T \leq 100$), the number of independent test cases.

The first line of each test case contains $N$, $M$, $K$, and $L$ ($0 \leq K \leq N - 1, 1 \leq L \leq N - 1$).

The following line contains $s_1, s_2, \ldots, s_K$ ($2 \leq s_i \leq N$, $s_i$ are all distinct).

The following line contains $d_1, d_2, \ldots, d_L$ ($2 \leq d_i \leq N$, $d_i$ are all distinct).

The following $M$ lines contain $u$ and $v$, denoting there is an undirected edge between farms $u$ and $v$. All edges are considered to have equal length. It is guaranteed that there aren't any multi-edges or self loops.

It is guaranteed that both the sum of $N$ and the sum of $M$ do not exceed $10^6$ over all test cases.

OUTPUT FORMAT (print output to the terminal / stdout):

For each test case, output a binary string of length $N - 1$. The $i$'th character in the string should be $1$ if the answer holds true for the $(i+1)$'th farm.

SAMPLE INPUT:

1
7 7 0 1

5
1 2
2 3
3 4
4 5
5 6
6 7
3 6

SAMPLE OUTPUT:

111110
Since $5$ is the only destination farm, the answer holds true if the $i$'th farm lies on any shortest path from $1$ to $5$.

There are two shortest paths from $1$ to $5$, which are $1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5$ and $1 \rightarrow 2 \rightarrow 3 \rightarrow 6 \rightarrow 5$.

Since there are no farms that already contain flower fields, the answer for farm $i$ holds true if farm $i$ lies on at least one of the two aforementioned paths.

SAMPLE INPUT:

1
6 6 0 2

5 3
1 2
2 3
3 4
4 5
5 6
2 5

SAMPLE OUTPUT:

11010

There are two destination farms: $5$ and $3$. Since there are no farms that already contain flower fields, the $i$'th farm must lie on a shortest path to either $5$ or $3$. Since farm $2$ lies on a shortest path to farm $5$, so the answer holds for farm $2$. Trivially, farm $3$ lies on the shortest path to farm $3$ and farm $5$ lies on the shortest path to farm $5$.

SAMPLE INPUT:

3
4 3 2 1
2 3
4
1 2
2 3
3 4
4 4 2 1
2 3
4
1 2
1 3
2 4
3 4
5 5 2 1
2 4
5
1 2
1 3
2 4
3 4
4 5

SAMPLE OUTPUT:

111
000
1011

For the first test case, the answer holds true for the $i$'th farm if FJ can pass through farm $i$, farm $2$, and farm $3$ (in no particular order) on some shortest path to farm $4$. It can be shown that the answer holds true for all farms.

SCORING:

  • Inputs 4-6: $K = 0$ and $L = 1$
  • Inputs 7-9: $K = 0$
  • Inputs 10-23: No additional constraints

Problem credits: Chongtian Ma

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 7.8k平均输出 token(含思考) ↓
  • 24.7输出速度 tokens/s ↑
  • 298 秒延迟 ↓
  • 1.22×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 29.1k平均输出 token(含思考) ↓
  • 197.2输出速度 tokens/s ↑
  • 151 秒延迟 ↓
  • 0.93×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

2/3

解出的采样 ↑

  • 87.0采样 1 · 答案错误
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 35.9k平均输出 token(含思考) ↓
  • 406.1输出速度 tokens/s ↑
  • 106 秒延迟 ↓
  • 0.48×CPU 时间 / 官方解 ↓
  • 同阶 1、更优 1时间复杂度(相对官方解)

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 194 行
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--) {
        int N, M, K, L;
        cin >> N >> M >> K >> L;

        vector<int> flowers(K);
        for (int &x : flowers) cin >> x;

        vector<char> isDestination(N + 1, false);
        for (int i = 0; i < L; ++i) {
            int x;
            cin >> x;
            isDestination[x] = true;
        }

        vector<vector<int>> adj(N + 1);
        vector<pair<int, int>> edges(M);
        for (int i = 0; i < M; ++i) {
            int u, v;
            cin >> u >> v;
            edges[i] = {u, v};
            adj[u].push_back(v);
            adj[v].push_back(u);
        }

        vector<int> dist(N + 1, -1);
        queue<int> q;
        dist[1] = 0;
        q.push(1);

        int maxDist = 0;
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            maxDist = max(maxDist, dist[u]);

            for (int v : adj[u]) {
                if (dist[v] == -1) {
                    dist[v] = dist[u] + 1;
                    q.push(v);
                }
            }
        }

        vector<vector<int>> layers(maxDist + 1);
        for (int v = 1; v <= N; ++v) {
            layers[dist[v]].push_back(v);
        }

        // Directed acyclic graph containing exactly the edges that can be
        // used by shortest paths starting at farm 1.
        vector<vector<int>> dag(N + 1);
        for (auto [u, v] : edges) {
            if (dist[u] + 1 == dist[v]) {
                dag[u].push_back(v);
            } else if (dist[v] + 1 == dist[u]) {
                dag[v].push_back(u);
            }
        }

        sort(flowers.begin(), flowers.end(), [&](int a, int b) {
            return dist[a] < dist[b];
        });

        bool possible = true;
        for (int i = 1; i < K; ++i) {
            if (dist[flowers[i]] == dist[flowers[i - 1]]) {
                possible = false;
                break;
            }
        }

        vector<char> answer(N + 1, false);

        if (possible) {
            vector<int> chain;
            chain.reserve(K + 1);
            chain.push_back(1);
            for (int x : flowers) chain.push_back(x);

            vector<char> forward(N + 1, false);
            vector<char> backward(N + 1, false);

            // Process the portions between consecutive required flowers.
            // Their distance-layer intervals are disjoint, so altogether
            // these loops take O(N + M).
            for (int i = 0; i + 1 < (int)chain.size() && possible; ++i) {
                int a = chain[i];
                int b = chain[i + 1];
                int da = dist[a];
                int db = dist[b];

                forward[a] = true;
                for (int d = da; d < db; ++d) {
                    for (int u : layers[d]) {
                        if (!forward[u]) continue;
                        for (int v : dag[u]) {
                            forward[v] = true;
                        }
                    }
                }

                if (!forward[b]) {
                    possible = false;
                    break;
                }

                for (int d = db; d >= da; --d) {
                    for (int u : layers[d]) {
                        bool canReach = (u == b);

                        if (d < db) {
                            for (int v : dag[u]) {
                                if (backward[v]) {
                                    canReach = true;
                                    break;
                                }
                            }
                        }

                        backward[u] = canReach;
                        if (canReach && forward[u]) {
                            answer[u] = true;
                        }
                    }
                }

                for (int d = da; d <= db; ++d) {
                    for (int u : layers[d]) {
                        forward[u] = false;
                        backward[u] = false;
                    }
                }
            }

            if (possible) {
                // Final portion: from the last required flower (or farm 1)
                // to any reachable destination.
                int a = chain.back();
                int da = dist[a];

                forward[a] = true;
                for (int d = da; d < maxDist; ++d) {
                    for (int u : layers[d]) {
                        if (!forward[u]) continue;
                        for (int v : dag[u]) {
                            forward[v] = true;
                        }
                    }
                }

                for (int d = maxDist; d >= da; --d) {
                    for (int u : layers[d]) {
                        bool canReach = isDestination[u] && forward[u];

                        for (int v : dag[u]) {
                            if (backward[v]) {
                                canReach = true;
                                break;
                            }
                        }

                        backward[u] = canReach;
                        if (canReach && forward[u]) {
                            answer[u] = true;
                        }
                    }
                }

                possible = backward[a];
            }
        }

        string result(N - 1, '0');
        if (possible) {
            for (int v = 2; v <= N; ++v) {
                if (answer[v]) result[v - 2] = '1';
            }
        }

        cout << result << '\n';
    }

    return 0;
}