-
Notifications
You must be signed in to change notification settings - Fork 0
/
2502.cpp
112 lines (105 loc) · 1.63 KB
/
2502.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=128;
const int M=65536;
const int I=1e9;
struct edge{
int t,d,next;
};
int h[N];
int a[N][N];
edge e[M];
int d[N],gap[N],cur[N],t[M],s[N];
int n,m,i,j,k,u,v,w,ans,S,T,U,V;
void addedge(int u,int v,int w){
h[0]++;
e[h[0]]=(edge){v,w,h[u]};
h[u]=h[0];
}
int isap(int S,int T,int n){
int i,k,u,v,sum;
memset(t,0,sizeof(t));
memset(cur,-1,sizeof(cur));
memset(gap,0,sizeof(gap));
gap[0]=n;
sum=0;
while(t[S]<=n){
u=s[0]?e[s[s[0]]].t:S;
if(u==T){
k=I;
for(i=1;i<=s[0];i++)k=min(k,e[s[i]].d);
sum+=k;
for(i=1;i<=s[0];i++){
e[s[i]].d-=k;
e[s[i]^1].d+=k;
}
s[0]=0;
}else{
v=cur[u]==-1?h[u]:cur[u];
while(v!=-1){
if(e[v].d>0 && t[e[v].t]==t[u]-1)break;
v=e[v].next;
}
cur[u]=v;
if(v==-1){
gap[t[u]]--;
if(!gap[t[u]])return sum;
t[u]++;
gap[t[u]]++;
if(s[0])s[0]--;
}else{
s[0]++;
s[s[0]]=v;
}
}
}
return sum;
}
int main(){
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&m);
for(j=1;j<=m;j++){
scanf("%d",&k);
a[i][k]=1;
d[i]--;
d[k]++;
}
}
memset(h,-1,sizeof(h));
U=n+1;
V=n+2;
S=n+3;
T=n+4;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++)
if(a[i][j]){
addedge(i,j,I);
addedge(j,i,0);
}
if(d[i]>0){
addedge(S,i,d[i]);
addedge(i,S,0);
}else{
addedge(i,T,-d[i]);
addedge(T,i,0);
}
addedge(U,i,I);
addedge(i,U,0);
addedge(i,V,I);
addedge(V,i,0);
}
addedge(V,U,I);
u=h[0];
addedge(U,V,0);
v=h[0];
isap(S,T,n+4);
ans=e[v].d;
e[u].d=e[v].d=0;
ans-=isap(V,U,n+2);
printf("%d\n",ans);
return 0;
}