题解:CF2109D D/D/D
注意到图是无向图,因此可以来回走一条边刷步数,但是这样步数的奇偶性不变。
建分层图,把每个点拆成奇点和偶点,连边
设到第
:::success[CODE]
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 3e+6 + 6;
int n, m, T, tot, k;
int dis[maxn], a[maxn], h[maxn];
bool vis[maxn];
struct V {
int to, nxt;
}e[maxn];
void add(int x, int y) {
e[++tot] = {y, h[x]};
h[x] = tot;
}
void AE(int x, int y) {
add(x, y), add(y, x);
}
struct P {
int p, w;
friend bool operator < (const P &x, const P &y) {
return x.w > y.w;
}
};
priority_queue<P> q;
void GOGOGO() {
cin >> n >> m >> k;
int res = 1e18, sum = 0;
for (int i = 1; i <= k; i++) cin >> a[i], sum += a[i];
sort(a + 1, a + k + 1);
for (int i = 1; i <= k; i++) {
if (a[i] & 1) {
res = a[i];
break;
}
}
tot = 0;
for (int i = 1; i <= n * 2; i++) {
h[i] = 0;
}
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
AE(x, y + n), AE(y, x + n);
}
for (int i = 1; i <= n * 2; i++) {
dis[i] = 1e18;
vis[i] = 0;
}
dis[1] = 0;
q.push({1, 0});
while (!q.empty()) {
int p = q.top().p;
q.pop();
if (vis[p]) continue;
vis[p] = 1;
for (int i = h[p]; i; i = e[i].nxt) {
int j = e[i].to;
if (dis[j] > dis[p] + 1) {
dis[j] = dis[p] + 1;
q.push({j, dis[j]});
}
}
}
for (int i = 1; i <= n; i++) {
if (sum & 1) {
if (dis[i + n] <= sum || dis[i] <= sum - res) cout << 1;
else cout << 0;
} else {
// cout << dis[i] << ' ' << dis[i + n] << '\n';
if (dis[i] <= sum || dis[i + n] <= sum - res) cout << 1;
else cout << 0;
}
}
cout << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> T;
while (T--) GOGOGO();
return 0;
}
:::