Codeforces 1220E. Tourism


傳送門

這是一道英語題,首先要讀懂題目:

$\text{Alex believes that his trip will be interesting only if he will not use any road twice in a row.}$

這句話意思是不會連續走一條路,但是同一條路是可以走多次的

所以對於一個邊雙聯通分量,是可以全部走一遍並可以從聯通分量里的任意一個點離開的

所以就可以直接縮點,然后就變成樹上問題

發現對於樹上的點,如果它的大小為 $1$ 並且它的兒子都沒法返回 ,那么到達它以后就沒法返回,反之一定可以返回(自己內部繞一圈或者走到兒子再回來即可)

考慮樹上搞搞 $dp$,設 $f[x]$ 表示走完 $x$ 的子樹以后並能返回 $x$ 的父親時得到的最大價值,$g[x]$ 表示走完 $x$ 的子樹不返回的最大價值

那么對於一個可以返回父親的的子節點 $v$ ,有轉移:$f[x]+=f[v]$

對於 $g[x]$ ,比較復雜,首先 $g[x]$ 可以是 $x$ 走完所有可以返回的兒子,最后再走到一個不能返回的兒子:$g[x]=f[x]+g[v]$

也可以是走到某個雖然可以返回,但是沒必要返回的兒子里面不返回(因為兒子再往下走到不能返回的節點最終價值可能更大):$g[x]=g[x]-f[v]+g[v]$,這里減去 $f[v]$ 是因為之前加上了

然后最后答案就是 $g[root]$

 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=4e5+7;
int n,m,a[N];
int fir[N],from[N<<1],to[N<<1],cntt;
inline void add(int a,int b) { from[++cntt]=fir[a]; fir[a]=cntt; to[cntt]=b; }
int dfn[N],low[N],bel[N],st[N],sz[N],Top,cnt,tot;
void Tarjan(int x,int fa)
{
    dfn[x]=low[x]=++cnt; st[++Top]=x;
    for(int i=fir[x];i;i=from[i])
    {
        int &v=to[i]; if(v==fa) continue;
        if(!dfn[v]) Tarjan(v,x),low[x]=min(low[x],low[v]);
        else if(!bel[v]) low[x]=min(low[x],dfn[v]);
    }
    if(low[x]!=dfn[x]) return;
    tot++; while(st[Top]!=x) bel[st[Top--]]=tot;
    bel[st[Top--]]=tot;
}
struct edge {
    int u,v;
    edge (int _u=0,int _v=0) { u=_u,v=_v; }
    inline bool operator < (const edge &tmp) const {
        return u!=tmp.u ? u<tmp.u : v<tmp.v;
    }
    inline bool operator == (const edge &tmp) const {
        return u==tmp.u&&v==tmp.v;
    }
};
vector <edge> tmp;
vector <int> V[N];
ll val[N];
void build()
{
    for(int i=1;i<=n;i++) val[bel[i]]+=a[i],sz[bel[i]]++;
    for(int i=1;i<=n;i++)
        for(int j=fir[i];j;j=from[j])
        {
            int &v=to[j]; if(bel[i]==bel[v]) continue;
            tmp.push_back(edge(bel[i],bel[v]));
        }
    sort(tmp.begin(),tmp.end());
    tmp.resize( unique(tmp.begin(), tmp.end()) - tmp.begin() );
    for(auto E: tmp) V[E.u].push_back(E.v);
}
ll f[N],g[N];
bool ret[N];
void dfs(int x,int fa)
{
    f[x]=val[x]; if(sz[x]>1) ret[x]=1;
    ll mx=0;
    for(auto v:V[x])
    {
        if(v==fa) continue;
        dfs(v,x);
        if(ret[v]) f[x]+=f[v],ret[x]=1;
        else mx=max(mx,g[v]);
    }
    g[x]=f[x]+mx;
    for(auto v:V[x])
        if(v!=fa&&ret[v])
            g[x]=max(g[x], f[x]-f[v]+g[v] );
}
int main()
{
    n=read(),m=read(); int x,y;
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=m;i++)
    {
        x=read(),y=read();
        add(x,y); add(y,x);
    }
    int s=read();
    for(int i=1;i<=n;i++) if(!dfn[i]) Tarjan(i,0);
    build();
    dfs(bel[s],0);
    printf("%lld\n",g[bel[s]]);
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM