Codeforces Round #338 (Div. 2)B. Longtail Hedgehog解题报告

来源:互联网 发布:淘宝刷到单被骗咋追回 编辑:程序博客网 时间:2024/05/16 11:50

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 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:
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 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.

这里写图片描述

题目大意就不说了;
做完本题,此题虽然很水,但是比赛是却没有思路,有一些感想,拿到一道题,首先迅速抽象出模型,然后思考用什么数据结构,接着是算法,最后是实现的细节(特殊情况,溢出等):
1、 快速抽象模型。此题模型是:找上升连道路长度乘以该道路终点度数的最大值。‘
2、 图用邻接表,vector G[MAXN]。还需要用个数组T[MAXN] ,T[i]表示以结点i为终点的最大上升道路长度,简单的dp就可以求。
3、 算法。无非就是遍历每条上升道路,用道路长度T[i]乘以道路终点的度数G[i].size(),取T[i]* G[i].size()最大值。
4、 细节。Ans要用long long ,中间计算也要long long,例如 (long long)T[i]*G[i].size();。否则都会溢出。‘
由于没有抽象出模型,没有考虑算法,一开始我去寻找道路长度的最大值maxT等等,其实有了算法分析那一步的话,从T[i]* G[i].size()完全可以看出没有任何必要求max T,因为结果有T[i]和G[i].size()两个变量决定,其中一个取极值,并不能得到结果的极值。这个甚至是常识,但是没有按步骤来,造成犯这种低级错误。
所以,通过这道题发现,我的思维并没有很强大,思考问题处于无序状态,还是需要一些基本的步骤来分析解决问题。

#include <iostream>#include<cmath>#include<cstring>#include<cstdio>#include<vector>using namespace std;vector<int> G[100005];int T[100005];int main(){    freopen("input.txt","r",stdin);    freopen("output.txt","w",stdout);    int n,m,x,y,Min=2000000;    cin>>n>>m;    for(int i=0;i<m;i++){        scanf("%d %d",&x,&y);        G[x].push_back(y);        G[y].push_back(x);    }    fill(T,T+n,1);    for(int i=1;i<n;i++){            for(int j=0;j<G[i].size();j++){                if(i<G[i][j]){                    if(T[G[i][j]]<T[i]+1)                        T[G[i][j]]=T[i]+1;                }            }    }   long long ans=0;    for(int i=1;i<=n;i++){            if((long long)T[i]*G[i].size()>ans)                ans=(long long)T[i]*G[i].size();    }    cout<<ans<<endl;    return 0;}
0 0
原创粉丝点击