hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】

Equivalent Sets

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


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.
 
题意:n个点m条边的有向图,问最少增加多少边使图强连通。
题解:求每个scc的入度和出度,然后分别求出入度中0的个数in和出度out,取in和out中较大的一个; 
因为入度或出度为0证明这个scc和别的scc未相连,需要用一条边相连,这条边就是要加入的边,又因为一个scc可能连接多个scc,即只考虑入度或者只考虑出度都不准确
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<vector>
#define MAX 50010
#define INF 0x3f3f3f
using namespace std;
int n,m;
int ans,head[MAX];
int low[MAX],dfn[MAX];
int instack[MAX],sccno[MAX];
vector<int>newmap[MAX];
vector<int>scc[MAX];
int scccnt,dfsclock;
int in[MAX],out[MAX];
stack<int>s;
struct node
{
    int beg,end,next;
}edge[MAX];
void init()
{
    ans=0;
    memset(head,-1,sizeof(head));
}
void add(int beg,int end)
{
    edge[ans].beg=beg;
    edge[ans].end=end;
    edge[ans].next=head[beg];
    head[beg]=ans++;
}
void getmap()
{
    int i,a,b;
    while(m--)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
}
void tarjan(int u)
{
    int v,i,j;
    s.push(u);
    instack[u]=1;
    low[u]=dfn[u]=++dfsclock;
    for(i=head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].end;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
        low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        scccnt++;
        while(1)
        {
            v=s.top();
            s.pop();
            instack[v]=0;
            sccno[v]=scccnt;
            if(v==u)
            break;
        }  
    }
}
void find(int l,int r)
{
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(instack,0,sizeof(instack));
    memset(sccno,0,sizeof(sccno));
    dfsclock=scccnt=0;
    for(int i=l;i<=r;i++)
    {
        if(!dfn[i])
            tarjan(i);
    }
}
void suodian()
{
    int i;
    for(i=1;i<=scccnt;i++)
    {
        newmap[i].clear();
        in[i]=0;out[i]=0;
    }
    for(i=0;i<ans;i++)
    {
        int u=sccno[edge[i].beg];
        int v=sccno[edge[i].end];
        if(u!=v)
        {
            newmap[u].push_back(v);
            in[v]++;out[u]++;
        }
    }
}
void solve()
{
    int i,j;
    if(scccnt==1)
    {
        printf("0\n");
        return ;
    }
    else
    {
        int minn=0;
        int maxx=0;
        for(i=1;i<=scccnt;i++)
        {
            if(!in[i])
                minn++;
            if(!out[i])
                maxx++;
        }
        printf("%d\n",max(minn,maxx));
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        getmap();
        find(1,n);
        suodian();
        solve();
    }
    return 0;
}

  

posted @   非我非非我  阅读(603)  评论(0编辑  收藏  举报
编辑推荐:
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
阅读排行:
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· C# 13 中的新增功能实操
· Supergateway:MCP服务器的远程调试与集成工具
· Vue3封装支持Base64导出的电子签名组件
点击右上角即可分享
微信分享提示