WA on #1 #2 #3 #10 #11 搞不明白了()
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
const int maxm = 5e5 + 5;
struct edge {
int to, nxt;
} e[maxm], E[maxm];
int tot = 1, head[maxn];
void add(int u, int v) {
e[++tot].to = v;
e[tot].nxt = head[u];
head[u] = tot;
}
int TOT = 1, HEAD[maxn];
void ADD(int U, int V) {
E[++TOT].to = V;
E[TOT].nxt = HEAD[U];
HEAD[U] = TOT;
}
int n, m, s, cnt, top, dfncnt;
int dfn[maxn], low[maxn], stk[maxn];
int a[maxn], scc[maxn], in[maxn];
ll ans, dis[maxn];
bool vis[maxn];
void getscc(int u) {
low[u] = dfn[u] = ++dfncnt;
stk[++top] = u;
vis[u] = true;
for(int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if(!dfn[v]) { getscc(v); low[u] = min(low[u], low[v]); }
else if(vis[v]) low[u] = min(low[u], low[v]);
}
vis[u] = false;
if(dfn[u] == low[u]) {
int v;
while((v = stk[top--])) {
scc[v] = u;
if(u == v) break;
a[u] += a[v];
}
}
}
void toposort() {
queue<int> q;
q.push(scc[s]);
dis[scc[s]] = a[scc[s]];
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = HEAD[u]; i; i = E[i].nxt) {
int v = E[i].to;
in[v]--;
dis[v] = max(dis[v], dis[u] + a[v]);
if(!in[v]) q.push(v);
}
}
}
int main() {
scanf("%d %d", &n, &m);
for(int i = 1, u, v; i <= m; i++) {
scanf("%d %d", &u, &v);
add(u, v);
}
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
scanf("%d", &s);
getscc(s);
for(int i = 1; i <= n; i++)
for(int j = head[i]; j; j = e[j].nxt) {
if(!scc[i] || !scc[e[j].to] || scc[i] == scc[e[j].to]) continue;
ADD(scc[i], scc[e[j].to]);
in[scc[e[j].to]]++;
}
toposort();
int q, x;
scanf("%d", &q);
for(int i = 1; i <= q; i++) {
scanf("%d", &x);
ans = max(ans, dis[scc[x]]);
}
printf("%lld\n", ans);
return 0;
}