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 直 ...
随机推荐
- 【转】在ubuntu环境下搭建svn server遇到的一些问题
原文网址:http://www.cnblogs.com/pcchinadreamfly/archive/2012/11/24/2786046.html 前段时间在ubuntu 12.04lts上倒腾了 ...
- Delphi webservice 定义 转
webservice Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务. 简介 它是一种构建应用程序的普遍 ...
- 计算机视觉code与软件
Research Code A rational methodology for lossy compression - REWIC is a software-based implementatio ...
- POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...
- HDOJ 1081(ZOJ 1074) To The Max(动态规划)
Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...
- HDOJ 2212 DFS
Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...
- poj 1704 Georgia and Bob(阶梯博弈)
Georgia and Bob Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8656 Accepted: 2751 D ...
- MVC 5 第二章 项目结构
通过本章学习,你将了解到一个MVC 5应用程序的项目组成以及项目文件的相关信息,从而更好地架构设计出自己的项目结构. 单从MVC的字面意思我们便能够注意到M-模型, View-视图, Controll ...
- CPP数组
数组为函数参数,求出一组数中的最大者;
- C# 调试
1.监视窗口