QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#865162 | #5418. Color the Tree | xxzx | WA | 33ms | 18208kb | C++17 | 2.6kb | 2025-01-21 15:43:26 | 2025-01-21 15:43:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define clo 1000.*clock()/CLOCKS_PER_SEC
#ifndef xxzx
#define endl '\n'
#endif
using ll=long long;
using PII=pair<int,int>;
const int N=1e5+10;
bool memory1;
int n,a[N],mn[N][20],Log[N];
vector<int> eg[N];
int GetMin(int l,int r) {
int len=Log[r-l+1];
return min(mn[l][len],mn[r-(1<<len)+1][len]);
}
int fa[N][20],dep[N],dfn[N],tim;
vector<int> nd[N];
void dfs(int x,int from) {
fa[x][0]=from,dep[x]=dep[from]+1,nd[dep[x]].push_back(x),dfn[x]=++tim;
for(auto y:eg[x]) if(y!=from) dfs(y,x);
}
int lca(int x,int y) {
if(dep[x]<dep[y]) swap(x,y);
for(int h=dep[x]-dep[y],i=0;h;h>>=1,i++)
if(h&1) x=fa[x][i];
if(x==y) return x;
for(int i=19;i>=0;i--)
if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
int vc[N];
ll f[N];
vector<int> g[N];
void dfs1(int x,int from,int D) {
// D-[dep[fa[x]]+1,dep[x]]
f[x]=GetMin(D-dep[x],D-(dep[from]+1));
if(g[x].size()) {
ll res=0;
for(auto y:g[x]) res+=f[y];
f[x]=min(f[x],res);
}
}
ll ans;
void sol(vector<int> &nd,int D) {
sort(nd.begin(),nd.end(),[](const int &x,const int &y){ return dfn[x]<dfn[y]; });
int sz=nd.size(),m=0;
for(auto i:nd) vc[++m]=i;
for(int i=0;i+1<sz;i++) vc[++m]=lca(nd[i],nd[i+1]);
sort(vc+1,vc+m+1,[](const int &x,const int &y){ return dfn[x]<dfn[y]; });
m=unique(vc+1,vc+m+1)-vc-1;
for(int i=1;i<=m;i++) g[vc[i]].clear(),f[vc[i]]=0x3f3f3f3f3f3f3f3f;
for(int i=1;i+1<=m;i++) g[lca(vc[i],vc[i+1])].push_back(vc[i+1]);
dfs1(vc[1],0,D);
ans+=f[vc[1]];
// cerr<<"D "<<D<<" "<<f[vc[1]]<<" "<<GetMin(0,1)<<" "<<mn[1][0]<<endl;
}
void solve() {
cin>>n;
for(int i=0;i<n;i++) cin>>a[i],mn[i][0]=a[i];
for(int j=1;j<20;j++) for(int i=0;i+(1<<j)-1<n;i++)
mn[i][j]=min(mn[i][j-1],mn[i+(1<<j-1)][j-1]);
for(int i=1;i<=n;i++) eg[i].clear(),nd[i].clear();
for(int i=1,x,y;i<n;i++) {
cin>>x>>y;
eg[x].push_back(y),eg[y].push_back(x);
}
tim=0;
dfs(1,0);
for(int j=1;j<20;j++) for(int i=1;i<=n;i++) fa[i][j]=fa[fa[i][j-1]][j-1];
ans=0;
for(int d=1;d<=n;d++) if(nd[d].size()) sol(nd[d],d);
cout<<ans<<endl;
}
bool memory2;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
for(int i=2;i<N;i++) Log[i]=Log[i>>1]+1;
int _; cin>>_;
while(_--) solve();
#ifdef xxzx
cerr<<"Time: "<<clo<<"MS"<<endl;
cerr<<"Memory: "<<abs(&memory1-&memory2)/1024./1024.<<"MB"<<endl;
#endif
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 18208kb
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: 33ms
memory: 14940kb
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:
-5208492444341520367 -217020518514229885 8246779703540740867 -4991471925827290293 8680820740569200882 8680820740569200913 -4991471925827290298 126 -4774451407313060332 -4774451407313060348 -651061555542689962 -4774451407313060373 8463800222054970821 -651061555542689949 8680820740569200886 8029759185...
result:
wrong answer 1st numbers differ - expected: '180', found: '-5208492444341520367'