The main idea: at least add a few sides to make the diagram Unicom.
Idea: Let the figure unicom words will certainly be related to the degree. So find out the largest fill can be.
#include <map> #include <queue> #include <cmath> #include <cstdio> #include <stack># include<iostream> #include <cstring> #include <algorithm> #define LL int#define inf 0x3f3f3f3f#define EPS 1e-8#include<vector> #define LS l,mid,rt<<1#define rs mid+1,r,rt<<1|1using namespace Std;const int NV = 21000;const int ne = 51000;struct node{int to,next;} Q[ne<<1];int Head[ne<<1],dfn[nv],num[nv],in[nv],stk[nv],low[nv];int Cnt,top,tim,scc,out[nv],n,mi; BOOL vis[nv];void Add (int a,int b) {q[cnt].to = b; Q[cnt].next = Head[a]; Head[a] = cnt++;} void Tarjan (int u) {dfn[u] = low[u] = tim++; stk[top++] = u; Vis[u] = 1; for (int i = head[u];~i;i=q[i].next) {int v = q[i].to; if (!dfn[v]) {Tarjan (v); Low[u] = min (low[u],low[v]); } else if (Vis[v]) {Low[u] = min (low[u],dfn[v]); }} if (dfn[u]== Low[u]) {scc++; while (top>0&&stk[toP]! = u) {int now = Stk[--top]; Num[now] = SCC; Vis[now] = 0; }}}void Init () {SCC = CNT = top = 0; Tim = 1; memset (head,-1,sizeof (head)); memset (In,0,sizeof (in)); Memset (out,0,sizeof (out)); memset (dfn,0,sizeof (DFN)); memset (low,0,sizeof (Low)); memset (vis,false,sizeof (VIS)); memset (num,0,sizeof (num));} int main () {int n,m,i,j,k,cla,a,b; scanf ("%d", &CLA); while (cla--) {init (); scanf ("%d%d", &n,&m); for (i = 0;i < m;++ i) {scanf ("%d%d", &a,&b); Add (A, b); } for (i = 1;i <= n;++ i) if (!dfn[i]) Tarjan (i); if (scc==1) {puts ("0"); continue; } for (i = 1;i <= n;++ i) {for (j = head[i]; ~j;j = q[j].next) {int v = q[j].to; if (Num[i]!=num[v]) {out[num[i]]++; IN[NUM[V]]++; } } } int s2 = 0,S1 = 0; for (i = 1;i <= scc;++ i) {if (!in[i]) s1++; if (!out[i]) s2++; } printf ("%d\n", Max (S1,S2)); } return 0;}
HDU 2767 proving equivalences (strong unicom)