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,即只考虑入度或者只考虑出度都不准确
#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;
}

  

hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】的更多相关文章

  1. poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】

    Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 503 ...

  2. POJ 1236--Network of Schools【scc缩点构图 &amp;&amp; 求scc入度为0的个数 &amp;&amp; 求最少加几条边使图变成强联通】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13325   Accepted: 53 ...

  3. hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. hdu 3836 Equivalent Sets trajan缩点

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

  5. poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13800   Accepted: 55 ...

  6. Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13801   Accepted: 55 ...

  7. hdu 3836 Equivalent Sets

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Description To prove two sets A ...

  8. HUD——T 3836 Equivalent Sets

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

  9. [tarjan] hdu 3836 Equivalent Sets

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

随机推荐

  1. 新开窗口不被拦截的方法-window.open和表单提交form

    $("#btn").click(function() { var w = window.open(); setTimeout(function() { w.location = & ...

  2. 强大的字符串格式化函数 - format

    自python2.6开始,新增了一种格式化字符串的函数str.format(),它通过{}和:来代替% 位置方法格式化 >>>'{}-{}'.format('simon','ting ...

  3. php中浮点数计算问题

    如果用php的+-*/计算浮点数的时候,可能会遇到一些计算结果错误的问题,比如echo intval( 0.58*100 );会打印57,而不是58,这个其实是计算机底层二进制无法精确表示浮点数的一个 ...

  4. iOS项目架构文档

    设计的项目架构主要引用MVVM+MVC架构,并以功能模块分级.以下为目录结构. 初级目录: 我们只需要关注SGZH文件夹下的目录,其他为Xcode管理的目录.可以看到此目录为项目初级目录,我们开发过程 ...

  5. 【HDU 4436】 str2int (广义SAM)

    str2int Problem Description In this problem, you are given several strings that contain only digits ...

  6. http://www.cnblogs.com/xdp-gacl/p/3951952.html

    http://www.cnblogs.com/xdp-gacl/p/3951952.html http://www.cnblogs.com/kristain/articles/2409021.html

  7. 以面到点的学习MFC

    市面上讲解学习MFC的书籍不胜其多,但是阅读的同学呢,看了一些内容以后,就无法阅读下去了,觉得MFC好难,有的呢,就会直接去看别人写的程序,不管是大或小,有时候也能明白一点点,但是总是走不出自己开发设 ...

  8. python-urllib2模块

    参考: http://blog.csdn.net/wklken/article/details/7364390 http://hankjin.blog.163.com/blog/static/3373 ...

  9. 根据指定的commit查找对应的log

    find commit by hash sha in git 问题: I need to find a commit in Git by given hash SHA. For example, if ...

  10. 轻松学习RSA加密算法原理 (转)

    轻松学习RSA加密算法原理 (转) http://blog.csdn.net/q376420785/article/details/8557266 http://www.ruanyifeng.com/ ...