题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1143
首先用传递闭包,知道一个点是否可以到达另一个点,即mp[i][j]==1表示i可以到j;mp[i][j]==0表示i不可以到j。
然后变成求有向无环图的最大独立集。
这是个经典问题,要变成二分图。
将每个点拆成两个点x和y
如果有边i->j,那么连边ix->jy。
然后求二分图的最大匹配,N-最大匹配就是答案。
#include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<algorithm> #include<cstring> #include<string> #include<cmath> #include<queue> #include<stack> #include<map> #include<utility> #include<set> #include<bitset> #include<vector> #include<functional> #include<deque> #include<cctype> #include<climits> #include<complex> //#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj using namespace std; typedef long long LL; typedef double DB; typedef pair<int,int> PII; typedef complex<DB> CP; #define mmst(a,v) memset(a,v,sizeof(a)) #define mmcy(a,b) memcpy(a,b,sizeof(a)) #define re(i,a,b) for(i=a;i<=b;i++) #define red(i,a,b) for(i=a;i>=b;i--) #define fi first #define se second #define m_p(a,b) make_pair(a,b) #define SF scanf #define PF printf #define two(k) (1<<(k)) template<class T>inline T sqr(T x){return x*x;} template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;} template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-9; inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;} const DB Pi=acos(-1.0); inline int gint() { int res=0;bool neg=0;char z; for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar()); if(z==EOF)return 0; if(z=='-'){neg=1;z=getchar();} for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar()); return (neg)?-res:res; } inline LL gll() { LL res=0;bool neg=0;char z; for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar()); if(z==EOF)return 0; if(z=='-'){neg=1;z=getchar();} for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar()); return (neg)?-res:res; } const int maxN=100; int N,M; int mp[maxN+10][maxN+10]; int first[maxN+100],now; struct Tedge{int v,next;}edge[maxN*maxN+10000]; int ans; inline void addedge(int u,int v) { now++; edge[now].v=v; edge[now].next=first[u]; first[u]=now; } int vis[maxN+100]; int form[maxN+100]; inline int DFS(int u) { int i,v; vis[u]=1; for(i=first[u],v=edge[i].v;i!=-1;i=edge[i].next,v=edge[i].v) if(!form[v] || (!vis[form[v]] && DFS(form[v]))) { form[v]=u; return 1; } return 0; } int main() { freopen("bzoj1143.in","r",stdin); freopen("bzoj1143.out","w",stdout); int i,j,k; N=gint();M=gint(); re(i,1,M){int u=gint(),v=gint();mp[u][v]=1;} re(k,1,N)re(i,1,N)re(j,1,N)if(i!=k && j!=k && i!=j && mp[i][k] && mp[k][j]) mp[i][j]=1; mmst(first,-1);now=-1; re(i,1,N)re(j,1,N)if(mp[i][j])addedge(i,j); ans=0; re(i,1,N) { re(j,1,N)vis[j]=0; ans+=DFS(i); } printf("%d ",N-ans); return 0; }