1. 程式人生 > >Asteroids 【POJ

Asteroids 【POJ

題目連結

思路

二分匹配,每個x對應一個y。

完整程式碼:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef long long ll;
const int maxN=505;
int N, K;
vector<int> vt[maxN];
bool vis[maxN];
int fa[maxN];
bool match(int u)
{
    int len=(int)vt[u].size();
    for(int i=0; i<len; i++)
    {
        int v=vt[u][i];
        if(vis[v]) continue;
        vis[v]=true;
        if(fa[v]==-1 || match(fa[v]))
        {
            fa[v]=u;
            return true;
        }
    }
    return false;
}
int Hungery()
{
    int ans=0;
    for(int i=1; i<=N; i++)
    {
        memset(vis, false, sizeof(vis));
        if(match(i)) ans++;
    }
    return ans;
}
int main()
{
    while(scanf("%d%d", &N, &K)!=EOF)
    {
        for(int i=1; i<=N; i++) vt[i].clear();
        memset(fa, -1, sizeof(fa));
        while(K--)
        {
            int e1, e2;
            scanf("%d%d", &e1, &e2);
            vt[e1].push_back(e2);
        }
        printf("%d\n", Hungery());
    }
    return 0;
}