Test instructions: At least a few edges constitute strong connectivity, as in the previous question
#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>#include <set>#include <map>#include <string>#include <cstring>#include <stack>#include <queue>#include <vector>#include <cstdlib>#define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1)#define CL (A, B) memset (A,b,sizeof (a));#define LL Long Long#define P pair<int,int>#define X First#define Y Second#define PB Push_back#define FREAD (ZCC) freopen (ZCC, "R", stdin)#define FWRITE (ZCC) freopen (ZCC, "w", stdout)using namespace STD;Const intmaxn=30005;Const intinf=999999; vector<int>G[MAXN];intLOW[MAXN],DFN[MAXN],BELONG[MAXN],S[MAXN];BOOLINS[MAXN];intCnt,num,top;voidDfsintu) {dfn[u]=low[u]=++num; S[++top]=u; ins[u]=true;intN=g[u].size (); for(intI=0; i<n;i++) {intV=g[u][i];if(!dfn[v]) {DFS (v); Low[u]=min (Low[u],low[v]); }Else if(Ins[v]&&dfn[v]<low[u]) {LOW[U]=DFN[V]; } }if(Dfn[u]==low[u]) {intV cnt++; Do{v=s[top--]; ins[v]=false; belong[v]=cnt; } while(U!=V); }}voidTarjan (intN) {cnt=num=top=0; CL (Belong,0); CL (INS,false); CL (DFN,0); for(intI=1; i<=n;i++) {if(!dfn[i]) DFS (i); }}intIN[MAXN],OUT[MAXN];intMain () {intTintN,m; while(~scanf("%d%d", &n,&m)) {if(m==0){printf("%d\n", n);Continue; } for(intI=0; i<=n;i++) g[i].clear (); for(intI=0; i<m;i++) {intx, y;scanf("%d%d", &x,&y); G[X].PB (y); } Tarjan (n);if(cnt<=1){printf("0\n");Continue; } cl (In,0); CL (out,0); for(intI=1; i<=n;i++) { for(intj=0; J<g[i].size (); j + +) {if(Belong[i]!=belong[g[i][j]]) {in[belong[g[i][j]]]++; out[belong[i]]++; } } }intxx=0, yy=0; for(intI=1; i<=cnt;i++) {if(!in[i]) xx++;if(!out[i]) yy++; }printf("%d\n", Max (XX,YY)); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu3836equivalent Sets (strong connectivity + Plus edge form strong connectivity)