QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#218887#7103. Red Black Treeeuphria#TL 0ms3820kbC++203.1kb2023-10-18 20:02:102023-10-18 20:02:10

Judging History

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

  • [2023-10-18 20:02:10]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3820kb
  • [2023-10-18 20:02:10]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define endl '\n'
using ll = long long;
using ull = unsigned long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
const int N = 1e6 + 10;
const int M = 131;
const int INF = 0x3f3f3f3f;
const long long LNF = 0x3f3f3f3f3f3f3f3f;
const long long mod = 1e9 + 7;

void solve(){
    int n, m, q; cin >> n >> m >> q;
    vector<int> bac(n + 1);
    for(int i = 1; i <= m; i++){
        int x; cin >> x;
        bac[x] = 1;
    }
    vector<vector<pair<int, ll>>> g(n + 1);
    for(int i = 1; i < n; i++){
        int a, b, c; cin >> a >> b >> c;
        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }
    int idx = 0;
    vector<vector<int>> father(n + 1, vector<int>(18));
    vector<ll> val(n + 1), dfn(n + 1), deep(n + 1), dis(n + 1);
    function <void(int, int, ll)> dfs = [&] (int u, int fa, ll v){
        val[u] = v;
        dfn[u] = ++idx;
        father[u][0] = fa;
        deep[u] = deep[fa] + 1;
        for(int i = 1; i <= 17; i++) father[u][i] = father[father[u][i - 1]][i - 1];
        for(auto [a, b] : g[u]){
            if(a == fa) continue;
            dis[a] = dis[u] + b;
            dfs(a, u, (bac[a] ? a : v));
        }
    };
    dfs(1, 0, 1);
    function<int(int, int)> LCA = [&] (int a, int b){
        if(deep[a] < deep[b]) swap(a, b);
        for(int i = 17; i >= 0; i--){
            if(deep[father[a][i]] >= deep[b]) a = father[a][i];
        }
        if(a == b) return a;
        for(int i = 17; i >= 0; i--){
            if(father[a][i] != father[b][i]){
                a = father[a][i]; b = father[b][i];
            }
        }
        return father[a][0];
    };
    //for(int i = 1; i <= n; i++) cout << "dfn " << i << ' ' << dfn[i] << '\n';
    while(q--){
        int k; cin >> k;
        vector<int> s(k + 1);
        for(int i = 1; i <= k; i++){
            cin >> s[i];
        }
        function <bool(ll)> check = [&] (ll mid){
            ll mn = idx, mx = 0;
            for(int i = 1; i <= k; i++){
                //cout << "vertex " << s[i] << ' ' << val[s[i]] << ' ' << dis[s[i]] << ' ' << dis[val[s[i]]] << '\n';
                if(dis[s[i]] - dis[val[s[i]]] > mid){
                    mn = min(mn, dfn[s[i]]);
                    mx = max(mx, dfn[s[i]]);
                }
            }
            int lca = LCA(mn, mx);
            //cout << mn << ' ' << mx << ' ' << lca << ' ' << mid << '\n';
            for(int i = 1; i <= k; i++){
                if(dis[s[i]] - dis[val[s[i]]] > mid){
                    if(dis[s[i]] - dis[lca] > mid) return false;
                }
            }
            return true;
        };
        ll l = 0, r = 1e14;
        while(l < r){
            ll mid = (l + r) >> 1;
            if(check(mid)) r = mid;
            else l = mid + 1;
        }
        cout << l << '\n';
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(12);
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
    return 0 - 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Time Limit Exceeded

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
493591722
493591722
476331520
493591722
1717669833
1717669833
1312647007
1168076167
544553693
312739786
285643094
189278119
317919339
0
894940447
188120609
605409472
479058444
371688030
138229410
521644963
1135085137
1135085137
1135085137
228871196
377073574
377073574
377073574...

result: