codeforces 338(Div 2) B. Longtail Hedgehog 解题报告
题目链接:http://codeforces.com/problemset/problem/615/B
题目意思:要画一只 hedgehog,由 tail 和 spines 组成。我们要求得 beauty 最大值: tail * spines。
以下摘自 udon 原话,大家细细品味:(不一定是正确无误的哦,可能有误导他人成分。。。)
1、对于所有点 x,求出 x 的度数 d[x],O(n+m)
2、对于所有点 x,求出以点 x 为结尾的最长链长度 l[x],由于尾巴节点要求递增,符合DAG性质,从 1 点开始 BFS 就可以了,O(n+m)
3、枚举所有 x,比较出 l[x] * d[x] 最大值,得出答案, O(n),总复杂度O(n+m)
spines其实就是tail最后一个点的度数,也就是tail确定之后,spines就自然出来了(spines等价于度数)
这句话主要是给我看的:我们不是要spines最优(我之前一直被这个变量迷惑了= =),而是要 tail * spines 最优
以下也是他的话:
############################ 听udon一席话,胜读十年书呢 ^_^
分享下捻尼题既思路,其实就系物理常用控制变量法:
1、发现最后答案 res = max(tails * spines),有两个变量
2、简化下问题,我先假设spines一定的情况下,答案貌似就系tails最大值,已经系经典题目了
但系甘样要枚举所有度数下的tails最优值,超时
3、我再假设tails不变,情况下,根据定义spines就系tails最后一个点既度数数,嗟系
tails可以确定spines,唔洗求最优值!!
4、甘样我地久唔洗枚举度数,枚举tails就可以了
#############################
(1) DP 版本
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; typedef long long LL; const int maxn = 1e5 + ;
vector<int> edge[maxn];
LL dp[maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE int n, m;
while (scanf("%d%d", &n, &m) != EOF) { int u, v;
for (int i = ; i < m; i++) {
scanf("%d%d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
LL ans = -;
for (int i = ; i <= n; i++) {
dp[i] = ;
sort(edge[i].begin(), edge[i].end());
for (int j = ; j < edge[i].size(); j++) {
if (edge[i][j] > i) continue;
dp[i] = max(dp[i], dp[edge[i][j]]+);
}
ans = max(ans, dp[i]*(int)edge[i].size());
}
printf("%lld\n", ans);
for (int i = ; i <= n; i++) {
edge[i].clear();
}
} return ;
}
(2)DFS版本(记忆化搜索)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; typedef long long LL; const int maxn = 1e5 + ;
vector<int> edge[maxn];
int vis[maxn];
LL ans; int dfs(int st)
{
if (vis[st])
return vis[st]; // 记忆化搜索,避免超时
int ret = ; // 设为-1是错的,因为 st 编号的数可能前面根本没有比它小的数
for (int i = ; i < edge[st].size(); i++) {
if (edge[st][i] < st) { // 求出以 st 之前的最长递增序列
ret = max(ret, dfs(edge[st][i]));
}
}
vis[st] = ret + ;
return vis[st];
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE int n, m;
while (scanf("%d%d", &n, &m) != EOF) { int u, v;
for (int i = ; i < m; i++) {
scanf("%d%d", &u, &v);
edge[u].push_back(v);
edge[v].push_back(u);
}
memset(vis, , sizeof(vis));
ans = -;
for (int i = ; i <= n; i++) {
if (!vis[i]) {
ans = max( ans, (LL)dfs(i)*(int)edge[i].size() );
}
} printf("%lld\n", ans);
for (int i = ; i <= n; i++) {
edge[i].clear();
}
} return ;
}
codeforces 338(Div 2) B. Longtail Hedgehog 解题报告的更多相关文章
- 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 Round #335 (Div. 2)B. Testing Robots解题报告
B. Testin ...
- codeforces 814B.An express train to reveries 解题报告
题目链接:http://codeforces.com/problemset/problem/814/B 题目意思:分别给定一个长度为 n 的不相同序列 a 和 b.这两个序列至少有 i 个位置(1 ≤ ...
- codeforces 558B. Amr and The Large Array 解题报告
题目链接:http://codeforces.com/problemset/problem/558/B 题目意思:给出一个序列,然后找出出现次数最多,但区间占用长度最短的区间左右值. 由于是边读入边比 ...
- codeforces 515B. Drazil and His Happy Friends 解题报告
题目链接:http://codeforces.com/problemset/problem/515/B 题目意思:有 n 个 boy 和 m 个 girl,有 b 个 boy 和 g 个 girl ( ...
- codeforces 514B. Han Solo and Lazer Gun 解题报告
题目链接:http://codeforces.com/problemset/problem/514/B 题目意思:给出双头枪的位置(x0, y0),以及 n 个突击队成员的坐标.双头枪射击一次,可以把 ...
- codeforces 471C.MUH and House of Cards 解题报告
题目链接:http://codeforces.com/problemset/problem/471/C 题目意思:有 n 张卡,问能做成多少种不同楼层(floor)的 house,注意这 n 张卡都要 ...
- codeforces C. Vasily the Bear and Sequence 解题报告
题目链接:http://codeforces.com/problemset/problem/336/C 题目意思:给出一个递增的正整数序列 a1, a2, ..., an,要求从中选出一堆数b1, b ...
随机推荐
- django学习<一>:安装
这两天打算摸索下和python相关的东西,然后正好小伙伴有个关于网站的任务,就怀着好奇的心态了解了下,然后就很自然地开始涉及django的问题. 首先就是django安装的问题,想不到第一步就出问题了 ...
- 黄学长模拟day1 大逃亡
给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上,矩形的行号从0到X-1,列号从 ...
- POJ 1905 Expanding Rods
Expanding Rods Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...
- 备份Oracle数据库的脚本
@echo off goto bakoracle :bakoracle echo. echo ★☆★ 自动备份Oracle数据库 ★☆★ echo. set backpath=E:\Oracle ...
- HDU 4930 Fighting the Landlords(模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4930 解题报告:斗地主,加了一个四张可以带两张不一样的牌,也可以带一对,判断打出一手牌之后,如果对手没 ...
- python 环境安装
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz tar zxf Python-2.7.3.tgz cd Python-2. ...
- Xcode 6制作动态及静态Framework和各种坑
Xcode 6制作动态及静态Framework http://www.cocoachina.com/ios/20141126/10322.html 有没有写SDK或者要将一些常用的工具类做成Frame ...
- tableView 选中cell时,获取到当前cell
// >> 找到当前选中的cell,设置选中时的cell背景色 SideTableViewCell * cell = (SideTableViewCell *)[tableView cel ...
- pro*c添加SQLCHECK后编译报错PLS-S-00201
如果在pro*c中调用数据库了里的函数,就需要在proc的cfg配置文件中添加一行: SQLCHECK=SEMANTICS 但是添加之后又会出现PLS-S-00201错误,原因在与添加SQLCHECK ...
- 2015安徽省赛 J.镜像树
http://xcacm.hfut.edu.cn/problem.php?id=1214 乱搞题 数组+结构体 递归遍历 #include<iostream> #include<cs ...