Asteroids

http://poj.org/problem?id=3041

 1 #include<cstdio>

 2 #include<cstring>

 3 #include<algorithm>

 4 #include<vector>

 5 using namespace std;

 6 const int MAXN=555;

 7 int n,k;

 8 bool use[MAXN];

 9 int from[MAXN],ans;

10 vector<int>g[MAXN];

11 bool match(int x)

12 {

13     for(int i=0;i<g[x].size();i++)

14     {

15         if(!use[g[x][i]]){

16             use[g[x][i]]=true;

17             if(from[g[x][i]]==-1||match(from[g[x][i]])){

18                 from[g[x][i]]=x;

19                 return true;

20             }

21         }

22     }

23     return false;

24 }

25 

26 int hun()

27 {

28 

29     ans=0;

30     memset(from,255,sizeof(from));

31     for(int i=1; i<=n; i++)

32     {

33         memset(use,false,sizeof(use));

34         if(match(i))

35         ans++;

36     }

37     return ans;

38 }

39 int main()

40 {

41     int a,b;

42    while(scanf("%d%d",&n,&k)!=EOF){

43     for(int i=1;i<=k;i++)

44     {

45         scanf("%d%d",&a,&b);

46         g[a].push_back(b);

47     }

48     printf("%d\n",hun());

49    }

50     return 0;

51 }
View Code
 1 #include<cstdio>

 2 #include<cstring>

 3 #include<algorithm>

 4 #include<vector>

 5 using namespace std;

 6 const int MAXN=520;

 7 int n,k;

 8 bool use[MAXN];

 9 int match[MAXN],ans;

10 int g[MAXN][MAXN];

11 bool dfs(int x)

12 {

13     for(int i=1;i<=n;i++)

14     {

15         if(!use[i]&&g[x][i]){

16             use[i]=true;

17             if(match[i]==-1||dfs(match[i])){

18                 match[i]=x;

19                 return true;

20             }

21         }

22     }

23     return false;

24 }

25 

26 int hun()

27 {

28 

29     ans=0;

30     memset(match,-1,sizeof(match));

31     for(int i=1; i<=n; i++)

32     {

33         memset(use,false,sizeof(use));

34         if(dfs(i))

35         ans++;

36     }

37     return ans;

38 }

39 int main()

40 {

41     int a,b;

42     while(scanf("%d%d",&n,&k)!=EOF)

43     {

44         memset(g,0,sizeof(g));

45         for(int i=1; i<=k; i++)

46         {

47             scanf("%d%d",&a,&b);

48              g[a][b]=1;

49         }

50         printf("%d\n",hun());

51     }

52     return 0;

53 }
View Code

 

你可能感兴趣的:(id)