https://atcoder.jp/contests/abc238/tasks/abc238_e
#include<iostream>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;
const int N = 1e6 + 10;
int n, q;
int p[N];
int find (int tar) {
if (p[tar] != tar) return p[tar] = find(p[tar]);
return p[tar];
}
void solve() {
cin >> n >> q;
for (int i = 0; i <= n; i ++) p[i] = i;
while ( q--) {
int l, r;
cin >> l >> r;
if (l > r) swap(l, r);
int fl = find(l - 1), fr = find(r);
if (fl != fr) {
p[fr] = fl;
}
}
int p0 = find(0), pn = find(n);
if (p0== pn) puts("Yes");
else puts("No");
}
int main () {
int t;
t =1;
while (t --) solve();
return 0;
}
前缀和表示 a[l] + ... + a[r] = b[r] - b[l - 1]; 如果b[0]能求b[n]当且仅当0和n有边,这里可以用并查集来实现
|