小小呆的专栏

talk is easy,show me the code!

HDOJ--3836--Equivalent Sets(tarjan算法)//求连接几个SCC最少的边

Equivalent Sets

Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others)
Total Submission(s): 3621    Accepted Submission(s): 1264


Problem Description
To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent.
You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets.
Now you want to know the minimum steps needed to get the problem proved.
 

Input
The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000.
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
 

Output
For each case, output a single integer: the minimum steps needed.
 

Sample Input
4 0 3 2 1 2 1 3
 

Sample Output
4 2
Hint
Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.
 
思路:利用tarjan算法求出SCC的数量,然后根据缩点后每个节点的入度出度情况来去求连接这几个SCC最少的边数。这里要利用到一个结论,连接几个SCC最少需要的边是缩点后几个节点的0入度数量和0出度数量中的最大值。详细见代码。
ac代码:
#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#include<algorithm>
#define INF 0x3f3f3f
#define MAXN 20000+10
using namespace std;
vector<int>mp[MAXN];//存储图 
vector<int>sccno[MAXN];//存储每个强连通分量;
vector<int>NEW[MAXN]; 
stack<int>q;//将遍历过得节点记录在栈里 
bool Instack[MAXN];//记录是否在栈里。 
int  In[MAXN],Out[MAXN];//记录入度,出度。	
int low[MAXN],dfn[MAXN],scc[MAXN];//sccno记录每个节点 属于哪个强连通分量 
int dfs_clock,scc_cnt; 
int n,m;
void get_map(){
	for(int i=1;i<=n;i++)
		mp[i].clear();
	for(int i=0;i<m;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		mp[u].push_back(v);
	}
}
void tarjan(int u){
	int v;//下一个节点; 
	low[u]=dfn[u]=++dfs_clock;
	Instack[u]=true;
	q.push(u); 
		for(int j=0;j<mp[u].size();j++){//遍历U节点的每一条边。 
					v=mp[u][j];
			if(!dfn[v]){
				tarjan(v);
				low[u]=min(low[u],low[v]);//更新low数组 
			}
			else	if(Instack[v])
				low[u]=min(low[u],dfn[v]);//建立反向边。
		} 	 
	if(dfn[u]==low[u]){
		scc_cnt++;
		while(1){
			v=q.top();
			q.pop();
			scc[v]=scc_cnt;
			sccno[scc_cnt].push_back(v); 
			Instack[v]=false;
			if(u==v)
				break;
		}
	} 
}
void find_cut(){
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(Instack,false,sizeof(Instack));
	dfs_clock=scc_cnt=0;
	for(int i=1;i<=n;i++)
		if(!dfn[i])
			tarjan(i);
}
void SCC(){
	memset(In,0,sizeof(In));
	memset(Out,0,sizeof(Out));
	for(int i=1;i<=scc_cnt;i++)
		NEW[i].clear();
	for(int i=1;i<=n;i++){
		for(int j=0;j<mp[i].size();j++){//遍历所有边,构建新图。
			 int u=scc[i];//找到这两个点是否是同一个强连通分量 
			 int v=scc[mp[i][j]];
			 if(u!=v) {//如果不是,构建新边。
			 NEW[u].push_back(v);
			 In[v]++;
			 Out[u]++; 
			 }	
		}
	}
}
void solve(){
	int sumin=0,sumout=0;
		if(scc_cnt==1){
			printf("0\n");
		}
		else{
			for(int i=1;i<=scc_cnt;i++){
				if(In[i]&&Out[i])
					continue;
				if(!In[i]) 
					sumin++;
				if(!Out[i])
					sumout++;
			}
				int ans=max(sumin,sumout);
				printf("%d\n",ans);
		}
}
int main(){
	while(scanf("%d%d",&n,&m)!=EOF){
		get_map();
		find_cut();
		SCC();
		solve(); 
	}
	
	return 0;
} 
自己终于算得上是稍微学会了一点强连通吧,也算是有点进步了。

阅读更多
版权声明:本文为小小呆原创文章,转载请注明出处! https://blog.csdn.net/gui951753/article/details/49967609
个人分类: 强连通
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

HDOJ--3836--Equivalent Sets(tarjan算法)//求连接几个SCC最少的边

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭