g-strongly Connected-hdu 4635 (for connected components)

Source: Internet
Author: User

Test instructions: To give you a diagram, ask the maximum number of edges can be added so that the graph is still not strong connected graph, if the original image is strongly connected output '-1 ' analysis: The first to find out the connected components of the indentation, because it is the most added edge, so can be seen as two parts x, Y, only part of the other part of So is x* (x+1) +x*y+y* (y+1)-m, only need to come out of the degree or the minimum point of 0 of the connected component can be. #include <stdio.h>
#include <string.h>
#include <algorithm>
usingnamespaceStd

ConstintMAXN = 1e5+5;
Const intoo = 1e9;

structedge{intV, Next;} E[MAXN];
intHEAD[MAXN], CNT;
voidAddedge (intUintV
{
E[CNT].V = v;
E[cnt].next = Head[u];
Head[u] = cnt++;
}

intDFN[MAXN], LOW[MAXN], Index;
intSTACK[MAXN], top, INSTACK[MAXN];
intBLG[MAXN], BNT, NBLG[MAXN];///What is the connected component and how many points are there in the connected component?
intOUTEDGE[MAXN], INEDGE[MAXN];

voidInIt (intN
{
CNT = Index = top = BNT =0;
for(intI=0; i<=n; i++)
{
Head[i] =-1;
Dfn[i] =0;
Nblg[i] =0;
Outedge[i] =0;
Inedge[i] =0;
}
}
voidTarjan (intU
{
intV

Low[u] = dfn[u] = ++index;
Stack[++top] = u;
Instack[u] =true;

for(intJ=head[u]; j!=-1; J=e[j].next)
{
v = e[j].v;
if(!dfn[v])
{
Tarjan (v);
Low[u] = min (Low[u], low[v]);
}
Elseif(Instack[v] = =true)
Low[u] = min (Low[u], dfn[v]);
}

if(Low[u] = = Dfn[u])
{
++BNT;
Do
{
v = stack[top--];
INSTACK[V] =false;
BLG[V] = BNT;
nblg[bnt]++;
}
while(U! = v);
}
}

intMain ()
{
intT, t=1;

scanf"%d", &t);

while(t--)
{
intI, J, u, V, N, M;

scanf"%d%d", &n, &m);

InIt (N);

for(i=0; i<m; i++)
{
scanf"%d%d", &u, &v);
Addedge (U, v);
}

for(i=1; i<=n; i++)
{
if(!dfn[i])
Tarjan (i);
}

for(i=1; i<=n; i++)
for(J=head[i]; j!=-1; J=e[j].next)
{
v = e[j].v;
if(Blg[i]! = Blg[v])
{
inedge[Blg[v]]++;
outedge[Blg[i]]++;
}
}

intx, Y=oo;

for(i=1; i<=bnt; i++)
{
if(!outedge[i] | |!inedge[i])
y = min (y, nblg[i]);
}

x = n-y;

if(BNT = =1)
printf"Case %d: -1\n", t++);
Else
printf"Case %d:%lld\n", t++, (Long Long) x* (x1) +x*y+y* (y1)-M);
}

return0; 

}

g-strongly Connected-hdu 4635 (for connected components)

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.