UVA11324 The Largest Clique 强连通分量

此题要求出一个最大团,那么首先求出scc,然后缩点,重构成一个DAG图,每一个节点的点权为每一个scc的节点数量,最后求一条点权最大的路径即可(dp记忆化搜索)。

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 the transitive 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 u and v between 1 and n which define a directed edge from u tov 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<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>

using namespace std;

#define MAXN 2200

struct node
{
	int to,next;
}edge[999999];

int head[MAXN],en;
int low[MAXN],dfn[MAXN],stack[MAXN],top,set[MAXN],col,num;
bool vis[MAXN],instack[MAXN];
int n,m;

void add(int a,int b)
{
	edge[en].to=b;
	edge[en].next=head[a];
	head[a]=en++;
}

void tarjan(int u)
{
	vis[u]=1;
	dfn[u]=low[u]=++num;
	instack[u]=true;
	stack[++top]=u;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].to;
		if(!vis[v])
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else
			if(instack[v])
				low[u]=min(dfn[v],low[u]);
	}
	if (dfn[u]==low[u])
	{
		int j;
		col++;
		do
		{
			j=stack[top--];
			instack[j]=false;
			set[j]=col;
		}
		while (j!=u);
	}
}
void solve()
{
	int i;
	top=col=num=0;
	memset(instack,0,sizeof(instack));
	memset(vis,0,sizeof(vis));
	memset(set,-1,sizeof(set));
	for (i=1;i<=n;i++)
		if (!vis[i])
			tarjan(i);
}

vector<int> g[MAXN];
int dp[MAXN],cnt[MAXN];

int dfs_dag(int u)
{
    if(dp[u]>0) return dp[u];
    dp[u]=cnt[u];
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        dp[u]=max(dp[u],dfs_dag(v)+cnt[u]);
    }
    return dp[u];
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int u,v;
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof(head));en=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        solve();
        for(int i=0;i<=col;i++) g[i].clear();
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=n;i++)
        {
            u=set[i];
            cnt[u]++;
            for(int j=head[i];j!=-1;j=edge[j].next)
            {
                v=set[edge[j].to];
                if(u==v) continue;
                g[u].push_back(v);
            }
        }
        int ans=0;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=col;i++)
            ans=max(ans,dfs_dag(i));
        printf("%d\n",ans);
        //system("pause");
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值