Files
python/TangDou/CSP-J/J2_2019/4_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

53 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 1e5 + 10, M = N << 1;
// 20个测试点,过了7个,得了35分,其它TLE
// 邻接表
int e[M], h[N], idx, ne[M];
void add(int a, int b) {
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
bool bfs(int start, int dist) {
queue<PII> q;
q.push({start, dist});
while (q.size()) {
auto u = q.front();
q.pop();
if (u.x == 1 && u.y == 0) return true; // 在用完所有步数后,到达1号点,成功!
for (int i = h[u.x]; ~i; i = ne[i]) {
int v = e[i];
if (u.y) q.push({v, u.y - 1});
}
}
return false;
}
int main() {
// 链式前向星
memset(h, -1, sizeof h);
int n, m, Q;
cin >> n >> m >> Q;
while (m--) {
int a, b;
cin >> a >> b;
add(a, b), add(b, a); // 无向图,双向建边
}
while (Q--) {
int u, dist;
cin >> u >> dist;
cout << (bfs(u, dist) ? "Yes" : "No") << endl;
}
return 0;
}