Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstring>
- #include <algorithm>
- #include <stack>
- using namespace std;
- int n, m;
- vector<int>graph[100005];
- vector<int>rev_graph[100005];
- vector<int> v;
- bool visited[100005];
- stack<int>s;
- void dfs(int node){
- visited[node]=true;
- for(int i=0; i<graph[node].size(); i++){
- int sosed=graph[node][i];
- if(!visited[sosed]){
- dfs(sosed);
- }
- }
- s.push(node);
- }
- void rev_dfs(int node){
- visited[node]=true;
- v.push_back(node);
- for(int i=0; i<rev_graph[node].size(); i++){
- int rev_sosed=rev_graph[node][i];
- if(!visited[rev_sosed]){
- rev_dfs(rev_sosed);
- }
- }
- }
- bool special[100005];
- int main()
- {
- cin>>n>>m;
- for(int i=0; i<m; i++){
- int a, b;
- cin>>a>>b;
- graph[a].push_back(b);
- rev_graph[b].push_back(a);
- }
- memset(visited, false, sizeof visited);
- memset(special, false, sizeof special);
- for(int i=1; i<=n; i++){
- if(!visited[i]){
- dfs(i);
- }
- }
- memset(visited, false, sizeof visited);
- while(!s.empty()){
- int node=s.top();
- if(!visited[node]){
- v.clear();
- rev_dfs(node);
- // cout << v.size() << endl;
- if(v.size() > 1) {
- for(int p = 0; p < v.size(); p++) {
- special[v[p]] = true;
- }
- }
- }
- s.pop();
- }
- for(int i=1; i<=n; i++){
- // cout<<v[i];
- if(special[i]){
- cout<<1 << " ";
- }
- else{
- cout<<0 << " ";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement