hdu6165 (topological sort +tarjan indent)

Source: Internet
Author: User

Test instructions: Whether any two points can be reached;

Problem-solving ideas: First, the diagram is simplified, such as the assumption that there is a ring, then, the ring within the two points must be able to reach each other, then do not consider the point within the ring, it is simple to think of the Tarjan algorithm will be the ring into a point, and then is to determine the end of the point after the figure of any Can reach each other must have a path to connect all points, through the topology sort, if the same level of two or more in the ranking is 0 o'clock so certainly can not reach, because there is no path to connect the two points, the problem belongs to the template-type problem, as long as the idea is correct, it can be written, seemingly there are violent Dfs, not to see yet;

Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
UsingNamespaceStd
Constint maxn=2e5+5;
Constint maxm=5e5+5;
int Head[maxn],tot;
int LOW[MAXN],DFN[MAXN],STA[MAXN],BEL[MAXN];The value of the Bel array is 1~SCC
int index,top;
int SCC;Number of strongly connected components
BOOL INSTACK[MAXN];
int NUM[MAXN];Each strong connected component contains the number of points, array number 1~SCC
num arrays do not necessarily need to be combined with actual
int n,m;

struct Edge
{
int from,to,nxt;
}EDGE[MAXM];

void Addedge (int U,int v)
{
Edge[tot].from=u;
Edge[tot].to=v;
Edge[tot].nxt=head[u];
head[u]=tot++;
}//chain forward star deposit diagram;

void Tarjan (int u)
{
int V;
Low[u]=dfn[u]=++index;
Sta[top++]=u;
instack[u]=True
Forint i=head[u];i!=-1;I=EDGE[I].NXT)
{
v=edge[i].to;
if (!dfn[v])
{
Tarjan (v);
if (Low[u]>low[v]) low[u]=low[v];
}Elseif (Instack[v]&&low[u]>dfn[v])
LOW[U]=DFN[V];
}
if (Low[u]==dfn[u])
{
scc++;
Do
{
V=sta[--top];
instack[v]=False
BEL[V]=SCC;
num[scc]++;
}while (V!=U);
}
}//tarjan algorithm to calculate strong unicom diagram;

void Solve (int n)
{
memset (DFN,0,sizeof (DFN));
memset (Instack,Falsesizeof (Instack));
memset (NUM,0,sizeof (num));
index=scc=top=0;
Forint i=1;i<=n;i++)
{
if (!dfn[i]) Tarjan (i);
}
}

void Init ()
{
tot=0;
memset (head,-1,sizeof (head));
}

int IN[MAXN];
vector<Int> G[MAXN];
void Suodian ()
{
Memset (In,0,sizeof (in));
Forint i=1;i<=scc;i++) g[i].clear ();
Forint i=0;i<m;i++)
{
int U=bel[edge[i].from];
int v=bel[edge[i].to];
if (U!=V)
{
G[u].push_back (v);
in[v]++;
}
}
int cnt=0,p;
Forint i=1;i<=scc;i++)
{
if (in[i]==0) {cnt++;p =i;}
}
if (cnt>=2)printf"Light My fire!\n");
Else
{
queue<Int> Q;
while (!q.empty ()) Q.pop ();
Q.push (P);
BOOL Flag=True
while (!q.empty ())
{
int Fs=q.front ();
Q.pop ();
int du=0;
int sz=g[fs].size ();
Forint i=0;i<sz;i++)
{
int to=g[fs][i];
in[to]--;
if (in[to]==0)
{
du++;
Q.push (to);
}
}
if (du>=2) {flag=Falsebreak;}
}
if (flag)printf"I Love your My Love and we love Save us!\n");
Elseprintf"Light My fire!\n");
}
}//the topological sort after shrinking point;

int main ()
{

int T;
scanf"%d", &t);
while (t--)
{
scanf"%d%d", &n,&m);
Init ();
int u,v;
Forint i=1;i<=m;i++)
{
scanf ("%d%d", &u,&v);
Addedge (U,V);
}
Solve (n);
cout<<scc<<endl;
for (int i=1;i<=n;i++)
// {
cout<<bel[i]<<endl;
// }
cout<<num[1]<< "" <<num[2]<<endl;
Suodian ();
}
return 0;
}

hdu6165 (topological sort +tarjan indent)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.