题意:给出每个朋友之间的联系表,如果a联系b,b联系c,那么a是可以联系c的。问最少要去掉多少个朋友才能使得s无法和t联系上。如果有一样的方案输出字典序最小的。
解法:很像最小割有木有?但是这次不是边而是点,那么我们拆点不就好了。跑一次最大流发现一开始流量为f,那么我们从1到n枚举点,试着把第i个点删去看看流量是否有变化。如果有变化,说明第i个点在最小点割集里面。记录一下这个点,并且以后不再进入这个点,更新最大流量为此时的流量。不断循环即可。
要特判如果s到t本身就有边输出no answer。
代码如下:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const int maxn = 400 + 5;
const int maxm = 1e5 + 5;
const int INF = 0x3f3f3f3f;
inline int read() {
int x=0,t=1,c;
while(!isdigit(c=getchar()))if(c=='-')t=-1;
while(isdigit(c))x=x*10+c-'0',c=getchar();
return x*t;
}
int head[maxn],cur[maxn],nx[maxm<<1],to[maxm<<1],flow[maxm<<1],ppp=0;
int ban, vst[maxn], n, s, t;
bool G[maxn][maxn];
struct Dinic {
int dis[maxn];
int s, t;
long long ans;
void init() {
memset(head, -1, sizeof(head));
ppp = 0;
}
void AddEdge(int u, int v, int c) {
to[ppp]=v;flow[ppp]=c;nx[ppp]=head[u];head[u]=ppp++;swap(u,v);
to[ppp]=v;flow[ppp]=0;nx[ppp]=head[u];head[u]=ppp++;
}
bool BFS() {
memset(dis, -1, sizeof(dis));
dis[t] = 1;
queue<int> Q;
Q.push(t);
while(!Q.empty()) {
int x = Q.front();
Q.pop();
for(int i = head[x]; ~i; i = nx[i]) {
if(to[i] == ban * 2 || to[i] == ban * 2 + 1 || vst[to[i]])
continue;
if(flow[i^1] && dis[to[i]] == -1) {
dis[to[i]] = dis[x] + 1;
Q.push(to[i]);
}
}
}
return dis[s] != -1;
}
int DFS(int x, int maxflow) {
if(x == t || !maxflow){
ans += maxflow;
return maxflow;
}
int ret = 0, f;
for(int &i = cur[x]; ~i; i = nx[i]) {
if(dis[to[i]] == dis[x] - 1 && (f = DFS(to[i], min(maxflow, flow[i])))) {
ret += f;
flow[i] -= f;
flow[i^1] += f;
maxflow -= f;
if(!maxflow)
break;
}
}
return ret;
}
long long solve(int source, int tank) {
s = source;
t = tank;
ans = 0;
while(BFS()) {
memcpy(cur, head, sizeof(cur));
DFS(s, INF);
}
return ans;
}
}dinic;
vector <int> vec;
void build() {
dinic.init();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(G[i][j] && i != j)
dinic.AddEdge(i * 2 + 1, j * 2, 1);
}
}
for(int i = 1; i <= n; i++) {
dinic.AddEdge(i * 2, i * 2 + 1, 1);
}
dinic.AddEdge(s * 2, s * 2 + 1, INF);
dinic.AddEdge(t * 2, t * 2 + 1, INF);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
dinic.init();
n = read(), s = read(), t = read();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
int tmp = read();
G[i][j] = tmp;
if(tmp && i != j) {
dinic.AddEdge(i * 2 + 1, j * 2, 1);
}
}
}
for(int i = 1; i <= n; i++) {
dinic.AddEdge(i * 2, i * 2 + 1, 1);
}
dinic.AddEdge(s * 2, s * 2 + 1, INF);
dinic.AddEdge(t * 2, t * 2 + 1, INF);
ban = -10;
long long Min_flow = dinic.solve(s * 2, t * 2 + 1);
// cout << Min_flow << '\n';
for(int i = 1; i <= n; i++) {
if(i != s && i != t) {
ban = i;
build();
long long tmp = dinic.solve(s * 2, t * 2 + 1);
// cout << "i is " << i << " tmp is " << tmp << '\n';
if(tmp != Min_flow) {
vec.push_back(i);
vst[i * 2] = 1;
vst[i * 2 + 1] = 1;
Min_flow = tmp;
}
}
}
if(G[s][t]) {
printf("NO ANSWER!");
} else {
cout << vec.size() << '\n';
for(int i = 0; i < vec.size(); i++)
printf("%d%c", vec[i], i + 1 == vec.size() ? '\n' : ' ');
}
return 0;
}