Uva11324 The Largest Clique tarjan+dp

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值