Father Christmas flymouse【Tarjan缩点+DFS】

38 篇文章 0 订阅
29 篇文章 0 订阅

After retirement as contestant from WHU ACM Team, flymouse volunteered to do the odds and ends such as cleaning out the computer lab for training as extension of his contribution to the team. When Christmas came, flymouse played Father Christmas to give gifts to the team members. The team members lived in distinct rooms in different buildings on the campus. To save vigor, flymouse decided to choose only one of those rooms as the place to start his journey and follow directed paths to visit one room after another and give out gifts en passant until he could reach no more unvisited rooms.

During the days on the team, flymouse left different impressions on his teammates at the time. Some of them, like LiZhiXu, with whom flymouse shared a lot of candies, would surely sing flymouse’s deeds of generosity, while the others, like snoopy, would never let flymouse off for his idleness. flymouse was able to use some kind of comfort index to quantitize whether better or worse he would feel after hearing the words from the gift recipients (positive for better and negative for worse). When arriving at a room, he chould choose to enter and give out a gift and hear the words from the recipient, or bypass the room in silence. He could arrive at a room more than once but never enter it a second time. He wanted to maximize the the sum of comfort indices accumulated along his journey.

Input

The input contains several test cases. Each test cases start with two integers N and M not exceeding 30 000 and 150 000 respectively on the first line, meaning that there were N team members living in Ndistinct rooms and M direct paths. On the next N lines there are N integers, one on each line, the i-th of which gives the comfort index of the words of the team member in the i-th room. Then follow Mlines, each containing two integers i and j indicating a directed path from the i-th room to the j-th one. Process to end of file.

Output

For each test case, output one line with only the maximized sum of accumulated comfort indices.

Sample Input

2 2
14
21
0 1
1 0

Sample Output

35

Hint

32-bit signed integer type is capable of doing all arithmetic.


  利用Tarjan缩点,变成一幅有向无环图(DAG),然后利用DFS去搜索最大值即可。

  为什么可以直接上DFS,是因为Tarjan缩点之后一定是一个有向无环图,以此为保证才能继续DFS直接搜,并且可以记忆化最大值。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 3e4 + 7;
const int maxM = 1e5 + 5e4 + 7;
int N, M, head[maxN], cnt;
ll val[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxM], path[maxM];
void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
int dfn[maxN], low[maxN], _Index, Stap[maxN], Stop, Bcnt, Belong[maxN];   //tarjan
ll w[maxN];
bool instack[maxN];
vector<int> vt[maxN];
void tarjan(int u)
{
    int v;
    dfn[u] = low[u] = ++_Index;
    instack[u] = true;
    Stap[++Stop] = u;
    for(int i=head[u]; ~i; i=edge[i].nex)
    {
        v = edge[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            if(low[v] < low[u]) low[u] = low[v];
        }
        else if(instack[v] && dfn[v] < low[u]) low[u] = dfn[v];
    }
    if(low[u] == dfn[u])
    {
        Bcnt++;
        do
        {
            v = Stap[Stop--];
            instack[v] = false;
            Belong[v] = Bcnt;
            w[Bcnt] += val[v];
            vt[Bcnt].push_back(v);
        } while (v != u);
    }
}
int top[maxN], num;
void addPath(int u, int v)
{
    path[num] = Eddge(top[u], v);
    top[u] = num++;
}
void link_line()
{
    for(int i=1; i<=Bcnt; i++)
    {
        int len = (int)vt[i].size();
        for(int j=0; j<len; j++)
        {
            int u = vt[i][j];
            for(int k=head[u]; ~k; k=edge[k].nex)
            {
                int v = edge[k].to;
                if(Belong[u] != Belong[v])
                {
                    addPath(Belong[u], Belong[v]);
                }
            }
        }
    }
}
bool read[maxN];
ll dp[maxN];
ll dfs(int u)
{
    if(read[u]) return dp[u];
    read[u] = true;
    ll minn = 0;
    for(int i=top[u]; ~i; i=path[i].nex)
    {
        int v = path[i].to;
        minn = max(minn, dfs(v));
    }
    return dp[u] = w[u] + minn;
}
inline void init()
{
    cnt = num = 0;
    memset(head, -1, sizeof(head));
    memset(top, -1, sizeof(top));
    memset(dfn, 0, sizeof(dfn));
    _Index = Stop = Bcnt = 0;
    memset(instack, false, sizeof(instack));
    memset(w, 0, sizeof(w));
    for(int i=1; i<=N; i++) vt[i].clear();
    memset(read, false, sizeof(read));
    memset(dp, 0, sizeof(dp));
}
int main()
{
    while(scanf("%d%d", &N, &M)!=EOF)
    {
        init();
        for(int i=0; i<N; i++)
        {
            scanf("%lld", &val[i]);
            if(val[i] < 0) val[i] = 0;
        }
        for(int i=1; i<=M; i++)
        {
            int e1, e2; scanf("%d%d", &e1, &e2);
            addEddge(e1, e2);
        }
        for(int i=1; i<=N; i++) if(!dfn[i]) tarjan(i);
        link_line();
        ll ans = 0;
        for(int i=1; i<=Bcnt; i++) ans = max(ans, dfs(i));
        printf("%lld\n", ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值