题目连接

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. Flex4/Flash开发在线音乐播放器 , 含演示地址

    要求 必备知识 本文要求基本了解 Adobe Flex编程知识和JAVA基础知识. 开发环境 MyEclipse10/Flash Builder4.6/Flash Player11及以上 演示地址 演 ...

  2. python基础(set)补充

    1.函数参数(引用)  函数的传参,传的是引用 def func(args): args.appand(123) li=[11,22,33] func(li) print(li) [11,22,33, ...

  3. 【练习】ORACLE统计信息--直方图

    ①创建表tSQL> create table t as select * from dba_objects; Table created. --收集直方图 SQL> exec dbms_s ...

  4. js时间冒泡,阻止事件冒泡

    首先解释一下事件冒泡神什么, 在js中,假如在div中嵌套一个div 如 <style type="text/css"> #box1{width:500px;heigh ...

  5. VC7 HTML Dialog开发实例讲解

    开发环境:VS7,Windows XP,Windows 2K 在VS7中添加了一种新的对话框类:CDHtmlDialog,顾名思义就是能够显示DHTML内容的对话框,但不同与以前的CHTMLView不 ...

  6. Windows phone 8 学习笔记(3) 通信(转)

    Windows phone 8 可利用的数据通信方式比较广泛,在硬件支持的前提下,我们可以利用WiFi.蓝牙.临近感应等多种方式.数据交互一般通过套接字来完成,我们将在本文详细的分析. 快速导航:一. ...

  7. 分享4种CSS3效果(360度旋转、旋转放大、放大、移动)

    转自:http://www.j                     q-school.com/Show.aspx?id=281 本文仅供自己学习而转载,由于效果掩饰地址的转载出现问题,强烈建议去源 ...

  8. 实现在Android开发中的Splash Screen开场屏的效果

    很多网友可能发现近期Tencent推出的手机QQ Android版包含了一个开场屏Splash Screen载入效果,通常游戏或大型软件打开时可能需要一个释放解析资源的过程,需要一个前台的动画播放和后 ...

  9. MyEclipse中使用debug调试程序

    最基本的操作是:       1.首先在一个java文件中设断点,然后debug as-->open debug Dialog,然后在对话框中选类后--> Run       当程序走到断 ...

  10. HTML中Meta标签大全

    在网页的HTML源代码中一个重要的代码“”(即通常所说的META标签).META标签用来描述一个HTML网页文档的属性,例如作者.日期和时间.网页描述.关键词.页面刷新等. 1.META标签的keyw ...