题目:

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. UML 简单介绍

    Unified modeling Language - 统一建模语言

  2. HDU1698Just a Hook(线段树 + 区间修改 + 求和)

    题目链接 分析:1-N区间内初始都是1,然后q个询问,每个询问修改区间[a,b]的值为2或3或者1,统计最后整个区间的和 本来想刷刷手速,结果还是写了一个小时,第一个超时,因为输出的时候去每个区间查找 ...

  3. redis auth php操作

    <?php//Connecting to Redis server on localhost$redis = new Redis();$redis->connect('192.168.33 ...

  4. Tomcat server.xml配置

    参考文献:http://www.cnblogs.com/xdp-gacl/p/3734395.html 一.Tomcat配置虚拟主机 方法:在conf/server.xml里配置Host元素,其中na ...

  5. Favorite Games

    Samurai II: Vengeance: http://www.madfingergames.com/games

  6. NGUI架构和Draw Call合并原理

    http://bbs.9ria.com/thread-282804-1-1.html http://www.unitymanual.com/blog-97-238.html

  7. Linux下非root用户如何安装软件

    Linux下非root用户如何安装软件 从windows转移到Linux的用户最开始总会有各种不适,因为这种不适最终放弃linux的不在少数.对于这类人只能说可惜,还没有领略到linux的美好就过早放 ...

  8. 学海无涯的整理Ing..........

    1.文章:Understanding JavaScript Function Invocation and “this” http://yehudakatz.com/2011/08/11/unders ...

  9. memcached命令行操作详解,命令选项的详细解释

    连接到memcached命令行下:  telnet 127.0.0.1 11211 1.set / add / replace : 格式:<command> <key> < ...

  10. Spring入门_04_注解注入

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...