Poj--1815--friendship (Minimum cut point set) (enumeration to find the smallest dictionary order)

Source: Internet
Author: User

Friendship
Time Limit: 2000MS Memory Limit: 20000KB 64bit IO Format: %i64d &%i64u

Submit Status

Description

In the modern society, each person has the his own friends. Since all the people is very busy, they communicate with each and only by phone. You can assume this 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 would also know a ' s number. 

Sometimes, someone may meet something bad which makes him lose touch with all the others. For example, he or lose his phone number book and a change his phone number at the same time. 

In this problem, you'll know the relations between every and among N people. To make it easy, we number these n people by,..., N. Given-Special people with the number s and T, when some people meet bad things, S could lose touch with T. Your job is to compute the minimal number of people so can make this situation happen. It is supposed this bad thing would 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 are not equ Al 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 would be 1, otherwise the number would be 0. 

You can assume that number of 1s would not exceed for the input. 

Output

If There is no-lose touch with B, print "No answer!" Otherwise, the first line contains a single number T, which was the minimal number you had got, and if T is not zero, the Second line was needed, which contains t integers in ascending order that indicate the number of people who meet bad things . The integers is 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 are A1, A2, ..., at (1 <= A1 < A2 < ...< at <=n), the score would be (a1-1) *n^t+ (a2-1) *n^ (t-1) +...+ (At-1) *n. The input would assure that there won ' t is the other solutions with the minimal score. 

Sample Input

3 1 31 1 01 1 10 1 1

Sample Output

12
n Individuals, the matrix of N*n represents the relationship between each person, 1 for recognition, 0 for not recognizing, now to delete the fewest points, so that s and H do not know, output dictionary order the smallest scheme
A relatively ordinary minimum cut point set, after the construction of the maximum flow we find each time the maximum flow is the minimum cost, enumeration of each can be deleted point, if the deletion of this point after the maximum flow return value decreases, indicating that the point deletion is meaningful, we will add him to the group of deletions, the next traversal, If S has not reached H, it is stated that the minimum cut purpose has been achieved
#include <cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace std;# Define MAXN 600#define maxm 1000000#define INF 0x3f3f3f3fint n,s,h,cnt;int VIS[MAXN],DIS[MAXN],CUR[MAXN],HEAD[MAXN], Rec[maxn];int map[maxn][maxn],temp[maxn][maxn];struct node{int U,v,cap,flow,next;} edge[maxm];void Add (int a,int b,int c) {node E={a,b,c,0,head[a]};edge[cnt]=e;head[a]=cnt++;node E1={a,b,0,0,head[b]}; edge[cnt]=e1;head[b]=cnt++;} BOOL BFS (int s,int t) {queue<int>q;memset (vis,0,sizeof (Vis)); memset (dis,-1,sizeof (dis)); Q.push (s); vis[s]=1; Dis[s]=0;while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=head[u];i!=-1;i=edge[i].next) {node e=edge[i];if ( E.CAP&GT;E.FLOW&AMP;&AMP;!VIS[E.V] {vis[e.v]=1;dis[e.v]=dis[e.u]+1;if (e.v==t) return True;q.push (E.V);}} return false;} int DFS (int x,int a,int t) {if (a==0| | x==t) return a;int flow=0,f;for (int &i=cur[x];i!=-1;i=edge[i].next) {node &e=edge[i];if (DIS[X]+1==DIS[E.V] && (F=dfs (E.v,min (e.cap-e.flow,a), T)) >0) {a-=f;fLow+=f;edge[i].flow+=f;edge[i^1].flow-=f;if (a==0) break;}} return flow;} int maxflow (int s,int t) {int flow=0;while (BFS (s,t)) {memcpy (cur,head,sizeof (head)); Flow+=dfs (s,inf,t);} return flow;} void Getmap () {Cnt=0;memset (head,-1,sizeof (head)); for (int i=1;i<=n;i++)//Minimum cut build {for (int j=1;j<=n;j++) {if (map[ I][j]) {if (i==j) {if (i==s| | I==H) Add (I,i+n,inf); Elseadd (i,i+n,1);} Elseadd (I+n,j,inf);}}} int main () {while (scanf ("%d%d%d", &n,&s,&h)!=eof) {memset (map,0,sizeof (map)); Memset (Head,-1,sizeof ( Head), cnt=0;for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {scanf ("%d", &map[i][j]);}} Getmap (), if (Map[s][h]) {printf ("NO answer!\n"); continue;} int Ans=maxflow (s,h+n); if (ans==0) {printf ("0\n"); continue;} int top=0;for (int i=1;i<=n;i++) {if (i==s| | I==H) continue;for (int j=1;j<=n;j++)//Enumerate every point that can be deleted {temp[i][j]=map[i][j];temp[j][i]=map[j][i];map[i][j]=map[j][ i]=0;} Getmap (); int T=maxflow (S,H+N); if (T<ans)//If the cost is reduced after deletion rec[top++]=i,ans=t;else{for (int j=1;j<=n;j++) {Map[i][j] =temp[i][j];map[j][i]=temp[j][i];//if not reduced, reply}}}printf ("%d\n", top), for (int i=0;i<top;i++) {if (i) printf ("");p rintf ("%d", Rec[i]);} printf ("\ n");} return 0;}

Poj--1815--friendship (Minimum cut point set) (enumeration to find the smallest dictionary order)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.