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 - );
}

B.Mr. Kitayuta's Technology

[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) 解题报告的更多相关文章

  1. Codeforces Round #324 (Div. 2)解题报告

    ---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...

  2. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  3. Codeforces Round #380 (Div. 2) 解题报告

    第一次全程参加的CF比赛(虽然过了D题之后就开始干别的去了),人生第一次codeforces上分--(或许之前的比赛如果都参加全程也不会那么惨吧),终于回到了specialist的行列,感动~.虽然最 ...

  4. Codeforces Round #216 (Div. 2)解题报告

    又范低级错误! 只做了两题!一道还被HACK了,囧! A:看了很久!应该是到语文题: 代码:#include<iostream> #include<];    ,m2=;    ;i ...

  5. Codeforces Round #281 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/493 A题 写完后就交了,然后WA了,又读了一遍题,没找出错误后就开始搞B题了,后来回头重做的时候才发现,球员被红牌罚下场后还可 ...

  6. Codeforces Round #277 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/486 A题.Calculating Function 奇偶性判断,简单推导公式. #include<cstdio> ...

  7. Codeforces Round #276 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...

  8. Codeforces Round #350 (Div. 2)解题报告

    codeforces 670A. Holidays 题目链接: http://codeforces.com/contest/670/problem/A 题意: A. Holidays On the p ...

  9. Codeforces Round #479 (Div. 3)解题报告

    题目链接: http://codeforces.com/contest/977 A. Wrong Subtraction 题意 给定一个数x,求n次操作输出.操作规则:10的倍数则除10,否则减1 直 ...

随机推荐

  1. Win32中目录的操作

    1 系统和当前目录 1.1 获取Windows目录 UINT GetWindowsDirectory( LPTSTR lpBuffer, //BUFF的地址  UINT uSize    //BUFF ...

  2. StringBuilder字符串缓冲区

    JDK1.5出现StringBuiler:构造一个其中不带字符的字符串生成器,初始容量为 16 个字符.该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候( ...

  3. HDOJ 1716 排列2(next_permutation函数)

    Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡 ...

  4. The Fortified Forest - POJ 1873(状态枚举+求凸包周长)

    题目大意:有个国王他有一片森林,现在他想从这个森林里面砍伐一些树木做成篱笆把剩下的树木围起来,已知每个树都有不同的价值还有高度,求出来砍掉那些树可以做成篱笆把剩余的树都围起来,要使砍伐的树木的价值最小 ...

  5. poj 1328 Radar Installation【贪心区间选点】

    Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) ...

  6. 挣值管理不是搞数字游戏(3)——进阶指标:CV、SV、CPI、SPI、EAC

    摘要: 要考PMP(Project Management Professional ),挣值管理是必考的知识.软件项目有很大的特殊性,不少人认为挣值管理不太适用于软件项目.挣值管理相关资料也比较超多, ...

  7. mysql常用总结

    用户管理mysql>use mysql;查看mysql> select host,user,password from user ;创建mysql> create user zx_r ...

  8. php类获取静态变量值以及调用

    <?php class Test{ public static $static_var = 20; } echo Test::$static_var;exit;

  9. CBO学习----03--选择率(Selectivity)

    第3章 单表选择率(Single Table Selectivity) Selectivity是优化器估算Rows(Cards)的重要依据. /**************************** ...

  10. C# WinForm 判断窗体控件是否修改过

    本文转载:http://www.cnblogs.com/LinFx/archive/2011/12/23/2299895.html 1.自定义控件, 和接口 ) return IsModify(con ...