QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#231497#7103. Red Black Treengpin04WA 1281ms29364kbC++144.3kb2023-10-29 13:17:172023-10-29 13:17:17

Judging History

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

  • [2023-10-29 13:17:17]
  • 评测
  • 测评结果:WA
  • 用时:1281ms
  • 内存:29364kb
  • [2023-10-29 13:17:17]
  • 提交

answer

#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define bit(n) (1LL << (n))
#define getbit(x, i) (((x) >> (i)) & 1)
#define pii pair<int, int>
#define ALL(x) x.begin(), x.end()
using namespace std;
const int M = 5e5 + 5;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int oo = 1e9;
const long long ooo = 1e18;
const double pi = acos(-1);

template<typename T1, typename T2> bool mini(T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi(T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("file.inp", "r",stdin);
    #endif
    
    auto solve = [&]() {
        int n, m, q;
        cin >> n >> m >> q;
        vector <int> red(n, -1);
        for (int i = 1; i <= m; i++) {
            int v; cin >> v;
            v--;
            red[v] = v;
        }

        vector <vector <int>> adj(n);
        vector <int> fr(n), to(n), w(n);
        for (int i = 1; i < n; i++) {
            int u, v;
            cin >> u >> v >> w[i];
            u--, v--;
            fr[i] = u, to[i] = v;
            adj[u].push_back(i);
            adj[v].push_back(i);
        }

        vector <long long> dp(n);
        vector <int> h(n);
        vector <vector <int>> anc(n, vector <int> (20, -1));

        function <void(int, int)> dfs = [&](int u, int p) {
            if (red[u] == -1)
                red[u] = red[p];
            anc[u][0] = p;
            for (int ind : adj[u]) {
                int v = fr[ind] ^ to[ind] ^ u;
                if (v == p)
                    continue;
                dp[v] = dp[u] + w[ind];
                h[v] = h[u] + 1;
                dfs(v, u);
            }
        };
        
        dfs(0, -1);

        for (int j = 1; j < 20; j++)
        for (int i = 0; i < n; i++) {
            int p = anc[i][j - 1];
            if (p == -1)
                anc[i][j] = -1;
            else
                anc[i][j] = anc[p][j - 1];
        }

        auto getlca = [&](int u, int v) {   
            if (h[u] < h[v])    
                swap(u, v);
            
            for (int i = 19; i >= 0; i--) 
                if (h[u] - bit(i) >= h[v])
                    u = anc[u][i];
                
            if (u == v)
                return v;
            
            for (int i = 19; i >= 0; i--)
                if (anc[u][i] != anc[v][i])
                    u = anc[u][i], v = anc[v][i];
            return anc[v][0];
        };  

        // for (int i = 0; i < n; i++)
        //     cerr << red[i] << " \n"[i == n - 1];

        while (q--) {
            int k; cin >> k;
            vector <int> v(k);
            for (int &x : v) {
                cin >> x;
                x--;
            }

            sort(ALL(v), [&](int i, int j) {
                return dp[i] - dp[red[i]] < dp[j] - dp[red[j]];
            });

            vector <pair <int, long long>> cand;
            long long mx = 0;
            for (int i = 0; i < k; i++) {
                int c = getlca(v[i], v.back());
                if (red[c] != red[v.back()] || red[c] != red[v[i]])
                    maxi(mx, dp[v[i]] - dp[red[v[i]]]);
                else {
                    cand.push_back({c, dp[v[i]] - dp[red[v[i]]]});
                }
            }

            sort(ALL(cand), [&](pair <int, int> a, pair <int, int> b) {
                pair <int, int> pa = {-h[a.fi], a.se};
                pair <int, int> pb = {-h[b.fi], b.se};
                return pa > pb;
            });

            long long ans = ooo;

            for (int i = 0; i < (int) cand.size();) {
                int j = i;
                while (j < (int) cand.size() && cand[i].fi == cand[j].fi)   
                    j++;
                
                // cerr << cand[i].fi << " " << cand[i].se << "\n";
                int p = cand[i].fi;
                mini(ans, max(mx, dp[v.back()] - dp[p]));

                for (int k = i; k < j; k++) 
                    maxi(mx, cand[i].se);

                i = j;
            }

            cout << ans << "\n";
        }
    };

    int t; cin >> t;
    while (t--) 
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3424kb

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: 1281ms
memory: 29364kb

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 59th lines differ - expected: '2307445864', found: '2094962422'