『题解』UVa11324 The Largest Clique
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的更多相关文章
- 『题解』洛谷P1063 能量项链
原文地址 Problem Portal Portal1:Luogu Portal2:LibreOJ Portal3:Vijos Description 在\(Mars\)星球上,每个\(Mars\)人 ...
- UVA11324 The Largest Clique[强连通分量 缩点 DP]
UVA - 11324 The Largest Clique 题意:求一个节点数最大的节点集,使任意两个节点至少从一个可以到另一个 同一个SCC要选一定全选 求SCC 缩点建一个新图得到一个DAG,直 ...
- UVA11324 The Largest Clique —— 强连通分量 + 缩点 + DP
题目链接:https://vjudge.net/problem/UVA-11324 题解: 题意:给出一张有向图,求一个结点数最大的结点集,使得任意两个结点u.v,要么u能到达v, 要么v能到达u(u ...
- 『题解』Coderforces352A Jeff and Digits
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Jeff's got n cards, each card contains ...
- UVa11324 The Largest Clique(强连通分量+缩点+记忆化搜索)
题目给一张有向图G,要在其传递闭包T(G)上删除若干点,使得留下来的所有点具有单连通性,问最多能留下几个点. 其实这道题在T(G)上的连通性等同于在G上的连通性,所以考虑G就行了. 那么问题就简单了, ...
- uva11324 The Largest Clique --- 强连通+dp
给一个有向图G,求一个子图要求当中随意两点至少有一边可达. 问这个子图中最多含多少个顶点. 首先找SCC缩点建图.每一个点的权值就是该点包括点的个数. 要求当中随意两点可达,实际上全部边仅仅能同方向, ...
- UVA11324 The Largest Clique(DP+缩点)
题意:给一张有向图G,求一个结点数最大的结点集,使得该结点中任意两个结点 u 和 v满足:要么 u 可以到达 v, 要么 v 可以到达 u(u 和 v 相互可达也可以). 分析:”同一个强连通分量中的 ...
- UVA11324 The Largest Clique (强连通缩点+DP最长路)
<题目链接> 题目大意: 给你一张有向图 G,求一个结点数最大的结点集,使得该结点集中的任意两个结点 u 和 v 满足:要么 u 可以达 v,要么 v 可以达 u(u,v相互可达也行). ...
- UVA11324 The Largest Clique
嘟嘟嘟 很自然的想到先tarjan把强联通分量缩点,因为对于一个强联通分量,要么不选,要么全选,所以可看成一个点. 然后转化成了求DAG上的一条最长路(每一个点都有权值).刚开始我想用dijkstra ...
随机推荐
- 从零开始入门 K8s | 可观测性:监控与日志
作者 | 莫源 阿里巴巴技术专家 一.背景 监控和日志是大型分布式系统的重要基础设施,监控可以帮助开发者查看系统的运行状态,而日志可以协助问题的排查和诊断. 在 Kubernetes 中,监控和日志 ...
- Redis系列总结--这几点你会了吗?
文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 前面几篇已经对Redis中几个关键知识点做了介绍,本篇主要对Redis系列做一下总结以及对Redis中常见面试 ...
- HTML5 video视频字幕的使用和制作
一.video支持视频格式: 以下是三种最常用的格式 1. ogg格式:带有Theora视频编码(免费)+Vorbis音频编码的Ogg文件(免费) 支持的浏览器:firefox.chrome.oper ...
- python编程基础之三十六
文件处理:文件处理包括读文件,写文件 读文件: 1.打开文件 2.读取文件 3.关闭文件 写文件: 1.打开文件 2.写如文件 3.关闭文件 无论是读取文件还是写文件都时需要打开文件,和关闭文件 打开 ...
- Kubernetes集群的部署方式及详细步骤
一.部署环境架构以及方式 第一种部署方式 1.针对于master节点 将API Server.etcd.controller-manager.scheduler各组件进行yum install.编译安 ...
- 【干货系列之萌新知识点】python与变量和运算符
一.注释 注释一行:# 为注释符 注释多行:'或者"为注释符 二.print输出 print()函数,作用是打印一些信息语屏幕上. 例如:print("hello world!&q ...
- Cocos2d-x 学习笔记(7) 内存管理 Sprite SpriteFrame Texture2D
1. 总结 Sprite和SpriteFrame和Texture2D关系紧密,三个类都继承了Ref类.精灵有成员精灵帧和纹理,精灵帧有成员纹理.精灵帧和纹理被精灵帧引用时,引用计数增加,不再被引用时, ...
- 《Java并发编程实战》读书笔记-第一部分 基础知识小结
并发技巧清单 可变状态是至关重要的 所有的并发问题都可以归结为如何协调对并发状态的访问.可变状态越少,就越容易确保线程安全性. 尽量将域声明为final类型,除非需要它们是可变的. 不可变对象一定是线 ...
- 《Java并发编程实战》读书笔记-第1章 简介
并发简史 在早期的计算机中不包含操作系统,从头至尾都只执行一个程序,并且这个程序能访问计算机所有资源.操作系统的出现使得计算机每次能运行多个程序,并且不同的程序都在单独的进程中运行:操作系统为各个独立 ...
- 第三方软件 Serv-u提权
Serv-U FTP Server,是一种被广泛运用的FTP服务器端软件,支持3x/9x/ME/NT/2K等全Windows系列.可以设定多个FTP服务器.限定登录用户的权限.登录主目录及空间大小等 ...