25pts求条,样例都没过,不知道哪错了
查看原帖
25pts求条,样例都没过,不知道哪错了
1345783
BK小鹿楼主2024/12/16 22:29

rt,求助

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5, M = 2e5 + 5;

int n, m, q;
int h[N], e[M], ne[M], w[M], idx;
int d1[N], d2[N];//奇,偶 
bool st[N], st1[N];

#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
char *p1,*p2,buf[1<<20+5];
inline int read(){
	int x=0,f=1;
	char c=gc();
	while(!isdigit(c)){
        if(c=='-')f=-1;
		c=gc();
	}while(isdigit(c)){
		x=x*10+(c^48);
		c=gc();
	}return x*f;
}

void add(int a, int b, int c) {
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
} 

void spfa(int S) {
	memset(d1, 0x3f, sizeof d1);memset(d2, 0x3f, sizeof d2);memset(st, 0, sizeof st);memset(st1, 0, sizeof st);
	queue<int> q, q1;
	
	q1.push(S);q.push(S);st[S] = 1, st1[S] = 1;d1[S] = 0, d2[S] = 0;
	while (!q.empty()) {
		int t = q.front();q.pop();
		st[t] = 0;
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if ((d1[t] + 1 % 2 == 1) && d1[t] + 1 <= d1[j]) {
				d1[j] = d1[t] + 1;
				if (!st[j]) {
					q.push(j);
					st[j] = 1;
				}
			}
		}
	}
	
	while (!q1.empty()) {
		int t = q1.front();q1.pop();
		st1[t] = 0;
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if ((d2[t] + 1 % 2 == 0) && d2[t] + 1 <= d2[j]) {
				d2[j] = d2[t] + 1;
				if (!st1[j]) {
					q1.push(j);
					st1[j] = 1;
				}
			}
		}
	}	
}

int main() {
	memset(h, -1, sizeof h);
	n = read(), m = read(), q = read();
	while (m -- ) {
		int u, v;
		u = read(), v = read();
		add(u, v, 1), add(v, u, 1);
	}
	spfa(1);
	while (q -- ) {
		int a, L;
		a = read(), L = read();
		if (L % 2 == 1) {
			if (d1[a] <= L) puts("Yes");
			else puts("No");
		} else {
			if (d2[a] <= L) puts("Yes");
			else puts("No");
		}
	}
	return 0;
}
2024/12/16 22:29
加载中...