hdu 3836 Equivalent Sets trajan缩点

时间:2022-04-12 00:05:34

Equivalent Sets

Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others)

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.

 
Source

题意:给你n个点,m条边的有向图,最少加几条边使得改图为强连通;

思路:对于一个缩完点的图,要使得其强连通,入度和出度都至少为1;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-6
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=1e6+,inf=1e9+;
const LL INF=5e17+,mod=1e9+; struct is
{
int u,v;
int next;
}edge[];
int head[];
int belong[];
int dfn[];
int low[];
int stackk[];
int instack[];
int number[];
int in[N],out[N];
int n,m,jiedge,lu,bel,top;
void update(int u,int v)
{
jiedge++;
edge[jiedge].u=u;
edge[jiedge].v=v;
edge[jiedge].next=head[u];
head[u]=jiedge;
}
void dfs(int x)
{
dfn[x]=low[x]=++lu;
stackk[++top]=x;
instack[x]=;
for(int i=head[x];i;i=edge[i].next)
{
if(!dfn[edge[i].v])
{
dfs(edge[i].v);
low[x]=min(low[x],low[edge[i].v]);
}
else if(instack[edge[i].v])
low[x]=min(low[x],dfn[edge[i].v]);
}
if(low[x]==dfn[x])
{
int sum=;
bel++;
int ne;
do
{
sum++;
ne=stackk[top--];
belong[ne]=bel;
instack[ne]=;
}while(x!=ne);
number[bel]=sum;
}
}
void tarjan()
{
memset(dfn,,sizeof(dfn));
bel=lu=top=;
for(int i=;i<=n;i++)
if(!dfn[i])
dfs(i);
}
int main()
{
int i,t;
while(~scanf("%d%d",&n,&m))
{
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(head,,sizeof(head));
jiedge=;
for(i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
update(u,v);
}
tarjan();
int x=;
int z=;
for(i=;i<=jiedge;i++)
if(belong[edge[i].v]!=belong[edge[i].u])
{
if(!out[belong[edge[i].u]])x++;
if(!in[belong[edge[i].v]])z++;
out[belong[edge[i].u]]++;
in[belong[edge[i].v]]++;
}
x=bel-x;
z=bel-z;
if(bel==)
printf("0\n");
else
printf("%d\n",max(x,z));
}
return ;
}

hdu 3836 Equivalent Sets trajan缩点的更多相关文章

  1. hdu 3836 Equivalent Sets

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

  2. &lbrack;tarjan&rsqb; hdu 3836 Equivalent Sets

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

  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&plus;缩点)

    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&lpar;强连通&rpar;

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

  7. HDU - 3836 Equivalent Sets &lpar;强连通分量&plus;DAG&rpar;

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

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

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

  9. HDU 3836 Equivalent SetsTarjan&plus;缩点&rpar;

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

随机推荐

  1. vc&plus;&plus; internet

    1.用VC开发ActiveX文档服务器 MFC 4.2不支持开发ActiveX容器,但支持ActiveX服务器.只要在使用MFC AppWizard生成应用程序框架时选择支持Active Docume ...

  2. Event Recommendation Engine Challenge分步解析第六步

    一.请知晓 本文是基于: Event Recommendation Engine Challenge分步解析第一步 Event Recommendation Engine Challenge分步解析第 ...

  3. mysql 5&period;7 ERROR 1820 &lpar;HY000&rpar;&colon;

    在首次登录Mysql 5.7 后,mysql数据库做出了很多的调整.执行大部分操作会提示这个错误 : ERROR 1820 (HY000): You must reset your password ...

  4. Node&period;js SDK与fabric链码交互开发

    1.本篇背景 前面已经对链码开发作了比较详细的介绍,并且对官方提供的 fabcar 链码进行了解读,本篇将介绍如何使用 Node.js SDK 与区块链网络中的链码进行交互. 本篇内容基本来自官方 H ...

  5. 2018&period;06&period;29 NOIP模拟 1807(简单递推)

    1807 题目背景 SOURCE:NOIP2015-SHY-2 题目描述 给出一个由数字('0'-'9')构成的字符串.我们说一个子序列是好的,如果他的每一位都是 1.8.0.7 ,并且这四个数字按照 ...

  6. Unity核心对象模型

    总结的Unity引擎部分的核心对象模型类图,供大家学习时参考,根基类为Object,下一层包括核心基类GameObject,及其他作为资源的Mesh,Material,Shader,Texture,S ...

  7. vim学习日志(5):vim下wimrc的配置,解决中文乱码问题

    解决linux下vim乱码的情况:(修改vimrc的内容) 全局的情况下:即所有用户都能用这个配置 文件地址:/etc/vimrc 在文件中添加: ,ucs-bom,gb18030,gbk,gb231 ...

  8. centos xampp 隐藏phpmyadmin地址

    /opt/lampp/etc/extra/httpd-xampp.conf Alias /phpmyadmin "/opt/xampp/phpMyAdmin/" 改为 Alias ...

  9. Leecode刷题之旅-C语言&sol;python-66加一

    /* * @lc app=leetcode.cn id=66 lang=c * * [66] 加一 * * https://leetcode-cn.com/problems/plus-one/desc ...

  10. BZOJ 1101 &lbrack;POI2007&rsqb;Zap &vert; 第一道莫比乌斯反&lpar;繁&rpar;演&lpar;衍&rpar;

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=1101 题解: http://www.cnblogs.com/mrha/p/8203612.h ...