*51nod 1815

从若干个数中选出最大的任意两数取模之后的结果

严格次大值

对于此题

首先缩点

然后拓扑排序

维护到达每个点的最大值与严格次大值

感觉思路与代码都OK啊

then....

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <ctime> using namespace std; #define LL long long #define gc getchar()
inline int read() {int x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
inline LL read_LL() {LL x = ; char c = gc; while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc; return x;}
#undef gc const int N = 4e5 + , M = 2e6 + ;
int n, m, S, Q;
int A[N]; int Dfn[N], Low[N], Belong[N], Beljs, Tim;
bool vis[N];
int Stack[N], topp; pair <int, int> Pair[N];
struct Node {int u, v, nxt;} G[M];
int head1[N], cnt; inline void Link(int u, int v) {G[++ cnt].v = v; G[cnt].nxt = head1[u]; head1[u] = cnt;} void Tarjan(int u) {
Dfn[u] = Low[u] = ++ Tim;
vis[u] = ;
Stack[++ topp] = u;
for(int i = head1[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(!Dfn[v]) {
Tarjan(v);
Low[u] = min(Low[u], Low[v]);
} else if(vis[v]) Low[u] = min(Low[u], Low[v]);
}
if(Low[u] == Dfn[u]) {
int Max = , CMax = ;
Max = A[u];
vis[u] = ; Belong[u] = ++ Beljs;
Pair[Beljs].first = A[u]; Pair[Beljs].second = -;
while(Stack[topp] != u) {
int a = Stack[topp];
vis[a] = ;
Belong[a] = Beljs;
topp --;
if(A[a] > Max) {CMax = Max, Max = A[a];}
else if(A[a] != Max) CMax = max(CMax, A[a]);
Pair[Beljs] = make_pair(Max, CMax);
} topp --;
}
} Node E[M];
int head2[N], Du[N]; inline void ReLink(int u, int v) {Du[v] ++; E[++ cnt].v = v; E[cnt].nxt = head2[u]; head2[u] = cnt;} int use[N]; void Rebuild() {
for(int i = ; i <= Beljs; i ++) head2[i] = -;
cnt = ;
for(int u = ; u <= n; u ++)
for(int i = head1[u]; ~ i; i = G[i].nxt) {
int v = G[i].v;
if(Belong[u] != Belong[v] && use[Belong[v]] != u) {
use[Belong[v]] = u;
// cout << Belong[u] << " " << Belong[v] << "\n";
ReLink(Belong[u], Belong[v]);
}
}
} int Que[N]; bool beuse[N]; void Topsort() {
int h = , t = ;
// for(int i = 1; i <= Beljs; i ++) if(Du[i] == 0) Que[++ t] = i;
Que[++ t] = Belong[S];
while(h <= t) {
int topu = Que[h ++];
beuse[topu] = ;
for(int i = head2[topu]; ~ i; i = E[i].nxt) {
int v = E[i].v;
Du[v] --;
if(Du[v] == ) Que[++ t] = v;
int B[];
// cout << Pair[topu].first << " " << Pair[topu].second << " " << Pair[v].first << " " << Pair[v].second << "\n";
B[] = Pair[topu].first, B[] = Pair[topu].second, B[] = Pair[v].first, B[] = Pair[v].second;
sort(B + , B + );
Pair[v].first = B[];
int j = ;
while(B[j] == B[]) j --;
Pair[v].second = B[j];
}
}
} int main() {
// srand(time(0) - 99999);
// freopen("gg.in", "r", stdin);
// freopen("gg.out", "w", stdout);
n = read(), m = read(), Q = read(), S = read();
for(int i = ; i <= n; i ++) A[i] = read();
for(int i = ; i <= n; i ++) head1[i] = -;
for(int i = ; i <= m; i ++) {
int u = read(), v = read();
Link(u, v);
}
for(int i = ; i <= n; i ++)
if(!Dfn[i])
Tarjan(i);
// for(int i = 1; i <= Beljs; i ++) {
// cout << Pair[i].first << " " << Pair[i].second << "\n";
// }
// return 0;
Rebuild();
// for(int i = 1; i <= Beljs; i ++) {
// for(int j = head2[i]; ~ j; j = E[j].nxt) {
// int v = E[j].v;
// cout << i << " " << v << "\n";
// }
// }
Topsort();
for(int i = ; i <= Q; i ++) {
int a = read();
if(beuse[Belong[a]] == ) cout << - << " ";
else cout << Pair[Belong[a]].second << " ";
// if(ans == 0) cout << -1 << " ";
// cout << ans << " ";
}
return ;
}
/*
8 10 5 1
5 5 5 7 5 5 5 5
1 2
2 3
3 1
3 4
5 1
6 5
1 6
6 8
8 7
7 6
1 2 3 4 5
*/
上一篇:51nod图论题解(4级,5级算法题)


下一篇:《Postgre SQL 即学即用 (第三版)》 分享 pdf下载