layout: post

title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)

author: "luowentaoaa"

catalog: true

mathjax: true

tags:

- 双连通分量

- 基础DP

- 图论

- 训练指南


The Largest Clique

UVA - 11324

题意

给一张有向图G,求一个结点数最大的结点集,使得该结点中任意两个结点 u 和 v满足:要么 u 可以到达 v, 要么 v 可以到达 u(u 和 v 相互可达也可以)。

题解

同一个强连通分量中的点要么都选,要么不选。把强连通分量收缩点后得到SCC图,让每个SCC结点的权等于它的结点数,则题目转化为求SCC图上权最大的路径。所以转化成了dp求DAG上的最长路。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e3+50;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
vector<int>G[maxn],g[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt,sccnum[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;
sccnum[scc_cnt]++;
if(x==u)break;
}
}
}
void find_scc(int n){
dfs_clock=scc_cnt=0;
memset(sccno,0,sizeof(sccno));
memset(pre,0,sizeof(pre));
memset(sccnum,0,sizeof(sccnum));
for(int i=0;i<n;i++)
if(!pre[i])dfs(i);
}
int d[maxn];
int dp(int i){
int &ans=d[i];
if(ans>=0)return ans;
ans=sccnum[i];
for(int j=0;j<g[i].size();j++){
int v=g[i][j];
ans=max(ans,dp(v)+sccnum[i]);
}
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
// freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
for(int i=0;i<=n;i++)G[i].clear(),g[i].clear();
int u,v;
for(int i=0;i<m;i++){
cin>>u>>v;
u--;v--;
G[u].push_back(v);
}
find_scc(n);
memset(d,-1,sizeof(d));
for(int u=0;u<n;u++)
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(sccno[u]!=sccno[v])
g[sccno[u]].push_back(sccno[v]);
}
int ans=0;
for(int i=1;i<=scc_cnt;i++)
ans=max(ans,dp(i));
cout<<ans<<endl;
} return 0;
}

最新文章

  1. jQuery核心技术-----------------------------------------------------()
  2. Provisional headers are shown,本地测试成功,服务器运行却失败
  3. EntityFramework_MVC4中EF5 新手入门教程之二 ---2.执行基本的 CRUD 功能
  4. 简单的python http接口自动化脚本
  5. oracle linux下oracle 10g启动EM、isqlplus及相关命令语法
  6. python scrapy 基础
  7. Office OpenXML-Excel(一)
  8. Hibernate与 MyBatis的比较(转)
  9. scrapy框架第一章
  10. WORD2010如何把全角字母和数字批量转换成半角
  11. Java18-java语法基础——集合框架
  12. 【PyQt5-Qt Designer】制作炫酷的启动界面+进度条
  13. 用div画一个圣诞树
  14. mybatis四(动态sql)
  15. 如何将水晶报表(Crystal Report)导入葡萄城报表
  16. LeetCode practice
  17. 在Windows子系统(WSL)中配置开机启动服务
  18. 任务调度 Quartz 学习(二) CronTrigger
  19. [eShopOnContainers 学习系列] - Index - 开篇索引
  20. Linux --Mysql基础命令

热门文章

  1. hdu 2838 Cow Sorting (树状数组)
  2. 【转】The test form is only available for requests from the local machine 解决方法
  3. [Leetcode] Reverse nodes in k group 每k个一组反转链表
  4. 安徽师大附中%你赛day9 T3 贵 解题报告
  5. BZOJ1875: [SDOI2009]HH去散步 图上边矩乘
  6. 写一个JavaScript“返回顶部”功能
  7. Codeforces Round #525 (Div. 2)A. Ehab and another construction problem
  8. Linux下只允许用户远程scp
  9. 通过js修改微信内置浏览器title
  10. CSS样式实现溢出超出DIV边框宽度高度的内容自动隐藏方法