Uva11324 The Largest Clique tarjan+dp

本文介绍了一种解决图论中最大团问题的方法。通过构造转置闭包图并使用Tarjan算法进行强连通分量分析,进而重建图并利用动态规划求解最大团的大小。适用于竞赛和实际应用。

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Download as PDF

Problem B: The Largest Clique

Given a directed graph G, consider the following transformation. First, create a new graph T(G) to have the same vertex set as G. Create 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 thetransitive 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.

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 uand v between 1 and n which define a directed edge from u to v in G.

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

Output for sample input

4

Zachary Friggstad


题意:求最大团

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
#include<algorithm>
#define N 1005
#define M 50005
using namespace std;
int n, m;
struct egde {
	int v, nxt;
} e[M<<1]; //要进行重构图,可以将两个图的边写在一个数组里边。
int pos, head[2][N]; //两个图的head要分开。
void add(int u, int v, int f) {
	e[pos].v = v;
	e[pos].nxt = head[f][u];
	head[f][u] = pos++;
}
int bcnt, belong[N], pre[N], cok;
stack<int>S;
int val[N]; //每个节点的权值;
int vis[N];
int dp[N]; //表示以x为节点开始的最大权值
int dfs(int u) {
	int lowu = pre[u] = ++cok;
	S.push(u);
	for (int i = head[0][u]; i != -1; i = e[i].nxt) {
		int v = e[i].v;
		if (!pre[v]) {
			int lowv = dfs(v);
			lowu = min(lowu, lowv); //如果该点的子节点能达到更高的节点
		} else if (!belong[v]) //只能通过没有确定编号的其他点
			lowu = min(lowu, pre[v]);
	}
	if (lowu == pre[u]) { //如果u只能连回自己,那么u就是第一个被发现的节点
		bcnt++;
		while(1) {
			int x=S.top();
			S.pop();
			belong[x] = bcnt;
			if(x==u)break;
		}
	}
	return lowu;
}
void tarjan(int n) {
	cok = bcnt = 0;
	memset(pre, 0, sizeof(pre));
	memset(belong, 0, sizeof(belong));
	for (int i = 1; i <= n; i++)
		if (!pre[i])
			dfs(i);
}
void rebuild(int n) {
	memset(head[1], -1, sizeof(head[1]));
	memset(val, 0, sizeof(val));
	for (int u = 1; u <= n; u++) {
		val[belong[u]]++;
		for (int j = head[0][u]; ~j; j = e[j].nxt) {
			int v = e[j].v;
			if (belong[v] != belong[u])
				add(belong[u], belong[v], 1); //没必要在此时判重:1.有向图有重边也没关系;2.待全部插入完毕后再判重效率会高很多;
		}
	}
	//去重O(N^2)
	for(int i=1;i<=bcnt;i++){
		memset(vis,0,sizeof(vis));
		int pre;//记录前面一个节点
		for(int j=head[1][i];~j;j=e[j].nxt){
			int v=e[j].v;
			if(vis[v])e[pre].nxt=e[j].nxt;
			else vis[v]=true;
			pre=j;
		}
	}

}

int DP(int u) {
	if (dp[u] != -1)
		return dp[u];
	int res = 0;
	for (int i = head[1][u]; ~i; i = e[i].nxt) {
		int v = e[i].v;
		res = max(res, DP(v));
	}
	return dp[u] = res + val[u];
}
int main() {
	int T;
	//freopen("D:\\a.in","r",stdin);
	scanf("%d", &T);
	while (T--) {
		scanf("%d%d", &n, &m);
		memset(head[0], -1, sizeof(head[0]));
		pos = 0;
		while (m--) {
			int u, v;
			scanf("%d%d", &u, &v);
			add(u, v, 0); //第一个图
		}
		tarjan(n);
		rebuild(n);
		memset(dp, -1, sizeof(dp));
		int ans = 0;
		for (int i = 1; i <= bcnt; i++) {
			ans = max(dp[i]==-1?DP(i):dp[i], ans);
		}
		printf("%d\n", ans);
	}
}




源码地址: https://pan.quark.cn/s/d1f41682e390 miyoubiAuto 米游社每日米游币自动化Python脚本(务必使用Python3) 8更新:更换cookie的获取地址 注意:禁止在B站、贴吧、或各大论坛大肆传播! 作者已退游,项目不维护了。 如果有能力的可以pr修复。 小引一波 推荐关注几个非常可爱有趣的女孩! 欢迎B站搜索: @嘉然今天吃什么 @向晚大魔王 @乃琳Queen @贝拉kira 第三方库 食用方法 下载源码 在Global.py中设置米游社Cookie 运行myb.py 本地第一次运行时会自动生产一个文件储存cookie,请勿删除 当前仅支持单个账号! 获取Cookie方法 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 按刷新页面,按下图复制 Cookie: How to get mys cookie 当触发时,可尝试按关闭,然后再次刷新页面,最后复制 Cookie。 也可以使用另一种方法: 复制代码 浏览器无痕模式打开 http://user.mihoyo.com/ ,登录账号 按,打开,找到并点击 控制台粘贴代码并运行,获得类似的输出信息 部分即为所需复制的 Cookie,点击确定复制 部署方法--腾讯云函数版(推荐! ) 下载项目源码和压缩包 进入项目文件夹打开命令行执行以下命令 xxxxxxx为通过上面方式或取得米游社cookie 一定要用双引号包裹!! 例如: png 复制返回内容(包括括号) 例如: QQ截图20210505031552.png 登录腾讯云函数官网 选择函数服务-新建-自定义创建 函数名称随意-地区随意-运行环境Python3....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值