QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#175464#7103. Red Black Tree_SheepsheepWA 7ms57364kbC++142.3kb2023-09-10 18:21:512023-09-10 18:21:52

Judging History

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

  • [2023-09-10 18:21:52]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:57364kb
  • [2023-09-10 18:21:51]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define double long double
const ll N = 2e6+9 ;
const ll INF = 1e18 ;
ll gcd( ll a , ll b ){ return a == 0 ? b : gcd(b%a,a) ; }
int n , m , q ;
vector<pair<int,ll>>g[ N ] ;
ll cost[ N ] , deep[ N ] , f[ N ][ 30 ] , dis[ N ] ;
bool cmp( int a , int b ){
    return cost[ a ] > cost[ b ] ;
}
void dfs( ll x , ll fa ){
    f[ x ][ 0 ] = fa ;

    for( auto u : g[x] ){
        if( u.first == fa ) continue ;
        cost[ u.first ] = min( cost[u.first] , cost[ x ] + u.second ) ;
        deep[ u.first ] = deep[ x ] + u.second ;
        dis[ u.first ] = dis[ x ] + 1 ;
        dfs( u.first , x ) ;
    }

    for( int i = 1 ; i <= 25 ; i ++ ) f[ x ][ i ] = f[ f[x][i-1] ][i-1] ;
}
int LCA( int x , int y ){
    if( x == y ) return x ;
    if( dis[ x ] < dis[ y ] ) swap( x , y ) ;
    for( int i = 25 ; i >= 0 ; i -- ) if( dis[ f[x][i] ] >= dis[ y ] ){
        x = f[x][i] ;
    }
    if( x == y ) return x ;
    for( int i = 25 ; i >= 0 ; i -- ) if( f[x][i] != f[y][i] ){
        x = f[x][i] ; y = f[y][i] ;
    }
    return f[x][0] ;
}
void solve(){
    cin >> n >> m >> q ; //点,红色点数,询问次数

    for( int i = 1 ; i <= n ; i ++ ){
        g[i].clear() ;
        deep[ i ] = dis[i] = 0 ; cost[ i ] = INF ;
    }

    for( int i = 1 , x ; i <= m ; i ++ ){
        cin >> x ; cost[ x ] = 0 ;
    }
    for( int i = 1 ; i < n ; i ++ ){
        ll x , y , w ; cin >> x >> y >> w ;
        g[ x ].push_back( {y,w} ) ;
        g[ y ].push_back( {x,w} ) ;
    }

    dfs( 1 , 0 ) ;

    while( q-- ){
        ll k ; cin >> k ;

        vector<ll>v(k+1) ;
        for( int i = 1 ; i <= k ; i ++ ) cin >> v[ i ] ;
        sort( v.begin()+1 , v.end() , cmp ) ;

        ll lca = v[1] , ans = cost[ v[1] ] , maxdeep = deep[ v[1] ] ;

        for( int i = 1 ; i <= k ; i ++ ){

            lca = LCA(lca,v[i]) ;
            maxdeep = max( maxdeep , deep[i] ) ;

            ll res = max( ((i!=k)?cost[ v[i+1] ]:0) , maxdeep-deep[lca] ) ;
            ans = min( ans , res ) ;

        }

        cout << ans << "\n" ;
    }
}
int main(){
    //ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    ll tt = 1 ; //cin >> tt ;
    while( tt-- ) solve() ;
    return 0 ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 7ms
memory: 57364kb

input:

2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3

output:

0
0

result:

wrong answer 1st lines differ - expected: '4', found: '0'