QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#601319 | #5418. Color the Tree | bruteforce_ | RE | 0ms | 3504kb | C++20 | 2.6kb | 2024-09-29 22:19:42 | 2024-09-29 22:19:42 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+7,inf=1e18,S=21;
vector<vector<int>> e,leaf,fa,mi,pos;
vector<int> a,dep,dfn,_2;
int tot;
int n;
int query(int l,int r)
{
l++; r++;
int len=r-l+1;
int t=_2[len];
return min(mi[l][t],mi[r-(1<<t)+1][t]);
}
bool cmp(int x,int y)
{
return dfn[x]<dfn[y];
}
void dfs1(int u)
{
dep[u]=dep[fa[u][0]]+1;
leaf[dep[u]].push_back(u);
dfn[u]=++tot;
for(auto v:e[u])
{
if(v==fa[u][0]) continue;
fa[v][0]=u;
dfs1(v);
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y])
swap(x,y);
for(int i=S-1; i>=0; i--)
{
if(dep[fa[x][0]]>=dep[y])
x=fa[x][0];
}
if(x==y) return x;
for(int i=S-1; i>=0; i--)
{
if(fa[x][0]!=fa[y][0])
{
x=fa[x][0];
y=fa[y][0];
}
}
return fa[x][0];
}
//vector<int> dp;
int dfs2(int u,int fa,int d)
{
if(dep[u]==d)
{
return query(0,d-1);
}
int res=0;
for(auto v:e[u])
{
if(v==fa) continue;
res+=dfs2(v,u,d);
}
e[u].clear();
return min(res,query(d-dep[u],d-dep[fa]-1));
}
int solve(int id)
{
if(!leaf[id].size()) return 0;
sort(leaf[id].begin(),leaf[id].end(),cmp);
int n=leaf[id].size();
for(int i=0; i<n-1; i++)
{
leaf[id].push_back(lca(leaf[id][i],leaf[id][i+1]));
}
sort(leaf[id].begin(),leaf[id].end(),cmp);
leaf[id].erase(unique(leaf[id].begin(),leaf[id].end()),leaf[id].end());
n=leaf[id].size();
for(int i=0; i<n-1; i++)
{
int l=lca(leaf[id][i],leaf[id][i+1]);
e[leaf[id][i+1]].push_back(l);
e[l].push_back(leaf[id][i+1]);
}
int res=dfs2(leaf[id][0],0,id);
return res;
}
void O_o()
{
cin>>n;
pos.assign(n+2,vector<int>(S,0));
mi.assign(n+2,vector<int>(S,inf));
_2.assign(n+2,0);
e.assign(n+1,vector<int>());
fa.assign(n+1,vector<int>(S,0));
leaf.assign(n+1,vector<int>());
a.assign(n+1,0);
_2[0]=-1;
for(int i=1; i<=n; i++)
{
cin>>a[i];
mi[i][0]=a[i];
pos[i][0]=i+1;
_2[i]=_2[i>>1]+1;
}
for(int j=1; j<S; j++)
{
for(int i=1; i<=n; i++)
{
pos[i][j]=pos[pos[i][j-1]][j-1];
mi[i][j]=min(mi[i][j-1],mi[pos[i][j-1]][j-1]);
}
}
for(int i=1; i<n; i++)
{
int x,y;
cin>>x>>y;
e[x].push_back(y);
e[y].push_back(x);
}
dep.assign(n+1,0);
dfn.assign(n+1,0);
tot=0;
dfs1(1);
for(int j=1; j<S; j++)
{
for(int i=1; i<=n; i++)
{
fa[i][j]=fa[fa[i][j-1]][j-1];
}
}
int ans=0;
e.assign(n+1,vector<int>());
for(int i=1; i<=n; i++)
{
ans+=solve(i);
}
cout<<ans<<"\n";
}
signed main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cout<<fixed<<setprecision(12);
int T=1;
cin>>T;
while(T--)
{
O_o();
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3504kb
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
Runtime Error
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...