Login
Register
Problem list
Online status
Huhu_Miao
:
2024-11-03 01:42:46
/** * author: Huhu_Miao * created: 2024.11.3 01:41:00 (UTC+8) **/ #include
struct node { bool exist; int in_degree; }; void solve(){ int n , m; std::cin >> n >> m; std::vector
> succ(n,std::vector
(0)); std::vector
nodes(n,{true,0}); while(m--){ int from , to; std::cin >> from >> to; nodes[to].in_degree++; succ[from].push_back(to); } bool zero_in_degree_node_exist = false; do{ zero_in_degree_node_exist = false; for(int i = 0 ; i < n ; i++){ if(nodes[i].in_degree == 0 && nodes[i].exist){ zero_in_degree_node_exist = true; nodes[i].exist = false; for(int elem : succ[i]) nodes[elem].in_degree--; break; } } }while(zero_in_degree_node_exist); int ans = 0; for(auto node:nodes){ if(node.in_degree == 0) ans++; } std::cout << ans << '\n'; } 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