训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
2024-09-27 16:19:00
layout: post
title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
author: "luowentaoaa"
catalog: true
mathjax: true
tags:
- 双连通分量
- 基础DP
- 图论
- 训练指南
The Largest Clique
题意
给一张有向图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;
}
最新文章
- jQuery核心技术-----------------------------------------------------()
- Provisional headers are shown,本地测试成功,服务器运行却失败
- EntityFramework_MVC4中EF5 新手入门教程之二 ---2.执行基本的 CRUD 功能
- 简单的python http接口自动化脚本
- oracle linux下oracle 10g启动EM、isqlplus及相关命令语法
- python scrapy 基础
- Office OpenXML-Excel(一)
- Hibernate与 MyBatis的比较(转)
- scrapy框架第一章
- WORD2010如何把全角字母和数字批量转换成半角
- Java18-java语法基础——集合框架
- 【PyQt5-Qt Designer】制作炫酷的启动界面+进度条
- 用div画一个圣诞树
- mybatis四(动态sql)
- 如何将水晶报表(Crystal Report)导入葡萄城报表
- LeetCode practice
- 在Windows子系统(WSL)中配置开机启动服务
- 任务调度 Quartz 学习(二) CronTrigger
- [eShopOnContainers 学习系列] - Index - 开篇索引
- Linux --Mysql基础命令
热门文章
- hdu 2838 Cow Sorting (树状数组)
- 【转】The test form is only available for requests from the local machine 解决方法
- [Leetcode] Reverse nodes in k group 每k个一组反转链表
- 安徽师大附中%你赛day9 T3 贵 解题报告
- BZOJ1875: [SDOI2009]HH去散步 图上边矩乘
- 写一个JavaScript“返回顶部”功能
- Codeforces Round #525 (Div. 2)A. Ehab and another construction problem
- Linux下只允许用户远程scp
- 通过js修改微信内置浏览器title
- CSS样式实现溢出超出DIV边框宽度高度的内容自动隐藏方法