codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)
题目链接:
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 + 剪枝)的更多相关文章
- Codeforces Round #338 (Div. 2) B. Longtail Hedgehog dp
B. Longtail Hedgehog 题目连接: http://www.codeforces.com/contest/615/problem/B Description This Christma ...
- 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 ...
- [Codeforces 163D]Large Refrigerator (DFS+剪枝)
[Codeforces 163D]Large Refrigerator (DFS+剪枝) 题面 已知一个长方体的体积为V,三边长a,b,c均为正整数,求长方体的最小表面积S V以质因数分解的形式给出 ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- [网页游戏开发]Morn组件赋值
在讲解List之前,我们先介绍一下Morn组件赋值功能 默认属性赋值 界面逻辑开发过程中,经常会涉及到动态更改UI属性,比如: 界面有一个按钮,一个多选框和一个文本,分别命名为myButton,myC ...
- WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeView
Dictionary中的<string, CustomeType>CustomeType是一个集合,将其绑定生成一棵树,树的第一层节点是Dictionary的Key,第二层是Custome ...
- JAVA运行时异常及常见的5中RuntimeExecption
最近在抽时间看面试题,很多面试题都提出了写出java常见的5个运行时异常.现在来总结一下, java运行时异常是可能在java虚拟机正常工作时抛出的异常. java提供了两种异常机制.一种是运行时异常 ...
- 常见mysql分布式数据中间件
一般分为三种: 1. proxy sharding,目前由cobar,mycat,drds,atlas修改,这几个产品的起源一般是mysqlproxy 或 ameoba,特点是mysql协议基本兼容, ...
- Spark简单集群搭建
1. 上传spark-2.2.0-bin-hadoop2.7.tgz安装包到/home/dtouding目录下 2. 解压安装包到/bigdata/目录下,tar –zxvf spark-2.2.0- ...
- springboot 项目 docker化部署
最近公司项目需要docker化,项目所使用的框架是springboot,linux环境.第一次接触docker化方面的技术.做的时候,所接触的新知识比较多,留下此文,以便以后用到的时候快速入手. 修改 ...
- tuple built-in function
tuple tips: 1.对于Python中的tuple类型来说,他与其它的序列类型来讲最大的不同就是tuple是不可变的. 2.当你需要创建一个只有一个元素的tuple时,需要在元祖分隔符里面加一 ...
- 【转载】Java中StringTokenizer类的作用
StringTokenizer是一个用来分隔String的应用类,相当于VB的split函数.1.构造函数public StringTokenizer(String str)public String ...
- 简易五子棋 V1.1.0
main.cpp #include "fivechess.cpp" int main() { fivechess a; a.RunGame(); getchar(); return ...
- CodeForces161D: Distance in Tree(树分治)
A tree is a connected graph that doesn't contain any cycles. The distance between two vertices of a ...