竹園論壇

標題: 3041 - Asteroids [打印本頁]

作者: domen111    時間: 2015-3-9 22:33
標題: 3041 - Asteroids
本帖最後由 domen111 於 2015-3-9 22:36 編輯

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

這題和TOJ201很類似,解法是轉換成二分圖匹配,把行星轉換成匹配邊,把每個射擊(行or列)轉換成點,不過這篇重點並不在怎麼轉換所以就不多做說明了。

如果這題直接用這篇(tioj 1376)的解法會RE掉,RE code: http://ideone.com/dEm0eN,但tioj那題並不會,大概是因為點數太少了(只有10個點)。

所以重點就是必須要檢查某個點是否走過,不能重複走已經走過的點,也就是code裡面的used陣列

至於RE的原因呢? 我目前覺得應該是遞迴過深,以下是一組Sylveon提供會造成同一點重複find的測資


AC code:
[C++] 純文本查看 復制代碼
#include<iostream>
#include<vector>
#include<cstring>
#define endl '\n'
using namespace std;
typedef long long ll;
int n,k;
vector<int> g[510];
int match[510]={};
bool used[510];
bool find(int i)
{
        used=true;
        for(int j=0;j<g.size();j++)
        {
                int &m=match[g[j]];
                if(m==0 || !used[m] && find(m))
                {
                        m=i;
                        return true;
                }
        }
        return false;
}
int main()
{
        ios::sync_with_stdio(0);
        cin.tie(0);
        
        cin>>n>>k;
        for(int i=0;i<k;i++)
        {
                int a,b;
                cin>>a>>b;
                g[a].push_back(b);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
                memset(used,0,sizeof used);
                if(find(i))
                        ans++;
        }
        cout<<ans<<endl;
}






歡迎光臨 竹園論壇 (http://forum.tfcis.org/) Powered by Discuz! X3.2