Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4635
First determine whether the diagram is strongly connected. If it is not strongly connected, then the point is shrunk.
Our goal is to add the most to the edge, then the final figure, definitely two sets, these two sets are strong unicom,
A collection has only one-way edges to a collection. Let's first make the graph full and then ask by deleting the edges: there is n (n-1) edge, then delete the existing edge m
, and then delete the two-set Edge n1* (N-N1), N1 is the number of vertices in one of the sets, because this is a one-way edge.
So the answer is ans=n* (n-1)-m-n1* (N-N1),
We want to make ans the biggest, then the n1* (N-N1) will be smaller
#include <stdio.h>#include<string.h>#include<vector>#include<algorithm>#defineN 100005#defineINF 0XFFFFFFFusing namespacestd;intHead[n], CNT;inttop, Is[n], stack[n], low[n], dfn[n], time, N, M;intNblock, block[n];intCnt[n], out[n], in[n];structedge{intV, Next;} E[n];voidInit () { time= CNT = top = Nblock =0; memset (Cnt,0,sizeof(Cnt)); memset (Low,0,sizeof(low)); memset (DFN,0,sizeof(DFN)); memset (Stack,0,sizeof(Stack)); Memset (IS,0,sizeof(IS)); memset (out,0,sizeof(out)); Memset (In,0,sizeof(in)); memset (Block,0,sizeof(Block)); memset (Head,-1,sizeof(head));}voidADD (intUintv) {E[CNT].V=v; E[cnt].next=Head[u]; Head[u]= cnt++;}voidTajar (intUintfather) {Stack[top++]=u; Low[u]= Dfn[u] = + +Time ; Is[u]=1; intv; for(intI=head[u]; i!=-1; I=E[i].next) {v=e[i].v; if(!Dfn[v]) {Tajar (V, u); Low[u]=min (Low[u], low[v]); } Else if(Is[v]) Low[u]=min (Low[u], dfn[v]); } if(low[u]==Dfn[u]) { ++Nblock; Do{v=stack[--top]; IS[V]=0; BLOCK[V]=Nblock; Cnt[nblock]++; } while(u!=v); }}intMain () {intT, t=1, x, y; scanf ("%d", &T); while(t--) {Init (); scanf ("%d%d", &n, &m); for(intI=1; i<=m; i++) {scanf ("%d%d", &x, &y); ADD (x, y); } for(intI=1; i<=n; i++) { if(!Low[i]) Tajar (i,-1); } for(intI=1; i<=n; i++) { for(intJ=head[i]; j!=-1; j=E[j].next) { intU =Block[i]; intv =BLOCK[E[J].V]; if(U! =v) {Out[v]++; In[u]++; }}} y=INF; for(intI=1; i<=nblock; i++) { if(! In[i] | | !out[i]) y=min (y, cnt[i]); } x= N-y; Long LongAns= (Long Long) N (n1)-x*y-m; if(nblock==1) printf ("Case %d: -1\n", t++); Elseprintf ("Case %d:%lld\n", t++, ans); } return 0;}View Code
Strongly connected---hdu4635 (strong unicom component)