Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //cf - 506B
- const int N = 1e5 + 100;
- vector<int> g_bi[N], g_uni[N];
- int col[N];
- bool cycle_found;
- set<int> visited;
- int comp = 0;
- vector<int> node;
- bool find_cycle;
- int ache[N];
- void cycle(int u) {
- if(find_cycle) return;
- col[u] = 1;
- for(int v : g_uni[u]) {
- if(col[v] == 1) {
- find_cycle = true;
- return;
- }
- if(col[v] == 0) cycle(v);
- if(find_cycle) return;
- }
- col[u] = 2;
- }
- int vis[N];
- void dfs(int u) {
- vis[u] = comp;
- node.push_back(u);
- for(int v : g_bi[u]) {
- if(vis[v]==0) dfs(v);
- }
- }
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- int n, m;
- cin >> n >> m;
- for(int i = 1; i <= m; i++) {
- int u, v;
- cin >> u >> v;
- g_uni[u].push_back(v);
- g_bi[u].push_back(v);
- g_bi[v].push_back(u);
- ache[u] = ache[v] = 1;
- }
- int ans = 0;
- for(int i = 1; i <= n; i++) {
- ans += ache[i];
- }
- for(int i = 1; i <= n; i++) {
- if(ache[i] && vis[i] == 0) {
- comp++;
- node.clear();
- dfs(i);
- ans--;
- find_cycle = false;
- for(int u : node) {
- if(col[u] == 0 && find_cycle != true) {
- cycle(u);
- }
- }
- ans += find_cycle;
- }
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement