HDU 4635 strongly connected (strong connected component reduction + mathematical thinking)

Source: Internet
Author: User

Test instructions: Given a graph, if the graph is not strong connected at the beginning, find out the maximum number of edges so that the graph can also maintain the properties of non-strong connected graphs.

Idea: It is not difficult to think of shrinking point into a complete picture, and then find it into a non-strong connected graph need to remove how many edges, but how to deal with it ... Some people give the answer, find the number of points in the least number of blocks, all of its edges are removed ... It seems to be right, but what if there is one in this block? This side is not to be deleted. So I think this approach is a bit of a problem, so the best way to do this is the way they give, the last time to divide the dots into two sets X and Y,x and y are all full tiles, and then let each point in X point to every point in Y, Y without an edge pointing to X, assuming x has a dot, There are b points in Y, a+b = n, easy to get ans = a * (A-1) + b* (b-1)-a*b-m, the equivalent variant is ans = n*n-n-a*b-m, according to the nature of the inequalities we learned in high school, AXB in a=b when the maximum value, a and B difference between the more , the smaller the AXB, so we can make a smaller, so we can choose a degree or out of 0 of the component as X, select the least point of the block as X, then ans is the largest.

Sentiment: Feeling this problem is very good to combine the idea of graph theory and mathematics divide together.

#include <iostream>#include<cstdio>#include<cstring>#include<stack>using namespacestd;#defineMAXN 100010intDFN[MAXN],LOW[MAXN],ID[MAXN],SUM[MAXN],inch[MAXN], out[MAXN];intHEAD[MAXN],ALL,TOT,SCC,VIS[MAXN];structedge{intTO,NXT;} Edge[maxn];stack<int>St;voidinit () {memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); memset (ID,0,sizeof(ID)); memset (SUM,0,sizeof(sum)); All=0; SCC=0;  while(!st.empty ()) St.pop (); memset (inch,0,sizeof(inch)); memset ( out,0,sizeof( out));}voidTarjan (intu) {Dfn[u]= Low[u] = + +All ;    St.push (U);  for(inti = head[u];i! =-1; i =edge[i].nxt) {        intv =edge[i].to; if(!Dfn[v])            {Tarjan (v); Low[u]=min (low[u],low[v]); }        Else if(!id[v]) low[u] =min (low[u],dfn[v]); }    if(Low[u] = =Dfn[u]) {        intnum; SCC++;  while(!St.empty ()) {num=St.top ();            St.pop (); Id[num]=SCC; SUM[SCC]++; if(num = = u) Break; }    }}intMain () {intT,n,m,a,b,ca =0 ; scanf ("%d",&t);  while(t--) {scanf ("%d%d",&n,&m); Tot=0; memset (Head,-1,sizeof(head));  for(inti =0; I < m;i++) {scanf ("%d%d",&a,&b); Edge[i].to=b; EDGE[I].NXT=Head[a]; Head[a]=i;        } init ();  for(inti =1; I <= n;i++)        {            if(!Dfn[i]) Tarjan (i); } printf ("Case %d:",++CA); if(SCC = =1) {puts ("-1"); Continue; }         for(intU =1; u <= n;u++)        {             for(inti = head[u];i! =-1; i =edge[i].nxt) {                intv =edge[i].to; if(Id[u]! =Id[v]) {                    inch[id[v]]++;  out[id[u]]++; }            }        }        Long LongTmpans = (Long Long) (n*n-n-m); Long LongAns =0;  for(inti =1; I <= scc;i++)        {            if(inch[i]==0|| out[i]==0) ans= Max (ans,tmpans-sum[i]* (nsum[i])); } printf ("%i64d\n", ans); }    return 0;}

HDU 4635 strongly connected (strong connected component reduction + mathematical thinking)

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.