题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=3836

Equivalent Sets

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
3

题目大意:给你一张有向图要求最少加多少条边时该图变成强连通图。
Tarjan缩点。。

#include<bits/stdc++.h>
using namespace std;
const int N = 20100;
struct Tarjan_scc {
stack<int> s;
bool instack[N];
struct edge { int to, next; }G[N * 3];
int idx, scc, tot, in[N], out[N], dfn[N], low[N], head[N], sccnum[N];
inline void init(int n) {
idx = scc = tot = 0;
while (!s.empty()) s.pop();
for (int i = 0; i < n + 2; i++) {
head[i] = -1;
instack[i] = false;
in[i] = out[i] = dfn[i] = low[i] = sccnum[i] = 0;
}
}
inline void add_edge(int u, int v) {
G[tot].to = v, G[tot].next = head[u], head[u] = tot++;
}
inline void built(int m) {
int u, v;
while (m--) {
scanf("%d %d", &u, &v);
add_edge(u, v);
}
}
inline void tarjan(int u) {
dfn[u] = low[u] = ++idx;
instack[u] = true;
s.push(u);
for (int i = head[u]; ~i; i = G[i].next) {
int &v = G[i].to;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (instack[v] && dfn[v] < low[u]) {
low[u] = dfn[v];
}
}
if (dfn[u] == low[u]) {
int v = 0;
scc++;
do {
v = s.top(); s.pop();
instack[v] = false;
sccnum[v] = scc;
} while (u != v);
}
}
inline void solve(int n, int m) {
init(n);
built(m);
for (int i = 1; i <= n; i++) {
if (!dfn[i]) tarjan(i);
}
int x1 = 0, x2 = 0;
for (int u = 1; u <= n; u++) {
for (int i = head[u]; ~i; i = G[i].next) {
int v = G[i].to;
if (sccnum[u] != sccnum[v]) {
in[sccnum[v]]++;
out[sccnum[u]]++;
}
}
}
for (int i = 1; i <= scc; i++) {
if (!in[i]) x1++;
if (!out[i]) x2++;
}
printf("%d\n", 1 == scc ? 0 : max(x1, x2));
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m;
while (~scanf("%d %d", &n, &m)) {
go.solve(n, m);
}
return 0;
}

hdu 3836 Equivalent Sets的更多相关文章

  1. [tarjan] hdu 3836 Equivalent Sets

    主题链接: http://acm.hdu.edu.cn/showproblem.php? pid=3836 Equivalent Sets Time Limit: 12000/4000 MS (Jav ...

  2. hdu 3836 Equivalent Sets trajan缩点

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  3. hdu 3836 Equivalent Sets(强连通分量--加边)

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  4. hdu——3836 Equivalent Sets

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  5. hdu 3836 Equivalent Sets(tarjan+缩点)

    Problem Description To prove two sets A and B are equivalent, we can first prove A is a subset of B, ...

  6. hdu - 3836 Equivalent Sets(强连通)

    http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...

  7. HDU - 3836 Equivalent Sets (强连通分量+DAG)

    题目大意:给出N个点,M条边.要求你加入最少的边,使得这个图变成强连通分量 解题思路:先找出全部的强连通分量和桥,将强连通分量缩点.桥作为连线,就形成了DAG了 这题被坑了.用了G++交的,结果一直R ...

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

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  9. HUD——T 3836 Equivalent Sets

    http://acm.hdu.edu.cn/showproblem.php?pid=3836 Time Limit: 12000/4000 MS (Java/Others)    Memory Lim ...

随机推荐

  1. 怎样去掉FireFox的导入向导

    用robotframework的时候,用ride去打开firefox,但是每次都会出现导入向导,影响了后续的操作,怎样才能去掉呢? 网上查到的解决方案是:到firefox的profiles.ini所在 ...

  2. UIBlurEffect实现模糊效果

    //使用图片初始化背景 Pattern 图案,模式 self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageN ...

  3. oracle创建索引后sqlldr导入错误

    SQL*Loader-: Error calling once/load initialization ORA-: Table TABLE_LOG has index defined upon it. ...

  4. LA3211 飞机调度 Now or later-二分法&TwoSet

    https://vjudge.net/problem/UVALive-3211 As you must have experienced, instead of landing immediately ...

  5. 使用UI Automation实现自动化测试 --工具使用

    当前项目进行三个多月了,好久也没有写日志了:空下点时间,补写下N久没写的日志 介绍下两个工具 我本人正常使用的UISpy.exe工具和inspect.exe工具 这是UISPY工具使用的图,正常使用到 ...

  6. Loadrunner:安装LR11.0破解步骤及License

    破解步骤: 1.关闭LR相关程序 2.运行LicenseDelete程序,清除LR原来的License 3.将lm70.dll和mlr5lprg.dll这两个文件复制并粘贴到LR安装目录下的bin文件 ...

  7. Spring与Quartz的整合实现定时任务调度

    摘自: http://kevin19900306.iteye.com/blog/1397744 最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现.对于一个新手来说,花了我 ...

  8. windows7安装远程服务器AD域管理工具

    目的:在win7上安装“远程服务器管理工具”,这样可以在客户端进行对服务器的AD域的操作,避免了远程登陆进服务器的麻烦. 前提条件:一般此工具只有管理员才具有有效使用权限,所以,在域administr ...

  9. iOS键盘类型

    一.键盘风格 UIKit框架支持8种风格键盘. typedef enum { UIKeyboardTypeDefault,                // 默认键盘:支持所有字符 UIKeyboa ...

  10. input文本框实现宽度自适应代码实例,input文本框

    本章节介绍一下如何让一个文本框的宽度能够随着文本框中的内容的宽度增长而增长,也就是能够实现宽度自适应效果. 代码实例如下: <!DOCTYPE html> <html> < ...