# Light OJ : 1168 – Wishing Snake

0

Algorithm :

1. Make a graph from input data and Determine the SCC of the graph.
2. Make a new Graph from SCC. Consider Each SCC as a node of this graph.
3. If any Node of SCC_graph have more than 1 out degree and It is not possible to traverse all node from the SCC which contain’s the node 0 of the input graph then Print “NO”.
4. Otherwise Print “YES”;

/*
+-+ +-+ +-+
|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;

const int fx[]={+1,-1,+0,+0};
const int fy[]={+0,+0,+1,-1};

vector&lt;int&gt;graph[MAX],rev_graph[MAX],scc_graph[MAX];
int visited[MAX];
int node[MAX],scc_num[MAX],node_num,scc;
stack&lt;int&gt;st;
bool test,scc_vis[MAX];

void allclear()
{
loop(i,node_num+2)
{
visited[i]=0;
graph[i].clear();
rev_graph[i].clear();
scc_num[i]=0;
}
loop(i,scc+2)
{
scc_graph[i].clear();
scc_vis[i]=0;
}
node_num=0;
scc=0;
}

void dfs1(int u)
{
visited[u]++;
loop(i,SZ(graph[u]))
{
int v=graph[u][i];
if(visited[v]==0)
dfs1(v);
}
st.push(u);
}

void dfs2(int u)
{
visited[u]++;
scc_num[u]=scc;
loop(i,SZ(rev_graph[u]))
{
int v=rev_graph[u][i];
if(visited[v]!=2)
dfs2(v);
}
}

void dfs3(int u)
{
scc_vis[u]=1;
if(SZ(scc_graph[u])&gt;1)
{test=0;return;}
loop(i,SZ(scc_graph[u]))
dfs3(scc_graph[u][i]);
}

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)
{
ms(node,-1);
int n,m,u,v;
node_num=0;
node[0]=node_num;
sf(n);
loop(i,n)
{
sf(m);
loop(j,m)
{
sff(u,v);
if(node[u]==-1) node[u]=++node_num;
if(node[v]==-1) node[v]=++node_num;
graph[node[u]].pb(node[v]);
rev_graph[node[v]].pb(node[u]);
}
}

loop(i,node_num)
{
if(visited[i]==0)
dfs1(i);
}

scc=0;
while(!st.empty())
{
u=st.top();
st.pop();
if(visited[u]!=2)
{
scc++;
dfs2(u);
}
}

loop(i,node_num)
{
loop(j,SZ(graph[i]))
{
u=graph[i][j];
if(scc_num[i]!=scc_num[u])
scc_graph[scc_num[i]].pb(scc_num[u]);
}
}

test=1;
dfs3(scc_num[0]);
if(test)
{
REP(i,1,scc+1)
{
if(!scc_vis[i])
test=0;
}
}
PRINT_CASE;
if(test)
pf(&quot;YES\n&quot;);
else
pf(&quot;NO\n&quot;);
allclear();

}
return 0;
}

# Light OJ : 1417 – Forwarding Emails

0

/*
+-+ +-+ +-+
|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             50005
#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;

int n,dfsnum,dis[MAX];
vector&lt;int&gt; graph[MAX],rev_graph[MAX];
bool mp[MAX],mp1[MAX],mp2[MAX];

void allclear()
{
loop(i,n+2)
{
graph[i].clear();
rev_graph[i].clear();
dis[i]=0;
mp[i]=mp1[i]=mp2[i]=0;
}
dfsnum=0;
}

void dfs1(int u)
{
mp[u]=1;
dfsnum++;
loop(i,SZ(graph[u]))
{
int v=graph[u][i];
if(!mp[v])
dfs1(v);
}
dis[u]=++dfsnum;
}

void dfs2(int u)
{
mp1[u]=1;
loop(i,SZ(rev_graph[u]))
{
int v=rev_graph[u][i];
if(!mp1[v])
dfs2(v);
}
}

void dfs3(int u)
{
mp2[u]=1;
dfsnum++;
loop(i,SZ(graph[u]))
{
int v=graph[u][i];
if(!mp2[v])
dfs3(v);
}
}

void dfs4(int u)
{
mp2[u]=0;
loop(i,SZ(graph[u]))
{
int v=graph[u][i];
if(mp2[v])
dfs4(v);
}
}

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)
{
sf(n);
allclear();
int a,b;
loop(i,n)
{
sff(a,b);
graph[a].pb(b);
rev_graph[b].pb(a);
}
REP(i,1,n+1)
{
if(!mp[i])
dfs1(i);
}

vector&lt;pii&gt;top_sort;
REP(i,1,n+1)
{
top_sort.pb(MP(dis[i],i));
}

sort(top_sort.rbegin(),top_sort.rend());
vector&lt;int&gt;scc;

loop(i,SZ(top_sort))
{
int u=top_sort[i].ss;
if(!mp1[u])
{
scc.pb(u);
dfs2(u);
}
}

top_sort.clear();
loop(i,SZ(scc))
{
int u=scc[i];
if(!mp2[u])
{
dfsnum=0;
dfs3(u);
top_sort.pb(MP(dfsnum,u));
dfs4(u); // this DFS is clearing only visited node.
}
}

sort(top_sort.rbegin(),top_sort.rend());
PRINT_CASE;
if(SZ(top_sort)==1)
pf(&quot;%d\n&quot;,top_sort[0].ss);
else
{
loop(i,SZ(top_sort)-1)
{
if(top_sort[i].ff!=top_sort[i+1].ff) // This is for determine the smallest one.
{
pf(&quot;%d\n&quot;,top_sort[i].ss);
break;
}
}
}
}
return 0;
}

# Light OJ 1210 – Efficient Traffic System

0

/*
+-+ +-+ +-+
|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             20010
#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;

int n,m;
vector&lt;int&gt;graph[MAX],reverse_graph[MAX];
int finish[MAX],start,scc,belong[MAX],indegree[MAX],outdegree[MAX];
bool visited[MAX];

bool test(pii a, pii b)
{
return a.ff&gt;b.ff;
}

void dfs1(int n)
{
visited[n]=1;
start++;
loop(i,SZ(graph[n]))
{
int v=graph[n][i];
if(!visited[v])
dfs1(v);
}
finish[n]=++start;
}

void dfs2(int n)
{
visited[n]=1;
belong[n]=scc;
loop(i,SZ(reverse_graph[n]))
{
int v=reverse_graph[n][i];
if(visited[v]==0)
{
dfs2(v);
}
}
}

void dfs3(int u)
{
visited[u]=1;
loop(i,SZ(graph[u]))
{
int v=graph[u][i];
if(belong[u]!=belong[v])
{
indegree[belong[v]]++;
outdegree[belong[u]]++;
}
if(!visited[v])
dfs3(v);
}
}

void allclear()
{
loop(i,n+2)
{
graph[i].clear();
reverse_graph[i].clear();
visited[i]=0;
belong[i]=-1;
indegree[i]=0;
finish[i]=0;
outdegree[i]=0;
}
start=0;
scc=0;
}

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)
{
sff(n,m);
allclear();
while(m--)
{
int u,v;
sff(u,v);
graph[u].pb(v);
reverse_graph[v].pb(u);
}

vector&lt;pii&gt;v;
for(int i=1; i&lt;=n; i++)
if(!visited[i])
dfs1(i);
for(int i=1; i&lt;=n; i++)
{
v.pb(MP(finish[i],i));
}
sort(all(v),test);
loop(i,n+2)
visited[i]=0;

loop(i,SZ(v))
{
int u=v[i].ss;
if(!visited[u])
{
scc++;
dfs2(u);
}
}

loop(i,n+2)
visited[i]=0;
for(int i=1; i&lt;=n; i++)
{
if(!visited[i])
dfs3(i);
}

int in=0,out=0;
for(int i=1; i&lt;=scc &amp;&amp; scc&gt;1; i++)
{
if(!indegree[i])
in++;
if(!outdegree[i])
out++;
}
PRINT_CASE;
pf(&quot;%d\n&quot;,max(in,out));
}
return 0;
}

# Light OJ 1034 – Hit the Light Switches

0

/*
+-+ +-+ +-+
|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             10005
#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;

int n,m;
vector&lt;int&gt;graph[MAX],topsort;
bool visited[MAX],test;

void allclear()
{
loop(i,n+2)
{
graph[i].clear();
visited[i]=0;
}
topsort.clear();
}

void dfs(int u)
{
visited[u]=1;
loop(i,SZ(graph[u]))
{
int v=graph[u][i];
if(!visited[v])
dfs(v);
}
if(test)
topsort.pb(u);
}

int main()
{
///freopen(&quot;in.txt&quot;,&quot;r&quot;,stdin);
///freopen(&quot;out.txt&quot;,&quot;w&quot;,stdout);
int t,a,b;
sf(t);
TEST_CASE(t)
{
sff(n,m);
loop(i,m)
{
sff(a,b);
graph[a].pb(b);
}
test=1;
int ans=0;
REP(i,1,n+1)
{
if(!visited[i])
dfs(i);
}

test=0;
loop(i,n+2) visited[i]=0;
for(int i=SZ(topsort)-1; i&gt;=0; i--)
{
int u=topsort[i];
if(!visited[u])
{
dfs(u);
ans++;
}
}
PRINT_CASE;
pf(&quot;%d\n&quot;,ans);
allclear();
}
return 0;
}

Another solution of this problem using Strongly Connected components(SCC) is here…

/*
+-+ +-+ +-+
|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             10005
#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;

int n,m;
vector&lt;int&gt;graph[MAX],reverse_graph[MAX],scc;
vector&lt;pii&gt;v;
int indegree[MAX],topsort[MAX],finish;
bool visited[MAX];

void allclear()
{
loop(i,n+2)
{
graph[i].clear();
reverse_graph[i].clear();
indegree[i]=0;
visited[i]=0;
topsort[i];
}
scc.clear();
v.clear();
finish=0;
}

void dfs1(int u)
{
finish++;
visited[u]=1;
loop(i,SZ(graph[u]))
{
int v=graph[u][i];
if(!visited[v])
dfs1(v);
}
topsort[u]=++finish;
}

void dfs2(int u)
{
visited[u]=1;
loop(i,SZ(reverse_graph[u]))
{
int v=reverse_graph[u][i];
if(!visited[v])
dfs2(v);
}
}

void dfs3(int u)
{
visited[u]=1;
loop(i,SZ(graph[u]))
{
int v=graph[u][i];
if(!visited[v])
dfs3(v);
}
}
int main()
{
///freopen(&quot;in.txt&quot;,&quot;r&quot;,stdin);
///freopen(&quot;out.txt&quot;,&quot;w&quot;,stdout);
int t,a,b;
sf(t);
TEST_CASE(t)
{
sff(n,m);
loop(i,m)
{
sff(a,b);
graph[a].pb(b);
reverse_graph[b].pb(a);
indegree[b]++;
}

int ans=0;
REP(i,1,n+1)
{
if(!visited[i])
dfs1(i);
}

REP(i,1,n+1)
{
v.pb(MP(topsort[i],i));
}
sort(all(v));
loop(i,n+2)
visited[i]=0;

for(int i=SZ(v)-1; i&gt;=0; i--)
{
int u=v[i].ss;
if(!visited[u])
{
scc.pb(u);
dfs2(u);
}
}

loop(i,n+2)
visited[i]=0;

loop(i,SZ(scc))
{
if(!visited[scc[i]])
{
ans++;
dfs3(scc[i]);
}
}
PRINT_CASE;
pf(&quot;%d\n&quot;,ans);
allclear();
}
return 0;
}