QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#60666#2169. 'S No Problemcaptured#WA 4ms10976kbC++172.4kb2022-11-05 23:20:352022-11-05 23:20:37

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-05 23:20:37]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:10976kb
  • [2022-11-05 23:20:35]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;


#define endl "\n"
#define LL long long
#define FOR(i,n) for(int i=0;i<n;i++)
#define deb(x) cerr<<#x<<" : "<<x<<" "
#define dnl cerr<<endl

typedef pair<int,int> pii;

const int maxn = 2e5+7;


vector<pii> g[maxn];

int mark[maxn];
int D[maxn];
int parent[maxn];
void dfs(int u,int p,int cost){
    D[u] = cost;
    parent[u] = p;
    for(pii x:g[u]){
        if(x.first!=p)dfs(x.first,u,cost+x.second);
    }
}


vector<int> cnodes;
int D2[maxn];
void dfs2(int u,int p,int cost,int root){
    D2[u] = cost;
    cnodes.push_back(u);
    for(pii x:g[u]){
        if(x.first!=p){
            if( !mark[x.first] or x.first==root )
            dfs2(x.first,u,cost+x.second,root);
        }
    }
}

void solve(int cs){
    int n;
    LL total = 0;
    cin>>n;
    for(int i=2;i<=n;i++){
        int u,v,w;cin>>u>>v>>w;
        g[u].push_back({v,w});
        g[v].push_back({u,w});
        total += w;
        total += w;
    }

    dfs(1,-1,0);
    int a=-1,maxi=0;
    for(int i=1;i<=n;i++){
        if(D[i] > maxi){
            maxi = D[i];
            a = i;
        }
    }
    dfs(a,-1,0);
    int b=-1;
    maxi=0;
    for(int i=1;i<=n;i++){
        if(D[i] > maxi){
            maxi = D[i];
            b = i;
        }
    }
    LL diam = maxi;

    vector<int> diameter;
    int z = b;
    while( z != a ){
        diameter.push_back(z);
        z = parent[z];
    }
    diameter.push_back(a);
//    for(int d:diameter){
//        deb(d);
//    }
//    dnl;
    for(int d:diameter){
        mark[d]=1;
    }

    int subdiam = 0;

    for(int d:diameter){
        cnodes.clear();
        dfs2(d,-1,0,d);
        int aa=-1,ma=-1;
        for(int v:cnodes){
            if(D2[v] > ma){
                ma = D2[v];
                aa = v;
            }
        }

//        deb(d);dnl;
//        for(int sd:cnodes){
//            deb(sd);deb(D2[sd]);dnl;
//        }
//        dnl;
        cnodes.clear();
        dfs2(aa,-1,0,d);
        for(int v:cnodes){
            subdiam = max(subdiam,D2[v]);
        }
    }

    LL ans = total - diam - subdiam;
//    deb(total);deb(diam);deb(subdiam);dnl;

    cout<<ans<<endl;
}

/*
ATTAATTA

*/
int main(){

    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

    int t=1;
//    cin>>t;
    for(int cs=1;cs<=t;cs++)solve(cs);
}


详细

Test #1:

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

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

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: 10924kb

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: 4ms
memory: 10016kb

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

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'