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)的更多相关文章

  1. UVA11324 The Largest Clique (强连通缩点+DP最长路)

    <题目链接> 题目大意: 给你一张有向图 G,求一个结点数最大的结点集,使得该结点集中的任意两个结点 u 和 v 满足:要么 u 可以达 v,要么 v 可以达 u(u,v相互可达也行). ...

  2. POJ3160 Father Christmas flymouse[强连通分量 缩点 DP]

    Father Christmas flymouse Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 3241   Accep ...

  3. UVA11324 The Largest Clique —— 强连通分量 + 缩点 + DP

    题目链接:https://vjudge.net/problem/UVA-11324 题解: 题意:给出一张有向图,求一个结点数最大的结点集,使得任意两个结点u.v,要么u能到达v, 要么v能到达u(u ...

  4. UVA11324 The Largest Clique[强连通分量 缩点 DP]

    UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直 ...

  5. 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 ...

  6. BZOJ 2707: [SDOI2012]走迷宫 [高斯消元 scc缩点]

    2707: [SDOI2012]走迷宫 题意:求s走到t期望步数,\(n \le 10^4\),保证\(|SCC| \le 100\) 求scc缩点,每个scc高斯消元,scc之间直接DP 注意每次清 ...

  7. 洛谷 P6030 - [SDOI2012]走迷宫(高斯消元+SCC 缩点)

    题面传送门 之所以写个题解是因为题解区大部分题解的做法都有 bug(u1s1 周六上午在讨论区里连发两个 hack 的是我,由于我被禁言才让 ycx 代发的) 首先碰到这种期望题,我们套路地设 \(d ...

  8. HDU 3072--Intelligence System【SCC缩点新构图 &amp;&amp; 求连通全部SCC的最小费用】

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. 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, ...

随机推荐

  1. Linux命令大全(个人整理,如不全面望谅解)

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...

  2. Kafka needs no Keeper(关于KIP-500的讨论)

    写在前面的 最近看了Kafka Summit上的这个分享,觉得名字很霸气,标题直接沿用了.这个分享源于社区的KIP-500,大体的意思今后Apache Kafka不再需要ZooKeeper.整个分享大 ...

  3. Verilog模块概念和实例化#转载自Jason from Lofter

    Verilog模块概念和实例化 模块的概念 模块(module)是verilog最基本的概念,是v设计中的基本单元,每个v设计的系统中都由若干module组成. 1.模块在语言形式上是以关键词modu ...

  4. 领扣(LeetCode)两个列表的最小索引总和 个人题解

    假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答案不止一个,则输出所有答 ...

  5. C语言1博客作业06

    这个作业属于哪个课程 C语言程序设计II 这个作业的要求在哪里 https://www.cnblogs.com/sanying/p/11771502.html 我在这个课程的目标是 端正态度,认真对待 ...

  6. expect 自动填充密码

    它的脚本以#!/usr/bin/expect开头,执行时用expoct,而不是bash.我的一个给samba自动创建用户并且自动填写默认密码的脚本如下: vim smb_passwd.exp #!/u ...

  7. 从静态代理,jdk动态代理到cglib动态代理-一文搞懂代理模式

    从代理模式到动态代理 代理模式是一种理论上非常简单,但是各种地方的实现往往却非常复杂.本文将从代理模式的基本概念出发,探讨代理模式在java领域的应用与实现.读完本文你将get到以下几点: 为什么需要 ...

  8. Kafka幂等性原理及实现剖析

    1.概述 最近和一些同学交流的时候反馈说,在面试Kafka时,被问到Kafka组件组成部分.API使用.Consumer和Producer原理及作用等问题都能详细作答.但是,问到一个平时不注意的问题, ...

  9. SoapUI使用JDBC请求连接数据库及断言的使用

    SoapUI提供了用来配置JDBC数据库连接的选项,因此我们可以在测试中使用JDBC数据源.JDBC数据接收器和JDBC请求步骤. 为了能够配置数据连接,就必须有驱动程序和连接串,SoapUI中已经提 ...

  10. OSI层次模型

    ISO:国际标准化组织 层(layer):描述所有的有效的通讯过程,并把逻辑上的组叫做层. 分层优点: 促进标准化工作,允许各个供应商进行开发 各层之间相互独立,把网络操作分成低复杂度性单元 灵活性好 ...