QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#353951#7103. Red Black TreeMasterGwxWA 625ms36608kbC++142.6kb2024-03-14 19:46:442024-03-14 19:46:45

Judging History

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

  • [2024-03-14 19:46:45]
  • 评测
  • 测评结果:WA
  • 用时:625ms
  • 内存:36608kb
  • [2024-03-14 19:46:44]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
typedef long long ll;
const int N=2e5+5;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const ll INF=1e18+3;
#define DEBUG 
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(x)
#else
#define debug(...) {}
#endif
//debug(n,m);debug("a,b");
//double max:0x7f,0x43 min:0xfe 0x c2  int:0x3f 
//运算符 priority  后缀 前缀 */+- 位移 比较 &^| && || 赋值
int f[20][N];
vector<vector<array<int,2>>> h(N);
void solve()
{
    int n,m,q;
    cin>>n>>m>>q;
    vector<bool> a(n+1);
    for(int i=1;i<=m;i++) {
      int x;
      cin>>x;
      a[x]=1;
    }
    for(int i=1;i<=n;i++) h[i].clear();
  
    for(int i=1;i<n;i++){
      int x,y,d;
      cin>>x>>y>>d;
      h[x].push_back({y,d});
      h[y].push_back({x,d});
    }
    int tot=0;
    vector<int> dfn(2*n+5),id(2*n+5),lg2(2*n+5);
    vector<ll> dis(n+1),di(n+1);
    auto dfs=[&](auto self,int x,int pre)-> void{
      f[0][++tot]=x;
      dfn[x]=tot;    
      for(auto [y,d]:h[x]){
        if(y==pre) {
          if(a[x]) dis[x]=0; else dis[x]=dis[pre]+d;
          di[x]=di[pre]+d;
          continue;
        }
        self(self,y,x);
        f[0][++tot]=x;
      }
      return;
    };
    auto prelca=[&]()->void{
      for(int i=2;i<=2*n;i++) lg2[i]=lg2[i/2]+1;
      for(int i=1;i<=20;i++){
        int r=(1<<(i-1));
        for(int l=1;l<=2*n-r+1;l++){
          f[i][l]=dfn[f[i-1][l]]>dfn[f[i-1][r+l]]?f[i-1][r+l]:f[i-1][l];
        }
      }
    };
    auto find_lca=[&](int x,int y)->int {
      if(x==y) return x;
      x=dfn[x],y=dfn[y];
      if(x>y) swap(x,y);
      int lg=lg2[y-x+1];
      return dfn[f[lg][x]]>dfn[f[lg][y-(1<<lg)+1]]?f[lg][y-(1<<lg)+1]:f[lg][x];
    };
    dfs(dfs,1,0);
    //cout<<tot<<endl;
    prelca();
    //for(int i=1;i<=23;i++) cout<<f[0][i]<<" "; cout<<endl;
    //for(int i=1;i<=23;i++) cout<<dfn[f[0][i]]<<" "; cout<<endl;
    //cout<<find_lca(4,12)<<endl;
    while(q--){
      int c;
      cin>>c;
      vector<int> qe(c+5);
      for(int i=0;i<c;i++) cin>>qe[i];
      sort(qe.begin(),qe.end(),[&](int x,int y){
        return dis[x]>dis[y];
      });
      ll ans=INF;
      ll dt=di[qe[0]],dq=qe[0];
      for(int i=0;i<c;i++){
        dq=find_lca(dq,qe[i]);
        dt=max(dt,di[qe[i]]);
        ans=min(ans,max(dt-di[dq],dis[qe[i+1]]));
      }
      cout<<ans<<"\n";
    }
    return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  #if 1 
        int T; cin>>T; while(T--) solve();
    #else 
        solve();
    #endif 
  return 0; 
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 7792kb

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

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:

81585014
81585014
0
317070839
317070839
255904892
308184846
754080476
754080476
754080476
419236908
267693594
28658321
159828095
268784451
154318137
0
358587147
127591837
289748355
357799995
343216841
265085963
134607965
543762696
543762696
532407411
101439762
181713164
181713164
181713164
181713164...

result:

wrong answer 1st lines differ - expected: '148616264', found: '81585014'