标签:
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 7171 | Accepted: 2930 |
Description
Input
Output
Sample Input
1 0 2 1 1 2 2 0 0 0
Sample Output
1 1 2
Source
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 11 #define N 505 12 13 int max(int x,int y){return x>y?x:y;} 14 int min(int x,int y){return x<y?x:y;} 15 int abs(int x,int y){return x<0?-x:x;} 16 17 int from[N]; 18 bool visited[N]; 19 int n, m; 20 int map[N][N]; 21 22 int march(int u){ 23 int i, v; 24 for(v=1;v<=n;v++){ 25 if(!visited[v]&&map[u][v]){ 26 visited[v]=true; 27 if(from[v]==-1||march(from[v])){ 28 from[v]=u; 29 return 1; 30 } 31 } 32 } 33 return 0; 34 } 35 36 main() 37 { 38 int i, j, k, u, v; 39 while(scanf("%d %d",&n,&m)==2){ 40 if(n==0&&m==0) break; 41 memset(map,0,sizeof(map)); 42 for(i=0;i<m;i++){ 43 scanf("%d %d",&u,&v); 44 map[u][v]=1; 45 } 46 for(i=1;i<=n;i++){ 47 for(j=1;j<=n;j++){ 48 for(k=1;k<=n;k++){ 49 if(map[i][j]&&map[j][k]&&!map[i][k]) map[i][k]=1; 50 } 51 } 52 } 53 int num=0; 54 memset(from,-1,sizeof(from)); 55 for(i=1;i<=n;i++){ 56 memset(visited,false,sizeof(visited)); 57 if(march(i)) num++; 58 } 59 printf("%d\n",n-num); 60 } 61 }
标签:
原文地址:http://www.cnblogs.com/qq1012662902/p/4639673.html