# Light OJ 1108 – Instant View of Big Bang

Algorithm:

1. Make a graph by reverse the edge direction.
2. Run Bellman Ford in reverse Graph and check if there exist a negative weight cycle. Reversing edge doesn’t affect in cycle because if we reverse the edge direction then cycle remain unchanged.
3. If exist a negative cycle then run a DFS on the reverse graph for finding the nodes which are reachable from negative cycle and store them in a vector and after sorting the vector print them.
4. If there is no negative cycle then print impossible.

```
/*
+-+ +-+ +-+
|R| |.| |S|
+-+ +-+ +-+
*/

#include &lt;bits/stdc++.h&gt;

#define pii             pair &lt;int,int&gt;
#define sc              scanf
#define pf              printf
#define Pi              2*acos(0.0)
#define ms(a,b)         memset(a, b, sizeof(a))
#define pb(a)           push_back(a)
#define MP              make_pair
#define db              double
#define ll              long long
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define D(x)            cout&lt;&lt;#x &quot; = &quot;&lt;&lt;(x)&lt;&lt;endl
#define VI              vector &lt;int&gt;
#define DBG             pf(&quot;Hi\n&quot;)
#define MOD             100007
#define MAX             1005
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0)
#define SZ(a)           (int)a.size()
#define sf(a)           scanf(&quot;%d&quot;,&amp;a)
#define sff(a,b)        scanf(&quot;%d%d&quot;,&amp;a,&amp;b)
#define sfff(a,b,c)     scanf(&quot;%d%d%d&quot;,&amp;a,&amp;b,&amp;c)
#define loop(i,n)       for(int i=0;i&lt;n;i++)
#define REP(i,a,b)      for(int i=a;i&lt;b;i++)
#define TEST_CASE(t)    for(int z=1;z&lt;=t;z++)
#define PRINT_CASE      printf(&quot;Case %d: &quot;,z)
#define all(a)          a.begin(),a.end()
#define intlim          2147483648
#define inf             1000000
#define ull             unsigned long long

using namespace std;

struct data
{
int u,v,w;
data(int x, int y, int z)
{
u=x,v=y,w=z;
}
};

int n,m,total_edge;
vector&lt;data&gt; graph;
vector&lt;int&gt;ans,reverse_graph[MAX];
map&lt;int,bool&gt;mp;
int d[MAX];
bool visited[MAX];

void dfs(int u)
{
mp[u]=1;
ans.pb(u);
loop(i,SZ(reverse_graph[u]))
{
int v=reverse_graph[u][i];
if(!mp[v])
dfs(v);
}
}

bool bellmanford()
{
for(int i=1;i&lt;n;i++)
{
loop(i,m)
{
int u=graph[i].u;
int v=graph[i].v;
if(d[u]+graph[i].w&lt;d[v])
{
d[v]=d[u]+graph[i].w;
}
}
}

bool negative_cycle=0;

loop(i,m)
{
int u=graph[i].u;
int v=graph[i].v;
if(d[u]+graph[i].w&lt;d[v])
{
negative_cycle=1;
d[v]=d[u]+graph[i].w;
if(!mp[u])
dfs(u);
}
}
return negative_cycle;
}

void allclear()
{
graph.clear();
ans.clear();
mp.clear();

loop(i,n+2)
{d[i]=inf;reverse_graph[i].clear();}
}

int main()
{
///freopen(&quot;in.txt&quot;,&quot;r&quot;,stdin);
///freopen(&quot;out.txt&quot;,&quot;w&quot;,stdout);
int t;
sf(t);
TEST_CASE(t)
{
int u,v,w;
sff(n,m);
allclear();
loop(i,m)
{
sfff(u,v,w);
graph.pb(data(v,u,w));//reversing graph
reverse_graph[v].pb(u);
}

PRINT_CASE;
if(bellmanford())
{
sort(all(ans));
pf(&quot;%d&quot;,ans[0]);
REP(i,1,SZ(ans))
pf(&quot; %d&quot;,ans[i]);
pf(&quot;\n&quot;);
}
else
pf(&quot;impossible\n&quot;);
}
return 0;
}

```
5 1 vote
Article Rating
Subscribe
Notify of