Father Christmas flymouse【Tarjan缩点+DFS】

版权声明:https://blog.csdn.net/qq_41730082 https://blog.csdn.net/qq_41730082/article/details/86931724

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;
}

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/86931724