QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#231394#7103. Red Black Treengpin04AC ✓656ms34208kbC++144.1kb2023-10-29 10:29:322023-10-29 10:29:33

Judging History

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

  • [2023-10-29 10:29:33]
  • 评测
  • 测评结果:AC
  • 用时:656ms
  • 内存:34208kb
  • [2023-10-29 10:29:32]
  • 提交

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;

vector <int> adj[N];


long long dp[N];

long long maxcand[N];

int cand[N];
int anc[N][17];
int red[N];
int ver[N];
int fr[N];
int to[N];
int h[N];
int w[N];
int n,m,q;


void 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);
    }
}

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

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    
    auto solve = [&]() {
        int n, m, q;
        cin >> n >> m >> q;
    
        for (int i = 0; i < n; i++)
            red[i] = maxcand[i] = -1;

        for (int i = 1; i <= m; i++) {
            int v; cin >> v;
            v--;
            red[v] = v;
        }

        for (int i = 0; i < n; i++)
            adj[i].clear();

        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);
        }
        
        dp[0] = h[0] = 0;
        dfs(0, -1);

        for (int j = 1; j < 17; 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];
        }

        vector <long long> ans(q);
        for (int id = 0; id < q; id++) {
            int k; cin >> k;
            
            for (int i = 0; i < k; i++) {
                int x;
                cin >> x;
                x--;
                ver[i] = x;
            }
            
            int maxv = -1;
            for (int i = 0; i < k; i++) {
                if (maxv < 0)
                    maxv = ver[i];
                else {
                    if (dp[maxv] - dp[red[maxv]] < dp[ver[i]] - dp[red[ver[i]]])
                        maxv = ver[i];
                }
            }

            int sz = 0;
            long long mx = 0;
            for (int i = 0; i < k; i++) {
                int c = getlca(ver[i], maxv);
                if (red[c] != red[maxv] || red[c] != red[ver[i]])
                    maxi(mx, dp[ver[i]] - dp[red[ver[i]]]);
                else {
                    if (maxcand[c] == -1)
                        cand[sz++] = c;
                    maxi(maxcand[c], dp[ver[i]] - dp[red[ver[i]]]);
                }
            }

            sort(cand, cand + sz, [&](int i, int j) {
                return h[i] < h[j];
            });

            ans[id] = ooo;

            for (int i = 0; i < sz; i++) {
                int p = cand[i];
                // cerr << p << " \n"[i == sz - 1];
                mini(ans[id], max(mx, dp[maxv] - dp[p]));   
                maxi(mx, maxcand[p]);
                maxcand[p] = -1;
            }
        }

        for (int i = 0; i < q; i++)
            cout << ans[i] << "\n";
    };

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

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

詳細信息

Test #1:

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

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: 656ms
memory: 34208kb

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