poj 1815 Friendship(最小割,求割集)
2016-09-14 15:09
399 查看
题目链接
Friendship
Description
In modern society, each person has his own friends. Since all the people are very busy, they communicate with each other only by phone. You can assume that people A can keep in touch with people B, only if
1. A knows B's phone number, or
2. A knows people C's phone number and C can keep in touch with B.
It's assured that if people A knows people B's number, B will also know A's number.
Sometimes, someone may meet something bad which makes him lose touch with all the others. For example, he may lose his phone number book and change his phone number at the same time.
In this problem, you will know the relations between every two among N people. To make it easy, we number these N people by 1,2,...,N. Given two special people with the number S and T, when some people meet bad things, S may lose touch with T. Your job is to
compute the minimal number of people that can make this situation happen. It is supposed that bad thing will never happen on S or T.
Input
The first line of the input contains three integers N (2<=N<=200), S and T ( 1 <= S, T <= N , and S is not equal to T).Each of the following N lines contains N integers. If i knows j's number, then the j-th number in the (i+1)-th line will be 1, otherwise the
number will be 0.
You can assume that the number of 1s will not exceed 5000 in the input.
Output
If there is no way to make A lose touch with B, print "NO ANSWER!" in a single line. Otherwise, the first line contains a single number t, which is the minimal number you have got, and if t is not zero, the second line is needed, which contains t integers in
ascending order that indicate the number of people who meet bad things. The integers are separated by a single space.
If there is more than one solution, we give every solution a score, and output the solution with the minimal score. We can compute the score of a solution in the following way: assume a solution is A1, A2, ..., At (1 <= A1 < A2 <...< At <=N ), the score will
be (A1-1)*N^t+(A2-1)*N^(t-1)+...+(At-1)*N. The input will assure that there won't be two solutions with the minimal score.
Sample Input
Sample Output
Source
POJ Monthly
这个题解很详细,顺便补充一点,求割集时当枚举到一个点,假设其入点为v,出点为v',那么跑完最大流后若这一条边是满流边,那么这个点才有可能是属于割集,而且不需要将这一点与其他点的所有联系断去,只需要将v到v'这条边断去就可以了。
顺便提出一个易错点:满流边不一定属于割集,割集中的边一定是满流边!
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN=400+10;
const int MAXM=MAXN*MAXN;
const int INF=0x3f3f3f3f;
int g[MAXN][MAXN],mg[MAXN][MAXN];
bool flag[MAXN];
struct Node
{
int from,to,next;
int cap;
}edge[MAXM];
int tol;
int n,s,t;
int dep[MAXN];//dep为点的层次
int head[MAXN];
void init()
{
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)//第一条边下标必须为偶数
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].cap=w;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].from=v;
edge[tol].to=u;
edge[tol].cap=0;
edge[tol].next=head[v];
head[v]=tol++;
}
int BFS(int start,int end)
{
int que[MAXN];
int front,rear;
front=rear=0;
memset(dep,-1,sizeof(dep));
que[rear++]=start;
dep[start]=0;
while(front!=rear)
{
int u=que[front++];
if(front==MAXN)front=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>0&&dep[v]==-1)
{
dep[v]=dep[u]+1;
que[rear++]=v;
if(rear>=MAXN)rear=0;
if(v==end)return 1;
}
}
}
return 0;
}
int dinic(int start,int end)
{
int res=0;
int top;
int stack[MAXN];//stack为栈,存储当前增广路
int cur[MAXN];//存储当前点的后继
while(BFS(start,end))
{
memcpy(cur,head,sizeof(head));
int u=start;
top=0;
while(1)
{
if(u==end)
{
int min=INF;
int loc;
for(int i=0;i<top;i++)
if(min>edge[stack[i]].cap)
{
min=edge[stack[i]].cap;
loc=i;
}
for(int i=0;i<top;i++)
{
edge[stack[i]].cap-=min;
edge[stack[i]^1].cap+=min;
}
res+=min;
top=loc;
u=edge[stack[top]].from;
}
for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)
if(edge[i].cap!=0&&dep[u]+1==dep[edge[i].to])
break;
if(cur[u]!=-1)
{
stack[top++]=cur[u];
u=edge[cur[u]].to;
}
else
{
if(top==0)break;
dep[u]=-1;
u=edge[stack[--top]].from;
}
}
}
return res;
}
void build()
{
init();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(g[i][j]) addedge(i+n,j,INF);
}
}
for(int i=1;i<=n;i++)
{
if(i!=s&&i!=t) addedge(i,i+n,1);
else addedge(i,i+n,INF);
}
}
int main()
{
while(~scanf("%d%d%d",&n,&s,&t))
{
memset(g,0,sizeof(g));
memset(mg,0,sizeof(mg));
memset(flag,false,sizeof(flag));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&g[i][j]);
//mg[i][j]=g[i][j];
}
}
build();
int ans=dinic(s,t+n);
if(ans>=INF) puts("NO ANSWER!");
else
{
int mans=ans;
printf("%d\n",ans);
if(!ans) continue;
for(int i=1;i<=n;i++)
{
if(i==s||i==t) continue;
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
mg[j][k]=g[j][k];
if(j==i||k==i)
g[j][k]=0;
}
}
build();
int re=dinic(s,t+n);
if(re<ans) ans--,flag[i]=true;
else
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
g[j][k]=mg[j][k];
}
}
if(!ans) break;
}
int cnt=0;
for(int i=1;i<=n;i++)
{
if(flag[i])
{
cnt++;
printf("%d%c",i,cnt==mans?'\n':' ');
}
}
}
}
return 0;
}
Friendship
Time Limit: 2000MS | Memory Limit: 20000K | |
Total Submissions: 10532 | Accepted: 2919 |
In modern society, each person has his own friends. Since all the people are very busy, they communicate with each other only by phone. You can assume that people A can keep in touch with people B, only if
1. A knows B's phone number, or
2. A knows people C's phone number and C can keep in touch with B.
It's assured that if people A knows people B's number, B will also know A's number.
Sometimes, someone may meet something bad which makes him lose touch with all the others. For example, he may lose his phone number book and change his phone number at the same time.
In this problem, you will know the relations between every two among N people. To make it easy, we number these N people by 1,2,...,N. Given two special people with the number S and T, when some people meet bad things, S may lose touch with T. Your job is to
compute the minimal number of people that can make this situation happen. It is supposed that bad thing will never happen on S or T.
Input
The first line of the input contains three integers N (2<=N<=200), S and T ( 1 <= S, T <= N , and S is not equal to T).Each of the following N lines contains N integers. If i knows j's number, then the j-th number in the (i+1)-th line will be 1, otherwise the
number will be 0.
You can assume that the number of 1s will not exceed 5000 in the input.
Output
If there is no way to make A lose touch with B, print "NO ANSWER!" in a single line. Otherwise, the first line contains a single number t, which is the minimal number you have got, and if t is not zero, the second line is needed, which contains t integers in
ascending order that indicate the number of people who meet bad things. The integers are separated by a single space.
If there is more than one solution, we give every solution a score, and output the solution with the minimal score. We can compute the score of a solution in the following way: assume a solution is A1, A2, ..., At (1 <= A1 < A2 <...< At <=N ), the score will
be (A1-1)*N^t+(A2-1)*N^(t-1)+...+(At-1)*N. The input will assure that there won't be two solutions with the minimal score.
Sample Input
3 1 3 1 1 0 1 1 1 0 1 1
Sample Output
1 2
Source
POJ Monthly
这个题解很详细,顺便补充一点,求割集时当枚举到一个点,假设其入点为v,出点为v',那么跑完最大流后若这一条边是满流边,那么这个点才有可能是属于割集,而且不需要将这一点与其他点的所有联系断去,只需要将v到v'这条边断去就可以了。
顺便提出一个易错点:满流边不一定属于割集,割集中的边一定是满流边!
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN=400+10;
const int MAXM=MAXN*MAXN;
const int INF=0x3f3f3f3f;
int g[MAXN][MAXN],mg[MAXN][MAXN];
bool flag[MAXN];
struct Node
{
int from,to,next;
int cap;
}edge[MAXM];
int tol;
int n,s,t;
int dep[MAXN];//dep为点的层次
int head[MAXN];
void init()
{
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)//第一条边下标必须为偶数
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].cap=w;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].from=v;
edge[tol].to=u;
edge[tol].cap=0;
edge[tol].next=head[v];
head[v]=tol++;
}
int BFS(int start,int end)
{
int que[MAXN];
int front,rear;
front=rear=0;
memset(dep,-1,sizeof(dep));
que[rear++]=start;
dep[start]=0;
while(front!=rear)
{
int u=que[front++];
if(front==MAXN)front=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap>0&&dep[v]==-1)
{
dep[v]=dep[u]+1;
que[rear++]=v;
if(rear>=MAXN)rear=0;
if(v==end)return 1;
}
}
}
return 0;
}
int dinic(int start,int end)
{
int res=0;
int top;
int stack[MAXN];//stack为栈,存储当前增广路
int cur[MAXN];//存储当前点的后继
while(BFS(start,end))
{
memcpy(cur,head,sizeof(head));
int u=start;
top=0;
while(1)
{
if(u==end)
{
int min=INF;
int loc;
for(int i=0;i<top;i++)
if(min>edge[stack[i]].cap)
{
min=edge[stack[i]].cap;
loc=i;
}
for(int i=0;i<top;i++)
{
edge[stack[i]].cap-=min;
edge[stack[i]^1].cap+=min;
}
res+=min;
top=loc;
u=edge[stack[top]].from;
}
for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)
if(edge[i].cap!=0&&dep[u]+1==dep[edge[i].to])
break;
if(cur[u]!=-1)
{
stack[top++]=cur[u];
u=edge[cur[u]].to;
}
else
{
if(top==0)break;
dep[u]=-1;
u=edge[stack[--top]].from;
}
}
}
return res;
}
void build()
{
init();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(g[i][j]) addedge(i+n,j,INF);
}
}
for(int i=1;i<=n;i++)
{
if(i!=s&&i!=t) addedge(i,i+n,1);
else addedge(i,i+n,INF);
}
}
int main()
{
while(~scanf("%d%d%d",&n,&s,&t))
{
memset(g,0,sizeof(g));
memset(mg,0,sizeof(mg));
memset(flag,false,sizeof(flag));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&g[i][j]);
//mg[i][j]=g[i][j];
}
}
build();
int ans=dinic(s,t+n);
if(ans>=INF) puts("NO ANSWER!");
else
{
int mans=ans;
printf("%d\n",ans);
if(!ans) continue;
for(int i=1;i<=n;i++)
{
if(i==s||i==t) continue;
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
mg[j][k]=g[j][k];
if(j==i||k==i)
g[j][k]=0;
}
}
build();
int re=dinic(s,t+n);
if(re<ans) ans--,flag[i]=true;
else
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
g[j][k]=mg[j][k];
}
}
if(!ans) break;
}
int cnt=0;
for(int i=1;i<=n;i++)
{
if(flag[i])
{
cnt++;
printf("%d%c",i,cnt==mans?'\n':' ');
}
}
}
}
return 0;
}
相关文章推荐
- Friendship (poj 1815 最小点割集+枚举)
- poj 1815 Friendship(最小点割集)
- POJ 1815 Friendship (最小点割集)
- poj 1815(最小割 割集)
- POJ 1815 Friendship(最小割+拆点法)
- POJ 1815 Friendship 最小割
- POJ 1815 Friendship 最小割
- [最小点割集]poj 1815 #贪心枚举
- POJ 1815 - Friendship 求最小割点..要求字典序...枚举+最小割
- POJ1815 Friendship(字典序最小最小割割边集)
- poj 1815 Friendship (最小割+拆点+枚举)
- POJ 1815 Friendship(字典序最小最小割)
- poj 1815 Friendship 字典序最小+最小割
- POJ 1815 Friendship 最小割 + 字典序输出割点
- poj--1815--Friendship(最小割点集)(枚举求最小字典序)
- poj 1815 Friendship (最小割+拆点+枚举)
- 【最小割+枚举】POJ-1815 Friendship
- poj 1815 Friendship 最小割 拆点 输出字典序
- poj 1815 Friendship【最小割】
- poj 1815 Friendship 求字典序最小的最小割