QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#109028 | #5418. Color the Tree | shihoghmean | WA | 31ms | 3632kb | C++17 | 3.8kb | 2023-05-27 09:56:57 | 2023-05-27 09:56:58 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define ll long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fr(i,a,b) for(int i=a;i>=b;i--)
#define py puts("Yes")
#define pn puts("No")
#define pt puts("")
#define pb push_back
#define wt(x) write(x),puts("")
#define wr(x) write(x) ,putchar(' ')
#define tx printf("fds")
#define mp make_pair
#define fi first
#define se second
inline int read(){
int x=0,k=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') k=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+ch-48;
ch=getchar();
}
return x*k;
}
void write(int x){
if(x<0){
x=-x;
putchar('-');
}
if(x>9) write(x/10);
putchar(x%10+'0');
}
int power(int x,int y,int mod){
int num=1;
while(y){
if(y&1) num=(num*x)%mod;
x=x*x%mod;
y>>=1;
}
return num;
}
int mul(int x,int y,int mod){
int num=0;
while(y){
if(y&1) num=(num+x)%mod;
x=(x+x)%mod;
y>>=1;
}
return num;
}
const int N=1e6+7,mod=998244353;
int n,m,tot,cnt,ans,k;
int a[N],b[N];
int head[N];
struct edge{
int to,next;
}e[N];
void add(int u,int v){
e[++tot]={v,head[u]};
head[u]=tot;
}
struct tree{
int ls,rs;
int num;
}t[N];
int siz[N],dep[N],son[N],mdep[N],top[N];
int rt[N];
int rmq[N][20];
int newnode(int x){
t[x]={0,0,0};
return x;
}
void pu(int id){
int x=0,y=0;
if(t[id].ls) x=t[t[id].ls].num;
if(t[id].rs) y=t[t[id].rs].num;
t[id].num=x+y;
}
void update(int id,int l,int r,int x,int k){
if(l==r){
t[id].num=k;
return ;
}
int mid=(l+r)>>1;
if(x<=mid){
if(!t[id].ls) t[id].ls=newnode(++cnt);
update(t[id].ls,l,mid,x,k);
}
else{
if(!t[id].rs) t[id].rs=newnode(++cnt);
update(t[id].rs,mid+1,r,x,k);
}
pu(id);
}
int RMQ(int l,int r){
int x=log2(r-l+1);
return min(rmq[l][x],rmq[r-(1<<x)+1][x]);
}
int merge(int ddp,int dp,int x,int y,int l,int r){
if(!x||!y) return x+y;
if(l==r){
t[x].num=min(t[x].num+t[y].num,RMQ(l-dp+1,l-ddp+1));
return x;
}
int mid=(l+r)>>1;
t[x].ls=merge(ddp,dp,t[x].ls,t[y].ls,l,mid);
t[x].rs=merge(ddp,dp,t[x].rs,t[y].rs,mid+1,r);
pu(x);
return x;
}
void dfs(int u,int fa){
dep[u]=mdep[u]=dep[fa]+1;
siz[u]=1;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==fa) continue;
dfs(v,u);
siz[u]+=siz[v];
mdep[u]=max(mdep[u],mdep[v]);
if(mdep[v]>mdep[son[u]]){
son[u]=v;
}
}
}
void dfs1(int u,int fa,int tp){
top[u]=tp;
if(u==tp) rt[u]=newnode(++cnt);
update(rt[top[u]],1,n,dep[u],b[dep[u]-dep[top[u]]]);
if(son[u]) dfs1(son[u],u,tp);
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==fa||v==son[u]) continue;
dfs1(v,u,v);
}
}
void dfs2(int u,int fa){
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==fa) continue;
dfs2(v,u);
if(son[u]!=v) rt[top[u]]=merge(dep[top[u]],dep[u],rt[top[u]],rt[top[v]],1,n);
}
}
signed main(){
int tt=read();
while(tt--){
tot=0;
n=read();
fo(i,1,n) head[i]=0,rt[i]=0,son[i]=0,siz[i]=0,dep[i]=0,mdep[i]=0,top[i]=0;
fo(i,0,n-1) a[i]=read(),b[i]=a[i];
fo(i,1,n-1)b[i]=min(b[i],b[i-1]);
fo(i,1,n){
rmq[i][0]=a[i-1];
}
fo(i,1,19){
fo(j,1,n){
rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<(i-1))][i-1]);
}
}
fo(i,2,n){
int x=read(),y=read();
add(x,y);
add(y,x);
}
cnt=0;
dfs(1,0);
dfs1(1,0,1);
dfs2(1,0);
wt(t[rt[1]].num);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3404kb
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: 31ms
memory: 3632kb
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:
138 120 189 190 148 227 221 104 100 81 155 73 143 127 143 86 153 230 105 174 138 168 78 188 195 267 187 211 119 118 211 233 88 246 239 173 173 180 166 113 109 114 180 123 226 191 182 91 195 59 56 31 115 150 186 115 103 208 53 119 181 170 169 134 233 72 163 273 80 350 142 133 122 147 240 269 113 210 ...
result:
wrong answer 1st numbers differ - expected: '180', found: '138'