标签:
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 7455 | Accepted: 3053 |
Description
Input
Output
Sample Input
1 0 2 1 1 2 2 0 0 0
Sample Output
1 1
题解:需要加上floyd,用上vector 竟然re了;
ac代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define mem(x,y) memset(x,y,sizeof(x)) 7 const int MAXN=510; 8 int mp[MAXN][MAXN],vis[MAXN],usd[MAXN]; 9 int N; 10 bool dfs(int u){ 11 for(int i=1;i<=N;i++){ 12 if(!vis[i]&&mp[u][i]){ 13 vis[i]=1; 14 if(!usd[i]||dfs(usd[i])){ 15 usd[i]=u;return true; 16 } 17 } 18 } 19 return false; 20 } 21 void floyd(){ 22 for(int i=1;i<=N;i++) 23 for(int j=1;j<=N;j++) 24 if(mp[i][j]==0){ 25 for(int k=1;k<=N;k++) 26 if(mp[i][k]&&mp[k][j]) 27 mp[i][j]=1; 28 } 29 } 30 int main(){ 31 int M; 32 while(scanf("%d%d",&N,&M),N|M){ 33 int a,b; 34 mem(mp,0);mem(usd,0); 35 while(M--){ 36 scanf("%d%d",&a,&b); 37 mp[a][b]=1; 38 } 39 int ans=0; 40 floyd(); 41 for(int i=1;i<=N;i++){ 42 mem(vis,0); 43 if(dfs(i))ans++; 44 } 45 printf("%d\n",N-ans); 46 } 47 return 0; 48 }
re代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 #define mem(x,y) memset(x,y,sizeof(x)) 8 const int MAXN=510; 9 int vis[MAXN],usd[MAXN]; 10 vector<int>vec[MAXN]; 11 int N; 12 bool dfs(int u){ 13 for(int i=0;i<vec[u].size();i++){ 14 int v=vec[u][i]; 15 if(!vis[v]){ 16 vis[v]=1; 17 if(!usd[v]||dfs(usd[v])){ 18 usd[v]=u;return true; 19 } 20 } 21 } 22 return false; 23 } 24 void floyd(){ 25 for(int i=1;i<=N;i++){ 26 for(int k=0;k<vec[i].size();k++) 27 for(int j=0;j<vec[vec[i][k]].size();j++){ 28 vec[i].push_back(vec[vec[i][k]][j]); 29 } 30 } 31 } 32 int main(){ 33 int M; 34 while(scanf("%d%d",&N,&M),N|M){ 35 int a,b; 36 mem(usd,0); 37 for(int i=0;i<=N;i++)vec[i].clear(); 38 while(M--){ 39 scanf("%d%d",&a,&b); 40 vec[a].push_back(b); 41 } 42 floyd(); 43 int ans=0; 44 for(int i=1;i<=N;i++){ 45 mem(vis,0); 46 if(dfs(i))ans++; 47 } 48 printf("%d\n",N-ans); 49 } 50 return 0; 51 }
Treasure Exploration(二分最大匹配+floyd)
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4952997.html