HDU3836 Equivalent Sets (Tarjan缩点+贪心)
题意:
给你一张有向图,问你最少加多少条边这张图强连通
思路:
缩点之后,如果不为1个点,答案为出度为0与入度为0的点的数量的最大值
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
//#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
#include<unordered_map> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 5e4+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); vector<int>g[maxn];
int f[maxn];
int color[maxn],dfn[maxn],low[maxn],stack[maxn],vis[maxn],cnt[maxn];
int in[maxn],out[maxn];
int top,n,m,sum,ans;
int deep = ;
void tarjan(int u){
dfn[u]=++deep;
low[u]=deep;
vis[u]=;
stack[++top]=u;
int sz=g[u].size();
for(int i=;i<sz;i++){
int v=g[u][i];
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else{
if(vis[v]){
low[u]=min(low[u],low[v]);
}
}
}
if(dfn[u]==low[u]){
color[u]=++sum;
vis[u]=;
while(stack[top]!=u){
color[stack[top]]=sum;
vis[stack[top--]]=;
}
top--;
}
return;
}
PI edge[maxn];
vector<int>v[maxn];
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);
}
void dfs(int x, int fa){
if(x!=fa){
int t1 = find(x);
int t2 = find(fa);
f[t1]=t2;
}
if(v[x].size()==&&x!=fa&&vis[x]==){vis[x]=;ans++;}
for(int i = ; i <(int)v[x].size(); i++){
int y = v[x][i];
dfs(y,fa);
}
}
int main() {
while(~scanf("%d %d", &n, &m)){
deep=;
top=ans=sum=;
for(int i = ; i <= n; i++){
f[i]=i;
color[i]=low[i]=dfn[i]=stack[i]=vis[i]=cnt[i]=;
g[i].clear();
v[i].clear();in[i]=out[i]=;
}
for(int i = ; i <= m; i++){
int x,y;
scanf("%d %d", &x, &y);
edge[i] = make_pair(x,y);
g[x].pb(y);
}
for(int i = ; i <= n; i++){
if(!dfn[i])tarjan(i);
}
/*for(int i = 1; i <= n; i++){
printf("--%d %d %d\n",i,color[i],low[i]);
}*/
//suo dian
for(int i = ; i <= n; i++)vis[i]=;
for(int i = ; i <= m; i++){
int x = edge[i].fst;
int y = edge[i].sc;
x = color[x];
y = color[y];
if(x!=y){
v[x].pb(y);
in[y]++;
out[x]++;
}
}
int In=;
int Ou=;
for(int i = ; i <= sum; i++){
if(in[i]==)In++;
if(out[i]==)Ou++;
}
int ans = max(In,Ou);
if(ans==)ans--;
printf("%d\n",ans); }
return ;
}
/*
4 3
1 2
2 3
4 3 4 3
1 2
2 3
3 4 8 6
1 2
1 3
1 5
3 4
3 6
6 7 6 5
1 2
2 3
3 1
3 5
1 4 2 2
1 2
2 1 3 3
1 2
2 3
1 3 7 6
1 2
2 3
1 3
4 5
4 6
4 7 4 5
1 2
1 3
1 4
2 3
3 4 4 5
1 2
1 3
1 4
2 3
4 3 3 1
1 2
*/
HDU3836 Equivalent Sets (Tarjan缩点+贪心)的更多相关文章
- hdu 3836 Equivalent Sets trajan缩点
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- 20190716NOIP模拟赛T2 通讯(tarjan缩点+贪心)
题目描述 “这一切都是命运石之门的选择.” 试图研制时间机器的机关SERN截获了中二科学家伦太郎发往过去的一条短 信,并由此得知了伦太郎制作出了电话微波炉(仮). 为了掌握时间机器的技术,SERN总部 ...
- 【洛谷P5008 逛庭院】tarjan缩点+贪心
既然没有题解,那么我就来提供给一份. -- 首先我们看到数据范围.妈耶!数据这么大,一开始还想用个DP来做,但是看着就不行,那么根据这个数据范围,我们大致可以猜到这道题的算法是一个贪心,那么我们怎么贪 ...
- hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- [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
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Description To prove two sets A ...
- 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 ...
- 【HDOJ2767】【Tarjan缩点】
http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 4000/2000 MS (Java/O ...
随机推荐
- docker+mysql 构建数据库的主从复制
docker+mysql 构建数据库的主从复制 在最近的项目中,决定将项目改造成数据库读写分离的架构,后续会有博文详细讲述我的开发改造,本文主要记录我是如何一步步的构建数据库的主从复制. 为什么使用d ...
- 3maven常用命令和配置依赖
依赖: 例:spring-context.jar 依赖 spring-aop.jar... A中的某些类 需要使用B中的某些类,则称为A依赖于B 在maven项目中,如果要使用 一个当时存在的Jar或 ...
- python的list()函数
list()函数将其它序列转换为 列表 (就是js的数组). 该函数不会改变 其它序列 效果图一: 代码一: # 定义一个元组序列 tuple_one = (123,','abc') print( ...
- 最大区间和变形 - codeforces
题意 : 可以选择操作一串区间,将区间内的某一个数全部变成一个新的数字,询问整个区间中某个数字的出现次数总共有多少个? 思路分析 : 首先最后选的一定是一个区间,然后 ans = cnt(1, l-1 ...
- 从网上下载DLL
1,微软官网 2:https://www.zhaodll.com/ 3:http://www.dllzj.com/
- Go 每日一库之 viper
简介 上一篇文章介绍 cobra 的时候提到了 viper,今天我们就来介绍一下这个库. viper 是一个配置解决方案,拥有丰富的特性: 支持 JSON/TOML/YAML/HCL/envfile/ ...
- arima.predict()参数选择以及相关的一些问题
在使用a ri ma进行模型建立时,需要注意以下几点 1.参数选择上predict必须起始时间在原始的数据及当中的,在下例中就是说2017必须在数据集里面,而2019不受限制,只哟在2017后面就好了 ...
- Linux内存管理解析(一) : 分段与分页机制
背景 : 在此文章里会从分页分段机制去解析Linux内存管理系统如何工作的,由于Linux内存管理过于复杂而本人能力有限.会尽量将自己总结归纳的部分写清晰. 从实模式到保护模式的寻址方式的不同 : 1 ...
- Creating Custom Helper Methods 创建自定义辅助器方法----辅助器方法 ------ 精通ASP.NET MVC 5
创建内联的辅助器方法 和 拓展方法 好像类似的功能. 不过写在前台更直观
- 分层有限状态机的C++实现
为了方便我的游戏开发,写了这么一个通用的分层有限状态机.希望在其稳定以后,可以作为一个组件加入到我的游戏引擎当中. 目前使用了std::function来调用回调函数,在未来可能会用委托机制代替. 第 ...