题目:

http://codeforces.com/problemset/problem/615/B

题意:题目描述很复杂,但实际上很简单。大意就是连续的几个点组成尾巴,要求尾巴的长度乘以尾巴终点的分支数的最大值,其中尾巴要满足的条件是有边相连,且尾巴上节点的编号一定是递增的,终点是最大值。

我刚开始的想法是,先用邻接表保存这个无向图(矩阵保存不了),在读取边的时候,用一个数组保存每个节点的分支数,然后用深搜,遍历尾巴的最大长度,同时每扩展一个点,用当前长度乘以该点的分支数,并不断更新这个最大值,最后输出。然后各种超时,自己尝试加了些剪枝还是过不了。

与舍友讨论过后,又有了新的优化思路:

  1. 首先是不用保存无向图,因为尾巴上的节点要求递增,所以只需要保存起点比终点小的有向图即可。
  2. 开一个数组,记录以当前节点为终点的尾巴的最大长度。
  3. 放弃搜索的方式,直接用结构体保存每一条边,然后对边进行排序,再从前往后扫描,对每条边终点的尾巴长度进行更新

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的更多相关文章

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

    B. Longtail Hedgehog 题目连接: http://www.codeforces.com/contest/615/problem/B Description This Christma ...

  2. codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)

    题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...

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

  4. Longtail Hedgehog(DP)

    Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  5. codeforces 338(Div 2) B. Longtail Hedgehog 解题报告

    题目链接:http://codeforces.com/problemset/problem/615/B 题目意思:要画一只 hedgehog,由 tail 和 spines 组成.我们要求得 beau ...

  6. Codeforces Round #338 (Div. 2)

    水 A- Bulbs #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1 ...

  7. Codeforces Round #338 (Div. 2) B dp

    B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces--615B--Longtail Hedgehog(贪心模拟)

     B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input stan ...

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

随机推荐

  1. linux系统性能监视命令

    preface as a linux engineer,you should know how to use these command of monitor system,so let's lear ...

  2. ecshop 给商品随机添加评论

    <?php /* * 随机插入商品评论 * * * */ define('IN_ECS', true); require(dirname(__FILE__) . '/includes/init. ...

  3. js JS 浮点计算BUG

    Number.prototype.toRound = function(d) { var s=this+"";if(!d)d=0; if(s.indexOf(".&quo ...

  4. C#读写文本文件

    static public string Read(string path) { StreamReader sr = new StreamReader(path,Encoding.Default); ...

  5. python设计模式之--单例模式

    python的单例模式就是一个类的实例只能自始自终自能创建一次.应用场景比如说数据库的连接池. #!/usr/bin/env python # coding=utf- class Foo(object ...

  6. $(document).ready、body.Onload()和 $(window).load的区别

    window.load(function(){...})和body.onload()都存在同样一个问题,那都是在页面所有元素(包括html标签以及引用到得所有图片,Flash等媒体)加载完毕后执行的, ...

  7. 自然语言18.2_NLTK命名实体识别

    QQ:231469242 欢迎nltk爱好者交流 http://blog.csdn.net/u010718606/article/details/50148261 NLTK中对于很多自然语言处理应用有 ...

  8. 自然语言0_nltk中文使用和学习资料汇总

    http://blog.csdn.net/huyoo/article/details/12188573 官方数据 http://www.nltk.org/book/ Natural Language ...

  9. python中的not具体使用及意思

    python中的not具体使用及意思 name='' while not name: name=raw_input(u'请输入姓名:') print name python中的not具体表示是什么: ...

  10. Winsock 入门 判读主机字节序 示例

    #include <stdio.h> union endian_u { /*最大成员的长度就是联合成员的长度.联合可以在定义时直接进行初始化,但这个初始化必须是联合第一个成员的类型,所以把 ...