Kill Captain America
Captain America is hiding from Thanos in a maze full of rooms.
The maze is designed in such a way that the room, within it, leads to
another room via gate.
Captain America is hiding only in those rooms
which are accessible directly/indirectly through every other room in the
maze.
Further provided that, the movement from one room (R1) to another room (R2) is one way(unidirectional) only .
Now, you being on Thanos side, want to kill Captain America.
#include <bits/stdc++.h>
#define pb push_back
#define endll "\n";
using namespace std;
void dfs(vector<int> adj[],int i,int vis[],stack<int>&s){
vis[i]=1;
for(int j=0;j<adj[i].size();j++){
if(vis[adj[i][j]]==0){
dfs(adj,adj[i][j],vis,s);
}
}
s.push(i);
}
vector<int> c1;
void dfs1(vector<int> adj[],int i,int vis[] ){
vis[i]=1;
for(int j=0;j<adj[i].size();j++){
if(vis[adj[i][j]]==0){
dfs1(adj,adj[i][j],vis);
}
}
c1.pb(i);
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
int t;cin>>t;
while(t--){
int n,e;cin>>n>>e;
vector<int> adj1[n+1],adj2[n+1];
int u,v;
for(int i=0;i<e;i++){
cin>>u>>v;
adj1[v].pb(u);
adj2[u].pb(v);
}
stack<int> s;
int vis[n+1];memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){
if(vis[i]==0){
dfs(adj1,i,vis,s);
}
}
stack<int> s1;memset(vis,0,sizeof(vis));
c1.clear();
dfs1(adj2,s.top(),vis);
if(c1.size()==0){
cout<<0<<endll;
continue;
}
int f=1;
stack<int> s2;memset(vis,0,sizeof(vis));
dfs(adj1,c1[0],vis,s2);
for(int i=1;i<=n;i++){
if(vis[i]==0){
f=0;break;
}
}
cout<<(f==0?0:c1.size())<<endll;
}
return 0;
}
Monday, 3 August 2020
New
Kill Captain America
About vidyanand kumar
Coding