QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#158112#7103. Red Black Treeucup-team520#AC ✓1060ms71340kbC++205.0kb2023-09-02 16:13:362023-09-02 16:13:37

Judging History

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

  • [2023-09-02 16:13:37]
  • 评测
  • 测评结果:AC
  • 用时:1060ms
  • 内存:71340kb
  • [2023-09-02 16:13:36]
  • 提交

answer


#include <bits/stdc++.h>   
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;   
using namespace std;  
#define ll long long
const ll INF_ADD=1e18;
#define pb push_back                   
#define mp make_pair          
#define nline "\n"                             
#define f first                                            
#define s second                                             
#define pll pair<ll,ll>   
#define all(x) x.begin(),x.end()       
#define vl vector<ll>             
#define vvl vector<vector<ll>>          
#define vvvl vector<vector<vector<ll>>>          
#ifndef ONLINE_JUDGE    
#define debug(x) cerr<<#x<<" "; _print(x); cerr<<nline;  
#else
#define debug(x);      
#endif  
void _print(int x){cerr<<x;}       
void _print(ll x){cerr<<x;}  
void _print(char x){cerr<<x;}     
void _print(string x){cerr<<x;}    
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());   
template<class T,class V> void _print(pair<T,V> p) {cerr<<"{"; _print(p.first);cerr<<","; _print(p.second);cerr<<"}";}
template<class T>void _print(vector<T> v) {cerr<<" [ "; for (T i:v){_print(i);cerr<<" ";}cerr<<"]";}
template<class T>void _print(set<T> v) {cerr<<" [ "; for (T i:v){_print(i); cerr<<" ";}cerr<<"]";}
template<class T>void _print(multiset<T> v) {cerr<< " [ "; for (T i:v){_print(i);cerr<<" ";}cerr<<"]";}
template<class T,class V>void _print(map<T, V> v) {cerr<<" [ "; for(auto i:v) {_print(i);cerr<<" ";} cerr<<"]";} 
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
typedef tree<pair<ll,ll>, null_type, less<pair<ll,ll>>, rb_tree_tag, tree_order_statistics_node_update> ordered_pset;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
const ll MOD=998244353;
const ll MAX=200200;   
vector<pair<ll,ll>> adj[MAX];
ll now=0,till=20;
vector<ll> tin(MAX,0),tout(MAX,0),depth(MAX,0);
vector<vector<ll>> jump(MAX,vector<ll>(till+1,0));
vector<ll> red(MAX,0),dist(MAX,0);
map<pair<ll,ll>,ll> weight;
vector<ll> track;
vector<ll> parent(MAX,0);
void dfs(ll cur,ll par){
    parent[cur]=par;
    if(red[cur]){
        track.push_back(depth[cur]);
    }
    dist[cur]=depth[cur]-track.back();
    jump[cur][0]=par;
    for(ll i=1;i<=till;i++)
        jump[cur][i]=jump[jump[cur][i-1]][i-1];
    tin[cur]=++now;
    for(auto chld:adj[cur]){
        if(chld.f!=par){
            depth[chld.f]=depth[cur]+chld.s;
            dfs(chld.f,cur);
        }
    }
    tout[cur]=++now;
    if(red[cur]){
        track.pop_back();
    }
} 
bool is_ancestor(ll a,ll b){
    if((tin[a]<=tin[b])&&(tout[a]>=tout[b]))
        return 1;
    return 0;
} 
ll lca(ll a,ll b){
    if(is_ancestor(a,b))
        return a;
    for(ll i=till;i>=0;i--){
        if(!is_ancestor(jump[a][i],b))
            a=jump[a][i];
    }
    return jump[a][0];
}  
void solve(){    
    ll n,m,q; cin>>n>>m>>q;
    for(ll i=1;i<=n;i++){
        red[i]=0;
    }
    for(ll i=1;i<=m;i++){
        ll u; cin>>u;
        red[u]=1;
    }
    weight.clear();  
    for(ll i=1;i<n;i++){
        ll u,v; cin>>u>>v;
        ll w; cin>>w;
        adj[u].push_back({v,w});
        adj[v].push_back({u,w});
    }
    track={-INF_ADD};
    dfs(1,1);
    while(q--){
        ll k; cin>>k;
        vector<pair<ll,ll>> consider;  
        while(k--){
            ll x; cin>>x;
            consider.push_back({dist[x],x});
        }
        sort(all(consider));
        reverse(all(consider));
        consider.push_back({-1,-1});
        consider.push_back({-1,-1});
        ll ans=consider[0].f;
        ll lca_node=-1,nax=-INF_ADD;
        ll len=consider.size();
        for(ll i=0;i<len;i++){
            auto l=consider[i],r=consider[i+1];
            if(l.s==-1){
                break;
            }
            if(lca_node==-1){
                lca_node=l.s;
            }
            lca_node=lca(lca_node,l.s);
            nax=max(nax,depth[l.s]);
            if(lca_node!=-1){
                ans=min(ans,max(r.f,nax-depth[lca_node])); 
            }
        }
        cout<<ans<<nline;
    }
    for(ll i=1;i<=n;i++){
        adj[i].clear();
    }
    return;      
}                                                                   
int main()                                                                                                           
{              
    ios_base::sync_with_stdio(false);                               
    cin.tie(NULL);                                       
    ll test_cases=1;               
    cin>>test_cases;
    while(test_cases--){
        solve();
    } 
    cout<<fixed<<setprecision(10);
    cerr<<"Time:"<<1000*((double long)clock())/(double long)CLOCKS_PER_SEC<<"ms\n"; 
} 

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 14ms
memory: 56608kb

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:

4
5
3
8
0
0
0

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 1060ms
memory: 71340kb

input:

522
26 1 3
1
1 4 276455
18 6 49344056
18 25 58172365
19 9 12014251
2 1 15079181
17 1 50011746
8 9 2413085
23 24 23767115
22 2 26151339
26 21 50183935
17 14 16892041
9 26 53389093
1 20 62299200
24 18 56114328
11 2 50160143
6 26 14430542
16 7 32574577
3 16 59227555
3 15 8795685
4 12 5801074
5 20 57457...

output:

148616264
148616264
0
319801028
319801028
255904892
317070839
1265145897
1265145897
1072765445
667742619
455103436
285643094
285643094
285643094
317919339
0
785245841
691421476
605409472
479058444
371688030
303203698
493383271
919185207
910180170
919185207
121535083
181713164
181713164
181713164
181...

result:

ok 577632 lines

Extra Test:

score: 0
Extra Test Passed