CodeForces 615B Longtail Hedgehog
题目:
http://codeforces.com/problemset/problem/615/B
题意:题目描述很复杂,但实际上很简单。大意就是连续的几个点组成尾巴,要求尾巴的长度乘以尾巴终点的分支数的最大值,其中尾巴要满足的条件是有边相连,且尾巴上节点的编号一定是递增的,终点是最大值。
我刚开始的想法是,先用邻接表保存这个无向图(矩阵保存不了),在读取边的时候,用一个数组保存每个节点的分支数,然后用深搜,遍历尾巴的最大长度,同时每扩展一个点,用当前长度乘以该点的分支数,并不断更新这个最大值,最后输出。然后各种超时,自己尝试加了些剪枝还是过不了。
与舍友讨论过后,又有了新的优化思路:
- 首先是不用保存无向图,因为尾巴上的节点要求递增,所以只需要保存起点比终点小的有向图即可。
- 开一个数组,记录以当前节点为终点的尾巴的最大长度。
- 放弃搜索的方式,直接用结构体保存每一条边,然后对边进行排序,再从前往后扫描,对每条边终点的尾巴长度进行更新
PS:记得用long long
#include<stdio.h>
#include<algorithm>
#define maxn 111111
#define maxm 222222
using namespace std;
struct node{
int u;
int v;
};
node e[maxm];
long long spine[maxm],tail[maxm];
bool cmp(node a,node b){
if(a.u == b.u)
return a.v < b.v;
else
return a.u < b.u;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
int u,v;
for(int i = ;i<=m;i++){
scanf("%d%d",&u,&v);
e[i].u = min(u,v);
e[i].v = max(u,v);
spine[u]++;
spine[v]++;
}
sort(e+,e++m,cmp);
for(int i = ;i<=m;i++){
//这里要取max,因为有可能一个节点同时是两个尾巴的终点
tail[e[i].v] = max(tail[e[i].u]+,tail[e[i].v]);
}
long long ans = ;
for(int i = ;i<=n;i++)
ans = max(ans,(tail[i]+)*spine[i]);//要加1,因为初始tail数组都是0,没有算上起始点
printf("%I64d\n",ans);
}
CodeForces 615B Longtail Hedgehog的更多相关文章
- 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 ...
- Longtail Hedgehog(DP)
Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 338(Div 2) B. Longtail Hedgehog 解题报告
题目链接:http://codeforces.com/problemset/problem/615/B 题目意思:要画一只 hedgehog,由 tail 和 spines 组成.我们要求得 beau ...
- Codeforces Round #338 (Div. 2)
水 A- Bulbs #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1 ...
- Codeforces Round #338 (Div. 2) B dp
B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standar ...
- Codeforces--615B--Longtail Hedgehog(贪心模拟)
B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 714A Meeting of Old Friends
A. Meeting of Old Friends time limit per test:1 second memory limit per test:256 megabytes input:sta ...
随机推荐
- 王高利:Linux__apache,安装,报错解决
今日编译apache时出错:#./configure --prefix--检查编辑环境时出现:checking for APR... noconfigure: error: APR not found ...
- HTML5系列四(WebWorker、地理定位)
WebWorker简单应用 先从一个简单例子说起,计算数值加法 <script> var worker = new Worker('sumCalculate.js'); worker.on ...
- OpenGLES入门笔记一
学习地址:http://www.raywenderlich.com/3664/opengl-tutorial-for-ios-opengl-es-2-0 中文翻译:http://www.cnblogs ...
- SSH和SSM项目的打通各个页面的方式
SSH项目: 这里采用的action的形式: 即在表现层为页面在action中配置一个返回值,然后在Struts.xml的配置文件中进行配置. SSM项目中,SpringMVC中利用注解来配置每个页面 ...
- BigDecimal数据加法返回值接收
1.相加 两个BigDecimal变量a,b. 如果想进行相加,即a加b的话,返回值需要使用a进行接收,如下: a = a.add(b); BigDecimal为不可变类, 所以执行运算的结果需要再返 ...
- python学习笔记-(六)深copy&浅copy
在python中,对象赋值实际上是对象的引用.当创建一个对象,然后把它赋给另一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用. 1. 赋值 赋值其实只是传递对象引用,引用对象 ...
- Spring MVC学习笔记——Controller接口
- 内嵌DB
SQLLite H2 MySQL Embeded 等 比较项目 SQLite H2 database engine MySQL Embedded Footprint 350KiB ~1MB <2 ...
- ecshop修改注册、增加手机
1.去掉“用户名”注册 a.去掉提交 user_passport.dwt页面去掉 <input name="username" type="text" s ...
- jquermobile 安装
代码 <script src="../Public/js/jquery-1.11.1.min.js"></script> <script src=&q ...