题目链接:

  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. 李洪强iOS开发之 - block的使用

     李洪强iOS开发之 - block的使用   01 - 定义block //支付block typedef void(^invokePayBlock)(CFBaseOrderModel *model ...

  2. linux i2c 标准接口(二)

    驱动程序操作法:i2c设备的驱动也可以通过普通的设备驱动实现,像往常的驱动一样实现,然后在应用层就可以像读取普通文件一样操作,无需再考虑读写时序.其实普通的设备驱动也可以用两种方法实现, 1)构建字符 ...

  3. 将MySQL服务绑定到固定的IP地址上

    近期将在线服务迁移到了阿里云.     阿里云提供了云盾,安全上确实比其他的云服务有了很大的提高,遗憾的是,没有防火墙,还是需要自己设置.阿里云的虚拟机跑在XEN上,直接使用YUM安装iptables ...

  4. ABAP debug遇到问题

    新项目的系统,调试是老出现这个框不断弹出,一堆出来 都来不及关. 不确定是不是因为可用对话框不够的原因.

  5. spring cloud 启动报错-must be declared as an @AliasFor [serviceId], not [name].

    项目加入FeignClient后再启动就报错,具体报错信息如下: org.springframework.core.annotation.AnnotationConfigurationExceptio ...

  6. POJ 3279 Dungeon Master

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21242   Accepted: 8265 D ...

  7. hdu2063 二分图(基础题)

    这个题目适合刚刚接触二分图的同学做哦: 给一个题目链接 点击打开链接. 题目大意,有K个男女匹配方式, 输入数据的第一行是三个整数K , M , N,分别表示可能的组合数目,女生的人数,男生的人数.0 ...

  8. codeforces 690D1 D1. The Wall (easy)(dfs)

    题目链接: D1. The Wall (easy) time limit per test 0.5 seconds memory limit per test 256 megabytes input ...

  9. VC++读写文件

    目录 第1章读写文件    1 1.1 API    1 1.2 低级IO    1 1.2.1 文件序号    1 1.2.2 文本文件与二进制文件    1 1.3 流IO    2 1.4 Un ...

  10. 关于布局(Layout)的一切

    之前在布局中有很多问题也有很多经验,遗憾都没记下来.现在一点点记下一些东西. 1.外层用LinearLayout的话,常常把orientation设成vertical, android:orienta ...