QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#450422#2169. 'S No Problemevirir#WA 2ms8004kbC++142.9kb2024-06-22 12:57:452024-06-22 12:57:45

Judging History

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

  • [2024-06-22 12:57:45]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:8004kb
  • [2024-06-22 12:57:45]
  • 提交

answer

#pragma GCC optimize("Ofast") 
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXN = 111111;
vector<pii>adj[MAXN];
int dp[MAXN],dp2[MAXN],pdp[MAXN],pdp2[MAXN];
void dfs(int v,int u){
    multiset<int,greater<int>>p;
    for(pii z:adj[v]){
        int x = z.fi;
        int w = z.se;
        if(x == u)continue;
        dfs(x,v);
        dp[v] = max(dp[v],dp[x]+w);
        p.insert(dp[x]+w);
    }
    auto it = p.begin();
    if(it != p.end()){
        dp2[v] += *it;
        it++;
        if(it != p.end())dp2[v] += *it;
    }
    for(pii z:adj[v]){
        int x = z.fi;
        if(x == u)continue;
        dp2[v] = max(dp2[v],dp2[x]);
    }
}
//dp[v] = longest path from v downwards
//dp2[v] = longest path in subtree v

int ans = 0;

void dfs1(int v,int u){
    multiset<int,greater<int>>path; //paths from v
    multiset<int,greater<int>>p;
    if(v==1)path.insert(0);
    p.insert(pdp2[v]);
    for(pii z:adj[v]){
        int x = z.fi;
        int w = z.se;
        if(x == u)path.insert(pdp[v] + w);
        else {
            path.insert(dp[x] + w);
            p.insert(dp2[x]);
        }
    }
    auto it = path.begin();
    int sum = 0;
    //the two paths interest at v
    for(int i=0;i<4;i++){
        if(it == path.end())break;
        sum += *it;
        it++;
    }
    ans = max(ans,sum);
    for(pii z:adj[v]){
        int x = z.fi;
        int w = z.se;
        if(x == u)continue;
        path.erase(path.find(dp[x]+w));
        p.erase(dp2[x]);
        pdp[x] = *path.begin();
        pdp2[x] = *p.begin();
        auto it = path.begin();
        int sum = 0;
        for(int i=0;i<2;i++){
            if(it == path.end())break;
            sum += *it;
            it++;
        }
        pdp2[x] = max(pdp2[x],sum);

        dfs1(x,v);
        path.insert(dp[x]+w);
        p.insert(dp2[x]);
    }
    ans = max({ans,dp2[v],pdp2[u]});
}
int main()
{
    owo
    int n;
    cin>>n;
    int tot = 0;
    for(int i=1;i<n;i++){
        int v,u,d;
        cin>>v>>u>>d;
        tot+=2*d;
        adj[v].pb({u,d});
        adj[u].pb({v,d});
    }
    dfs(1,0);
    dfs1(1,0);
    tot-=ans;
    cout<<tot<<'\n';    

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 6240kb

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: 0ms
memory: 6912kb

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: 2ms
memory: 8004kb

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: 2ms
memory: 7336kb

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: -100
Wrong Answer
time: 0ms
memory: 7040kb

input:

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

output:

17

result:

wrong answer 1st lines differ - expected: '16', found: '17'