Description
Problem Description:
Given the graph g= (v,e). Set P is a simple way for g (vertices do not intersect) collection. If each vertex in V happens to be on one of the lines of P, then p is a path overlay of G. The path in P can start at any vertex of V, and the length is arbitrary, specifically, it can be 0. The minimum path coverage for G is the path coverage with the least number of path bars in G. An effective algorithm is designed to find the minimum path coverage of a direction-free graph G.
Programming tasks:
For a given given direction-free graph G, programming to find a minimum path overlay of G.
Input Format
The 1th line of the file has 2 positive integers n and M. n is the number of vertices of the given direction-free graph G, and M is the number of sides of G. The next M-line has 2 positive integers i and j for each row, indicating a forward edge (I,J).
Output Format
Starting with line 1th, each line outputs a path ( no space at the end of the line). The last line of the file is the minimum number of paths.
Input Sample # #:
11 121 21 31 42 53 64 75 86 97 108 119 1110 11
Sample # # of output:
1 4 7 10 112 5 83 6 93
A bit of a reverse-thinking feeling.
Worst case, all points are a path.
Two dots, one less path, one point.
Split into points x and out point y, constitute a binary graph, ans=n-maximum number of matches
About printing:
The edge with 1 flow in the maximum flow is the matching edge, first processing to[i], starting from the point of ind[i]==0 printing
////main.cpp//wang24.3////Created by Candy on 29/11/2016.//copyright©2016 Candy. All rights reserved.//#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;Const intn=2005, m=1e6,inf=1e9;intRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*F;}intn,m,s,t,u,v;structedge{intv,ne,c,f;} E[m<<1];intCnt,h[n];inlinevoidInsintUintVintc) {CNT++; E[CNT].V=v;e[cnt].c=c;e[cnt].f=0; e[cnt].ne=h[u];h[u]=CNT; CNT++; E[CNT].V=u;e[cnt].c=0; e[cnt].f=0; e[cnt].ne=h[v];h[v]=CNT;}intVis[n],d[n],q[n],head=1, tail=1;BOOLBFs () {memset (Vis,0,sizeof(VIS)); memset (d,0,sizeof(d)); Head=tail=1; Q[tail++]=s;d[s]=0; vis[s]=1; while(head!=tail) { intu=q[head++]; for(intI=h[u];i;i=e[i].ne) { intv=e[i].v; if(!vis[v]&&e[i].c>e[i].f) {Vis[v]=1;d [v]=d[u]+1; Q[tail++]=v; if(v==t)return true; } } } return false;}intCur[n];intDfsintUinta) { if(u==t| | a==0)returnA; intflow=0, F; for(int&i=cur[u];i;i=e[i].ne) { intv=e[i].v; if(d[v]==d[u]+1&& (F=dfs (V,min (A,E[I].C-E[I].F)) >0) {Flow+=F; E[I].F+=F; e[((i-1)^1)+1].f-=F; A-=F; if(a==0) Break; } } returnflow;}intDinic () {intflow=0; while(BFS ()) { for(inti=s;i<=t;i++) cur[i]=H[i]; Flow+=DFS (S,inf); } returnflow;}intTo[n],ind[n];voidprint () { for(intu=1; u<=n;u++) for(intI=h[u];i;i=e[i].ne) { intv=e[i].v; if(e[i].c==1&&e[i].f==1) to[u]=v-n,ind[v-n]++; } for(intI=1; i<=n;i++)if(!Ind[i]) { intu=i; while(u!=0) printf ("%d", u), u=To[u]; Putchar ('\ n'); }}intMainintargcConst Char*argv[]) {N=read (); M=read (); s=0; t=n+n+1; for(intI=1; i<=m;i++) {u=read (); v=read (); INS (U,n+v,1); } for(intI=1; i<=n;i++) ins (S,i,1), Ins (N+i,t,1); intans=n-Dinic (); Print (); printf ("%d", ans); return 0;}
Network Flow 24 question minimum path coverage problem