【51Nod 1815】【算法马拉松 23】调查任务
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1815
tarjan缩点后在DAG上递推即可。
每个点维护所有根到它的路径上的值的最大值,严格次大值,最大的“根到这个点的一条路径中的严格次大值”(也就是答案)。
注意所有根到它的路径上的值的严格次大值不是答案。
时间复杂度\(O(n)\)。
#include<cstdio>
#include<bitset>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 400003;
const int M = 2000003;
struct node {int nxt, to;} E[M], E2[M], E3[M];
int cnt = 0, cnt3 = 0, point[N], point2[N], cur[N], du[N], point3[N];
void ins(int u, int v) {E[++cnt] = (node) {point[u], v}; point[u] = cnt;}
void ins2(int u, int v) {
E2[++cnt] = (node) {point2[u], v}; point2[u] = cnt;
E3[++cnt3] = (node) {point3[v], u}; point3[v] = cnt3;
}
bitset <N> inst;
int dfn[N], low[N], tot = 0, a[N], sta[N], statop, st[N], top, bel[N], n, m, q, s, fa[N];
void tarjan(int x) {
st[top = 1] = x; statop = 0;
while (top) {
int u = st[top];
if (!dfn[u]) {
dfn[u] = low[u] = ++cnt;
sta[++statop] = u;
inst[u] = 1;
}
if (cur[u]) {
int v = E[cur[u]].to;
if (!dfn[v]) fa[st[++top] = v] = u;
else if (inst[v]) low[u] = min(low[u], dfn[v]);
cur[u] = E[cur[u]].nxt;
} else {
low[fa[u]] = min(low[fa[u]], low[u]);
if (dfn[u] == low[u]) {
++tot;
while (sta[statop] != u) {
inst[sta[statop]] = 0;
bel[sta[statop]] = tot;
--statop;
}
inst[u] = 0;
bel[u] = tot;
--statop;
}
--top;
}
}
}
bitset <N> vis;
int max1[N], max2[N], max3[N], qu[N], A[4];
void BFS() {
int p = 0, q = 1;
vis[qu[1] = bel[s]] = 1;
while (p != q) {
int u = qu[++p];
for (int i = point2[u]; i; i = E2[i].nxt) {
int v = E2[i].to;
++du[v];
if (!vis[v]) {
vis[v] = 1;
qu[++q] = v;
}
}
}
p = 0; q = 1; qu[1] = bel[s];
while (p != q) {
int u = qu[++p];
for (int i = point3[u]; i; i = E3[i].nxt) {
int v = E3[i].to;
if (vis[v]) {
if (max1[v] > max1[u]) {max3[u] = max1[u]; max1[u] = max1[v];}
else if (max1[v] < max1[u] && max1[v] > max3[u]) max3[u] = max1[v];
if (max3[v] > max1[u]) {max3[u] = max1[u]; max1[u] = max3[v];}
else if (max3[v] < max1[u] && max3[v] > max3[u]) max3[u] = max3[v];
}
}
for (int i = point2[u]; i; i = E2[i].nxt) {
int v = E2[i].to;
max2[v] = max(max2[v], max2[u]);
if (max1[u] != max1[v]) max2[v] = max(max2[v], min(max1[u], max1[v]));
else max2[v] = max(max2[v], max(max3[u], max3[v]));
if (--du[v] == 0) qu[++q] = v;
}
}
}
int main() {
scanf("%d%d%d%d", &n, &m, &q, &s);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i);
int u, v;
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &u, &v);
ins(u, v);
}
cnt = 0;
for (int i = 1; i <= n; ++i)
cur[i] = point[i], max1[i] = max2[i] = -1;
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i);
cnt = 0;
for (int i = 1; i <= n; ++i)
for (int j = point[i]; j; j = E[j].nxt)
if (bel[i] != bel[E[j].to])
ins2(bel[i], bel[E[j].to]);
for (int i = 1; i <= n; ++i) {
int t = bel[i];
if (a[i] > max1[t]) max2[t] = max1[t], max1[t] = a[i];
else if (a[i] < max1[t] && a[i] > max2[t]) max2[t] = a[i];
}
for (int i = 1; i <= tot; ++i) max3[i] = max2[i];
BFS();
for (int i = 1; i <= q; ++i) {
scanf("%d", &u);
if (!vis[bel[u]]) printf("-1 ");
else if (max2[bel[u]] == -1) printf("0 ");
else printf("%d ", max2[bel[u]]);
}
return 0;
}
【51Nod 1815】【算法马拉松 23】调查任务的更多相关文章
- 51Nod 算法马拉松23 开黑记
惨啊……虽然开了半天黑,但是还是被dalao们踩了…… 第二次开黑,还是被卡在rank20了,我好菜啊……= = 写一写比赛经过吧…… 看到题之后习惯性都打开,A~D看上去似乎并没有什么思路,F应该是 ...
- 【51Nod 1756】【算法马拉松 23】谷歌的恐龙
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1765 设答案为\(X\). 则\[X=\frac{m}{n}\times ...
- 随便玩玩系列之一:SPOJ-RNG+51nod 算法马拉松17F+51nod 1034 骨牌覆盖v3
先说说前面的SPOJ-RNG吧,题意就是给n个数,x1,x2,...,xn 每次可以生成[-x1,x1]范围的浮点数,把n次这种操作生成的数之和加起来,为s,求s在[A,B]内的概率 连续形的概率 假 ...
- 51NOD 算法马拉松8
题目戳这里:51NOD算法马拉松8 某天晚上kpm在玩OSU!之余让我看一下B题...然后我就被坑进了51Nod... A.还是01串 水题..怎么乱写应该都可以.记个前缀和然后枚举就行了.时间复杂度 ...
- 51nod 算法马拉松 34 Problem D 区间求和2 (FFT加速卷积)
题目链接 51nod 算法马拉松 34 Problem D 在这个题中$2$这个质数比较特殊,所以我们先特判$2$的情况,然后仅考虑大于等于$3$的奇数即可. 首先考虑任意一个点对$(i, j)$ ...
- 51Nod 算法马拉松21(迎新年)
这次打算法马拉松是在星期五的晚上,发挥还算正常(废话,剩下的题都不会= =). 讲讲比赛经过吧. 8:00准时发题,拿到之后第一时间开始读. A配对,看上去像是二分图最大权匹配,一看范围吓傻了,先跳过 ...
- 推荐一个算法编程学习中文社区-51NOD【算法分级,支持多语言,可在线编译】
最近偶尔发现一个算法编程学习的论坛,刚开始有点好奇,也只是注册了一下.最近有时间好好研究了一下,的确非常赞,所以推荐给大家.功能和介绍看下面介绍吧.首页的标题很给劲,很纯粹的Coding社区....虽 ...
- 51Nod 算法马拉松15 记一次悲壮而又开心的骗分比赛
OwO 故事的起源大概是zcg前天发现51Nod晚上有场马拉松,然后他就很开心的过去打了 神奇的故事就开始了: 晚上的时候我当时貌似正在写线段树?然后看见zcg一脸激动告诉我第一题有九个点直接输出B就 ...
- 51Nod 算法马拉松22 开黑记
这是一场惨烈的开黑大战,始于全机房开黑指望刷进rank前十拿钱的壮志,终于被各路神犇怒踩成rank20,差点200点头盾不保的落魄,想起将近一年前ad和zcg等学长挤进rank10的壮举,不由得唏嘘, ...
随机推荐
- 你知道吗?衡量 Web 性能的几个关键指标
自网站诞生以来,响应速度/响应时间一直都是大家关心的话题,而速度慢乃是网站的一个杀手,正当大家以为四核和宽带能力的提升能够解决这些问题时,Wi-Fi和移动设备为热点移动互联网又悄然兴起. 在2006年 ...
- asp.net中模拟测试smtp发邮件
最近在编程人生里要测试一个会员邮件的功能,就写了下面的代码. 在asp.net 中,有时要测试发信SMTP,但如果在单元测试中,如果没方便好用的 smtp怎么办,其实还是有办法模拟的,下面讲解下: 在 ...
- [php]apache虚拟主机配置
1.所谓虚拟主机的配置,即url与磁盘目录的绑定 2.在httpd.conf中查询Virtual host,发现有注释说明需要在conf/extra/httpd-vhosts.conf中进行配置. 3 ...
- JQuery对RadioButton和CheckButton的操作
js对RadioButton和CheckButton的操作,在网站开发中会经常遇到,而JQuery操作RadioButton和CheckButton非常便捷.小编觉得网站开发人员有必要熟练掌握.所以小 ...
- python学习笔记(十)之格式化字符串
格式化字符串,可以使用format方法.format方法有两种形式参数,一种是位置参数,一种是关键字参数. >>> '{0} {1}'.format('Hello', 'Python ...
- css3图片响应式布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 2017ACM暑期多校联合训练 - Team 7 1008 HDU 6127 Hard challenge (极角排序)
题目链接 Problem Description There are n points on the plane, and the ith points has a value vali, and i ...
- 记一个logrotate的配置文件权限问题
问题描述 从git仓库更新了别人配置好的logrotate,发现不能正常运行.手工执行报错 error: Ignoring syslog because of bad file mode - must ...
- C++中string.find()函数,string.find_first_of函数与string::npos
查找字符串a是否包含子串b,不是用strA.find(strB) > 0而是strA.find(strB) != string:nposstring::size_type pos = strA. ...
- Linux中fork()函数的底层实现【转】
转自:http://blog.csdn.net/duoru_xiong/article/details/76358812 1. fork(),vfork(),clone()的区别 这三个系统调用的底层 ...