Solution to a problem P3627 [[APIO2009] looting plan -

Goo four hours all night

P3627 [APIO2009]

Looting Program (https://www.luogu.org/problemnew/show/P3627)

 



The answer is easy to see that the length of the longest chain of FIG directed (allowing repeated

I'll dp!

FIG ring but may have no aftereffect does not meet the assumptions of dp

I will tarjan!

(You too strong)

In the same point in the strongly connected components will be able to reach each other, after shrinking tarjan point, a strong link each component as a point where the sum value is the value of all the bank's

After the completion point reduction we reconstruct a new map, the original connection side into strongly connected components connected to the two points where the two points (if in the same strongly connected components in? This is not a pipe loop)

Then originally wanted to write bfs seeking longest road ...... read, read, feeling becomes spfa (laughs cry)

Finally, we calculate the starting point of the longest road every strongly connected component, enumerate every point, there is a bar on the judgment updated answers

#include<bits/stdc++.h>
#define ll long long
#define inf 0x7fffffff
using namespace std;
int n,m; 
#define maxn 500009
vector<int> son[maxn];
int c[maxn];
bool bar[maxn];
void add(int x,int y)
{
    son[x].push_back(y);
}
int s,p;
int dfsn[maxn],lowlink[maxn];
int sta[maxn];
int top=0;
int dfs_clock=0; 
 Int scc_cnt = 0 ; // the number of strongly connected components 
int SCC [MAXN]; // each point in the strongly connected component 
int Val [MAXN]; // value of the sum of the strongly connected components of each point is located 
int Q [MAXN];
 int Vy [MAXN]; // for each strongly connected component of the total value of 
void dfs_scc ( int X) // Tarjan !!! 
{ 
    DFSN [X] = lowlink [X] = ++ dfs_clock; 
    STA [ ++ Top] = X;
     for ( int I = 0 ; I <Son [X] .size (); I ++ ) 
    { 
        int now =son[x][i];
        if(!dfsn[now])
        {
            dfs_scc(now);
            lowlink[x]=min(lowlink[x],lowlink[now]);
        }else if(!scc[now])
        {
            lowlink[x]=min(lowlink[x],dfsn[now]);
        }
    }
    if(lowlink[x]==dfsn[x])
    {
        scc_cnt++;
        int ans=0;
        int p=0;
        while(sta[top]!=x)
        {
            q [ ++ P] = STA [Top]; // digging Tip: q is a record for the array, the strongly connected components in the point record, the global variable q must be open, without Memset
             // if each time you define a q in recursion, a stack will burst! ! ! ! Local Honey error occurs, Luo Gu ide no problem! ! 
            = + ANS C [STA [Top]]; 
            SCC [STA [Top -]] = scc_cnt; 
        } 
        Top - ; 
        SCC [X] = scc_cnt; 
        Q [ ++ P] = X; 
        ANS + = C [X] ; 
        Vy [scc_cnt] = ANS;
         for ( int I = . 1 ; I <= P; I ++ ) 
        { 
            Val [Q [I]] = ANS;
        }
    }
}
struct node{
    int x,y;
}e[maxn];;
vector<int> newmap[maxn];
int in[maxn],d[maxn];
struct point{
    int x,step;
};
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        e[i].x=x,e[i].y=y;
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&c[i]);
    }
    scanf("%d%d",&s,&p);
    memset(bar,0,sizeof(bar));
    for(int i=1;i<=p;i++)
    {
        int x;
        scanf("%d",&x);
        bar [X] = . 1; 
    } 
    For ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        IF (! Dfs_scc (I) DFSN [I]); 
    } 
    for ( int I = . 1 ; I <= m; I ++) // one side two endpoints must not be in the same strongly connected components 
    {
         IF (SCC [E [I] .x]! = SCC [E [I] .y]) 
        { 
            newMap [SCC [E [I] .x]]. push_back (SCC [E [I] .y]); 
        } 
    } 
     
    Queue < int > Q; 
    q.push (SCC [S]); 
    D [SCC [S]] = Val [S];
     the while (q.empty (! ))// wide search, no, in fact it is a SPFA 
    {
         int XX = q.front ();
         int X = XX; 
        q.pop (); 
       // the printf ( "X: D% D% \ n-", X , xx.step); 
        for ( int I = 0 ; I <newMap [X] .size (); I ++ ) 
        { 
            int to = newMap [X] [I];
             IF (D [to]> = D [X] Vy + [to]) Continue ; // similar relaxation operation ...... 
            D [to] = max (D [to], D [X] + Vy [to]); 
            q.push (to); 
        } 
    } 
    int = ANS 0 ;
     for ( int i = 1 ; i <= the n-; i ++ ) 
    { 
        IF (bar [i]) // If this point there is a bar, then we look at statistics answer strongly connected component of it in the 
        { 
            ANS = max (ANS, D [SCC [I]]); 
        } 
    } 
    the printf ( " % D \ n- " , ANS);
     return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/lzy-blog/p/11229001.html