QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#157787#7103. Red Black Treeucup-team1516#AC ✓1271ms26496kbC++173.9kb2023-09-02 15:50:522023-09-02 15:50:52

Judging History

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

  • [2023-09-02 15:50:52]
  • 评测
  • 测评结果:AC
  • 用时:1271ms
  • 内存:26496kb
  • [2023-09-02 15:50:52]
  • 提交

answer

#include <string.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

int p[100100],s[100100];
int dp[100100][20];

long long dist[100100],f[100100];

void dfs(int n,vector<vector<pair<int,int>>>&ki) {
    for(auto i:ki[n]) {
        if(i.first == p[n]) {
            continue;
        }
        p[i.first] = n;
        dist[i.first] = dist[n]+i.second;
        s[i.first] = s[n]+1;
        if(f[i.first] == -1) f[i.first] = f[n]+i.second;
        dfs(i.first,ki);
    }
}

int lca(int u,int v) {
    if(s[u] > s[v]) swap(u,v);
    for(int i = 19; i >= 0; i--) {
        if(1 & ((s[v]-s[u]) >> i)) {
            v = dp[v][i];
        }
    }
    if(u == v) return u;
    for(int i = 19; i >= 0; i--) {
        if(dp[u][i] != dp[v][i]) {
            u = dp[u][i];
            v = dp[v][i];
        }
    }
    return dp[u][0];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while(t--) {
        int n,m,q;
        cin >> n >> m >> q;
        for(int i = 0; i < n; i++) {
            f[i] = -1;
        }
        vector<int>r(m);
        for(int i = 0; i < m; i++) {
            cin >> r[i];
            r[i]--;
            f[r[i]] = 0;
        }
        vector<vector<pair<int,int>>>ki(n);
        for(int i = 0; i < n-1; i++) {
            int u,v,w;
            cin >> u >> v >> w;
            u--;
            v--;
            ki[u].push_back({v,w});
            ki[v].push_back({u,w});
        }
        p[0] = -1,dist[0] = 0;
        dfs(0,ki);
        for(int i = 0; i < n; i++) {
            dp[i][0] = p[i];
        }
        for(int i = 1; i < 20; i++) {
            for(int j = 0; j < n; j++) {
                if(dp[j][i-1] == -1) {
                    dp[j][i] = -1;
                    continue;
                }
                dp[j][i] = dp[dp[j][i-1]][i-1];
            }
        }
        while(q--) {
            int k;
            cin >> k;
            vector<int>v(k);
            for(int i = 0; i < k; i++) {
                cin >> v[i];
                v[i]--;
            }
            vector<pair<long long,int>>tmp;
            for(int i = 0; i < k; i++) {
                tmp.push_back({f[v[i]],v[i]});
            }
            sort(tmp.begin(),tmp.end());
            vector<int>rca(k);
            rca[k-1] = tmp[k-1].second;
            for(int i = k-2; i >= 0; i--) {
                rca[i] = lca(rca[i+1],tmp[i].second);
            }
            long long L = -1,R = tmp[k-1].first;
            while(L+1 < R) {
                long long mid = (L+R)/2;
                int l2 = -1,r2 = k-1;
                while(l2+1 < r2) {
                    int mid2 = (l2+r2)/2;
                    if(tmp[mid2].first > mid) {
                        r2 = mid2;
                    }
                    else {
                        l2 = mid2;
                    }
                }
                bool fl = true;
                for(int i:v) {
                    if(f[i] <= mid) continue;
                    if(dist[i]-dist[rca[r2]] > mid) {
                        fl = false;
                        break;
                    }
                }
                if(fl) {
                    R = mid;
                }
                else {
                    L = mid;
                }
            }
            cout << R << "\n";
        }
    }
}

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

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 5748kb

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: 1271ms
memory: 26496kb

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