题面
来源: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:
111110Since $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
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
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 29.1k平均输出 token(含思考) ↓
- 197.2输出速度 tokens/s ↑
- 151 秒延迟 ↓
- 0.93×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#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; }
#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), destinations(L); for (int &x : flowers) cin >> x; for (int &x : destinations) cin >> x; 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); while (!q.empty()) { int v = q.front(); q.pop(); for (int u : adj[v]) { if (dist[u] == -1) { dist[u] = dist[v] + 1; q.push(u); } } } int maxDist = 0; vector<vector<int>> layers(N + 1); for (int v = 1; v <= N; v++) { maxDist = max(maxDist, dist[v]); layers[dist[v]].push_back(v); } 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; } } if (!possible) { cout << string(N - 1, '0') << '\n'; continue; } vector<vector<int>> out(N + 1), in(N + 1); for (auto [u, v] : edges) { if (dist[u] + 1 == dist[v]) { out[u].push_back(v); in[v].push_back(u); } else if (dist[v] + 1 == dist[u]) { out[v].push_back(u); in[u].push_back(v); } } vector<unsigned char> forward(N + 1, 0); vector<unsigned char> backward(N + 1, 0); vector<unsigned char> good(N + 1, 0); auto processBounded = [&](int start, int finish) -> bool { int lo = dist[start], hi = dist[finish]; for (int v : layers[lo]) forward[v] = 0; forward[start] = 1; for (int d = lo + 1; d <= hi; d++) { for (int v : layers[d]) { bool reachable = false; for (int u : in[v]) { if (forward[u]) { reachable = true; break; } } forward[v] = reachable; } } if (!forward[finish]) return false; for (int v : layers[hi]) backward[v] = 0; backward[finish] = 1; for (int d = hi - 1; d >= lo; d--) { for (int v : layers[d]) { bool reachable = false; for (int u : out[v]) { if (backward[u]) { reachable = true; break; } } backward[v] = reachable; } } for (int d = lo; d <= hi; d++) { for (int v : layers[d]) { if (forward[v] && backward[v]) good[v] = 1; } } return true; }; auto processFinal = [&](int start) -> bool { int lo = dist[start]; for (int v : layers[lo]) forward[v] = 0; forward[start] = 1; for (int d = lo + 1; d <= maxDist; d++) { for (int v : layers[d]) { bool reachable = false; for (int u : in[v]) { if (forward[u]) { reachable = true; break; } } forward[v] = reachable; } } for (int d = lo; d <= maxDist; d++) { for (int v : layers[d]) backward[v] = 0; } for (int x : destinations) { if (dist[x] >= lo) backward[x] = 1; } for (int d = maxDist; d >= lo; d--) { for (int v : layers[d]) { for (int u : out[v]) { if (backward[u]) { backward[v] = 1; break; } } } } if (!backward[start]) return false; for (int d = lo; d <= maxDist; d++) { for (int v : layers[d]) { if (forward[v] && backward[v]) good[v] = 1; } } return true; }; if (K == 0) { possible = processFinal(1); } else { possible = processBounded(1, flowers[0]); for (int i = 0; possible && i + 1 < K; i++) { possible = processBounded(flowers[i], flowers[i + 1]); } if (possible) possible = processFinal(flowers.back()); } if (!possible) { cout << string(N - 1, '0') << '\n'; continue; } for (int x : flowers) good[x] = 1; string answer; answer.reserve(N - 1); for (int v = 2; v <= N; v++) { answer.push_back(good[v] ? '1' : '0'); } cout << answer << '\n'; } return 0; }
#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), destinations(L); for (int &x : flowers) cin >> x; for (int &x : destinations) cin >> x; 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); } vector<int> dist(N + 1, -1); queue<int> q; dist[1] = 0; q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (dist[v] == -1) { dist[v] = dist[u] + 1; q.push(v); } } } int maxDist = 0; for (int v = 1; v <= N; v++) { maxDist = max(maxDist, dist[v]); } vector<vector<int>> level(maxDist + 1); for (int v = 1; v <= N; v++) { level[dist[v]].push_back(v); } vector<char> isDestination(N + 1, false); for (int x : destinations) isDestination[x] = true; // canReachDestination[v]: whether v can reach a destination while // moving only along edges that increase dist by exactly one. vector<char> canReachDestination(N + 1, false); for (int d = maxDist; d >= 0; d--) { for (int v : level[d]) { if (isDestination[v]) { canReachDestination[v] = true; continue; } for (int u : adj[v]) { if (dist[u] == d + 1 && canReachDestination[u]) { canReachDestination[v] = true; break; } } } } sort(flowers.begin(), flowers.end(), [&](int a, int b) { return dist[a] < dist[b]; }); vector<int> required; required.push_back(1); for (int x : flowers) required.push_back(x); bool possible = canReachDestination[required.back()]; for (int i = 1; i < K; i++) { if (dist[flowers[i]] == dist[flowers[i - 1]]) { possible = false; } } string answer(N - 1, '0'); vector<char> forward(N + 1, false), backward(N + 1, false); // Process the gaps between consecutive required farms. for (int i = 0; i < K && possible; i++) { int a = required[i]; int b = required[i + 1]; int low = dist[a]; int high = dist[b]; forward[a] = true; for (int d = low + 1; d <= high; d++) { for (int v : level[d]) { for (int u : adj[v]) { if (dist[u] == d - 1 && forward[u]) { forward[v] = true; break; } } } } if (!forward[b]) { possible = false; break; } backward[b] = true; for (int d = high - 1; d >= low; d--) { for (int v : level[d]) { for (int u : adj[v]) { if (dist[u] == d + 1 && backward[u]) { backward[v] = true; break; } } } } for (int d = low + 1; d < high; d++) { for (int v : level[d]) { if (forward[v] && backward[v]) { answer[v - 2] = '1'; } } } for (int d = low; d <= high; d++) { for (int v : level[d]) { forward[v] = false; backward[v] = false; } } } if (!possible) { cout << string(N - 1, '0') << '\n'; continue; } // Process the gap after the last original flower field. int a = required.back(); int low = dist[a]; forward[a] = true; for (int d = low + 1; d <= maxDist; d++) { for (int v : level[d]) { for (int u : adj[v]) { if (dist[u] == d - 1 && forward[u]) { forward[v] = true; break; } } } } for (int d = low + 1; d <= maxDist; d++) { for (int v : level[d]) { if (forward[v] && canReachDestination[v]) { answer[v - 2] = '1'; } } } // Adding a farm that already contains a flower field changes nothing. for (int x : flowers) { answer[x - 2] = '1'; } cout << answer << '\n'; } return 0; }
#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> S(K), D(L); for (int i = 0; i < K; i++) cin >> S[i]; for (int i = 0; i < L; i++) cin >> D[i]; 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); } vector<int> dist(N + 1, -1); vector<int> order; order.reserve(N); queue<int> q; dist[1] = 0; q.push(1); while (!q.empty()) { int v = q.front(); q.pop(); order.push_back(v); for (int to : adj[v]) { if (dist[to] == -1) { dist[to] = dist[v] + 1; q.push(to); } } } vector<char> isDest(N + 1, false); for (int v : D) isDest[v] = true; sort(S.begin(), S.end(), [&](int a, int b) { if (dist[a] != dist[b]) return dist[a] < dist[b]; return a < b; }); vector<int> idxS(N + 1, 0); for (int i = 0; i < K; i++) { idxS[S[i]] = i + 1; } // maxLabel[v] = maximum index of an S vertex (or source as 0) // that can reach v in the shortest-path DAG. vector<int> maxLabel(N + 1, -1); vector<int> inMax(N + 1, -1); maxLabel[1] = 0; for (int v : order) { inMax[v] = maxLabel[v]; if (idxS[v] > 0) { maxLabel[v] = max(maxLabel[v], idxS[v]); } for (int to : adj[v]) { if (dist[to] == dist[v] + 1) { maxLabel[to] = max(maxLabel[to], maxLabel[v]); } } } bool valid = true; if (K > 0) { for (int i = 1; i < K; i++) { // S[i - 1] has label i, so it must be able to reach S[i]. if (inMax[S[i]] < i) { valid = false; break; } } } if (!valid) { cout << string(N - 1, '0') << '\n'; continue; } const int INF = 1e9; vector<int> minTo(N + 1, INF); vector<char> can(N + 1, false); for (int oi = (int)order.size() - 1; oi >= 0; oi--) { int v = order[oi]; int bestMin = (idxS[v] > 0 ? idxS[v] : INF); bool reachDest = isDest[v]; for (int to : adj[v]) { if (dist[to] == dist[v] + 1) { bestMin = min(bestMin, minTo[to]); if (can[to]) reachDest = true; } } minTo[v] = bestMin; can[v] = reachDest; } string ans; ans.reserve(N - 1); if (K == 0) { for (int f = 2; f <= N; f++) { ans.push_back(can[f] ? '1' : '0'); } cout << ans << '\n'; continue; } int B = S.back(); if (!can[B]) { cout << string(N - 1, '0') << '\n'; continue; } vector<int> cntDist(N + 1, 0); for (int v : S) { cntDist[dist[v]]++; } vector<int> lessCnt(N + 2, 0); int running = 0; for (int d = 0; d <= N; d++) { lessCnt[d] = running; running += cntDist[d]; } int distB = dist[B]; for (int f = 2; f <= N; f++) { if (idxS[f] > 0) { ans.push_back('1'); } else if (dist[f] > distB) { if (maxLabel[f] >= K && can[f]) { ans.push_back('1'); } else { ans.push_back('0'); } } else { int i = lessCnt[dist[f]]; if (maxLabel[f] >= i && minTo[f] <= i + 1) { ans.push_back('1'); } else { ans.push_back('0'); } } } cout << ans << '\n'; } return 0; }
#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 i = 0; i < K; i++) cin >> flowers[i]; vector<int> destinations(L); vector<char> isDest(N + 1, 0); for (int i = 0; i < L; i++) { cin >> destinations[i]; isDest[destinations[i]] = 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); } // BFS from farm 1 vector<int> dist(N + 1, -1); vector<int> order; order.reserve(N); queue<int> q; dist[1] = 0; q.push(1); while (!q.empty()) { int v = q.front(); q.pop(); order.push_back(v); for (int u : adj[v]) { if (dist[u] == -1) { dist[u] = dist[v] + 1; q.push(u); } } } int sz = (int)order.size(); // If there are no initial flowers, the answer for f is whether f // can reach some destination along the shortest-path DAG. if (K == 0) { vector<char> canReachDest(N + 1, 0); for (int p = sz - 1; p >= 0; p--) { int v = order[p]; bool can = isDest[v]; for (int u : adj[v]) { if (dist[u] == dist[v] + 1 && canReachDest[u]) { can = true; } } canReachDest[v] = can; } string ans; ans.reserve(N - 1); for (int f = 2; f <= N; f++) { ans.push_back(canReachDest[f] ? '1' : '0'); } cout << ans << '\n'; continue; } // Sort flowers by distance from 1. sort(flowers.begin(), flowers.end(), [&](int a, int b) { return dist[a] < dist[b]; }); // Two required vertices at the same distance can never both lie // on one shortest path. bool duplicateDistance = false; for (int i = 1; i < K; i++) { if (dist[flowers[i]] == dist[flowers[i - 1]]) { duplicateDistance = true; } } if (duplicateDistance) { cout << string(N - 1, '0') << '\n'; continue; } vector<int> idx(N + 1, 0); vector<int> sDist(K); for (int i = 0; i < K; i++) { idx[flowers[i]] = i + 1; sDist[i] = dist[flowers[i]]; } // canReachDest[v] = whether v can reach some destination in the // shortest-path DAG. vector<char> canReachDest(N + 1, 0); for (int p = sz - 1; p >= 0; p--) { int v = order[p]; bool can = isDest[v]; for (int u : adj[v]) { if (dist[u] == dist[v] + 1 && canReachDest[u]) { can = true; } } canReachDest[v] = can; } // The last required flower must be able to reach a destination. if (!canReachDest[flowers[K - 1]]) { cout << string(N - 1, '0') << '\n'; continue; } // dpAnc[v] = maximum flower index that can reach v in the // shortest-path DAG. Source 1 has index 0. vector<int> dpAnc(N + 1, -1); dpAnc[1] = 0; bool chainOK = true; for (int v : order) { if (idx[v] > 0) { int i = idx[v]; int prev = dpAnc[v]; if (i >= 2 && prev != i - 1) { chainOK = false; } dpAnc[v] = i; } int cur = dpAnc[v]; for (int u : adj[v]) { if (dist[u] == dist[v] + 1) { dpAnc[u] = max(dpAnc[u], cur); } } } if (!chainOK) { cout << string(N - 1, '0') << '\n'; continue; } // dpDesc[v] = minimum flower index reachable from v in the // shortest-path DAG. const int INF = 1e9; vector<int> dpDesc(N + 1, INF); for (int p = sz - 1; p >= 0; p--) { int v = order[p]; int best = idx[v] > 0 ? idx[v] : INF; for (int u : adj[v]) { if (dist[u] == dist[v] + 1) { best = min(best, dpDesc[u]); } } dpDesc[v] = best; } string ans; ans.reserve(N - 1); for (int f = 2; f <= N; f++) { if (idx[f] > 0) { ans.push_back('1'); continue; } int d = dist[f]; int i = lower_bound(sDist.begin(), sDist.end(), d) - sDist.begin(); bool ok = false; if (i == K) { // f is after the last initial flower. if (dpAnc[f] == K && canReachDest[f]) { ok = true; } } else { // f must lie between flower i and flower i+1, // where i is the number of flowers with smaller distance. if (dpAnc[f] == i && dpDesc[f] == i + 1) { ok = true; } } ans.push_back(ok ? '1' : '0'); } cout << ans << '\n'; } return 0; }
#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> Sinput(K); vector<char> inS(N + 1, 0); for (int i = 0; i < K; i++) { cin >> Sinput[i]; inS[Sinput[i]] = 1; } vector<int> D(L); vector<char> isD(N + 1, 0); for (int i = 0; i < L; i++) { cin >> D[i]; isD[D[i]] = 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); } vector<int> dist(N + 1, -1); vector<int> order; order.reserve(N); queue<int> q; dist[1] = 0; q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); order.push_back(u); for (int v : adj[u]) { if (dist[v] == -1) { dist[v] = dist[u] + 1; q.push(v); } } } vector<int> S = Sinput; sort(S.begin(), S.end(), [&](int a, int b) { if (dist[a] != dist[b]) return dist[a] < dist[b]; return a < b; }); vector<int> label(N + 1, 0); vector<int> sDist(K); for (int i = 0; i < K; i++) { label[S[i]] = i + 1; sDist[i] = dist[S[i]]; } vector<int> maxIdx(N + 1, 0); bool chain = true; for (int u : order) { int lab = label[u]; if (lab > 0) { if (lab > 1 && maxIdx[u] < lab - 1) { chain = false; } maxIdx[u] = max(maxIdx[u], lab); } if (maxIdx[u] > 0) { for (int v : adj[u]) { if (dist[v] == dist[u] + 1) { maxIdx[v] = max(maxIdx[v], maxIdx[u]); } } } } if (!chain) { cout << string(N - 1, '0') << '\n'; continue; } vector<char> canReachDest(N + 1, 0); for (int idx = (int)order.size() - 1; idx >= 0; idx--) { int u = order[idx]; bool ok = isD[u]; if (!ok) { for (int v : adj[u]) { if (dist[v] == dist[u] + 1 && canReachDest[v]) { ok = true; break; } } } canReachDest[u] = ok; } if (K > 0 && !canReachDest[S[K - 1]]) { cout << string(N - 1, '0') << '\n'; continue; } const int INF = 1e9; vector<int> minIdx(N + 1, INF); for (int idx = (int)order.size() - 1; idx >= 0; idx--) { int u = order[idx]; int best = INF; for (int v : adj[u]) { if (dist[v] == dist[u] + 1) { best = min(best, minIdx[v]); } } if (label[u] > 0) { best = min(best, label[u]); } minIdx[u] = best; } string ans; ans.reserve(N - 1); for (int f = 2; f <= N; f++) { if (inS[f]) { ans.push_back('1'); continue; } int t = lower_bound(sDist.begin(), sDist.end(), dist[f]) - sDist.begin(); if (t < K && sDist[t] == dist[f]) { ans.push_back('0'); continue; } bool ok = true; if (maxIdx[f] != t) ok = false; if (ok && t < K && minIdx[f] != t + 1) { ok = false; } if (ok) { if (t < K) { // There is an existing flower after f. // canReachDest[last S] is already true. } else { // f is after all existing flowers, or there are no flowers. if (!canReachDest[f]) ok = false; } } ans.push_back(ok ? '1' : '0'); } cout << ans << '\n'; } return 0; }
#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> S(K), Dst(L); for (int &x : S) cin >> x; for (int &x : Dst) cin >> x; 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); } // BFS distances from the barn vector<int> dist(N + 1, -1); { vector<int> q; q.reserve(N); q.push_back(1); dist[1] = 0; for (size_t h = 0; h < q.size(); ++h) { int u = q[h]; for (int v : adj[u]) if (dist[v] == -1) { dist[v] = dist[u] + 1; q.push_back(v); } } } // flower fields in increasing distance order vector<int> s = S; sort(s.begin(), s.end(), [&](int a, int b) { return dist[a] < dist[b]; }); bool possible = true; for (int i = 1; i < K; ++i) if (dist[s[i]] == dist[s[i-1]]) possible = false; // equal levels impossible vector<char> good(N + 1, 0); // vertices on some pretty path if (possible) { vector<int> fmark(N + 1, 0), bmark(N + 1, 0); int sf = 0, sb = 0; vector<int> tF, tB, stk; // forward reachable from `start` using only DAG edges, within band (<= hi) auto fwd = [&](int start, int hi) { ++sf; tF.clear(); stk.clear(); if (dist[start] > hi) return; fmark[start] = sf; tF.push_back(start); stk.push_back(start); while (!stk.empty()) { int u = stk.back(); stk.pop_back(); for (int v : adj[u]) { if (dist[v] == dist[u] + 1 && dist[v] <= hi && fmark[v] != sf) { fmark[v] = sf; tF.push_back(v); stk.push_back(v); } } } }; // vertices able to reach one of `starts` via DAG edges, within band (>= lo) auto bwd = [&](const vector<int>& starts, int lo) { ++sb; tB.clear(); stk.clear(); for (int x : starts) { if (dist[x] < lo || bmark[x] == sb) continue; bmark[x] = sb; tB.push_back(x); stk.push_back(x); } while (!stk.empty()) { int u = stk.back(); stk.pop_back(); for (int v : adj[u]) { if (dist[v] == dist[u] - 1 && dist[v] >= lo && bmark[v] != sb) { bmark[v] = sb; tB.push_back(v); stk.push_back(v); } } } }; // pieces between consecutive flower fields for (int i = 0; i < K; ++i) { int a = (i == 0 ? 1 : s[i - 1]); int b = s[i]; fwd(a, dist[b]); bwd(vector<int>{b}, dist[a]); if (fmark[b] != sf) { possible = false; break; } // b unreachable from a for (int v : tF) if (bmark[v] == sb) good[v] = 1; } // final piece: from the last field (or the barn) to some destination if (possible) { int a = (K == 0 ? 1 : s[K - 1]); int hi = 0; for (int d : Dst) hi = max(hi, dist[d]); fwd(a, hi); bwd(Dst, dist[a]); bool found = false; for (int d : Dst) if (fmark[d] == sf) { found = true; break; } if (!found) possible = false; else for (int v : tF) if (bmark[v] == sb) good[v] = 1; } } string ans(N - 1, '0'); for (int v = 2; v <= N; ++v) if (good[v]) ans[v - 2] = '1'; cout << ans << '\n'; } return 0; }
#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> isDest(N + 1, 0); for (int i = 0; i < L; ++i) { int x; cin >> x; isDest[x] = 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); } // BFS from farm 1 (order is non-decreasing in distance) vector<int> dist(N + 1, -1), order; order.reserve(N); dist[1] = 0; order.push_back(1); for (int h = 0; h < (int)order.size(); ++h) { int u = order[h]; for (int v : adj[u]) { if (dist[v] == -1) { dist[v] = dist[u] + 1; order.push_back(v); } } } // flowers sorted by distance, 1-indexed: S[1..K] sort(flowers.begin(), flowers.end(), [&](int a, int b) { return dist[a] < dist[b]; }); vector<int> S(K + 2, 0); vector<int> cnt(N + 1, 0); for (int i = 0; i < K; ++i) { S[i + 1] = flowers[i]; ++cnt[dist[flowers[i]]]; } // pref[d] = number of flowers with distance < d vector<int> pref(N + 1, 0); for (int d = 1; d <= N; ++d) pref[d] = pref[d - 1] + cnt[d - 1]; // A[v] = max j such that a DAG path 1->v contains S[1..j] vector<int> A(N + 1, 0); for (int idx = 1; idx < N; ++idx) { int v = order[idx]; int best = 0; for (int u : adj[v]) { if (dist[u] + 1 != dist[v]) continue; // DAG predecessor int cand = A[u]; if (A[u] + 1 <= K && S[A[u] + 1] == v) ++cand; if (cand > best) best = cand; } A[v] = best; } // B[v] = min j such that a DAG path v->destination contains S[j..K] const int INF = K + 2; vector<int> B(N + 1, INF); for (int idx = N - 1; idx >= 0; --idx) { int v = order[idx]; int best = INF; if (isDest[v]) best = (K >= 1 && S[K] == v) ? K : K + 1; for (int w : adj[v]) { if (dist[w] != dist[v] + 1) continue; // DAG successor if (B[w] >= INF) continue; int cand = B[w]; if (B[w] - 1 >= 1 && S[B[w] - 1] == v) --cand; if (cand < best) best = cand; } B[v] = best; } string ans(N - 1, '0'); for (int f = 2; f <= N; ++f) { int i = pref[dist[f]]; if (A[f] >= i && B[f] <= i + 1) ans[f - 2] = '1'; } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct FastScanner { static const size_t SIZE = 1 << 16; char buf[SIZE]; size_t idx = 0, len = 0; inline int gc() { if (idx >= len) { len = fread(buf, 1, SIZE, stdin); idx = 0; if (len == 0) return EOF; } return (unsigned char)buf[idx++]; } int nextInt() { int c = gc(); while (c != EOF && (c < '0' || c > '9')) c = gc(); int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = gc(); } return x; } }; int main() { FastScanner fs; int T = fs.nextInt(); string output; while (T--) { int N = fs.nextInt(), M = fs.nextInt(), K = fs.nextInt(), L = fs.nextInt(); vector<int> fl(K); vector<char> isFl(N + 1, 0), isDest(N + 1, 0); for (int i = 0; i < K; i++) { fl[i] = fs.nextInt(); isFl[fl[i]] = 1; } for (int i = 0; i < L; i++) { int x = fs.nextInt(); isDest[x] = 1; } vector<int> head(N + 1, -1), to(2 * M), nxt(2 * M); int ec = 0; for (int i = 0; i < M; i++) { int u = fs.nextInt(), v = fs.nextInt(); to[ec] = v; nxt[ec] = head[u]; head[u] = ec++; to[ec] = u; nxt[ec] = head[v]; head[v] = ec++; } // BFS from farm 1 (also gives vertices in increasing distance order) vector<int> dist(N + 1, -1), order(N); int qh = 0, qt = 0; dist[1] = 0; order[qt++] = 1; while (qh < qt) { int v = order[qh++]; for (int e = head[v]; e != -1; e = nxt[e]) { int u = to[e]; if (dist[u] == -1) { dist[u] = dist[v] + 1; order[qt++] = u; } } } // flowers sorted by distance, 1-indexed rank vector<int> fsorted = fl; sort(fsorted.begin(), fsorted.end(), [&](int a, int b) { return dist[a] < dist[b]; }); vector<int> fidx(N + 1, 0); for (int i = 0; i < K; i++) fidx[fsorted[i]] = i + 1; const int INF = K + 1; vector<int> anc(N + 1, 0); // max index of a flower that reaches v (self included) vector<int> predMax(N + 1, 0); // same, but strictly proper ancestors of v vector<int> desc(N + 1, INF); // min index of a flower reachable from v vector<char> reachDest(N + 1, 0); // forward pass (increasing distance) for (int idx = 0; idx < N; idx++) { int v = order[idx]; int pm = 0; for (int e = head[v]; e != -1; e = nxt[e]) { int u = to[e]; if (dist[u] + 1 == dist[v]) pm = max(pm, anc[u]); } predMax[v] = pm; anc[v] = max(pm, fidx[v]); } // backward pass (decreasing distance) for (int idx = N - 1; idx >= 0; idx--) { int v = order[idx]; int d = INF; bool rd = isDest[v]; for (int e = head[v]; e != -1; e = nxt[e]) { int u = to[e]; if (dist[u] == dist[v] + 1) { d = min(d, desc[u]); rd = rd || reachDest[u]; } } desc[v] = fidx[v] ? min(fidx[v], d) : d; reachDest[v] = rd; } // base feasibility of the flower set itself bool base = true; for (int j = 1; j < K; j++) if (dist[fsorted[j - 1]] == dist[fsorted[j]]) { base = false; break; } // same layer for (int j = 1; j < K && base; j++) if (predMax[fsorted[j]] != j) base = false; // s_j must reach s_{j+1} if (K >= 1 && !reachDest[fsorted[K - 1]]) base = false; // last flower must reach a destination string ans(N - 1, '0'); if (base) { vector<int> cnt(N + 1, 0), pref(N + 1, 0); for (int j = 0; j < K; j++) cnt[dist[fsorted[j]]]++; for (int d = 1; d <= N; d++) pref[d] = pref[d - 1] + cnt[d]; for (int f = 2; f <= N; f++) { bool good = false; if (isFl[f]) { good = true; // flower set unchanged } else { int d = dist[f]; if (cnt[d] == 0) { // f must not share a layer with a flower int i = pref[d - 1]; // # flowers closer than f if (predMax[f] == i && desc[f] == i + 1) { if (i < K) good = true; // last element is s_K (already checked) else good = (reachDest[f] != 0); } } } if (good) ans[f - 2] = '1'; } } output += ans; output += '\n'; } fwrite(output.data(), 1, output.size(), stdout); return 0; }