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
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.
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.
#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&&缩点】【求最少加多少条边使图强连通】的更多相关文章
- poj 3352 Road Construction【边双连通求最少加多少条边使图双连通&&缩点】
Road Construction Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10141 Accepted: 503 ...
- POJ 1236--Network of Schools【scc缩点构图 && 求scc入度为0的个数 && 求最少加几条边使图变成强联通】
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13325 Accepted: 53 ...
- hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 3836 Equivalent Sets trajan缩点
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13800 Accepted: 55 ...
- Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13801 Accepted: 55 ...
- hdu 3836 Equivalent Sets
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Description To prove two sets A ...
- HUD——T 3836 Equivalent Sets
http://acm.hdu.edu.cn/showproblem.php?pid=3836 Time Limit: 12000/4000 MS (Java/Others) Memory Lim ...
- [tarjan] hdu 3836 Equivalent Sets
主题链接: http://acm.hdu.edu.cn/showproblem.php? pid=3836 Equivalent Sets Time Limit: 12000/4000 MS (Jav ...
随机推荐
- webstorm快捷方式
刚开始在使用webstrom的时候,不知道快捷方式,感觉自己把webstorm当做记事本使用,真的挺傻的,在朋友的指导下,原来webstorm有快捷方式 一.界面操作 快捷键 说明 ctrl+shif ...
- Invoke()方法的使用
在多线程编程中,我们经常要在工作线程中去更新界面显示,而在多线程中直接调用界面控件的方法是错误的做法,Invoke 和 BeginInvoke 就是为了解决这个问题而出现的,使你在多线程中安全的更新界 ...
- 有反斜杠时候,CakePHP往pgsql插入数据异常
原始数据:INSERT INTO “public”.”tables” (“table”, “columns”) VALUES (‘table1\’, ‘{“col1″:false,”col2″:tru ...
- validate插件的使用
方法如下: 插件: jquery.validate.js jquery.validate.custom.js bootstrap html代码: <form id="form_name ...
- 查看 MySQL 数据库中每个表占用的空间大小
TABLE_SCHEMA : 数据库名TABLE_NAME:表名ENGINE:所使用的存储引擎TABLES_ROWS:记录数DATA_LENGTH:数据大小INDEX_LENGTH:索引大小 SELE ...
- php远程读取json的方法
<?php /** * @author 懒人 <service@kuitao8.com> * @since 2.0 */ header("Content-type:text ...
- 一个简单的makefile,一次性编译本文件夹下所有的cpp文件
代码: CXX := g++ CFLAGS := -g TARGET := xxx.exe SRCS := $(wildcard *.cpp) OBJS := $(patsubst %cpp,%o,$ ...
- Ubuntu下Memcache的安装与基本使用
安装Memcache Memcache分为两部分,Memcache服务端和客户端.Memcache服务端是作为服务来运行的,所有数据缓存的建立,存储,删除实际上都是在这里完成的.客户端,在这里我们指的 ...
- 如何跳到系统设置界面-b
NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"]; if ([[UIApplication sharedApplicati ...
- C#网页自动登录和提交POST信息的多种方法(转)
网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以用来提取网页HTML的组件比较多,常用的用WebBrowser.WebClient.HttpWebRequest这三 ...