QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#394439 | #5418. Color the Tree | marher | WA | 68ms | 22364kb | C++14 | 2.3kb | 2024-04-20 14:46:39 | 2024-04-20 14:46:39 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+50;
struct edge
{
int u,v,nxt;
}e[N];
int a[N][20],n,dep[N],head[N],T,lg[N],cnt,fa[N],top[N],siz[N],son[N],f[N];
ll ans;
vector<int>in[N];
void clear()
{
ans=cnt=0;
for(int i=1;i<=n;i++)
{
vector<int>().swap(in[i]);
son[i]=f[i]=siz[i]=top[i]=fa[i]=head[i]=dep[i]=0;
}
}
int find(int l,int r)
{
int k=lg[r-l+1]-1;
return min(a[l][k],a[r-(1<<k)+1][k]);
}
void dfs1(int x,int f)
{
fa[x]=f;siz[x]=1;dep[x]=dep[f]+1;
for(int i=head[x];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==f)continue;
dfs1(v,x);siz[x]+=siz[v];
if(siz[v]>siz[son[x]])son[x]=v;
}
}
void dfs2(int x,int topp)
{
top[x]=topp;in[dep[x]].push_back(x);
if(!son[x])return;dfs2(son[x],topp);
for(int i=head[x];i;i=e[i].nxt)if(e[i].v!=fa[x]&&e[i].v!=son[x])dfs2(e[i].v,e[i].v);
}
int LCA(int u,int v)
{
while(top[u]!=top[v])
{
if(dep[top[u]]<dep[top[v]])swap(u,v);
u=fa[top[u]];
}
return dep[u]<dep[v]?u:v;
}
vector<int>to[N];
void solve(int x,int fa,int d)
{
if(dep[x]==d)
{
f[x]=find(0,d-1);
return;
}
ll w=0;
for(auto v:to[x])solve(v,x,d),w+=f[v];
f[x]=min(w,1ll*find(d-dep[x],d-1));
}
void clear(int x)
{
for(auto v:to[x])clear(v);
vector<int>().swap(to[x]);
}
void solve(vector<int>&a)
{
int now=a[0],d=dep[now];a.push_back(1);
for(auto x:a)if(x!=now)
{
int lca=LCA(now,x);
if(x!=lca)to[lca].push_back(x);
if(now!=lca)to[lca].push_back(now);
now=lca;
}
solve(1,0,d);
ans+=f[1];clear(1);
}
void sol()
{
cin>>n;
for(int i=0;i<n;i++)cin>>a[i][0];
for(int j=1;(1<<j)<=n;j++)for(int i=0;i+(1<<j)<=n;i++)a[i][j]=min(a[i][j-1],a[i+(1<<(j-1))][j-1]);
for(int i=1;i<=n;i++)lg[i]=lg[i>>1]+1;
for(int i=1;i<n;i++)
{
int u,v;cin>>u>>v;
e[++cnt]=(edge){u,v,head[u]};head[u]=cnt;
e[++cnt]=(edge){v,u,head[v]};head[v]=cnt;
}
dfs1(1,1);dfs2(1,1);
for(int i=1;i<=n;i++)if(in[i].size())solve(in[i]);
cout<<ans<<'\n';
}
main()
{
// freopen("in.txt","r",stdin);
cin>>T;
while(T--)sol(),clear();
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 22364kb
input:
3 4 10 15 40 1 1 2 2 3 2 4 5 10 5 1 100 1000 1 2 2 3 2 4 4 5 4 1000 200 10 8 1 2 2 3 3 4
output:
35 17 1218
result:
ok 3 number(s): "35 17 1218"
Test #2:
score: -100
Wrong Answer
time: 68ms
memory: 22216kb
input:
3000 54 43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44 1 2 1 3 2 4 3 5 4 6 2 7 4 8 1 9 3 10 1 11 8 12 2 13 9 14 1 15 1 16 15 17 15 18 7 19 1 20 19 21 13 22 10 23 17 24 9 25 9 26 24 27 8 28...
output:
185 174 222 230 156 249 225 126 100 81 155 73 159 134 149 144 228 230 140 195 153 171 78 282 195 286 191 211 119 206 211 247 88 252 239 244 173 180 195 121 109 148 180 188 226 210 182 97 247 59 56 31 115 224 203 172 155 208 53 150 189 187 173 137 233 100 163 273 80 350 156 133 165 159 252 269 156 22...
result:
wrong answer 1st numbers differ - expected: '180', found: '185'