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 直 ...
随机推荐
- 嵌入式系统烧写uboot/bootloader/kernel的一般方法
嵌入式系统烧写uboot/bootloader/kernel的一般方法 本文介绍了在嵌入式系统中烧写uboot/bootloader/kernel 的一般方法,以及如果uboot或者内核出现错误, ...
- POJ --2104
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 34935 Accepted: 11134 Ca ...
- 【Objective-C基础教程-读书笔记】第1章 启程
在第1章里面,作者主要以一种站在世界中心呼唤爱的姿态,给读者们打打鸡血洗洗脑,鼓励大家,投入时间学习Objective-C,值得啊! 首先,Objective-C既能用来开发OS X平台上的APP,又 ...
- [Java] Java IO Files
Files 使用 FileInputStream 或 FileReader 可以用于读入文件,前者基于二进制,后者基于文本.使用它们不需要读取整个文件,但是只能按照它们存储的顺序,依次读取字节,或字符 ...
- Android传感器概述(六)
监视传感器事件 要监视原始的传感器数据,你须要实现两个通过SensorEventListener接口暴露的回调方法:onAccuracyChanged()和onSensorChanged().Andr ...
- delphi TColorDialog
TColorDialog 预览 实现过程 动态创建和使用颜色对话框 function ShowColorDlg:TColor;begin with TColorDialog.Cre ...
- spin_lock & mutex_lock的差别?
本文由该问题引入到内核锁的讨论,归纳例如以下 为什么须要内核锁? 多核处理器下,会存在多个进程处于内核态的情况,而在内核态下,进程是能够訪问全部内核数据的,因此要对共享数据进行保护,即相互排斥处理 有 ...
- linux device driver —— 字符设备
现在对linux设备驱动还没有什么认识,跟着书上敲了一个字符驱动,这里把代码贴一下. 测试环境是 Ubuntu 16.04 64bit 驱动程序: #include <linux/fs.h> ...
- [转] CSS transition
https://css-tricks.com/almanac/properties/t/transition/ The transition property is a shorthand prope ...
- JS 拼凑字符串
和Java一样,JS中直接用"+"号拼凑字符串是很耗费资源的,所以在大量拼凑字符串的情景中,我们也需要一个类似于StringBuffer的工具, 下面利用Array.join()方 ...