QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#354028#7103. Red Black TreeMasterGwxWA 686ms36504kbC++143.2kb2024-03-14 20:41:562024-03-14 20:41:56

Judging History

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

  • [2024-03-14 20:41:56]
  • 评测
  • 测评结果:WA
  • 用时:686ms
  • 内存:36504kb
  • [2024-03-14 20:41:56]
  • 提交

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);
int lg2[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+15),id(2*n+15);
    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) {
          continue;
        }
        di[y] = di[x] + d;
        if (a[y]) dis[y] = 0;
        else dis[y] = dis[x] + d;
        self(self,y,x);
        f[0][++tot]=x;
      }
      return;
    };
    auto prelca=[&]()->void{
      for(int i=1;i<20;i++){
        int r=(1ll<<(i-1));
        for(int l=1;l<=tot-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-(1ll<<lg)+1]]?f[lg][y-(1ll<<lg)+1]:f[lg][x];
    };
    dfs(dfs,1,0);
    prelca();

    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";
        vector<int> vec;
        int t; cin>>t;
        while (t--) {
            int x; cin>>x;
            vec.push_back(x);
        }
        vec.push_back(0);
        sort(vec.begin(), vec.end(), [&](int a, int b) {
            return dis[a] > dis[b];
        });

        int anc = vec[0];
        long long mx = dis[vec[0]], ans = dis[vec[1]];
        // 枚举新的红点要覆盖几个节点
        for (int i = 1; i + 1 < (int)vec.size(); i++) {
            anc = find_lca(anc, vec[i]);
            mx = max(mx, dis[vec[i]]);
            ans = min(ans, max(mx - dis[anc], dis[vec[i + 1]]));
        }
        cout<<ans<<"\n";
    }
    return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  for(int i=2;i<=N;i++) lg2[i]=lg2[i/2]+1;

  #if 1 
        int T; cin>>T; while(T--) solve();
    #else 
        solve();
    #endif 
  return 0; 
}

詳細信息

Test #1:

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

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: 686ms
memory: 36504kb

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:

wrong answer 691st lines differ - expected: '549790714', found: '374667774'