QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#626503#6437. Paimon's TreeCryingTL 0ms3640kbC++143.1kb2024-10-10 09:45:352024-10-10 09:45:35

Judging History

你现在查看的是最新测评结果

  • [2024-10-10 09:45:35]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3640kb
  • [2024-10-10 09:45:35]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef array<int,4> qr; //(pre,L,R,nxt)
typedef long long ll;
const ll N = 160,INF = 1e18;
template<typename T>void tomin(T& x,T y){x = min(x,y);}
template<typename T>void tomax(T& x,T y){x = max(x,y);}
//
int T,n,a[N],dis[N][N]; ll ans;
vector<int> e[N];

int arr[N],tot;
int find(int u,int fa,int r){
    if(u==r){
        arr[++tot] = u;
        return 1;
    }
    for(auto v : e[u])if(v != fa && find(v,u,r)){
        arr[++tot] = u;
        return 1;
    }
    return 0;
}
int vis[N],cnt;
void pdfs(int u,int fa){
    for(auto v : e[u])if(v != fa && !vis[v]){
        cnt++;
        pdfs(v,u);
    }
}

map<qr,int> _R;
map<qr,ll> dp[N];

void solve(){
    cin>>n; _R.clear(); ans = 0; for(int i=0;i<=n;i++)dp[i].clear();
    for(int i=1;i<=n;i++)cin>>a[i];

    for(int i=1;i<=n+1;i++)e[i].clear();
    for(int i=1,u,v;i<=n;i++){
        cin>>u>>v; 
        e[u].push_back(v); e[v].push_back(u);
    }
    //预处理
    for(int x=1;x<=n+1;x++)for(int y=1;y<=n+1;y++){
        tot = 0; find(x,0,y); reverse(arr+1,arr+1+tot); dis[x][y] = tot-1;
        fill(vis+1,vis+2+n,0); for(int i=1;i<=tot;i++)vis[arr[i]] = 1;
        vector<int> ex,ey;
        for(auto z : e[x])if(!vis[z])ex.push_back(z);
        for(auto z : e[y])if(!vis[z])ey.push_back(z);
        if(ex.empty() || (x==y && ex.size() == 1))ex.push_back(0);
        if(ey.empty())ey.push_back(0);

        for(auto pre : ex)for(auto suf : ey)if(pre && pre == suf)continue;else{
            if(vis[pre] || vis[suf])continue;
            vis[pre] = vis[suf] = 1; cnt = 0;
            for(int i=1;i<=tot;i++)pdfs(arr[i],0);
            qr I = {pre,x,y,suf};
            _R[I] = cnt;
            if(x==y)dp[0][I] = 0;
            vis[pre] = vis[suf] = 0;
        }
    }
    //
    for(int i=0;i<n;i++){
        for(auto [I,w] : dp[i]){ //转移
            int x = I[1],y = I[2],pre = I[0],nxt = I[3];
            int r = _R[I] - (i - dis[x][y]);
            assert(r >= 0);
            if(r > 0){ 
                tomax(dp[i+1][I],w);
            }
            if(pre){
                int flag = 0;
                for(auto z : e[pre])if(z != x){
                    flag = 1;
                    qr nI = {z,pre,y,nxt};
                    tomax(dp[i+1][nI],w + a[i+1]);
                }
                if(!flag){
                    qr nI = {0,pre,y,nxt};
                    tomax(dp[i+1][nI],w + a[i+1]);
                }
            }
            if(nxt){
                int flag = 0;
                for(auto z : e[nxt])if(z != y){
                    flag = 1;
                    qr nI = {pre,x,nxt,z};
                    tomax(dp[i+1][nI],w + a[i+1]);
                }
                if(!flag){
                    qr nI = {pre,x,nxt,0};
                    tomax(dp[i+1][nI],w + a[i+1]);
                }
            }
        }
    }
    for(auto [I,w] : dp[n])tomax(ans,w);

    cout<<ans<<endl;
}

int main(){
    //freopen("length.in","r",stdin);
    //freopen("length.out","w",stdout);

    cin>>T;
    while(T--)solve();

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3640kb

input:

2
5
1 7 3 5 4
1 3
2 3
3 4
4 5
4 6
1
1000000000
1 2

output:

16
1000000000

result:

ok 2 number(s): "16 1000000000"

Test #2:

score: -100
Time Limit Exceeded

input:

5000
19
481199252 336470888 634074578 642802746 740396295 773386884 579721198 396628655 503722503 971207868 202647942 2087506 268792718 46761498 443917727 16843338 125908043 691952768 717268783
9 4
4 18
12 9
10 9
4 1
6 12
2 18
9 13
6 14
4 8
2 3
10 17
2 20
19 20
5 1
12 15
15 16
4 7
17 11
4
240982681 ...

output:

5750811120
1896999359
4208559611
4140156713
5361004844
1875024569
3690026656
3702623113
3412485417
7807375141
5341435147
2355946110
3090496776
5626636202
4729664767
2207592767
572868833
4759005973
2944749369
2538044586
3083947956
5757497518
1421427135
3971435093
1197051728
396588615
251138097
221986...

result: