原文地址

Problem Portal

Portal1:UVa

Portal2:Luogu

Portal3:Vjudge

Description

Given a directed graph \(\text{G}\), consider the following transformation.

First, create a new graph \(\text{T(G)}\) to have the same vertex set as \(\text{G}\). Create a directed edge between two vertices u and v in \(\text{T(G)}\) if and only if there is a path between u and v in \(\text{G}\) that follows the directed edges only in the forward direction. This graph \(\text{T(G)}\) is often called the \(\texttt{transitive closure}\) of \(\text{G}\).



We define a \(\texttt{clique}\) in a directed graph as a set of vertices \(\text{U}\) such that for any two vertices u and v in \(\text{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 \(\text{G}\). It begins with a line of two integers \(n\) and \(m\), where \(0 \leq n \leq 1000\) is the number of vertices of \(\text{G}\) and \(0 \leq m \leq 50, 000\) is the number of directed edges of \(\text{G}\). The vertices of \(\text{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 \(\text{G}\).

Output

For each test case, output a single integer that is the size of the largest clique in \(\text{T(G)}\).

Sample Input

1
5 5
1 2
2 3
3 1
4 1
5 2

Sample Output

4

Chinese Description

给你一张有向图\(\text{G}\),求一个结点数最大的结点集,使得该结点集中的任意两个结点 \(u\) 和 \(v\) 满足:要么 \(u\) 可以达 \(v\),要么 \(v\) 可以达 \(u\)(\(u\), \(v\)相互可达也行)。

Solution

Tarjan缩点\(+\)记忆化搜索。

Source

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; const int MAXN=200005;
struct node {
int to, nxt;
} edge[MAXN];
int T, n, m, u, v, num, cnt, top, tot, ans, head[MAXN], DFN[MAXN], LOW[MAXN], sum[MAXN], vis[MAXN], sum1[MAXN], stack[MAXN], belong[MAXN];
inline void addedge(int u, int v) {//前向星存图
edge[num].to=v; edge[num].nxt=head[u]; head[u]=num; num++;
}
inline void init() {//初始化
num=cnt=top=tot=ans=0;
memset(head, -1, sizeof(head));
memset(DFN, 0, sizeof(DFN));
memset(LOW, 0, sizeof(LOW));
memset(vis, 0, sizeof(vis));
memset(sum, 0, sizeof(sum));
memset(sum1, -1, sizeof(sum1));
}
inline void tarjan(int u) {//Tarjan缩点
vis[u]=1;
stack[++top]=u;
DFN[u]=++cnt;
LOW[u]=cnt;
for (int i=head[u]; ~i; i=edge[i].nxt) {
int v=edge[i].to;
if (!DFN[v]) {
tarjan(v);
LOW[u]=min(LOW[u], LOW[v]);
} else
if (vis[v]) LOW[u]=min(LOW[u], DFN[v]);
}
if (DFN[u]==LOW[u]) {
tot++;
while (stack[top]!=u) {
vis[stack[top]]=0;
belong[stack[top]]=tot;
sum[tot]++;
top--;
}
vis[stack[top]]=0;
belong[stack[top]]=tot;
top--;
sum[tot]++;
}
}
inline int dfs(int u) {//记忆化搜索
if (sum1[u]!=-1) return sum1[u];
sum1[u]=sum[u];
int addd=0;
for (int i=1; i<=n; i++) {
if (belong[i]==u) {
for (int j=head[i]; ~j; j=edge[j].nxt) {
int v=edge[j].to, s1=belong[v];
if (u==s1) continue;
addd=max(addd, dfs(s1));
}
}
}
return sum1[u]+=addd;
}
int main() {
scanf("%d",&T);
while (T--) {
scanf("%d%d",&n, &m);
init();
for (int i=1; i<=m; i++) {
scanf("%d%d",&u, &v);
addedge(u, v);
}
for (int i=1; i<=n; i++)
if (!DFN[i]) tarjan(i);
for (int i=1; i<=tot; i++)
ans=max(ans, dfs(i));//寻找最大值
printf("%d\n",ans);//输出
}
return 0;
}

『题解』UVa11324 The Largest Clique的更多相关文章

  1. 『题解』洛谷P1063 能量项链

    原文地址 Problem Portal Portal1:Luogu Portal2:LibreOJ Portal3:Vijos Description 在\(Mars\)星球上,每个\(Mars\)人 ...

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

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

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

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

  4. 『题解』Coderforces352A Jeff and Digits

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Jeff's got n cards, each card contains ...

  5. UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)

    题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...

  6. uva11324 The Largest Clique --- 强连通+dp

    给一个有向图G,求一个子图要求当中随意两点至少有一边可达. 问这个子图中最多含多少个顶点. 首先找SCC缩点建图.每一个点的权值就是该点包括点的个数. 要求当中随意两点可达,实际上全部边仅仅能同方向, ...

  7. UVA11324 The Largest Clique(DP+缩点)

    题意:给一张有向图G,求一个结点数最大的结点集,使得该结点中任意两个结点 u 和 v满足:要么 u 可以到达 v, 要么 v 可以到达 u(u 和 v 相互可达也可以). 分析:”同一个强连通分量中的 ...

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

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

  9. UVA11324 The Largest Clique

    嘟嘟嘟 很自然的想到先tarjan把强联通分量缩点,因为对于一个强联通分量,要么不选,要么全选,所以可看成一个点. 然后转化成了求DAG上的一条最长路(每一个点都有权值).刚开始我想用dijkstra ...

随机推荐

  1. Senparc.Weixin.MP SDK 微信公众平台开发教程(二十二):在 .NET Core 2.0/3.0 中使用 MessageHandler 中间件

    概述 在 <Senparc.Weixin.MP SDK 微信公众平台开发教程(六):了解MessageHandler> 中我们已经了解了 MessageHandler 的运行原理和使用方法 ...

  2. Lab_0操作系统实验准备(全)

    一.实验介绍 实验目的: 知识储备: 二.相关下载 1.下载镜像文件 这个镜像文件是vdi类型的,只能用visualbox下载 链接:https://pan.baidu.com/s/1L7WX6ju4 ...

  3. 自己写的Weblogic的poc

    """ 暂时只试用于Linux,先试试用一下反弹shell CVE-2017-10271的EXp """ import requests i ...

  4. HTTP/1.1与HTTP/2有什么区别?

    介绍 超文本传输​​协议(HTTP)是一种应用协议,自1989年发明以来,它一直是事实上在万维网上进行通信的标准.从1997年发布HTTP / 1.1到最近,对它的修改很少.协议.但是在2015年,重 ...

  5. repr() Vs str()

    在python中,将对象转换为字符串有两个内建函数: str Vs repr .str 是一个友好的,人们可读的字符串.repr 应该包含关于对象内容的详细信息(有时他们会返回相同的内容,例如整数). ...

  6. 2019头条java面试总结 (包含面试题解析)

    2019滴滴java面试总结  (包含面试题) 本人8年开发经验.今年年初找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.滴滴等公司offer,岗位是Java后端开发. 面试了很多家公司,感觉大部分 ...

  7. MacOS访达增强工具-TotalFinder

    TotalFinder 是Mac上最好用的Finder增强工具,TotalFinder 提供了多标签式浏览.拷贝路径.剪切文件.显示隐藏文件.双栏窗口模式.彩色标签等功能 彩色的标签 将彩色带回El ...

  8. MFC连接Sqlserver

    下载 ado2.h和ado2.cpp文件 在VC++ 目录-->包含目录 -->添加  msado15.dll, msjro.dll 目录. // TODO: 连接sqlserver, 在 ...

  9. postman设置环境变量与全局变量

    1.环境变量可以设置多组 设置环境变量 编辑环境变量 2.全局变量只能设置一组 可以在Pre-request Script和Tests中设置全局变量 如:pm.globals.set("na ...

  10. Arduino学习笔记⑦ EEPROM断电保存数据

    1.前言     EEPROM,叫做电可擦可编程可读寄存器(是不是觉得好官方,不知道是什么鬼?反正我也一脸懵逼),只需要知道这是一种断电后数据不会丢失的存储设备,可以用来应对需要做记录做保存的场合.简 ...