本文主要是介绍HDU3836Equivalent Sets(强连通+加边构成强连通),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:至少加几条边构成强连通, 和上一题一样
#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 int maxn=30005;
const int inf=999999;vector<int> G[maxn];
int low[maxn],dfn[maxn],belong[maxn],s[maxn];
bool ins[maxn];
int cnt,num,top;void dfs(int u){dfn[u]=low[u]=++num;s[++top]=u;ins[u]=true;int N=G[u].size();for(int i=0;i<N;i++){int v=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]){int v;cnt++;do{v=s[top--];ins[v]=false;belong[v]=cnt;}while(u!=v);}
}
void Tarjan(int n){cnt=num=top=0;cl(belong,0);cl(ins,false);cl(dfn,0);for(int i=1;i<=n;i++){if(!dfn[i])dfs(i);}
}int in[maxn],out[maxn];
int main(){int T;int n,m;while(~scanf("%d%d",&n,&m)){if(m==0){printf("%d\n",n);continue;}for(int i=0;i<=n;i++)G[i].clear();for(int i=0;i<m;i++){int x,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(int i=1;i<=n;i++){for(int j=0;j<G[i].size();j++){if(belong[i]!=belong[G[i][j]]){in[belong[G[i][j]]]++;out[belong[i]]++;}}}int xx=0,yy=0;for(int i=1;i<=cnt;i++){if(!in[i])xx++;if(!out[i])yy++;}printf("%d\n",max(xx,yy));}return 0;
}
这篇关于HDU3836Equivalent Sets(强连通+加边构成强连通)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!