QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#172725 | #7103. Red Black Tree | xianggui | AC ✓ | 1008ms | 46720kb | C++20 | 3.6kb | 2023-09-09 20:23:12 | 2023-09-09 20:23:12 |
Judging History
answer
#include <bits/stdc++.h>
// #define int long long
using namespace std;
typedef long long ll;
int T;
int n,m,q,k;
int key[100010];
bool red[100010];
vector<pair<int, ll> > ed[100010];
int fa[100010][21],deep[100010];
ll sum[100010][21];
ll cost[100010];
int len=0;
char ibuf[(1<<20)+1], *iS, *iT, out[(1<<25)+1];
#define gh() (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1<<20)+1, stdin), (iS == iT ? EOF : *iS++) : *iS++)
template<typename T>
inline T read()
{
char ch=gh();
T x=0;
char t=0;
while(ch<'0'||ch>'9')
t |= ch=='-', ch=gh();
while(ch>='0'&&ch<='9')
x = (x<<3) + (x<<1) + (ch^48), ch=gh();
return t?-x:x;
}
void dfs(int x,int f)
{
for(int i=1;i<=20;i++)
{
fa[x][i]=fa[fa[x][i-1]][i-1];
sum[x][i]=sum[fa[x][i-1]][i-1]+sum[x][i-1];
}
for(auto i:ed[x])
{
int v=i.first;
ll val=i.second;
if(v==f) continue;
deep[v]=deep[x]+1;
fa[v][0]=x;
sum[v][0]=val;
dfs(v, x);
}
}
void dfs1(int x,int f,ll ss)
{
if(red[x]) ss=0;
cost[x]=ss;
for(auto i:ed[x])
{
int v=i.first;
ll val=i.second;
if(v==f) continue;
dfs1(v, x, ss+val);
}
}
int Lca(int x,int y)
{
if(x==y) return x;
if(deep[x]<deep[y]) swap(x,y);
for(int i=20;i>=0;i--)
{
int xx=fa[x][i];
if(deep[xx]>=deep[y])
{
x=xx;
}
}
if(x==y) return x;
for(int i=20;i>=0;i--)
{
int xx=fa[x][i];
int yy=fa[y][i];
if(xx!=yy)
{
x=xx;
y=yy;
}
}
return fa[x][0];
}
ll getsum(int x,int y)
{
ll res=0;
for(int i=20;i>=0;i--)
{
int xx=fa[x][i];
if(deep[xx]>=deep[y])
{
res+=sum[x][i];
x=xx;
}
}
return res;
}
bool cmp(int x,int y)
{
return cost[x]>cost[y];
}
signed main()
{
T = read<int>();
while(T--)
{
// cin>>n>>m>>q;
n=read<int>();
m=read<int>();
q=read<int>();
for(int i=1;i<=n;i++)
{
red[i]=0;
ed[i].clear();
}
for(int i=1;i<=m;i++)
{
int x;
// cin>>x;
x=read<int>();
red[x]=1;
}
for(int i=1;i<n;i++)
{
int x,y;
ll z;
// cin>>x>>y>>z;
x=read<int>();
y=read<int>();
z=read<ll>();
ed[x].push_back(make_pair(y, z));
ed[y].push_back(make_pair(x, z));
}
deep[1]=1;
dfs(1,0);
dfs1(1, 0, 0);
while(q--)
{
k=read<int>();
for(int i=1;i<=k;i++)
{
key[i]=read<int>();
}
sort(key+1,key+1+k,cmp);
key[k+1]=0;
ll lasum=0,lca=key[1];
ll ans=cost[key[1]];
for(int i=1;i<=k;i++)
{
ll nowl=Lca(lca, key[i]);
ll nowsum=max(lasum+getsum(lca, nowl), getsum(key[i], nowl));
ans=min(ans, max(nowsum, cost[key[i+1]]));
lasum=nowsum;
lca=nowl;
}
printf("%lld\n",ans);
}
}
return 0;
}
/*
1
12 2 3
1 3
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
1 2
3 1 3 4
4 1 11 5 12
1
11 1 1
1
1 2 3
2 5 1
2 6 1
1 3 1
3 7 1
3 8 3
1 4 5
4 9 2
4 10 1
7 11 1
2 1 2
*/
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 7900kb
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: 1008ms
memory: 46720kb
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