题目的意思是
老板给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; }
)