QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#376717#2169. 'S No Probleminstallb#WA 1ms5872kbC++201.7kb2024-04-04 15:43:512024-04-04 15:43:52

Judging History

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

  • [2024-04-04 15:43:52]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5872kb
  • [2024-04-04 15:43:51]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define M 100005
using pii = pair<int,int>;
vector<pii> edge[M];
int n;
int Mx[M],Se[M],D[M],Tr[M],Fr[M];
void dfs(int F,int now){
    for(auto &[to,w]:edge[now])if(to!=F){
        dfs(now,to);
        if(w+Mx[to]>Mx[now])
            Fr[now]=Tr[now],Tr[now]=Se[now],Se[now]=Mx[now],Mx[now]=Mx[to]+w;
        else if(w+Mx[to] > Se[now])
            Fr[now]=Tr[now],Tr[now]=Se[now],Se[now]=w+Mx[to];
        else if(w+Mx[to] > Tr[now])
            Fr[now]=Tr[now],Tr[now]=w+Mx[to];
        else Fr[now]=max(Fr[now],w+Mx[to]);
        D[now] = max(D[now],D[to]);
    }
    D[now] = max(D[now],Mx[now] + Se[now]);
}
int ans;
void redfs(int F,int now,int d,int mx){
    ans = max(ans, mx+Mx[now]+Se[now]+Tr[now]);
    ans = max(ans, Fr[now]+Mx[now]+Se[now]+Tr[now]);

    if(F)ans = max(ans, d + D[now]);
    int mxd=0,sed=0;
    
    for(auto &[to,w]:edge[now])if(to!=F){
        if(D[to] > mxd)sed=mxd,mxd=D[to];
        else sed=max(sed,D[to]);
    }
    
    for(auto &[to,w]:edge[now])if(to!=F){
        int dd = mxd == D[to]?sed:mxd;
        dd = max(dd,d);
        dd = max(dd,(Mx[to]+w==Mx[now]?Se[now]:Mx[now]) + mx);

        if(Mx[to]+w==Mx[now])dd = max(dd,Se[now]+Tr[now]);
        else dd = max(dd,Mx[now]+Se[now]);

        redfs(now,to,dd,max(mx,Mx[to]+w==Mx[now]?Se[now]:Mx[now])+w);
    }
}
int main(){
    cin>>n;
    int tt=0;
    for(int a,b,d,i=1;i<n;i++){
        scanf("%d%d%d",&a,&b,&d);
        edge[a].push_back(pii(b,d));
        edge[b].push_back(pii(a,d));
        tt+=d<<1;
    }
    dfs(0,1); 
    redfs(0,1,0,0);
    // cout<<tt<<' '<<ans<<endl;
    cout<<tt-ans<<endl;
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 5748kb

input:

5
1 2 1
1 3 2
1 4 3
1 5 4

output:

10

result:

ok single line: '10'

Test #2:

score: 0
Accepted
time: 1ms
memory: 5620kb

input:

6
6 2 1
4 6 1
3 1 1
1 5 1
1 6 10

output:

15

result:

ok single line: '15'

Test #3:

score: 0
Accepted
time: 0ms
memory: 5724kb

input:

6
1 3 1
3 2 1
3 4 10
5 4 1
4 6 1

output:

15

result:

ok single line: '15'

Test #4:

score: 0
Accepted
time: 1ms
memory: 5808kb

input:

6
1 3 1
2 3 1
3 5 1
4 5 1
5 6 1

output:

6

result:

ok single line: '6'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3776kb

input:

6
1 2 5
3 2 3
2 4 1
4 5 2
4 6 4

output:

16

result:

ok single line: '16'

Test #6:

score: 0
Accepted
time: 1ms
memory: 5828kb

input:

6
1 6 8
2 6 2
3 6 3
4 6 5
5 6 1

output:

20

result:

ok single line: '20'

Test #7:

score: 0
Accepted
time: 1ms
memory: 5672kb

input:

7
6 4 60
4 2 463
6 7 165
6 3 81
6 1 30
4 5 214

output:

1103

result:

ok single line: '1103'

Test #8:

score: -100
Wrong Answer
time: 1ms
memory: 5872kb

input:

7
1 3 463
1 5 214
7 2 165
7 4 81
7 1 60
7 6 30

output:

1092

result:

wrong answer 1st lines differ - expected: '1103', found: '1092'