CSP-J2019 加工零件
作者:互联网
2019 CSP 普及组的 T4,直接讲思路吧。
先假设每个点到点1只有唯一路径,那么我们有:
- 若 L<该路径长度 ,则对这个点的答案必定是"No"。
- 若 L>=该路径长度 且 奇偶性与该路径长度相同,则对这个点的答案为"Yes",否则为"No"。
令这个唯一路径为最短路,则我们只需要知道每个点到点1的奇最短路和偶最短路即可。
附上代码:
/*code by Szzkp*/
#include <bits/stdc++.h>
#define Rei register int
#define ll long long
using namespace std;
inline void read(int &x) {
x = 0;
char f = 0, c = getchar();
while(!isdigit(c)) f = c=='-', c = getchar();
if(f) while(isdigit(c)) x = x*10-c+48, c = getchar();
else while(isdigit(c)) x = x*10+c-48, c = getchar();
}
const int inf = 0x3f3f3f3f;
const int N = 1e5+5, M = 1e5+5;
int n, m, q, cnt;
int h[N], d[N][2];
struct edge {
int next, to;
edge(int nex = 0, int t = 0) {
next = nex, to = t;
}
} b[M<<1];
inline void link(int u, int v) {
b[++cnt] = edge(h[u], v), h[u] = cnt;
}
void bfs() {
queue<int> q;
q.push(1), d[1][0] = 0;
while(!q.empty()) {
int x = q.front(); q.pop();
for(Rei i = h[x]; i; i = b[i].next) {
int y = b[i].to, in = 0;
if(d[x][1] + 1 < d[y][0]) in = 1, d[y][0] = d[x][1] + 1;
if((d[x][0] ^ 1) < d[y][1]) in = 1, d[y][1] = (d[x][0] ^ 1);
if(in) q.push(y);
}
}
}
int main() {
read(n), read(m), read(q);
for(Rei i = 1; i <= m; ++i) {
int u, v;
read(u), read(v);
link(u, v), link(v, u);
}
memset(d, 0x3f, sizeof(d));
bfs();
for(Rei i = 1; i <= q; ++i) {
int x, L;
read(x), read(L);
printf(d[x][L&1] <= L? "Yes\n":"No\n");
}
return 0;
}
感谢阅读QAQ!
标签:int,next,read,while,零件,isdigit,J2019,CSP,getchar 来源: https://www.cnblogs.com/Szzkp/p/14472347.html