Login
Register
Problem list
Online status
Huhu_Miao
:
2024-11-20 09:02:44
/** * author: Huhu_Miao * created: 2024.11.20 09:01:00 (UTC+8) **/ #include
void printVector(const std::vector
& vec){ for(const int & elem : vec){ std::cout << elem << ' '; } std::cout << '\n'; } void solve(){ int n, m; std::cin >> n >> m; std::vector
inDegree(n+1,0); std::vector
> g(n+1,std::vector
(0)); std::vector
ans; while(m--){ int from, to; std::cin >> from >> to; g[from].push_back(to); inDegree[to]++; } std::queue
q; for(int i = 1 ; i < n+1 ; i++){ if(inDegree[i] == 0) q.push(i); } while(!q.empty()){ ans.push_back(q.front()); for(auto elem : g[q.front()]){ inDegree[elem]--; if(inDegree[elem] == 0) q.push(elem); } q.pop(); } if(ans.size() != n){ std::cout << 0 << '\n'; }else{ printVector(ans); } } int main(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int T; std::cin>>T; while(T--) solve(); return 0; }
Post Your Comment