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. (数据科学学习手札71)在Python中制作个性化词云图

    本文对应脚本及数据已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 一.简介 词云图是文本挖掘中用来表征词频的数据可视化 ...

  2. table的列固定

    <body onload="showFix(true,false,initTableId);"> <!doctype html> <html lang ...

  3. Springboot 自动配置浅析

    Introduction 我们知道,SpringBoot之所以强大,就是因为他提供了各种默认的配置,可以让我们在集成各个组件的时候从各种各样的配置文件中解放出来. 拿一个最普通的 web 项目举例.我 ...

  4. Asp.net Core 系列之--4.事务、日志及错误处理

    ChuanGoing 2019-11-17 这篇原本时想把事务处理.日志处理.错误处理.授权于鉴权一并介绍完的,授权和鉴权我想结合自定义权限来介绍,全部放到这里篇幅可能太长,因此权限部分将会在下篇来介 ...

  5. django_2:模板

    使用模板变量: 在html文件中,{{title}}即为模板变量, 在view.py文件中,render函数,增加第三个参数,以字典形式给值. def index(req): return rende ...

  6. Linux定时任务 crontab(-l -e)、at、batch

    1.周期性定时任务crontab cron['krɒn] 一时间单位  table crontab -e 进入编辑定时任务界面,每一行代表一个定时任务,#开头的行为注释行,一行分成6列 分钟 小时 日 ...

  7. VMware NAT模式ping通外网[CentOS7]

    使用一张网卡,NAT模式 在编辑里打开虚拟网络编辑器 dhcp设置的范围 你的虚拟机的IP 就在那个范围里 NAT设置里有填网关 这里我们vmware 的设置就OK了 在去把你的网络适配器改下iP 这 ...

  8. MAC终端中tree命令

    Mac没有自带的tree命令,需要额外安装才可以,操作方法有两种: 一.用find命令模拟tree效果 1.mac下默认是没有 tree命令的,不过我们可以使用find命令模拟出tree命令的效果,如 ...

  9. mysql锁简谈

    1.mysql锁, 作用:解决因资源共享而造成的并发问题. 实例:买最好一件衣服X A: X 买: X加锁----->试衣服……下单……付款……打包….------>X解锁 B: X 买: ...

  10. 基于Galera Cluster多主结构的Mysql高可用集群

    Galera Cluster特点 1.多主架构:真正的多点读写的集群,在任何时候读写数据,都是最新的 2.同步复制:集群不同节点之间数据同步,没有延迟,在数据库挂掉之后,数据不会丢失 3.并发复制:从 ...