Coder-Strike 2014 - Round 1 D. Giving Awards

OpenSoucre發表於2014-04-25

題目的意思是

老闆給n個人發工資,x欠y的工資,the joy of person x from his brand new money reward will be much less,

老闆想避免x後面是y領工資,故利用bfs,找到無後繼的點

在提交程式碼的時候輸出時格式好像沒有oj的平臺那麼嚴,可以包含多餘的空格(註釋的程式碼也可以提交

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

vector<int> ans;
void bfs(vector<vector <int> >& v, vector<bool>& visit, int a){
    if(visit[a]) return;
    visit[a] = true;
    for(int i = 0 ; i < v[a].size(); ++ i )
        bfs(v,visit,v[a][i]);
    ans.push_back(a);
}

int main(){
    int n,m;
    cin >>n >>m;
    vector<vector<int> > v(n+1);
    for(int i = 0 ; i < m ; ++ i){
        int a,b;
        cin >>a >>b;
        v[a].push_back(b);
    }
    vector<bool> visit(n+1,false);
    for(int i = 1; i <= n; ++ i){
        if(visit[i]) continue;
        bfs(v,visit,i);
    }
    copy(ans.begin(),ans.end(),ostream_iterator<int>(cout," "));
    /*
    for(int i = 0 ; i < ans.size(); ++ i){
        if(i == 0) cout<< ans[i];
        else cout<<" "<< ans[i];
    }*/
    cout<<endl;
}

 

 

 

 

相關文章