Description A team of cave scholars organized a training session in the grate Cave of Byte mountain. In training, every cave scholar will reach the bottom of a room from the highest room. They can only go down. Each successive chamber on a road is lower than its previous one. In addition, every cave scholar embarks from the highest room, along the different roads to the lowest room. Q: How many individuals can participate in the training at the same time?
Task:Write a program: l read into the description of the cave. L Calculate the number of people who can participate in training simultaneously. L OUTPUT the result. The first line of Input has an integer n (2<=n<=200) equal to the number of chambers in the Cavern. Use the 1~n to label the room, the larger the number is the lower. The highest chamber is recorded as 1, and the lowest chamber is n. The following n-1 line is a description of the channel. Line i+1 contains a room with a passage to the first room (only a room larger than the label i). The first number in this line is m,0
<=M
<=(N-i+1) that represents the number of channels that are described. The next M-number is the number of the room with the passage of the first room. The output outputs an integer. It is equal to the maximum number of cave scholars who can participate in training at the same time. Sample Input12
4 3 4) 2 5
1 8
2 9 7
2 6 11
1 8
2 9 10
2 10 11
1 12
2 10 12
1 12
1Sample Output3It's good for your health to brush water problems ... Network flow SB problem is the right to start from 1 or to reach the edge of N 1, the other is the INF
#include <cstdio> #include <iostream> #include <cstring> #include <cstdlib> #include < algorithm> #include <cmath> #include <queue> #include <deque> #include <set> #include <map > #include <ctime> #define LL long long#define INF 0x7ffffff#define pa pair<int,int> #define Pi 3.1415926535897932384626433832795028841971#define S 1#define T nusing namespace Std;inline ll read () {ll X=0,f=1;char C H=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int n,cnt=1,ans;struct edge{int to,next,v;} E[1000010];int head[200010];int h[200010];int q[200010];inline void ins (int u,int v,int W) {e[++cnt].to=v; E[cnt].v=w; E[cnt].next=head[u]; head[u]=cnt;} inline void Insert (int u,int v,int W) {ins (u,v,w); Ins (v,u,0);} inline bool BFs () {memset (h,-1,sizeof (h)); int t=0,w=1; H[s]=0;q[1]=s; while (t<w) {int NoW=Q[++T]; for (int i=head[now];i;i=e[i].next) if (e[i].v&&h[e[i].to]==-1) {h[e[i].to]=h[ now]+1; q[++w]=e[i].to; }} if (H[t]==-1) return 0; return 1;} inline int dfs (int x,int f) {if (x==t| |! f) return F; int w,used=0; for (int i=head[x];i;i=e[i].next) if (e[i].v&&h[e[i].to]==h[x]+1) {W=dfs (E[i].to,min (e[i].v,f-us ed)); E[i].v-=w; E[i^1].v+=w; Used+=w; if (used==f) return F; } if (!used) h[x]=-1; return used;} inline void Dinic () {while (BFS ()) Ans+=dfs (S,inf);} int main () {n=read (); for (int i=1;i<n;i++) {int x=read (); for (int j=1;j<=x;j++) {int to=read (); if (i==1| | to==n) Insert (i,to,1); else insert (i,to,inf); }} dinic (); printf ("%d\n", ans);}
bzoj2929 [Poi1999] Cave climbing line