HTTP://BEGIN.LYDSY.COM/JUDGEONLINE/PROBLEM.PHP?ID=2604 Description Headquarters recently intended to send a secret message to the following N staff. Because it is confidential, it is only possible to pass the message one to the other, that is, everyone knows the message and can only pass it on to a number of people whom he can convey and who has not yet known the message. For A, b two people, there may be a can send messages to B, and B can not send messages to a case. Now headquarters in order to prevent the disclosure of information, you are ordered to calculate at least how many people at the beginning of the headquarters to tell the news, in order to ensure that eventually everyone knows the news. Input first line, N, M.
Next M-line, two digits A, b for each line, indicates that a is capable of transmitting messages to number B.
(N person's number is 1~n)
1≤n≤100 000
1≤m≤300 output A number, at least the number of people who need to be notified by Headquarters sample Input
4 31 44) 31 2
Sample Output
2
#include <cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<queue>#include<stack>#include<algorithm>using namespacestd;#defineN 100100#defineINF 0XFFFFFFFstructnode{intV, Next;} A[n*3];intHead[n], CNT, N, M;BOOLUsed[n];intMx[n], my[n], depth;///The matched endpoint of the record, 0 indicates an unmatchedintDx[n], dy[n];///when a BFS is layered, the record point is in the same layer, 1 means not layeredvoidInit () {CNT=0; memset (Head,-1,sizeof(head));}voidADD (intUintv) {A[CNT].V=v; A[cnt].next=Head[u]; Head[u]= cnt++;}BOOLBFS ()///if you find that Y has an augmented path on this side, return 1, or 0.{Queue<int>Q; Depth=INF; memset (DX,-1,sizeof(DX)); memset (DY,-1,sizeof(DY)); for(intI=1; i<=n; i++) { if(Mx[i] = =false) {Dx[i]=0; Q.push (i); } } while(Q.size ()) {intU =Q.front (); Q.pop (); if(Dx[u] > depth) Break;///We 've found the augmented path, no need to look for the lower for(intJ=head[u]; j!=-1; j=A[j].next) { intv =a[j].v; if(Dy[v] = =-1) {Dy[v]= Dx[u] +1; if(My[v] = =false) Depth=Dy[v]; Else{dx[My[v]]= Dy[v] +1; Q.push (My[v]); } } } } if(Depth = =INF)return false; return true;}BOOLFind (inti) { for(intJ=head[i]; j!=-1; j=A[j].next) { intv =a[j].v; if(!used[v] && dx[i] = = dy[v]-1) {Used[v]=true; if(My[v] && dy[v] = =depth)Continue;///not on the next level because the lower layer has not been augmented if( ! MY[V] | |Find (My[v])) {My[v]=i; Mx[i]=v; return true; } } } return false;}intKarp () {intAns =0; memset (Mx,false,sizeof(Mx)); memset (My,false,sizeof(My)); while(BFS () = =true ) { ///If there is an augmented pathmemset (Used,false,sizeof(used)); for(intI=1; i<=n; i++) { if( ! Mx[i] && Find (i) = =true) ans++; } } returnans;}intMain () {intm, I, X, y; scanf ("%d%d", &n, &m); Init (); for(i=1; i<=m; i++) {scanf ("%d%d", &x, &y); ADD (x, y); } intAns =Karp (); printf ("%d\n", N-ans); return 0;}
(Minimum path overlay) news message delivery (Hust OJ 2604)