QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#690765#7103. Red Black TreeCapitanAC ✓923ms43968kbC++203.3kb2024-10-31 02:29:112024-10-31 02:29:12

Judging History

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

  • [2024-10-31 02:29:12]
  • 评测
  • 测评结果:AC
  • 用时:923ms
  • 内存:43968kb
  • [2024-10-31 02:29:11]
  • 提交

answer

/*
    in the name of god
*/
//#pragma GCC optimize("Ofast,O3,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,sse4a,avx,avx2,popcnt,tune=native")

#include <bits/stdc++.h>

using namespace std;
#define int long long 
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef long long ll ;

#define File          freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define md(x)         (((x%MD)+MD)%MD)
#define all(V)        V.begin(),V.end()
#define setprec(x)    fixed << setprecision(x)
#define Mp(a,b)       make_pair(a,b)
#define len(V)        (int)(V.size())
#define sep           ' '
#define endl          '\n'
#define pb(x)         push_back(x)
#define fi            first
#define sec           second
#define popcount(x)   __builtin_popcount(x)
#define lid           u<<1
#define rid           (lid)|1
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll N = 2e5 + 10,SQ=320,LOG=22;
const ll inf = 2e17 , MD = 1e9 + 7;
int n , m,q;
int st[N],ft[N],tim=1;
int par[N][LOG];
bool mark[N];
int dis[N],d2[N],d3[N];
vector<pii> g[N];
void run(){
    tim = 1;
    fill(dis,dis+n+1,0);
    fill(d2,d2+n+1,0);
    fill(d3,d3+n+1,0);
    for(int i = 1;i<=n;i++){
        g[i].clear();
        mark[i] = 0;
    }
}
void dfs(int u,int p,int w){
    if(mark[u]) dis[u] = 0;
    else dis[u] = dis[p] + w;
    d3[u] = d3[p] + w;
    d2[u] = (u==p ? 0 : d2[p] + 1);
    par[u][0] = p;
    for(int j = 1;j<LOG;j++) par[u][j] = par[par[u][j-1]][j-1];
    st[u] = tim++;
    for(auto h : g[u]){
        if(h.fi != p){
            dfs(h.fi,u,h.sec);
        }
    }
    ft[u]  = tim;
}
int lift(int u,int d){
    for(int j = 0;j<LOG;j++){
        if(d&(1<<j)) u = par[u][j];
    }
    return u;
}
int LCA(int u,int v){
    if(d2[u] > d2[v]) swap(u,v);
    v = lift(v,d2[v] - d2[u]);
    if(u==v) return u;
    for(int j = LOG-1;j>=0;j--){
        if(par[u][j] != par[v][j]){
            u = par[u][j],v=par[v][j];
        }
    }
    return par[u][0];
}
bool cmp(int u,int v){
    return (dis[u] < dis[v]);
}
int32_t main() {
    ios_base::sync_with_stdio(false);cout.tie(0);cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        cin >> n >> m >> q;
        run();
        for(int i = 1;i<=m;i++){
            int x;
            cin >> x;
            mark[x] = 1;
        }
        for(int i = 0;i<n-1;i++){
            int u,v,w;
            cin >> u >> v >> w;
            g[u].pb(Mp(v,w));
            g[v].pb(Mp(u,w));
        }
        dfs(1,1,0);
        for(int i = 0;i<q;i++){
            vector<int> tmp;
            int k;
            cin >> k ;
            for(int j = 0;j<k;j++){
                int p;
                cin >> p;
                tmp.pb(p);
            }
            sort(all(tmp),cmp);
            int ans  = inf;
            int lc = tmp.back();
            int mx = -inf;
            for(int j = k-1;j>=0;j--){
                lc = LCA(lc,tmp[j]);
                mx = max(mx,d3[tmp[j]]);
                //cout << tmp[j] << sep << lc << sep << mx << endl;
                ans = min(ans,max((j > 0 ? dis[tmp[j-1]] : -inf),mx-d3[lc]));
            }

            cout << ans << endl;

        }
    }
    return 0;
}

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

详细

Test #1:

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

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: 923ms
memory: 43968kb

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