UVA 11324 scc缩点+树上最长路
来源:互联网 发布:综合布线仿真软件下载 编辑:程序博客网 时间:2024/06/05 14:42
#include<bits/stdc++.h>using namespace std;const int maxn = 1010;vector<int>g[maxn];vector<int>m[maxn];int node[maxn];int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;int dp[maxn];stack<int>s;void dfs(int u){ pre[u] = lowlink[u] = ++dfs_clock; s.push(u); for(int i = 0; i<g[u].size(); i++) { int v= g[u][i]; if(!pre[v]) { dfs(v); lowlink[u] = min(lowlink[u],lowlink[v]); } else if(!sccno[v]) { lowlink[u] = min(lowlink[u],pre[v]); } } if(lowlink[u]==pre[u]) { scc_cnt++; for(;;) { int x= s.top(); s.pop(); sccno[x] = scc_cnt; if(x==u)break; } }}void find_scc(int n ){ dfs_clock = 0 ; scc_cnt = 0; memset(sccno,0,sizeof(sccno)); memset(pre,0,sizeof(pre)); for(int i = 0; i<n; i++) { if(!pre[i]) dfs(i); }}int get(int x){ if(dp[x])return dp[x]; int ans = 0; for(int i = 0; i<m[x].size(); i++) { ans = max(ans,get(m[x][i])); } return dp[x] = ans+node[x];}int main(){ int T; scanf("%d",&T); while(T--) { while(!s.empty()) s.pop(); int n,mv; scanf("%d%d",&n,&mv); for(int i = 0; i<=n; i++) { g[i].clear(); } for(int i = 0; i<=n; i++) { m[i].clear(); } int x,y; for(int i = 1; i<=mv; i++) { scanf("%d%d",&x,&y); x--; y--; g[x].push_back(y); } find_scc(n); memset(node,0,sizeof(node)); for(int i = 0; i<n; i++) { node[sccno[i]]++; } for(int i = 0; i<n; i++) { for(int j = 0; j<g[i].size(); j++) { int v= g[i][j]; if(sccno[i] != sccno[v]) m[sccno[i]].push_back(sccno[v]); } } int ans = 0; memset(dp,0,sizeof(dp)); for(int i = 0; i<=scc_cnt; i++) { ans = max(ans,get(i)); } cout<<ans<<endl; } return 0;}
0 0
- UVA 11324 scc缩点+树上最长路
- UVA 11324 有向图强连通分量缩点得SCC图,并在其上求最长路径
- uva 11324 最大团 (SCC缩点+DP)
- [UVA 11324]The Largest Clique[SCC][缩点]
- UVA 11324 - The Largest Clique(SCC缩点 + DP)
- poj 3160 Father Christmas flymouse (SCC缩点+SPFA求最长路)
- poj 3160 Father Christmas flymouse 【SCC缩点 + 虚拟源点SPFA求最长路】
- POJ 3126 --Father Christmas flymouse【scc缩点构图 && SPFA求最长路】
- POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】
- scc缩点
- UVa 10308 Roads in the North (树上的最长路)
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
- 树上最长路
- hdoj1827Summer Holiday【scc+缩点】
- poj2186Popular Cows【scc+缩点】
- hdoj3836Equivalent Sets【scc +缩点】
- hdoj2767Proving Equivalences【scc+缩点】
- 【BZOJ1179】[Apio2009]Atm【SCC】【最长路】
- IOS8 CoreLocation
- socket关闭之close()和shutdown()的差异
- 计算机视觉CV领域大牛及研究组主页链接
- 实习入职第四天:Thread有关知识点
- 频域分析法求解零状态响应的matlab过程
- UVA 11324 scc缩点+树上最长路
- Linux C语言遍历文件夹
- Leetcode 217. Contains Duplicate
- HDU 2539 点球大战(字符串+输出坑)
- CodeForces 4A Watermelon(太水了。。。。。)
- 安卓菜鸟开发笔记简易窃听器
- 如何跨UI实现功能
- HDU 2767 强联通分量
- c++第六次上机实验-项目2-数组合并