Siruseri 城中的道路都是单向的。不同的道路由路口连接。按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机。令人奇怪的是,Siruseri 的酒吧也都设在路口,虽然并不是每个路口都设有酒吧。
Banditji 计划实施 Siruseri 有史以来最惊天动地的 ATM 抢劫。他将从市中心 出发,沿着单向道路行驶,抢劫所有他途径的 ATM 机,最终他将在一个酒吧庆 祝他的胜利。
使用高超的黑客技术,他获知了每个 ATM 机中可以掠取的现金数额。他希 望你帮助他计算从市中心出发最后到达某个酒吧时最多能抢劫的现金总数。他可 以经过同一路口或道路任意多次。但只要他抢劫过某个 ATM 机后,该 ATM 机 里面就不会再有钱了。 例如,假设该城中有 6 个路口,道路的连接情况如下图所示:
市中心在路口 1,由一个入口符号→来标识,那些有酒吧的路口用双圈来表
示。每个 ATM 机中可取的钱数标在了路口的上方。在这个例子中,Banditji 能抢 劫的现金总数为 47,实施的抢劫路线是:1-2-4-1-2-3-5。
这道题其实思路不难:将每一个强连通分量缩点,对于新图,跑一次spfa/拓补排序即可。
#include<bits/stdc++.h>
using namespace std;
const int mn = 500005, mm = 500005;
struct edge{
int to, nxt;
}e[mm], E[mm];
queue<int> q;
int fir1[mn], fir2[mn], cnt1, cnt2;
int val1[mn], val2[mn];
int dfn[mn], low[mn], stk[mn], top, times, num, bel[mn];
int inn[mn], sum[mn], ans;
bool exi1[mn], exi2[mn], ins[mn];
inline void addedge1(int a, int b) {e[++cnt1] = (edge) {b, fir1[a]}, fir1[a] = cnt1;}
inline void addedge2(int a, int b) {E[++cnt2] = (edge) {b, fir2[a]}, fir2[a] = cnt2;}
inline int getint()
{
int ret = 0, flg = 1; char c;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flg = -1;
while(c >= '0' && c <= '9')
ret = ret * 10 + c - '0', c = getchar();
return ret * flg;
}
void dfs(int s)
{
dfn[s] = low[s] = ++times, stk[++top] = s, ins[s] = 1;
for(int i = fir1[s]; i; i = e[i].nxt)
{
int t = e[i].to;
if(!dfn[t])
dfs(t), low[s] = min(low[s], low[t]);
else if(ins[t])
low[s] = min(low[s], dfn[t]);
}
if(low[s] == dfn[s])
{
++num;
while(stk[top] != s)
bel[stk[top]] = num, exi2[num] |= exi1[stk[top]], val2[num] += val1[stk[top]], ins[stk[top--]] = 0;
bel[stk[top]] = num, exi2[num] |= exi1[stk[top]], val2[num] += val1[stk[top]], ins[stk[top--]] = 0;
}
}
inline void topu(int s)
{
s = bel[s], inn[s] = 0, q.push(s);
while(!q.empty())
{
s = q.front(), q.pop(), sum[s] += val2[s];
if(exi2[s]) ans = max(sum[s], ans);
for(int i = fir2[s]; i; i = E[i].nxt)
{
int t = E[i].to;
--inn[t], sum[t] = max(sum[t], sum[s]);
if(!inn[t]) q.push(t);
}
}
}
int main()
{
int n, m, s, p, a, b;
n = getint(), m = getint();
for(int i = 1; i <= m; i++)
a = getint(), b = getint(), addedge1(a, b);
for(int i = 1; i <= n; i++)
val1[i] = getint();
s = getint(), p = getint();
for(int i = 1; i <= p; i++)
exi1[getint()] = 1;
for(int i = 1; i <= n; i++)
if(!dfn[i]) dfs(i);
for(int i = 1; i <= n; i++)
for(int j = fir1[i]; j; j = e[j].nxt)
{
int t = e[j].to;
a = bel[i], b = bel[t];
if(a != b) addedge2(a, b), ++inn[b];
}
topu(s), printf("%d\n", ans);
}
然而这段代码在洛谷上面只能取得87分的好成绩。
下载了一组数据,发现它输出了0。
发生了什么呢?
观察这个辣鸡写的拓补排序:
inline void topu(int s)
{
s = bel[s], inn[s] = 0, q.push(s);
while(!q.empty())
{
s = q.front(), q.pop(), sum[s] += val2[s];
if(exi2[s]) ans = max(sum[s], ans);
for(int i = fir2[s]; i; i = E[i].nxt)
{
int t = E[i].to;
--inn[t], sum[t] = max(sum[t], sum[s]);
if(!inn[t]) q.push(t);
}
}
}
你是否意识到了什么。
它违背了一般拓补排序的顺序,直接从起点开始dp,忽略了其它度数为0的点。
既然你敢这样水题,那我就出这样一个毒瘤图来卡死你:
图中S为起点对应的新图中的点。不难发现,按照这个辣鸡的代码,它最终会把它变成这个熊样:
然后由于此时队列空了,这段代码就退出了。于是,最下方节点之后的信息就统计不到。导致结果偏小。
其实上面这段代码经过小改就可以通过测试。
发现上图中,左上角的大点,是从S出发无论如何也到不了的。于是,在缩点时,我们根本就不考虑它们,直接将从S所在的联通图缩点即可。
放到代码中,就是将主函数中的这一段:
for(int i = 1; i <= n; i++)
if(!dfn[i]) dfs(i);
换成这样:
dfs(s);
就可以了。
完整的代码如下:
#include<bits/stdc++.h>
using namespace std;
const int mn = 500005, mm = 500005;
struct edge{
int to, nxt;
}e[mm], E[mm];
queue<int> q;
int fir1[mn], fir2[mn], cnt1, cnt2;
int val1[mn], val2[mn];
int dfn[mn], low[mn], stk[mn], top, times, num, bel[mn];
int inn[mn], sum[mn], ans;
bool exi1[mn], exi2[mn], ins[mn];
inline void addedge1(int a, int b) {e[++cnt1] = (edge) {b, fir1[a]}, fir1[a] = cnt1;}
inline void addedge2(int a, int b) {E[++cnt2] = (edge) {b, fir2[a]}, fir2[a] = cnt2;}
inline int getint()
{
int ret = 0, flg = 1; char c;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flg = -1;
while(c >= '0' && c <= '9')
ret = ret * 10 + c - '0', c = getchar();
return ret * flg;
}
void dfs(int s)
{
dfn[s] = low[s] = ++times, stk[++top] = s, ins[s] = 1;
for(int i = fir1[s]; i; i = e[i].nxt)
{
int t = e[i].to;
if(!dfn[t])
dfs(t), low[s] = min(low[s], low[t]);
else if(ins[t])
low[s] = min(low[s], dfn[t]);
}
if(low[s] == dfn[s])
{
++num;
while(stk[top] != s)
bel[stk[top]] = num, exi2[num] |= exi1[stk[top]], val2[num] += val1[stk[top]], ins[stk[top--]] = 0;
bel[stk[top]] = num, exi2[num] |= exi1[stk[top]], val2[num] += val1[stk[top]], ins[stk[top--]] = 0;
}
}
inline void topu(int s)
{
s = bel[s], q.push(s);
while(!q.empty())
{
s = q.front(), q.pop(), sum[s] += val2[s];
if(exi2[s])
ans = max(sum[s], ans);
for(int i = fir2[s]; i; i = E[i].nxt)
{
int t = E[i].to;
--inn[t], sum[t] = max(sum[t], sum[s]);
if(!inn[t]) q.push(t);
}
}
}
int main()
{
//freopen("data.txt", "r", stdin);
int n, m, s, p, a, b;
n = getint(), m = getint();
for(int i = 1; i <= m; i++)
a = getint(), b = getint(), addedge1(a, b);
for(int i = 1; i <= n; i++)
val1[i] = getint();
s = getint(), p = getint();
for(int i = 1; i <= p; i++)
exi1[getint()] = 1;
/*for(int i = 1; i <= n; i++)
if(!dfn[i]) dfs(i);*/
dfs(s);
for(int i = 1; i <= n; i++)
if(bel[i])
for(int j = fir1[i]; j; j = e[j].nxt)
{
int t = e[j].to;
a = bel[i], b = bel[t];
if(a != b) addedge2(a, b), ++inn[b];
}
topu(s), printf("%d\n", ans);
}
如果你不想像上面一样投机取巧,而想老老实实地写拓扑写缩点,你需要对上面代码中的 v a l 2 [ ] val2[] val2[]稍加修改。现将一份这样的代码贴在下面。(感谢这个提供代码的神犇)
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<vector>
#include<map>
#define maxn 500005
#define INF 0x3f3f3f3f
using namespace std;
inline long long getint()
{
long long num=0,flag=1;char c;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;
while(c>='0'&&c<='9')num=num*10+c-48,c=getchar();
return num*flag;
}
int n,m,scccnt,S;
int fir[maxn],nxt[maxn],to[maxn],cnt;
int dfn[maxn],low[maxn],clc,sccno[maxn],d[maxn];
long long sz[maxn],num[maxn],ok[maxn];
bool vis[maxn];
vector<int>g[maxn];
int stk[maxn],tp;
inline void newnode(int u,int v)
{to[++cnt]=v,nxt[cnt]=fir[u],fir[u]=cnt;}
inline void tarjan(int u)
{
dfn[u]=low[u]=++clc;
stk[++tp]=u;
for(int i=fir[u];i;i=nxt[i])
if(!dfn[to[i]])tarjan(to[i]),low[u]=min(low[u],low[to[i]]);
else if(!sccno[to[i]])low[u]=min(low[u],dfn[to[i]]);
if(low[u]==dfn[u])
{
scccnt++;
while(1)
{
sccno[stk[tp]]=scccnt;
if(stk[tp--]==u)break;
}
}
}
inline void dfs(int u)
{
vis[u]=1;
for(int i=g[u].size()-1;i>=0;i--)
if(!vis[g[u][i]])dfs(g[u][i]);
}
inline void bfs()
{
queue<int>Q;
for(int i=1;i<=scccnt;i++)if(!d[i])Q.push(i);
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=g[u].size()-1;i>=0;i--)
{
num[g[u][i]]=max(num[g[u][i]],num[u]+sz[g[u][i]]);
if(!(--d[g[u][i]]))Q.push(g[u][i]);
}
}
}
int main()
{
//freopen("data.txt", "r", stdin);
n=getint(),m=getint();
for(int i=1;i<=m;i++)
{
int u=getint(),v=getint();
newnode(u,v);
}
for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
for(int i=1;i<=n;i++)num[sccno[i]]=sz[sccno[i]]+=getint();
S=getint();int q=getint();
while(q--)ok[sccno[getint()]]=1;
for(int u=1;u<=n;u++)for(int i=fir[u];i;i=nxt[i])
if(sccno[u]!=sccno[to[i]])g[sccno[u]].push_back(sccno[to[i]]),d[sccno[to[i]]]++;
dfs(sccno[S]);for(int i=1;i<=scccnt;i++)num[i]=sz[i]=vis[i]*num[i];
bfs();
long long ans=0;
for(int i=1;i<=scccnt;i++)if(ok[i])ans=max(ans,num[i]);
printf("%lld\n",ans);
}