Codeforces Round #286 (Div. 1) 解题报告
A.Mr. Kitayuta, the Treasure Hunter
很显然的一个DP,30000的数据导致使用map+set会超时。题解给了一个非常实用的做法,由于每个点有不超过250种状态,并且这些状态都是以包含d连续的一段数字,那么可以以对d的偏移量作为状态。这算是很常见的一个优化了。
#include<bits/stdc++.h>
using namespace std;
const int INF = ;
int f[INF][],a[INF];
int n, d, ans = , x;
int main() {
scanf ("%d %d", &n, &d);
for (int i = ; i <= n; i++) {
scanf ("%d", &x);
a[x]++;
}
f[d][] = a[d] + ;
for (int i = d; i <= x; i++)
for (int j = ; j < ; j++) {
if (f[i][j] == ) continue;
int k = j - + d, val = f[i][j];
ans = max (ans, val);
for (int t = max (, k - ); t <= k + ; t++)
if (i + t <= x)
f[i + t][t - d + ] = max (f[i + t][t - d + ], val + a[i + t]);
}
printf ("%d\n", ans - );
}
[Problem] Given an integer n and m pairs of integers (ai, bi) (1 ≤ ai, bi ≤ n), find the minimum number of edges in a directed graph that satisfies the following condition:
- For each i, there exists a path from vertex ai to vertex bi.
对于一个n个点的强连通图最少需要n条边,而非强连通的n个有边连接的点最少需要n-1条边。那么只要判断连通起来的k个点组成的块是不是有环,如果有环需要k条边,无环需要k-1条边。在连完一个连通的块后将其合并看成一个点。直到所有点都遍历过。
#include<bits/stdc++.h>
using namespace std;
const int INF = ;
struct Edge {
int v, next;
} edge[INF];
int head[INF], ncnt ;
int f[INF], vis[INF], cycle[INF], siz[INF];
int n, m, ans;
inline void adde (int u, int v) {
edge[++ncnt].v = v, edge[ncnt].next = head[u];
head[u] = ncnt;
}
inline int find (int x) {
if (f[x] == x) return x;
return f[x] = find (f[x]);
}
inline void uin (int x, int y)
{
siz[find (x)] += siz[find (y)];
f[find (y)] = find (x);
}
inline bool dfs (int u) {
vis[u] = ;
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
if (!vis[v]) dfs (v);
if (vis[v] == ) cycle[find (u)] = ;
}
vis[u] = ;
}
int main() {
scanf ("%d %d", &n, &m);
for (int i = ; i <= n; i++) f[i] = i, siz[i] = ;
for (int i = , x, y; i <= m; i++) {
scanf ("%d %d", &x, &y);
adde (x, y);
if (find (x) != find (y) ) uin (x, y);
}
for (int i = ; i <= n; i++)
if (!vis[i]) dfs (i);
for (int i = ; i <= n; i++) {
if (find (i) == i)
ans += siz[i] - ;
ans += cycle[i];
}
printf ("%d\n", ans);
}
D.Mr. Kitayuta's Colorful Graph
题意:一个n个点m条边的无向图中所有的边都有一个颜色,有q个询问,对每个询问(u,v)输出从u到v的纯色路径条数。(n,m,q<1e5)
首先应该先把所有相同颜色边连接的块做一次并查集,然后对所有询问(a,b)对a,b中有着较少的度的点的颜色查找b是否与a在一个集合。
unordered_map能够在让查找b的时间降到O(1)
#include<bits/stdc++.h>
using namespace std;
const int INF = ; unordered_map<int, int> f[INF], old[INF]; int n, m, q;
inline int find (int x, int c) {
if (f[x][c] < ) return x;
return f[x][c] = find (f[x][c], c);
}
inline void merge (int x, int y, int c) {
if (f[x].find (c) == f[x].end() ) f[x][c] = -;
if (f[y].find (c) == f[y].end() ) f[y][c] = -;
x = find (x, c), y = find (y, c);
if (x == y) return;
f[y][c] += f[x][c];
f[x][c] = y;
}
int main() {
scanf ("%d %d", &n, &m);
for (int i = , x, y, c; i <= m; i++) {
scanf ("%d %d %d", &x, &y, &c);
merge (x, y, c);
}
scanf ("%d", &q);
int a, b;
while (q--) {
scanf ("%d %d", &a, &b);
if (f[a].size() > f[b].size() ) swap (a, b);
if (old[a].find (b) == old[a].end() ) {
int ans = ;
for (auto &c : f[a])
if (f[b].find (c.first) != f[b].end() && find (a, c.first) == find (b, c.first) )
ans++;
old[a][b] = ans;
}
printf ("%d\n", old[a][b]);
}
}
Codeforces Round #286 (Div. 1) 解题报告的更多相关文章
- Codeforces Round #324 (Div. 2)解题报告
---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- Codeforces Round #380 (Div. 2) 解题报告
第一次全程参加的CF比赛(虽然过了D题之后就开始干别的去了),人生第一次codeforces上分--(或许之前的比赛如果都参加全程也不会那么惨吧),终于回到了specialist的行列,感动~.虽然最 ...
- Codeforces Round #216 (Div. 2)解题报告
又范低级错误! 只做了两题!一道还被HACK了,囧! A:看了很久!应该是到语文题: 代码:#include<iostream> #include<]; ,m2=; ;i ...
- Codeforces Round #281 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/493 A题 写完后就交了,然后WA了,又读了一遍题,没找出错误后就开始搞B题了,后来回头重做的时候才发现,球员被红牌罚下场后还可 ...
- Codeforces Round #277 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/486 A题.Calculating Function 奇偶性判断,简单推导公式. #include<cstdio> ...
- Codeforces Round #276 (Div. 2) 解题报告
题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...
- Codeforces Round #350 (Div. 2)解题报告
codeforces 670A. Holidays 题目链接: http://codeforces.com/contest/670/problem/A 题意: A. Holidays On the p ...
- Codeforces Round #479 (Div. 3)解题报告
题目链接: http://codeforces.com/contest/977 A. Wrong Subtraction 题意 给定一个数x,求n次操作输出.操作规则:10的倍数则除10,否则减1 直 ...
随机推荐
- 【转】更新SDK后,打包APK时报 the zipalign tool was not found in the sdk
原文网址:http://wyong.blog.51cto.com/1115465/1546632 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任 ...
- 博弈论(SG函数):HNOI 2007 分裂游戏
Description 聪聪和睿睿最近迷上了一款叫做分裂的游戏. 该游戏的规则试: 共有 n 个瓶子, 标号为 0,1,2.....n-1, 第 i 个瓶子中装有 p[i]颗巧克力豆,两个人轮流取豆子 ...
- delphi调用webservice 转
如今 Web Service 已越来越火了,在DotNet已开发的Web Service中,Delphi 7如何方便的调用DotNet写的Web Service呢?方法有两种,一种是在Delphi ...
- 使用LoadRunner对Web Services进行调用--Add Service Call
利用LoadRunner对Web Services进行测试时,通常有三种可供采用的方法: 在LoadRunner的Web Services虚拟用户协议中,[Add Service Call] 在Loa ...
- This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery 解决方法
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'的意思是,这版本的 MySQL 不支持使 ...
- 爬虫技术实战 | WooYun知识库
爬虫技术实战 | WooYun知识库 爬虫技术实战 大数据分析与机器学习领域Python兵器谱-大数据邦-微头条(wtoutiao.com) 大数据分析与机器学习领域Python兵器谱
- poj 3620 Avoid The Lakes【简单dfs】
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6795 Accepted: 3622 D ...
- 敏捷开发松结对编程系列:L型代码结构案例StatusFiltersDropdownList(中)
这是松结对编程的第22篇(专栏目录). 接前文 业务代码 比较长,基本上就是看被注释隔开的三大段,先显示状态群筛选链接,然后是单个状态筛选,然后是显示下拉框的当前选中项,最后显示下拉框. public ...
- sublime text2 快捷键
主要快捷键列表: Ctrl+L 选择整行(按住-继续选择下行)Ctrl+KK 从光标处删除至行尾Ctrl+Shift+K 删除整行Ctrl+Shift+D 复制光标所在整行,插入在该行之前Ctrl+J ...
- 程序员取悦女朋友的正确姿势---Tips(iOS美容篇)
前言 女孩子都喜欢用美图工具进行图片美容,近来无事时,特意为某人写了个自定义图片滤镜生成器,安装到手机即可完成自定义滤镜渲染照片.app独一无二,虽简亦繁. JH定律:魔镜:最漂亮的女人是你老婆魔镜: ...