Battle of floating cloud continent
(Seawar. PAS/C/CPP)
Description
Fu yunzhou is a picturesque island on the East Coast. fishermen who believe in sea gods are fishing for a living in this generation. Fu yunzhou
The leader is called Sibu, who is in charge of the event on the island. Lu Xing is the daughter of the Sibu family. Just like the residents of fuyunzhou,
Lu Xingxing is born with a love for nature and pity for small animals, but unlike everyone, she has long been
Traveling all over the rivers and mountains has a brave and decisive appeal and fighting spirit in the face of danger. During the tour, Lu xingwang recognized
He has mastered the art of war, and master the martial arts. The two friends of yiquxiang joined together and came to fuyunzhou. In this case
As a result, the pirates intrude into the floating cloud continent. In the face of the weak islanders, Lu star resolutely shoulder the heavy responsibility of the Organization to fight back the intruders.
The villages in fuyun continent are connected by one-way roads to form a directed acyclic graph. If you have configured a guard policy in a village
Wei, then the guard can go down a path from the village and install it on all the roads in the path.
Place combat facilities. Lu Xingxing wants to know the minimum number of guards required to make all roads put into combat facilities.
Wei?
Input Format
The first line is an integer N, indicating the number of villages in the floating cloud continent.
In the next n rows, the first number of each row is an integer, where the number of rows I + 1 is DI, indicating that
There are di roads. Each line is followed by an integer of DI, indicating the village to which these roads arrive.
Output Format
An integer (ANS) indicates the minimum number of guards required.
Sample Input
8
1 3
1 7
2 4 5
1 8
1 8
0
2 6 5
0
Sample output
4
Data scope and conventions
2 <= n <= 10 for 30% of data.
For 100% of the data, 2 <= n <=, 0 <= di <n, no duplicate edge or self ring.
Question:
At that time, I was not familiar with the network stream. Later, I responded: Isn't this a bare minimum stream with a lower bound? Unfortunately, I didn't want to ask ......
For more information, see my next blog.
Code:
1 const inf=maxlongint; 2 type node=record 3 from,go,next,v:longint; 4 end; 5 var tot,i,j,n,m,maxflow,l,r,s,t,x,y,ans,ss,tt:longint; 6 h,head,q,cur,indeg,outdeg:array[0..500] of longint; 7 e:array[0..50000] of node; 8 function min(x,y:longint):longint; 9 begin 10 if x<y then exit(x) else exit(y); 11 end; 12 procedure ins(x,y,z:longint); 13 begin 14 inc(tot); 15 e[tot].from:=x;e[tot].go:=y;e[tot].v:=z;e[tot].next:=head[x];head[x]:=tot; 16 end; 17 procedure insert(x,y,z:longint); 18 begin 19 ins(x,y,z);ins(y,x,0); 20 end; 21 function bfs:boolean; 22 var i,x,y:longint; 23 begin 24 fillchar(h,sizeof(h),0); 25 l:=0;r:=1;q[1]:=s;h[s]:=1; 26 while l<r do 27 begin 28 inc(l); 29 x:=q[l]; 30 i:=head[x]; 31 while i<>0 do 32 begin 33 y:=e[i].go; 34 if (e[i].v<>0) and (h[y]=0) then 35 begin 36 h[y]:=h[x]+1; 37 inc(r);q[r]:=y; 38 end; 39 i:=e[i].next; 40 end; 41 end; 42 exit (h[t]<>0); 43 end; 44 function dfs(x,f:longint):longint; 45 var i,y,used,tmp:longint; 46 begin 47 if x=t then exit(f); 48 used:=0; 49 i:=cur[x]; 50 while i<>0 do 51 begin 52 y:=e[i].go; 53 if (h[y]=h[x]+1) and (e[i].v<>0) then 54 begin 55 tmp:=dfs(y,min(e[i].v,f-used)); 56 dec(e[i].v,tmp);if e[i].v<>0 then cur[x]:=i; 57 inc(e[i xor 1].v,tmp); 58 inc(used,tmp); 59 if used=f then exit(f); 60 end; 61 i:=e[i].next; 62 end; 63 if used=0 then h[x]:=-1; 64 exit(used); 65 end; 66 procedure dinic; 67 begin 68 while bfs do 69 begin 70 for i:=0 to n+3 do cur[i]:=head[i]; 71 inc(maxflow,dfs(s,inf)); 72 end; 73 end; 74 procedure init; 75 begin 76 tot:=1; 77 readln(n); 78 s:=0;t:=n+1; 79 ss:=n+2;tt:=n+3; 80 for i:=1 to n do 81 begin 82 read(outdeg[i]); 83 for j:=1 to outdeg[i] do 84 begin 85 read(x); 86 inc(indeg[x]); 87 insert(i,x,inf); 88 insert(i,tt,1); 89 insert(ss,x,1); 90 end; 91 end; 92 for i:=1 to n do 93 begin 94 if indeg[i]=0 then insert(s,i,inf); 95 if outdeg[i]=0 then insert(i,t,inf); 96 end; 97 insert(t,s,inf); 98 end; 99 procedure main;100 begin101 s:=ss;t:=tt;102 maxflow:=0;103 dinic;104 ans:=e[tot].v;105 e[tot].v:=0;e[tot xor 1].v:=0;106 s:=n+1;t:=0;107 maxflow:=0;108 dinic;109 writeln(ans-maxflow);110 end;111 begin112 assign(input,‘input.txt‘);assign(output,‘output.txt‘);113 reset(input);rewrite(output);114 init;115 main;116 close(input);close(output);117 end.
View code