训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
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;
}
训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)的更多相关文章
- 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)
layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...
- POJ3177 Redundant Paths(边双连通分量+缩点)
题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ...
- 训练指南 UVA - 11419(二分图最小覆盖数)
layout: post title: 训练指南 UVA - 11419(二分图最小覆盖数) author: "luowentaoaa" catalog: true mathjax ...
- 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y))
layout: post title: 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y)) author: "luowentaoaa" cata ...
- 训练指南 UVA - 11354(最小生成树 + 倍增LCA)
layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...
- 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)
layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...
- 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)
layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...
- 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...
随机推荐
- POJ——2449 Remmarguts' Date
Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...
- BZOJ4602: [Sdoi2016]齿轮 DFS 逆元
这道题就是一个DFS,有一篇奶牛题几乎一样.但是这道题卡精度. 这道题网上的另一篇题解是有问题的.取对数这种方法可以被轻松卡.比如1e18 与 (1e9-1)*(1e9+1)取对数根本无法保证不被卡精 ...
- 完全解析线程池ThreadPool原理&使用
目录 1. 简介 2. 工作原理 2.1 核心参数 线程池中有6个核心参数,具体如下 上述6个参数的配置 决定了 线程池的功能,具体设置时机 = 创建 线程池类对象时 传入 ThreadPoolExe ...
- ionic2 手风琴效果
user.ts import { Component } from '@angular/core';import { IonicPage, NavController, NavParams } fro ...
- [模拟赛] StopAllSounds
Description 小松鼠开心地在树之间跳跃着,突然她停了下来.因为眼前出现了一个 拿着专克超萌小松鼠的法宝----超萌游戏机的游客! 超萌游戏机之所以拥有这个名字,是因为它的屏幕是一个n × 2 ...
- webkit在vs2008中编译
转载自:http://xjchilli.blog.163.com/blog/static/4534773920091016115533158/ webkit的官方网站写的webkit需要在vs2005 ...
- java 构造函数问题
1.构造函数什么时候被调用,被谁调用? 转摘:http://bbs.csdn.net/topics/350231037 当然,只有在NEW的时候,才会真正的创建这个对象,只有在创建时才会调用该类的构造 ...
- Spring - IoC(8): 基于 Annotation 的配置
除了基于 XML 的配置外,Spring 也支持基于 Annotation 的配置.Spring 提供以下介个 Annotation 来标注 Spring Bean: @Component:标注一个普 ...
- 滑杆(JSlider)和进度指示条(JProgressBar) 的使用
package first; import javax.swing.*; import javax.swing.border.TitledBorder; import java.awt.*; impo ...
- Extjs GridPanel 鼠标拖动选中单元格
本文主要是实现了一个拖动选择单元格并计算的功能