您的位置:首页 > 产品设计 > UI/UE

HDU 3836 Equivalent Sets(tarjan + 压缩强连通)

2016-10-13 23:07 435 查看
大体题意:

告诉你如果证明A等于B,要证明A是B的子集,并且B是A的子集。

现在给你n 个集合,告诉m 个集合的关系,求在加多少个关系使得这N个集合相等?

思路:

A是B的子集, 用 A到B有一条有向边来代替,那么这个问题就转换为了  在加多少边使得这个图强连通!

可以用tarjan算法求出所有的强连通分量,把每一个强连通分量压缩成一个点,这个点内部肯定是强连通,那么就是加边使得这些点强连通!

这样 每个点必须至少有一个入度 和至少有一个出度,我们可以统计出有s1个 没有入度的点,有s2个没有出度的点,假设s2>s1吧,那么我们必须加够s2个边,对于s1来说,多出来的边随便连好了!

详细见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 20000 + 10;
struct Node{
int v,next;
}p[50007];
int cnt,head[maxn];
void addedge(int u,int v){
p[cnt].v = v;
p[cnt].next = head[u];
head[u] = cnt++;
}
bool inStack[maxn];
int Stack[maxn],top,Time;
int dfn[maxn],low[maxn],Tar,botin[maxn],belong[maxn],botout[maxn];
void init(){
top = Time = cnt = Tar = 0;
memset(head,-1,sizeof head);
memset(inStack,0,sizeof inStack);
memset(dfn,-1,sizeof dfn);
memset(botin,0,sizeof botin);
memset(botout,0,sizeof botout);
}
void dfs(int k){
Stack[++top] = k;
dfn[k] = low[k] = ++Time;
inStack[k] = 1;
for (int i = head[k]; i != -1; i = p[i].next){
int v = p[i].v;
if (dfn[v] == -1){
dfs(v);
low[k] = min(low[k],low[v]);
}
else if (inStack[v]){
low[k] = min(low[k],dfn[v]);
}
}
if (dfn[k] == low[k]){
++Tar;
int cur = Stack[top];
while(cur != k){
belong[cur] = Tar;
inStack[cur] = 0;
cur = Stack[--top];
}
belong[k] = Tar;
inStack[k] = 0;
--top;
}

}
int main(){
int T,n,m;
while(scanf("%d %d",&n, &m) == 2){
init();
for (int i = 0; i < m; ++i){
int u,v;
scanf("%d %d",&u, &v);
addedge(u,v);
}
for (int i = 1; i <= n; ++i){
if (dfn[i] == -1)dfs(i);
}
for (int i = 1; i <= n; ++i){
for (int j = head[i]; j != -1; j = p[j].next){
int v=p[j].v;
if (belong[v] != belong[i]){
botout[belong[i]]++;
botin[belong[v]]++;
}
}
}
if (Tar == 1){
puts("0");
continue;
}
int noout=0,noin = 0;
for (int i = 1; i <= Tar; ++i){
if (botin[i] == 0)++noin;
if (botout[i] == 0)++noout;
}
printf("%d\n",max(noin,noout));
}

return 0;
}

HDU - 3836

Equivalent Sets

Time Limit: 4000MS Memory Limit: 104857KB 64bit IO Format: %I64d & %I64u
Submit Status

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.

Source

2011 Multi-University Training
Contest 1 - Host by HNU


Problem descriptions:

System Crawler 2016-10-13






Initialization.




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: