题目链接:

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

题目描述:

  给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链的beauty值 = P*degree[u]。问你所有链中最大的beauty值。

解题思路:

  因为只需要找到以每个节点为终点的最长递增链即可,所以建图的时候可以建成从编号小的节点到编号大的节点的有向图,然后用DFS搜索,但是直接暴搜会TLE,然后就开始了我的TLE之路,TLE24, TLE42,TLE56,TLE60.....,准备弃疗时,学弟帮我看了一下,竟然改对了,而且跑得飞快(✪▽✪),就是定义一个标志数组,记录当前节点之前的节点是不是已经遍历完了,如果遍历完了,那么当前节点就是最优的了,就从当前节点向下遍历。新技能get

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
#define LL __int64 const LL INF = 1e9+;
const int maxn = ;
struct node
{
int to, next;
} edge[maxn*]; int head[maxn], tot, du[maxn], in[maxn];
LL ans[maxn]; void add (int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot ++;
}
/**
记录in[i]为i点的入度
当入度为零时,当前节点更新为最优,
然后从当前节点继续向下遍历
向下遍历时层数参数为当前层加一
而不是简单的上一层参数加一
*/
void dfs (int u, LL x)
{ for (int i=head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].to;
ans[v] = max (ans[v], x);
in[v]--; if(in[v] == )
dfs (v, ans[v]+);
}
} int main ()
{
int n, m; scanf ("%d %d", &n, &m);
memset (head, -, sizeof(head));
tot = ; for (int i=; i<m; i++)
{
int u, v;
scanf ("%d %d", &u, &v);
if(u > v)
swap (u, v);
du[u] ++;
du[v] ++;
in[v]++;
add (u, v);
} for (int i=; i<=n; i++)
{
if (ans[i] == )
{
ans[i] = ;
dfs (i, );
}
} LL sum = ;
for (int i=; i<=n; i++)
sum = max (sum, ans[i] * du[i]); printf ("%I64d\n", sum);
return ;
}

codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)的更多相关文章

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

  3. [Codeforces 163D]Large Refrigerator (DFS+剪枝)

    [Codeforces 163D]Large Refrigerator (DFS+剪枝) 题面 已知一个长方体的体积为V,三边长a,b,c均为正整数,求长方体的最小表面积S V以质因数分解的形式给出 ...

  4. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  6. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  7. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  8. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  9. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. 理解OpenStack认证:Keystone PKI

    原文链接: https://www.mirantis.com/blog/understanding-openstack-authentication-keystone-pki/ The latest ...

  2. 关于HTTP1.1的长连接

    HTTP是一个构建在传输层的TCP协议之上的应用层的协议,在这个层的协议,是一种网络交互须要遵守的一种协议规范. HTTP1.0的短连接 HTTP 1.0规定浏览器与server仅仅保持短暂的连接.浏 ...

  3. 图像处理之 opencv 学习---opencv 中的常用算法

    http://blog.csdn.net/lindazhou2005/article/details/1534234 文中有提到鲁棒性 http://blog.csdn.net/chary8088/a ...

  4. MVC 基于FormsAuthentication 方式的权限验证

    1.登录的代码 1 [HttpPost] 2 public ActionResult Index(User entity) 3 { 4 User user = GetUser(entity.Name, ...

  5. activity四种状态

    finish()  使得activity死掉 activity 部分可见进入pause状态.全部不可见进入stop状态 .界面从死亡——运行(启动) MainAdctivity.onCreate.on ...

  6. safair 的css hack

    在css里面使用[;attribute:value;] css参考如下: .header-share li{float: right; margin-left: 20px; [;width: 50px ...

  7. HDU 5651xiaoxin juju needs help

    xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  8. Centos6.8更好yum源

    第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back ...

  9. 一步一步学Silverlight 2系列(10):使用用户控件

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  10. [原创]java实现word转pdf

    最近遇到一个项目需要把word 转成pdf,百度了一下网上的方案有很多,比如虚拟打印.给word 装扩展插件等,这些方案都依赖于ms word 程序,在java代码中也得使用诸如jacob或jcom这 ...