当前位置: 首页 > news >正文

Tarjan——强连通分量

Tarjan——强连通分量

B3609 [图论与代数结构 701] 强连通分量

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
const int N=10010;
const int M=200010;
struct Edge{int to,nex;
}edge[4*M];
int cnt;
int head[N];
void add_edge(int x,int y){edge[++cnt].to=y;edge[cnt].nex=head[x];head[x]=cnt;return;
}
int n,m;
int dfn[N],low[N],bel[N],scc,sccn[N],tot;vector<int>sccp[N];bool instk[N],vis[N];
int stk[N],top;
void tarjan(int u){vis[u]=1;dfn[u]=low[u]=++tot;instk[stk[++top]=u]=1;for(int pos=head[u];pos;pos=edge[pos].nex){int v=edge[pos].to;if(!dfn[v]){tarjan(v);low[u]=min(low[v],low[u]);}else if(instk[v]){low[u]=min(low[u],dfn[v]);//一定是 dfn[v]   因为low[v]有可能还没有更新完毕}}if(low[u]==dfn[u]){scc++;int cur;do{cur=stk[top--];bel[cur]=scc;sccn[scc]++;sccp[scc].push_back(cur);instk[cur]=0;}while(u!=cur);}
}
int in_num[N],out_num[N];
signed main(){IOScin>>n>>m;for(int i=1;i<=m;i++){int in1,in2;cin>>in1>>in2;add_edge(in1,in2);}for(int i=1;i<=n;i++){if(!vis[i])tarjan(i);}for(int i=1;i<=scc;i++)sort(sccp[i].begin(),sccp[i].end());cout<<scc<<endl;bool visscc[N]={};for(int i=1;i<=n;i++){if(!visscc[bel[i]]){for(int t:sccp[bel[i]])cout<<t<<' ';cout<<endl;visscc[bel[i]]=1;}}return 0; 
}

P3387 【模板】缩点

#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define endl '\n'
#define int long long 
const int N=1e4+114;
const int M=1e5+114;
struct Edge{int to,nex;
}edge[M],edge_scc[N];
int head[N],cnt;
void add_edge(int a,int to_b){edge[++cnt].nex=head[a];edge[cnt].to=to_b;head[a]=cnt;
}
int head_scc[N],cnt_scc;
int sccinnum[N];void add_edge_scc(int a,int to_b){edge_scc[++cnt_scc].nex=head_scc[a];edge_scc[cnt_scc].to=to_b;head_scc[a]=cnt_scc;
}
int val[N];
bool instk[N];
int stk[N],top;
int dfn[N],low[N],tot;
int sccnum,bel[N];
int sccval[N];
void tarjan(int u){dfn[u]=low[u]=++tot;instk[stk[++top]=u]=1;for(int i=head[u];i;i=edge[i].nex){int v=edge[i].to;if(!dfn[v]){tarjan(v);low[u]=min(low[u],low[v]);}else if(instk[v]){low[u]=min(low[u],dfn[v]);}}if(dfn[u]==low[u]){sccnum++;while(instk[u]){int topp=stk[top];bel[topp]=sccnum;sccval[sccnum]+=val[topp];instk[topp]=0;top--;}}return;
}
void solve(){int n,m;cin>>n>>m;for(int i=1;i<=n;i++){cin>>val[i];}for(int i=1;i<=m;i++){int a,to_b;cin>>a>>to_b;add_edge(a,to_b);}for(int i=1;i<=n;i++){//tarjanif(!dfn[i]){tarjan(i);}}for(int i=1;i<=n;i++){//建立新图 for(int j=head[i];j;j=edge[j].nex){int to=edge[j].to;if(bel[i]!=bel[to]){add_edge_scc(bel[i],bel[to]);sccinnum[bel[to]]++;}}}queue<int>que;//拓扑排序 for(int i=1;i<=sccnum;i++){if(sccinnum[i]==0){que.push(i);}}int ans[N]={};bool vis[N]={};while(!que.empty()){int pos=que.front();que.pop();vis[pos]=1;ans[pos]+=sccval[pos];for(int i=head_scc[pos];i;i=edge_scc[i].nex){int to=edge_scc[i].to;if(!vis[to]){ans[to]=max(ans[to],ans[pos]);sccinnum[to]--;if(sccinnum[to]==0)que.push(to);}}}int maxn=0;for(int i=1;i<=sccnum;i++){maxn=max(maxn,ans[i]);}cout<<maxn<<endl;
}
signed main(){IOSsolve();return 0;
}
http://www.jsqmd.com/news/51536/

相关文章:

  • 次短路 dijkstra
  • 优化需求评审流程论LLM与人工审查协同模式
  • 2025年11月少儿编程机构怎么选?家长必藏的口碑推荐指南
  • 超越监控:MyEMS 在水泥生产工艺中的深度集成与能效协奏(以印尼 SIG 水泥为例)
  • nvm和npm镜像源配置
  • 银河麒麟下Redis的安装和集群配置
  • 从开发板到工业核心:迅为RK3576的金属外壳,为何是行业应用的“点睛之笔”?
  • Transformer 架构中的 ResNet + LayerNorm 设计解析
  • 【IEEE出版 | EI期刊同步征稿 | 往届已快速成功EI检索】第六届新能源与电气科技国际学术研讨会 (ISNEET 2025)
  • dijkstra——单源最短路径(标准版)
  • 蓝桥杯python基础语法
  • Jenkins view权限
  • Acrobat DC 2025安装教程
  • 实用指南:Windows 环境下为银河麒麟(Linux ARM64)生成 node_modules 依赖
  • 从数据洞察到财务收益:MyEMS 如何通过 AI 优化调度帮助企业将能效提升转化为真金白银
  • 2025年11月英语学习软件推荐:从零基础到流利口语,最好的学英语软件全攻略
  • RAG项目实战:基于图文PDF的多模态问答RAG项目(二)之向量库建设
  • 【SAE出版|EI检索|北京航天航空大学主办】第六届应用力学与机械工程国际学术会议(ICAMME 2025)
  • 高频电流探头频率响应特性及其影响因素深度分析
  • C++ 基础学习总结:从入门到构建核心认知 - 实践
  • 题解:P14598 [COCI 2025/2026 #2] 搭塔 / Tornjevi
  • 2025英语自学软件推荐:AI时代,用这些工具让你的学习效率翻倍
  • 2025 年 11 月工时管理系统/软件实力推荐榜:高效工时管理软件,智能工时统计系统,企业工时管理平台精选与深度解析
  • Google推出适用于Go的Agent开发工具包 - 公众号
  • 2025年质量好的大冰花钛杯厂家推荐及选择指南
  • 2025年评价高的化工厂清淤机器人高评价厂家推荐榜
  • 2025年质量好的全自动opp束带机最新TOP品牌厂家排行
  • 2025年口碑好的斯诺克台球桌厂家最新TOP排行榜
  • 2025年评价高的衣柜灯热门厂家推荐榜单
  • 2025年口碑好的化工原料烘干机厂家最新权威实力榜