Codeforces--615B--Longtail Hedgehog(贪心模拟)
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:
- 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 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.
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.
Print the maximum possible value of the hedgehog's beauty.
8 6
4 5
3 5
2 5
1 2
2 8
6 7
9
4 6
1 2
1 3
1 4
2 3
2 4
3 4
12
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.
n个点,m条无向边,在连成的链中找一条递增的链,使得末尾节点的度数乘以深度最大,因为是无向边,又要求递增的链,所以尽量使小的数做起点,并且将所有的边按照节点大小进行排序,从最小的点开始遍历,记录每一个点最大的深度,然后找到最大的乘积
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int u,v;
}edge[200000+10];
bool cmp(node s1,node s2)
{
if(s1.u==s2.u)
return s1.v<s2.v;
return s1.u<s2.u;
}
int main()
{
int n,m;
cin>>n>>m;
__int64 dp[100100],dre[100100];
memset(dp,0,sizeof(dp));
memset(dre,0,sizeof(dre));
for(int i=0;i<m;i++)
{
cin>>edge[i].u>>edge[i].v;
if(edge[i].u>edge[i].v)
swap(edge[i].u,edge[i].v);//¾¡Á¿Ê¹Ð¡µÄµã×öÆðµã
dre[edge[i].u]++,dre[edge[i].v]++;
}
sort(edge,edge+m,cmp);
for(int i=0;i<m;i++)//¼Ç¼ÿһ¸öµãµÄ×î´óÉî¶È
dp[edge[i].v]=max(dp[edge[i].v],dp[edge[i].u]+1);
__int64 ans=0;
for(int i=1;i<=n;i++)
ans=max(ans,(dp[i]+1)*dre[i]);//ѰÕÒ×î´ó³Ë»ý
cout<<ans<<endl;
return 0;
}
Codeforces--615B--Longtail Hedgehog(贪心模拟)的更多相关文章
- CodeForces 615B Longtail Hedgehog
题目: http://codeforces.com/problemset/problem/615/B 题意:题目描述很复杂,但实际上很简单.大意就是连续的几个点组成尾巴,要求尾巴的长度乘以尾巴终点的分 ...
- CodeForces ---596B--Wilbur and Array(贪心模拟)
Wilbur and Array Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Su ...
- Codeforces 158 B. Taxi[贪心/模拟/一辆车最多可以坐4人同一个群的小朋友必须坐同一辆车问最少需要多少辆车]
http://codeforces.com/problemset/problem/158/B B. Taxi time limit per test 3 seconds memory limit pe ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog dp
B. Longtail Hedgehog 题目连接: http://www.codeforces.com/contest/615/problem/B Description This Christma ...
- codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)
题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog 记忆化搜索/树DP
B. Longtail Hedgehog This Christmas Santa gave Masha a magic picture and a pencil. The picture con ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- Longtail Hedgehog(DP)
Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- 15、Scala隐式转换和隐式参数
1.隐式转换 2.使用隐式转换加强现有类型 3.隐式转换函数的作用域与导入 4.隐式转换发生时机 5.隐式参数 1.隐式转换 要实现隐式转换,只要程序可见的范围内定义隐式转换函数即可.Scala会自动 ...
- Windows下环境变量显示、设置或删除操作详情
显示.设置或删除 cmd.exe 环境变量. SET [variable=[string]] variable 指定环境变量名. string 指定要指派给变量的一系列字符串. 要显示当前环境变量,键 ...
- 10java内存
java内存 1.栈---存储的是变量(不仅仅只有变量),不会对存储的内容进行赋值,存储的内容使用完成之后会立即进行清除 2.堆---存储的是对象.会对存储的内容进行赋值,存储内容使用完成之后会在某个 ...
- 前端领域的BEM到底是什么
前端领域的BEM到底是什么 BEM - Block Element Modfier(块元素编辑器) BEM方法确保每一个参加了同一网站开发项目的人,基于一套代码规范去开发,这样非常有利于团队成员理解彼 ...
- CF176E Archaeology(set用法提示)
题目大意: 给一棵树,每次激活或熄灭一个点,每次问这些点都联通起来所需的最小总边权 分析: 若根据dfs序给所有点排序,为$v1,v2,v3....vk$,那么答案就是$(dis(v1,v2)+dis ...
- Oracle ASM注意事项
ASM是负载均衡的存储策略,加新磁盘会将其它盘数据平均迁移到新磁盘,删除磁盘会将删除磁盘数据平均写回其它磁盘 1.同一磁盘组如果是在raid上,划分的磁盘越少越好,磁盘组分布在不同raid上性能好: ...
- [luogu2154 SDOI2009] 虔诚的墓主人(树状数组+组合数)
传送门 Solution 显然每个点的权值可以由当前点上下左右的树的数量用组合数\(O(1)\)求出,但这样枚举会T 那么我们考虑一段连续区间,对于一行中两个常青树中间的部分左右树的数量一定,我们可用 ...
- G - Power Strings
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...
- [bzoj2060][Usaco2010 Nov]Visiting Cows 拜访奶牛_树形dp
Visiting Cows 拜访奶牛 bzoj-2060 Usaco-2010 Nov 题目大意:题目链接. 注释:略. 想法:看起来像支配集. 只是看起来像而已. 状态:dp[pos][flag]表 ...
- js递归解决汉诺塔问题
汉诺塔是一个印度的古老传说.有三个圆柱,其中一个圆柱上放着若干圆盘,这些圆盘从上到下,直径递增,利用一个辅助圆柱,将原来柱子上的圆盘放到另一个柱子上,依旧是从上到下直径递增. 汉诺塔是一个经典的递归案 ...