【打CF,学算法——三星级】CodeForces 615B Longtail Hedgehog (DFS/拓扑排序)

本文通过一道CodeForces题目,详细介绍了如何寻找一张图中最大美丽度的刺猬(尾巴长度乘以尾巴末端点的度数)。解题思路包括统计点的度数,建立单向图,并利用DFS从最小序号点开始搜索,优化递归过程,避免无效更新。最后,计算每个点的长度和度数,以找到最大值。

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

【CF简介】

提交链接:CF 615B


题面:

B. Longtail Hedgehog
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This Christmas Santa gave Masha a magic picture and a pencil. The picture consists ofn points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions:

  1. Only segments already presented on the picture can be painted;
  2. The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment;
  3. The numbers of points from the beginning of the tail to the end should strictly increase.

Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is theendpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.

Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.

Input

First line of the input contains two integers n andm(2 ≤ n ≤ 100 000,1 ≤ m ≤ 200 000) — the number of points and the number segments on the picture respectively.

Then follow m lines, each containing two integersui andvi (1 ≤ ui, vi ≤ n,ui ≠ vi) — the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.

Output

Print the maximum possible value of the hedgehog's beauty.

Examples
Input
8 6
4 5
3 5
2 5
1 2
2 8
6 7
Output
9
Input
4 6
1 2
1 3
1 4
2 3
2 4
3 4
Output
12
Note

The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4,5). Therefore, the beauty of the hedgehog is equal to3·3 = 9.



题意:

     给定一张图,要求寻找序号递增的链,使得某链的长度乘以该链末端点的度数的值最大,求该值。


解题:

    先统计每个点的度数,入度。建图时,只加从序号小的点到序号大的点的单向边。随后,从序号小且未被访问的点出发,进行dfs搜索。每次搜索到一个新的节点,将该点入度减一,同时如果新的值大于原有值,则更新,但并不马上往下递归,若入度为0,方可往下递归(优化,避免无效更新)。最后,统计每个点的长度和度数,求最大值即可。


代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#define LL long long
using namespace std;
struct edge
{
	int fm,to;
}store[100010<<1];
int head[100010],nxt[200010];
int degree[100010],cnt=0,in[100010];
int len[100010];
bool vis[100010];
void addedge(int fm,int to)
{
	nxt[cnt]=head[fm];
	head[fm]=cnt;
	store[cnt].fm=fm;
	store[cnt].to=to;
	cnt++;
}
void dfs(int x)
{
	vis[x]=1;
	int tmp=len[x]+1;
	for(int i=head[x];~i;i=nxt[i])
	{
		len[store[i].to]=max(len[store[i].to],tmp);
		in[store[i].to]--;
		if(in[store[i].to]==0)
           dfs(store[i].to);
	}
}
int main()
{
	int n,m,a,b;
	LL ans=0,tmp;
	scanf("%d%d",&n,&m);
	memset(head,-1,sizeof(head));
	memset(degree,0,sizeof(degree));
	memset(in,0,sizeof(in));
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=m;i++)
    {
       scanf("%d%d",&a,&b);
	   degree[a]++;
	   degree[b]++;
	   if(a<b)
	   {
		   addedge(a,b);
		   in[b]++;
	   }
	   else
	   {
		   addedge(b,a);
		   in[a]++;
	   }
	}

	for(int i=1;i<=n;i++)
	{
		if(!vis[i])
		{
			len[i]=1;
			dfs(i);
		}
	}
	for(int i=1;i<=n;i++)
	{
		tmp=1LL*len[i]*degree[i];
		if(tmp>ans)
			ans=tmp;
	}
	printf("%lld\n",ans);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值