「luogu2764」最小路径覆盖问题
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=160,M=6010; 4 int n,m,link[N<<1]; 5 vector<int>point[N<<1]; 6 bool used[N<<1],start[N]; 7 bool find(int k){ 8 for(int i=0;i<point[k].size();i++)if(!used[point[k][i]]){ 9 used[point[k][i]]=1; 10 if(!link[point[k][i]]||find(link[point[k][i]])){ 11 link[point[k][i]]=k; 12 return 1; 13 } 14 } 15 return 0; 16 } 17 bool vis[N]; 18 int main(){ 19 int t1,t2,ans=0; 20 scanf("%d%d",&n,&m); 21 for(int i=1;i<=m;i++){ 22 scanf("%d%d",&t1,&t2); 23 point[t2].push_back(t1+n); 24 } 25 for(int i=1;i<=n;i++){ 26 memset(used,0,sizeof(used)); 27 if(find(i)) ans++; 28 } 29 ans=n-ans; 30 for(int i=1;i<=n;i++){ 31 if(vis[i]) continue; 32 vis[i]=1; 33 t1=link[i+n]; 34 while(t1){ 35 vis[t1]=start[t1]=1; 36 t1=link[t1+n]; 37 } 38 } 39 for(int i=1;i<=n;i++)if(!start[i]){ 40 t1=i; 41 while(t1){ 42 printf("%d ",t1); 43 t1=link[t1+n]; 44 } 45 printf("\n"); 46 } 47 printf("%d",ans); 48 }