提交时间:2022-07-19 11:51:47
运行 ID: 52289
#include<cstdio> #include<algorithm> #include<ctype.h> #include<cstring> #include<iostream> #include<vector> #include<queue> #include<ctime> #include<cmath> #include<queue> #include<map> #include<set> #include<unordered_map> #include<stack> #define pr pair<int,int> #define eps 1e-8 #define db double using namespace std; inline int read() { int x=0,f=1; char ch=getchar(); while(!isdigit(ch)) { if(ch=='-')f=-1; ch=getchar(); } while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar(); } return x*f; } const int N=20000005,INF=0x7f7f7f7f; int head[50005],to[N],nxt[N],w[N],cnt=1; void add(int u,int v,int wi) { to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; w[cnt]=wi; } int dis[50005],n,m,s,t,now[50005]; bool bfs() { queue<int>Q; fill(dis,dis+2*n+5,INF); dis[s]=0; Q.push(s); now[s]=head[s]; while(!Q.empty()) { int u=Q.front(); Q.pop(); for(int i=head[u]; i; i=nxt[i]) { int v=to[i]; if(w[i]>0&&dis[v]==INF) { dis[v]=dis[u]+1; now[v]=head[v]; if(v==t) return true; Q.push(v); } } } return false; } int dinic(int u,int f) { if(u==t) return f; int ret=0; for(int i=now[u]; i; i=nxt[i]) { int v=to[i]; now[u]=i; if(w[i]>0&&dis[v]==dis[u]+1) { int qwq=dinic(v,min(f,w[i])); w[i]-=qwq; w[i^1]+=qwq; ret+=qwq; f-=qwq; } } return ret; } int awa[N]; int main() { n=read(); s=2*n+1,t=2*n+2; m=read(); for(int i=1; i<=m; i++) { int k=read(); for(int j=1; j<=k; j++) awa[j]=read(); for(int j=1; j<=k; j++) for(int l=j+1; l<=k; l++) add(awa[j],awa[l]+n,INF),add(awa[l]+n,awa[j],0),add(awa[l],awa[j]+n,INF),add(awa[j]+n,awa[l],0); } for(int i=1; i<=n; i++) { add(s,i,1); add(i,s,0); add(i+n,t,1); add(t,i+n,0); } int ans=0; while(bfs()) ans+=dinic(s,INF); printf("%d\n",ans>>1); return 0; }