UVA11324 The Lagest Lique(SCC缩点+DP)
Given a directed graph G, con- sider the following transformation. First, create a new graph T(G) to have the same vertex set as G. Cre- ate a directed edge between two vertices u and v in T(G) if and only if there is a path between u and v in G that follows the directed edges only in the forward direction. This graph T(G) is often called the tran- sitive closure of G. We define a clique in a directed graph as a set of vertices U such that for any two vertices u and v in U, there is a directed edge either from u to v or from v to u (or both). The size of a clique is the number of vertices in the clique. Input The number of cases is given on the first line of input. Each test case describes a graph G. It begins with a line of two integers n and m, where 0 ≤ n ≤ 1000 is the number of vertices of G and 0 ≤ m ≤ 50, 000 is the number of directed edges of G. The vertices of G are numbered from 1 to n. The following m lines contain two distinct integers u and v between 1 and n which define a directed edge from u to v in G. Output For each test case, output a single integer that is the size of the largest clique in T(G). Sample Input 1 5 5 1 2 2 3 3 1 4 1 5 2 Sample Output 4The
题解:让你求至少单向可以连通的最多的节点数;
我们可以用个SCC缩点以后,然后进行记忆化搜索+DP,从没一个“点”开始,求最大值即可;
dp[x]=max(dp[x],sz[x]+DP(G[x][i]); DP为搜索函数,搜索点x的最多节点数;sz[x]为点“x”代表的点集的个数;
参考代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int t,n,m,u,v,times,blocks;
int dfn[maxn],lowv[maxn],belong[maxn],ins[maxn],sz[maxn];
int dp[maxn];
struct Node{
int u,v;
Node(int _u,int _v): u(_u),v(_v) { }
}; vector<Node> edges;
vector<int> G[maxn],GG[maxn];
stack<int> st; void Addedge(int u,int v)
{
G[u].push_back(edges.size());
edges.push_back(Node(u,v));
} void Init()
{
times=blocks=;
memset(dp,-,sizeof dp);
memset(dfn,,sizeof dfn);
memset(lowv,,sizeof lowv);
memset(sz,,sizeof sz);
memset(ins,,sizeof ins);
memset(belong,,sizeof belong);
while(!st.empty()) st.pop();
edges.clear();
for(int i=;i<=n;i++) G[i].clear();
} void Tarjan(int u)
{
dfn[u]=lowv[u]=++times;
st.push(u);
ins[u]=;
for(int i=;i<G[u].size();i++)
{
int v=edges[G[u][i]].v;
if(!dfn[v]) Tarjan(v),lowv[u]=min(lowv[u],lowv[v]);
else if(ins[v]) lowv[u]=min(lowv[u],dfn[v]);
}
if(dfn[u]==lowv[u])
{
++blocks;
int v;
do
{
v=st.top(); st.pop();
ins[v]=;
belong[v]=blocks;
sz[blocks]++;
} while(u!=v);
}
} int DP(int x)
{
if(dp[x]>) return dp[x];
dp[x]=sz[x];
for(int i=;i<GG[x].size();i++) dp[x]=max(dp[x],DP(GG[x][i])+sz[x]);
return dp[x];
} int main()
{
ios::sync_with_stdio(false);
cin>>t;
while(t--)
{
cin>>n>>m;
Init();
for(int i=;i<=m;i++)
{
cin>>u>>v;
Addedge(u,v);
}
for(int i=;i<=n;i++) if(!dfn[i]) Tarjan(i);
for(int i=;i<=n;i++) GG[i].clear();
for(int i=;i<=n;i++)
{
for(int j=;j<G[i].size();j++)
{
if(belong[i]!=belong[edges[G[i][j]].v])
GG[belong[i]].push_back(belong[edges[G[i][j]].v]);
}
}
int ans=;
for(int i=;i<=blocks;i++) ans=max(ans,DP(i));
cout<<ans<<endl;
}
return ;
}
UVA11324 The Lagest Lique(SCC缩点+DP)的更多相关文章
- UVA11324 The Largest Clique (强连通缩点+DP最长路)
		
<题目链接> 题目大意: 给你一张有向图 G,求一个结点数最大的结点集,使得该结点集中的任意两个结点 u 和 v 满足:要么 u 可以达 v,要么 v 可以达 u(u,v相互可达也行). ...
 - POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]
		
Father Christmas flymouse Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 3241 Accep ...
 - UVA11324 The Largest Clique —— 强连通分量 + 缩点 + DP
		
题目链接:https://vjudge.net/problem/UVA-11324 题解: 题意:给出一张有向图,求一个结点数最大的结点集,使得任意两个结点u.v,要么u能到达v, 要么v能到达u(u ...
 - UVA11324 The Largest Clique[强连通分量 缩点 DP]
		
UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直 ...
 - bzoj1093: [ZJOI2007]最大半连通子图  scc缩点+dag上dp
		
一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G'=(V ...
 - BZOJ 2707: [SDOI2012]走迷宫 [高斯消元 scc缩点]
		
2707: [SDOI2012]走迷宫 题意:求s走到t期望步数,\(n \le 10^4\),保证\(|SCC| \le 100\) 求scc缩点,每个scc高斯消元,scc之间直接DP 注意每次清 ...
 - 洛谷 P6030 - [SDOI2012]走迷宫(高斯消元+SCC 缩点)
		
题面传送门 之所以写个题解是因为题解区大部分题解的做法都有 bug(u1s1 周六上午在讨论区里连发两个 hack 的是我,由于我被禁言才让 ycx 代发的) 首先碰到这种期望题,我们套路地设 \(d ...
 - HDU 3072--Intelligence System【SCC缩点新构图 && 求连通全部SCC的最小费用】
		
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
 - POJ 2186 Popular cows(SCC 缩点)
		
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...
 
随机推荐
- ubuntu开机自启动服务
			
ubuntu下一个用来管理开机自启动服务的程序,今天在ss vps上安装时老是提示这个错误,百度后,下面的这个方法可行: vi /etc/apt/source.list 输入i,进入Insert模式 ...
 - NW.js打包一个桌面应用
			
1.安装nw(可以到官网:https://nwjs.io下载) npm install nw -g 2.创建一个最最简单的nw应用 在nwjs文件夹中 新建index.html和package.jso ...
 - 人人都懂区块链--pdf电子版学习资料下载
			
人人都懂区块链 21天从区块链“小白”到资深玩家电子版pdf下载 链接:https://pan.baidu.com/s/1TWxYv4TLa2UtTgU-HqLECQ 提取码:6gy0 好的学习资料需 ...
 - 前端与算法 leetcode 8. 字符串转换整数 (atoi)
			
目录 # 前端与算法 leetcode 8. 字符串转换整数 (atoi) 题目描述 概要 提示 解析 解法一:正则 解法二:api 解法二:手搓一个api 算法 传入测试用例的运行结果 执行结果 G ...
 - 基于.NetStandard的简易EventBus实现-基础实现
			
一.问题背景 最近离职来到了一家新的公司,原先是在乙方工作,这回到了甲方,在这一个月中,发现目前的业务很大一部分是靠轮询实现的,例如:通过轮询判断数据处于B状态了,则轮询到数据后执行某种动作,这个其实 ...
 - iOS UIKit x Android Widget
			
Android的事件回调Listener相当于iOS的delegate回调. Android的事件回调接口Listener相当于iOS的protocol回调协议. Android的UI容器(Adapt ...
 - python gui tkinter快速入门教程 | python tkinter tutorial
			
本文首发于个人博客https://kezunlin.me/post/d5c57f56/,欢迎阅读最新内容! python tkinter tutorial Guide main ui messageb ...
 - python3 之 文件read方法(read、readline、readlines)
			
目录 一.read方法 二.readline方法 三.readlines方法 正文 python3中,读取文件有三种方法:read().readline().readlines(). 此三种方法,均支 ...
 - Few-shot Object Detection via Feature Reweighting (ICCV2019)
			
论文:https://arxiv.org/abs/1812.01866 代码:https://github.com/bingykang/Fewshot_Detection 1.研究背景 深度卷积神经网 ...
 - Java流程控制之(四)中断
			
目录 break continue return 标签 在程序设计时,循环直接的跳转显得十分重要,虽然Java没有提供goto语句去控制程序的跳转,但为了控制循环,Java提供了continue,br ...