题解
2023-08-18 16:46:47
发布于:广东
5阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
template<typename T>
void read(T& FF) {
T RR = 1;
FF = 0;
char c;
cin>>c;
for (; !isdigit(c); c = getchar())
if (c == '-')
RR = -1;
for (; isdigit(c); c = getchar())
FF = (FF << 1) + (FF << 3) + (c ^ 48);
FF *= RR;
}
template<typename T>
void write(T x) {
if (x < 0)
putchar('-'), x *= -1;
if (x > 9)
write(x / 10);
putchar(x % 10 + 48);
}
const int MAXN = 100010;
vector<int> v[MAXN];
int ji[MAXN], ou[MAXN];
void bfs() {
memset(ji, 0x3f, sizeof(ji));
memset(ou, 0x3f, sizeof(ou));
queue<pair<int, int>> q;
for (int i = 0; i < v[1].size(); i++) {
ji[v[1][i]] = 1;
q.push(make_pair(v[1][i], 1));
}
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
for (int i = 0; i < v[x].size(); i++) {
if (y % 2 == 1) {
if (y + 1 < ou[v[x][i]]) {
ou[v[x][i]] = y + 1;
q.push(make_pair(v[x][i], y + 1));
}
} else {
if (y + 1 < ji[v[x][i]]) {
ji[v[x][i]] = y + 1;
q.push(make_pair(v[x][i], y + 1));
}
}
}
q.pop();
}
}
int main() {
int n, m, q;
read(n);
read(m);
read(q);
for (int i = 1; i <= m; i++) {
int x, y;
read(x);
read(y);
v[x].push_back(y);
v[y].push_back(x);
}
bfs();
while (q--) {
int x, y;
read(x);
read(y);
if (y % 2 == 0) {
if (ou[x] > y)
cout<<"No"<<'\n';
else
cout<<"Yes"<<'\n';
} else {
if (ji[x] > y)
cout<<"No"<<'\n';
else
cout<<"Yes"<<'\n';
}
}
return 0;
}
这里空空如也
有帮助,赞一个