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) ...
随机推荐
- String,StringBuilder与StringBuffer的区别
相信大家看到过很多比较String和StringBuffer区别的文章,也明白这两者的区别,然而自从Java 5.0发布以后,我们的比较列表上将多出一个对象了,这就是StringBuilder类.St ...
- gRPC错误码 http状态码 provide your APIs in both gRPC and RESTful style at the same time
How gRPC error codes map to HTTP status codes in the response https://github.com/grpc-ecosystem/grpc ...
- [转]GPS NEMA 0183协议
一. NMEA0183标准语句(GPS常用语句) $GPGGA例:$GPGGA,092204.999,4250.5589,S,14718.5084,E,1,04,24.4,19.7,M,,,,0000 ...
- redis的图形界面管理工具
大部分人都知道redis是一款用在缓存服务器上的软件,它与memcache类似,都可以存储海量的数据,用在大访问量的web网站.聊天记录存放等方面,但是又与memcache不同: 1.缓存数据可以持久 ...
- 51Nod 1282 时钟 —— 最小表示法 + 字符串哈希
题目链接:https://vjudge.net/problem/51Nod-1282 1282 时钟 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难 ...
- 第三届蓝桥杯C++B组省赛
1.微生物增值 2.古堡算式 3.海盗比酒量 4.奇怪的比赛 5.方阵旋转 6.大数乘法 7.放棋子 8.密码发生器 9.夺冠概率 10.取球博弈
- cassandra cpp driver中bind list——用cass_statement_bind_collection函数
CassError insert_into_collections(CassSession* session, const char* key, const char* items[]) { Cass ...
- springboot web项目搭建
1.选择spring initializr 2.填写应用名称及设置相关配置,建议使用默认配置即可 3.选择相关技术,我们现在web技术 4.填写项目名称 5.项目文件结构如下 6.直接运行 java ...
- Identifier expected after this token
Cursor cursor = db.query(true, "user", new String[]{"id","mode"}, &quo ...
- [CQOI 2015] 任务查询系统
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3932 [算法] 首先 , 我们可以将(Si , Ei , Pi)转化为在Si处加入P ...