hdu 3836 Equivalent Sets
题目连接
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的更多相关文章
- [tarjan] hdu 3836 Equivalent Sets
主题链接: http://acm.hdu.edu.cn/showproblem.php? pid=3836 Equivalent Sets Time Limit: 12000/4000 MS (Jav ...
- hdu 3836 Equivalent Sets trajan缩点
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- hdu 3836 Equivalent Sets(强连通分量--加边)
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- hdu——3836 Equivalent Sets
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- 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, ...
- hdu - 3836 Equivalent Sets(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...
- HDU - 3836 Equivalent Sets (强连通分量+DAG)
题目大意:给出N个点,M条边.要求你加入最少的边,使得这个图变成强连通分量 解题思路:先找出全部的强连通分量和桥,将强连通分量缩点.桥作为连线,就形成了DAG了 这题被坑了.用了G++交的,结果一直R ...
- hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- HUD——T 3836 Equivalent Sets
http://acm.hdu.edu.cn/showproblem.php?pid=3836 Time Limit: 12000/4000 MS (Java/Others) Memory Lim ...
随机推荐
- JS 在open打开的子窗口页面中调用父窗口页面的JS方法
需求的情景如下: 1:做新增或修改等操作的时候打开一个新的浏览器窗口(使用window.open(参数等)方法) 2:在新增或修改等的页面上有返回按钮.重置按钮.保存按钮,对于返回就直接关闭此窗口(使 ...
- Android 类库打包、发布方法
开发Android应用的时候,对于可用于多个应用的公用的部分,或是打算发布给第三方进行应用集成的部分,要把这部分打包成类库怎么做呢? Android应用使用ADT打包成apk,apk中包含了运行程序所 ...
- ASP.NET运行机制原理 ---浏览器与IIS的交互过程 自己学习 网上查了下别人写的总结的很好 就转过来了 和自己写的还好里嘻嘻
一.浏览器和服务器的交互原理 (一).浏览器和服务器交互的简单描述: 1.通俗描述:我们平时通过浏览器来访问网站,其实就相当于你通过浏览器去访问一台电脑上访问文件一样,只不过浏览器的访问请求是由被访问 ...
- 智能指针(三):unique_ptr使用简介
我们知道auto_ptr通过复制构造或者通过=赋值后,原来的auto_ptr对象就报废了.所有权转移到新的对象中去了.而通过shared_ptr可以让多个智能指针对象同时拥有某一块内存的访问权.但假如 ...
- WF4.0 Activities<第一篇>
一.基元工具 1.Delay Delay用于延迟一段时间执行下面的流程.在WF中实例是单线程运行的,Delay并不是Thread.Sleep方法实现的. Delay有一个Duration属性,用于设置 ...
- 在ASP.NET开始执行HTTP请求的处理程序之前
using Dscf.Client.Web.Class; using Dscf.Client.Web.DscfService; using Dscf.Client.Web.Handler; using ...
- PHP超时处理全面总结(转)
[ 概述 ] 在PHP开发中工作里非常多使用到超时处理到超时的场合,我说几个场景: 1. 异步获取数据如果某个后端数据源获取不成功则跳过,不影响整个页面展现 2. 为了保证Web服务器不会因为当个页面 ...
- JB for iOS 9.3
令人期待已久的JB再度现世,真是望穿秋水,千呼万唤始出来~ http://www.pangu.io/?flag=cn JB的过程顺利不卡关. 完成后就可以看到心爱的Cydia出现在桌面上了. 还需要A ...
- EnCase v7 search hits in compound files?
I used to conduct raw search in EnCase v6, and I'd like to see if EnCase v7 raw search could hit key ...
- 解决Android sdk更新不能下载或下载内容过慢
Android Android SDK 配置步骤 启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manag ...