[BZOJ1179][APIO2009][Tarjan][拓扑排序][递推]Atm

[Problem Description]


[Algorithm]
非递归Tarjan,拓扑排序,递推
[Analysis]
首先用强连通分量对图进行缩点。很明显只要能到强连通分量中的一个点,该强连通分量里面的所有点都可以取得到。缩点后图就变成了一个有向无环图,以拓扑排序的顺序递推即可
[Code]
/**************************************************************
    Problem: 1179
    User: gaotianyu1350
    Language: C++
    Result: Accepted
    Time:6848 ms
    Memory:33996 kb
****************************************************************/
 
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
 
const int MAXN = 500100;
 
int father[MAXN] = {0};
int dfsTime[MAXN] = {0}, linkTime[MAXN] = {0}, cct[MAXN] = {0}, st[MAXN] = {0};
bool statue[MAXN] = {0};
int nowTime = 0, cnt = 0, top = 0;
int point[MAXN] = {0}, next[MAXN] = {0}, v[MAXN] = {0}, cur[MAXN] = {0}, tot = 0;
int money[MAXN] = {0};
bool cctbar[MAXN] = {0}, bar[MAXN] = {0};
int cctmoney[MAXN] = {0}, f[MAXN] = {0};
int x[MAXN], y[MAXN], n, m, start, p;
int rudu[MAXN] = {0}, able[MAXN] = {0};
 
inline void memorycheck()
{
    int memory = sizeof(dfsTime) + sizeof(linkTime) + sizeof(cct) + sizeof(st) + sizeof(point)
               + sizeof(next) + sizeof(v) + sizeof(money) + sizeof(cctbar) + sizeof(bar)
               + sizeof(cctmoney) + sizeof(f) + sizeof(x) + sizeof(y);
    printf("%d\n", memory / 1024 / 1024);
}
 
inline void clear()
{
    tot = 0;
    memset(point, 0, sizeof(point));
    memset(next, 0, sizeof(next));
}
 
inline void addedge(int x, int y)
{
    tot++;
    next[tot] = point[x];
    point[x] = tot;
    v[tot] = y;
}
 
inline void dfs(int now)
{
    while (now)
    {
        if (!dfsTime[now])
        {
            dfsTime[now] = linkTime[now] = ++nowTime;
            st[++top] = now;
            cur[now] = point[now];
        }
        else
        {
            if (statue[now])
            {
                linkTime[now] = min(linkTime[now], linkTime[v[cur[now]]]);
                statue[now] =false;
            }
            cur[now] = next[cur[now]];
        }
         
        if (!cur[now])
        {
            if (dfsTime[now] == linkTime[now])
            {
                cnt++;
                while (1)
                {
                    cct[st[top--]] = cnt;
                    if (st[top + 1] == now) break;
                }
            }
            now = father[now];
            continue;
        }
         
        if (!dfsTime[v[cur[now]]])
        {
            statue[now] = true;
            father[v[cur[now]]] = now;
            now = v[cur[now]];
            continue;
        }
        else
            if (!cct[v[cur[now]]])
                linkTime[now] = min(linkTime[now], dfsTime[v[cur[now]]]);
    }
}
 
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++)
    {
        scanf("%d%d", &x[i], &y[i]);
        addedge(x[i], y[i]);
    }
    for (int i = 1; i <= n; i++) scanf("%d", &money[i]);
    scanf("%d%d", &start, &p);
    for (int i = 1; i <= p; i++)
    {
        int x;
        scanf("%d", &x);
        bar[x] = true;
    }
    for (int i = 1; i <= n; i++)
        if (!dfsTime[i])
            dfs(i);
    clear();
    for (int i = 1; i <= n; i++)
    {
        cctbar[cct[i]] |= bar[i];
        cctmoney[cct[i]] += money[i];
    }
    for (int i = 1; i <= m; i++)
        if (cct[x[i]] != cct[y[i]])
        {    
            addedge(cct[x[i]], cct[y[i]]);
            rudu[cct[y[i]]]++;
        }
    queue<int> q;
    while (!q.empty()) q.pop();
    for (int i = 1; i <= cnt; i++)
        if (!rudu[i])
            q.push(i);
    able[cct[start]] = 1;
    int ans = 0;
    while (!q.empty())
    {
        int now = q.front(); q.pop();
        f[now] += cctmoney[now] * able[now];
        if (cctbar[now])
            ans = max(ans, f[now]);
        for (int temp = point[now]; temp; temp = next[temp])
        {
            rudu[v[temp]]--;
            if (!rudu[v[temp]]) q.push(v[temp]);
            f[v[temp]] = max(f[v[temp]], f[now]);
            able[v[temp]] |= able[now];
        }
    }
    printf("%d\n", ans);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值