题目链接: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 解题报告的更多相关文章

  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 Round #335 (Div. 2)B. Testing Robots解题报告

                                                                                               B. Testin ...

  4. codeforces 814B.An express train to reveries 解题报告

    题目链接:http://codeforces.com/problemset/problem/814/B 题目意思:分别给定一个长度为 n 的不相同序列 a 和 b.这两个序列至少有 i 个位置(1 ≤ ...

  5. codeforces 558B. Amr and The Large Array 解题报告

    题目链接:http://codeforces.com/problemset/problem/558/B 题目意思:给出一个序列,然后找出出现次数最多,但区间占用长度最短的区间左右值. 由于是边读入边比 ...

  6. codeforces 515B. Drazil and His Happy Friends 解题报告

    题目链接:http://codeforces.com/problemset/problem/515/B 题目意思:有 n 个 boy 和 m 个 girl,有 b 个 boy 和 g 个 girl ( ...

  7. codeforces 514B. Han Solo and Lazer Gun 解题报告

    题目链接:http://codeforces.com/problemset/problem/514/B 题目意思:给出双头枪的位置(x0, y0),以及 n 个突击队成员的坐标.双头枪射击一次,可以把 ...

  8. codeforces 471C.MUH and House of Cards 解题报告

    题目链接:http://codeforces.com/problemset/problem/471/C 题目意思:有 n 张卡,问能做成多少种不同楼层(floor)的 house,注意这 n 张卡都要 ...

  9. codeforces C. Vasily the Bear and Sequence 解题报告

    题目链接:http://codeforces.com/problemset/problem/336/C 题目意思:给出一个递增的正整数序列 a1, a2, ..., an,要求从中选出一堆数b1, b ...

随机推荐

  1. scanf 格式化字符串详解

    scanf格式控制的完整格式: %     *     m     l或h     格式字符 ①格式字符与printf函数中的使用方式相同,以%d.%o.%x.%c.%s.%f.%e,无%u格式.%g ...

  2. solr6.1-----solrJ 程序管理索引库

    solrJ 是solr 提供的一个客户端,就是一个jar 包,把jar 添加到工程中整合solr 服务. 所需jar 包 D:\solr-6.1.0\dist 下面的 solr-solrj-6.1.0 ...

  3. HTML5+学习笔记1-------边看代码边研究中

    document.addEventListener('touchstart',function(){ return false; },true); touchstart当手指触摸屏幕时候触发,即使已经 ...

  4. 腾讯web前端笔试题及个人答案

    每道题都有答案,大多数答案亲测正确. 简答题 1.js中“5”+4=? 答案:54 2.js中void(0)=? 答案:undefined 3.js中NaN*4=? 答案:NaN 4.js中null* ...

  5. QT编写上位机程序一定要初始化变量以及谨慎操作指针

    背景: 在编写QT上位机界面时,界面在运行的时候经常出现卡死或者直接挂掉的怪现象. 正文: 上位机有个函数为check_receive():该函数的作用为定时调用循环检测USB是否有数据.若有,则将信 ...

  6. C#之interface接口

    C#中接口与抽象类很相似,他们都无法实例化自己的对象,但是他们也有很重要的区别.Interface与Abstract class中,类不能多重继承,但是接口可以多重继承. 这段代码表明,声明接口的方法 ...

  7. EasyUI中datagrid控件的使用 设置多行表头(两行或多行)

    EasyUI中的datagrid控件十分强大,能生成各种复杂的报表,现在因为项目需要,需要生成一个表头两行的表,找了一些说明文档,以下用一个实例来说明一下: 第一种方法: $('#divData'). ...

  8. centos下redis的安装

    1.下载redis的安装包 http://download.redis.io/releases/redis-3.2.0.tar.gz 2.把安装包放到/opt/src/目录(看个人喜好)下 3.执行t ...

  9. javascript中对象学习

    第一篇文章: javascript中this关键字的详细解析:   http://blog.csdn.net/wyj880220/article/details/7305952 Javascript ...

  10. Java多线程文件下载

    一. 多线程下载文件考虑处理步骤: 1. 如何获取文件的长度 2. 合理的创建线程数量,并计算每一个线程下载的长度 3. 如何将多个线程下载的字节写入到文件中 二. 代码实现如下: package c ...