[2017纪中11-5]轰炸 强联通分量+DAG最长路
来源:互联网 发布:剑灵正太捏脸数据 编辑:程序博客网 时间:2024/09/21 08:55
题面
读清题。。。
考虑一个强联通分量,两两互相可达,那么轰炸掉这个强联通分量至少需要其中点的个数轮。
考虑一条路径,轰炸一条路径需要它的长度轮。
于是tarjan缩点在拓扑排序一下跑一个DAG最长路即可。
代码:
#include<iostream>#include<cstdio>using namespace std;const int maxn=1000010;int n,m,tim,nc,top,ans,st[maxn],dfn[maxn],low[maxn],col[maxn],sz[maxn],rd[maxn],len[maxn],ord[maxn];bool vis[maxn],vst[maxn];struct edge{ int t; edge *next;}*con[maxn],*sd[maxn];void ins(int x,int y){ edge *p=new edge; p->t=y; p->next=con[x]; con[x]=p;}void ins2(int x,int y){ edge *p=new edge; p->t=y; p->next=sd[x]; sd[x]=p;}void tarjan(int v){ dfn[v]=low[v]=++tim; vst[v]=vis[v]=1; st[++top]=v; for(edge *p=con[v];p;p=p->next) if(!vis[p->t]) tarjan(p->t),low[v]=min(low[v],low[p->t]); else if(vst[p->t]) low[v]=min(low[v],dfn[p->t]); if(low[v]==dfn[v]) { nc++; while(st[top+1]!=v) { vst[st[top]]=0; col[st[top--]]=nc; sz[nc]++; } }}void suodian(){ for(int i=1;i<=n;i++) for(edge *p=con[i];p;p=p->next) if(col[i]!=col[p->t]) {ins2(col[i],col[p->t]);rd[col[p->t]]++;}}void tuopu(){ top=0; for(int i=1;i<=nc;i++) if(rd[i]==0)st[++top]=i; int pnt=0; while(top>0) { ord[++pnt]=st[top--]; for(edge *p=sd[ord[pnt]];p;p=p->next) { rd[p->t]--; if(rd[p->t]==0) st[++top]=p->t; } } for(int i=nc;i>=1;i--) { for(edge *p=sd[ord[i]];p;p=p->next) len[ord[i]]=max(len[ord[i]],len[p->t]); len[ord[i]]+=sz[ord[i]]; }}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); ins(x,y); } for(int i=1;i<=n;i++) if(!vis[i]) tarjan(i); ans=0; suodian(); tuopu(); for(int i=1;i<=nc;i++) ans=max(ans,len[i]); printf("%d",ans); return 0;}
阅读全文
0 0
- [2017纪中11-5]轰炸 强联通分量+DAG最长路
- 【UVa11324】最大团The Largest Clique【强联通分量】【DAG】
- UVA11324 The Largest Clique (强联通+缩点+DAG上DP最长路)
- 训练赛 Grouping(强连通分量缩点 + DAG求最长路)
- 强连通分量,DAG上的最长路,记忆化搜索(最大团,UVA 11324)
- 【APIO2009T3】抢掠计划-强连通分量缩点+DAG单源最长路
- 强联通分量
- 强联通分量
- poj2375 强联通分量
- 强联通分量
- poj1904(强联通分量)
- 强联通分量
- 强联通分量-tarjan
- 强联通分量-kosaraju
- 强联通分量-Gabow
- 强联通分量
- 强联通分量分解
- 强联通分量
- 剑指offer--二叉树中和为某一值的路径
- 通用方法
- libcurl windows下编译和post使用及https支持
- SSM重构web版Windows资源管理器
- NOIP2012 开车旅行 (倍增)
- [2017纪中11-5]轰炸 强联通分量+DAG最长路
- 关于组合和继承 ---- 代码复用& 选择问题
- 【Android View事件(四)】View滑动与实现滑动的几种方法
- C语言习题6
- 杂记
- 字符串的操作
- u3d_rpg游戏开发之物品管理(一)
- PAT 1038
- 深度学习网络调参技巧