Light OJ : 1417 – Forwarding Emails

Problem Link : http://www.lightoj.com/volume_showproblem.php?problem=1417

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

```