Codeforces Round #338 (Div. 2) B. Longtail Hedgehog

小明要画一只刺猬,需要找到最美丽的尾巴和最多的刺。题目描述了如何计算尾巴长度和刺的数量,并给出输入输出格式及样例测试。解题思路是枚举每个点作为尾巴终点,求最大美观值。

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

B. Longtail Hedgehog
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n 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:

Only segments already presented on the picture can be painted;
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;
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 the endpoint 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 and m(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 integers ui and vi (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.

Sample test(s)
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 numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3·3 = 9.

题目链接:http://codeforces.com/problemset/problem/615/B
题目大意:
寒假里,做功课之余,小明也非常喜欢画画。某一天,爸爸给他了一幅画和一只铅笔。这幅画包含了n个点和m条线(这些边可能会相交,但是我们不用去管这些)。每条线的两端是两个不同的点,没有任何两条线的两个端点是完全一样的。现在,小明想要给一些线条涂颜色,目的是画出一只刺猬来。在小明的印象中,刺猬应该有一个尾巴和几根刺。他觉得画尾巴的时候应该满足如下规律:
1、只有已经存在的线条才可以被涂上颜色。
2、尾巴应该是连续的,包含了一系列的点,每两个点之间的线条被涂上了颜色。
3、尾巴上的顶点的序号应该要严格递增。

小明觉得尾巴的长度就是尾巴上点的个数。同时,他也要去画刺猬的刺,因此他把一些线条也涂上了颜色,刺的一个端点必须在尾巴的末端点上。小明觉得刺猬的美观值应该是尾巴的长度乘以刺的数量。小明希望画出一只美观值最大的刺猬。虽然大家觉得小明的想法很幼稚,但是他毕竟还是一个小盆友。。。

Input
输入第一行包含两个正整数n和m,分别表示点和线的数量。(2<=n<=100000,1<=m<=200000)。
接下来m行,每行两个正整数ui和vi,表示第i条线两端的点的序号,1<= ui,vi<= n。数据保证没有两条线的两个端点完全一样。

Output

输出最大的美观值

解题思路:把每个点当作尾巴的末端点,所以连接这个点的点个数就是刺的个数,比这个点小的数的个数加上它本身就是尾巴长度。枚举每个点作为尾巴的末端点,记录得到的最大美观值,注意尾巴长度初始化为1,用long long。

代码如下:

#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#define ll long long 
using namespace std;
const int maxn=100000+10;
vector <int >vt[maxn];
ll dp[maxn];
ll num[maxn];
int main(void)
{
    int n,m,u,v;
    memset(num,0,sizeof(num));
    for(int i=0;i<maxn;i++)
        dp[i]=1;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&u,&v);
        vt[v].push_back(u);
        vt[u].push_back(v);
    }
    for(int i=1;i<=n;i++)
    {
        num[i]=(ll)vt[i].size();
    }
    ll ans=0;
    dp[1]=1;
    for(int i=1;i<=n;i++)
    {
        int len=vt[i].size();
        for(int j=0;j<len;j++)
        {
            int cur=vt[i][j];
            if(cur>i)
                dp[cur]=max(dp[cur],dp[i]+1);
        }
    }
    for(int i=1;i<=n;i++)
    {
        ll cur=dp[i]*num[i];
        ans=max(cur,ans);
    }
    printf("%lld\n",ans );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值