题解:P2273 [HNOI2002] 交换
深受题解区的启发,发现本题还能交题解,就来发一篇。
如果不考虑删除一条指令机制,维护一个可能会有最小值的寄存器集合
注意这里不用考虑
加上删除指令的机制,可以枚举一下删除的是哪个交换指令,再维护一个删除那个指令可能会有最小值的寄存器集合
最终算至少需要添加
这样就做完了。另外这道题卡常,我们可以用 bitset 卡过这道题。
#include <bits/stdc++.h>
using namespace std;
const int N = 2e4 + 5;
struct node {
int x, y;
}ce[N];
int n, m, ans;
bitset<N> a, b, c;
int read() {
int k = 0;
char c = getchar();
while(c < '0' || c > '9') {
c = getchar();
}
while(c >= '0' && c <= '9') {
k = k * 10 + c - '0';
c = getchar();
}
return k;
}
signed main() {
n = read(), m = read();
for (int i = 1; i <= m; i++) {
ce[i].x = read(), ce[i].y = read();
}
a.set();
for (int i = 1; i <= m; i++) {
if (a[ce[i].x] ^ 1 && a[ce[i].y] ^ 1) continue;
c = a;
for (int j = i + 1; j <= m; j++) {
if (c[ce[j].x] ^ 1 && c[ce[j].y] ^ 1) continue;
c.set(ce[j].x, 1), c.set(ce[j].y, 0);
}
a.set(ce[i].x, 1), a.set(ce[i].y, 0);
b |= c;
}
for (int i = 2; i <= n; i++) {
if (a[i] == 1) ans += 2;
else if (b[i] == 1) ans++;
}
cout << ans;
return 0;
}