forked from mrsac7/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
2138 - Reachable Nodes.cpp
47 lines (41 loc) · 949 Bytes
/
2138 - Reachable Nodes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Problem Name: Reachable Nodes
Problem Link: https://cses.fi/problemset/task/2138
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mxN = 5e4 + 5;
vector<bitset<mxN>> dp(mxN);
vector<int> v, adj[mxN];
bool vis[mxN];
void dfs(int s) {
if (vis[s]) return;
vis[s] = 1;
for (auto i: adj[s])
dfs(i);
v.push_back(s);
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m; cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y; cin >> x >> y;
adj[x].push_back(y);
}
for (int i = 1; i <= n; i++)
dfs(i);
for (auto s: v) {
dp[s][s] = 1;
for (auto i: adj[s])
dp[s] |= dp[i];
}
for (int i = 1; i <= n; i++)
cout << dp[i].count() << ' ';
}