题目:

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. JPA mysql wildfly jboss 存储时乱码

    首先确保mysql的库,表创建时指定的字符集collation. 可以直接用命令行插入中文,看查询出来是不是中文. insert into live_main_sync (cn_name, creat ...

  2. Character literal must contain exactly one character -- 一天一点小知识

    编程语言高度抽象化以后,错误也越来越让人难以理解了, NET编程最常见的一个错误, Object not set to the reference ,过了好久,才明白过来, 就是不明白为啥微软不说   ...

  3. runtime使用小例子 - 给对象O-C属性赋值

    这些日子在家里学习runtime,看runtime的一些方法和前辈们的博客,我也尝试着写几个runtime有效的运用 一.给对象属性赋值,例如一个WebEntity类 她有三个属性:NSString. ...

  4. asp.net与Matlab类型转换(待补全)

    上上篇的博客已经提到如何配置环境,即如何在asp.net中调用matlab生成的dll文件.这篇博客打算做个笔记,那就是matlab和C#数据类型如何转换.随着需求的增加,我会不断增加新的类型转换. ...

  5. easyUI datagrid editor扩展dialog

    easyUI datagrid简单使用:着重两点1.editor对象的click事件:2.将dialog窗体内的值填写到当前正编辑的单元格内 <!DOCTYPE html> <htm ...

  6. Python capitalize()方法

    Python capitalize()方法 capitalize()方法返回字符串的一个副本,只有它的第一个字母大写.对于8位的字符串,这个方法与语言环境相关. 语法 以下是capitalize()方 ...

  7. C++常用的#include头文件总结

    C++常用的#include头文件总结 这篇文章主要介绍了C++常用的#include头文件,对初学者理解C++程序设计大有好处的相关资料   本文详细罗列了C++所包含的头文件的名称及作用说明,比较 ...

  8. VS代码段扩展Snippet Designer is a Visual Studio plug in which allows you to create and search for snippets inside the IDE

    Snippet Designer is a Visual Studio plug in which allows you to create and search for snippets insid ...

  9. centos 7.0 nginx 1.7.9 安装过程

    系统用的是centos 7.0最小化安装 我现在安装完了 写一下步骤 还没完全搞懂 首先安装GCC [root@localhost ~]# yum install -y gcc gcc-c++ 已加载 ...

  10. Ansible简介及常用模块

    一.基础介绍 1.简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置. ...