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

来源:互联网 发布:淘宝货源只有阿里巴巴 编辑:程序博客网 时间:2024/05/17 01:00

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 );}
0 0
原创粉丝点击