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 ...
随机推荐
- spark在windows下的安装
Windows下最简的开发环境搭建这里的spark开发环境, 不是为apache spark开源项目贡献代码, 而是指基于spark的大数据项目开发. Spark提供了2个交互式shell, 一个 ...
- 【R】如何确定最适合数据集的机器学习算法 - 雪晴数据网
[R]如何确定最适合数据集的机器学习算法 [R]如何确定最适合数据集的机器学习算法 抽查(Spot checking)机器学习算法是指如何找出最适合于给定数据集的算法模型.本文中我将介绍八 ...
- ICloud没有密码怎么注销?
忘了密码的用这方法就可以啦1.现在我说你做 . 先进入 iCloud 点击 某某账户(要删的账户)2.第二栏密码那里删除原来的再输入ios(任意密码)3.然后点完成 之后会出现错误.请点 好.然后左上 ...
- Linux命令行修改IP、网关、DNS、主机名 的方法
修改主机名:[改里面的 HOSTNAME 即可] vim /etc/sysconfig/network 网卡eth0 IP修改为 102.168.0.1 ifconfig eth0 102.16 ...
- thusc滚粗记
day0 下午到了北京,雾霾还是那么大.. 到宾馆报个到,和哥哥吃了一波饭,去不起西郊...只能去五道口了... 晚上和wyz队长见面,wyz队长好帅啊...没带手机拍照真是个错误TAT day1 今 ...
- Github如何更新远程代码
1.git add . 2.git commit -m "Second commit" 3.git push origin master
- BZOJ 1024: [SCOI2009]生日快乐
Description 将一个 \(x\times y\) 的矩形分成 \(n\) 块,让最长边:最短边 最小. Sol 搜索. \(n\) 只有 \(10\) 写一个类似于记搜的东西就好了. Cod ...
- 项目: 推送水木文章到Kindle
代码在github:https://github.com/Viyu/PushNewsmth2Mail 当年买Kindle的时候,想再Kindle上阅读水木的帖子,但Kindle的Web上网体验太差,想 ...
- [CQOI2011]动态逆序对
(又是一道树套树……自己真是玩疯了……) (题意略) 从网上也看过题解,好像解法很多……比如CDQ+树状数组,树状数组套主席树,树状数组套平衡树……我用的是树状数组套splay. (我会说是因为我不会 ...
- git 忽略文件权限
git config --add core.filemode false 发现.git/config 中新增了一行: [core] ... filemode = false ref: http://b ...