题目链接:

  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. ViewGroup如何分发事件

    dispatchTouchEvent事件派发显示隧道方式.再是冒泡方式隧道方式传递,直道某一个元素消耗此事件,由上至下逐层分发视图.冒泡方式传递,当某个视图消耗事件后其return boolean 是 ...

  2. C语言socket send()数据缓存问题

    send()函数默认情况下会使用Nagle算法.Nagle算法通过将未确认的数据存入缓冲区直到积攒到一定数量一起发送的方法.来降低主机发送零碎小数据包的数目.所以假设send()函数发送数据过快的话, ...

  3. sanic官方文档解析之Custom Protocols(自定义协议)和Socket(网络套接字)

    1,Custom Protocol:自定义协议 温馨提示:自定义协议是一个高级用法,大多数的读者不需要用到此功能 通过特殊的自定义协议,你可以改变sanic的协议,自定义协议需要继承子类asyncio ...

  4. 把x指针指向的4个字节次序颠倒过来

    举例:x指向的内存地址,其字节内容从低到高依次分别为c1,c2,c3,c4(Delphi读取一个integer的时候,结果是c4c3c2c1,其排列规则是"高高低低"),那么结果是 ...

  5. C++11 std::function、std::bind和lambda表达式

    参考博客: C++可调用对象详解-https://www.cnblogs.com/Philip-Tell-Truth/p/5814213.html 一.关于std::function与std::bin ...

  6. React中Transition的作用

    /** * `Transaction` creates a black box that is able to wrap any method such that * certain invarian ...

  7. php与html 表单的结合

    PHP $_POST <!DOCTYPE html> <html> <body> <form method="post" action=& ...

  8. POJ2253 Frogger —— 最短路变形

    题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  9. POJ1077 Eight —— 正向BFS

    主页面:http://www.cnblogs.com/DOLFAMINGO/p/7538588.html 代码一:以数组充当队列,利用结构体中的pre追溯上一个状态在数组(队列)中的下标: #incl ...

  10. sql server filter table name

    https://stackoverflow.com/questions/26577464/how-to-find-a-table-in-sql-server-if-only-the-partial-t ...