Submission #1371760


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
using vi=vector<int>;
#define PB push_back
#define ALL(x) x.begin(),x.end()
int read(){
	int i;
	scanf("%d",&i);
	return i;
}

const int Nmax=114514;
int a[Nmax],ans[Nmax];
vi tr[Nmax];
bool hasP[Nmax];
void Update(int v,int x){
	if(ans[v]==-1||abs(ans[v]-a[v])>abs(x-a[v])||(abs(ans[v]-a[v])==abs(x-a[v])&&x<ans[v]))
		ans[v]=x;
}
void dfs(int v,set<int>&s){
	auto itr=s.lower_bound(a[v]);
	if(itr!=s.end())Update(v,*itr);
	if(itr!=s.begin())Update(v,*--itr);
	for(auto ch:tr[v])dfs(ch,s);
	s.insert(a[v]);
}

int main(){
	int n=read();
	REP(i,n)a[i]=read();
	REP(i,n-1){
		int s=read(),t=read();
		hasP[t]=true;
		tr[s].PB(t);
	}
	int root;
	REP(i,n)if(!hasP[i])root=i;
	memset(ans,-1,sizeof(ans));
	REP(_,2){
		set<int> s;
		dfs(root,s);
		REP(i,n)reverse(ALL(tr[i]));
	}
	REP(i,n)
		printf("%d\n",ans[i]);
}

Submission Info

Submission Time
Task I - 支配と友好
User maroonrk
Language C++14 (GCC 5.4.1)
Score 200
Code Size 982 Byte
Status AC
Exec Time 165 ms
Memory 16640 KB

Compile Error

./Main.cpp: In function ‘int read()’:
./Main.cpp:10:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&i);
                ^

Judge Result

Set Name All
Score / Max Score 200 / 200
Status
AC × 45
Set Name Test Cases
All sample01, sample02, sample03, binary00, binary01, binary02, binary03, hand00, line00, line01, line02, random00, random01, random02, random03, random04, random05, random06, random07, random08, sample01, sample02, sample03, star00, star01, star02, ternary00, ternary01, ternary02, ternary03, thin00, thin01, thin02, thin03, thin04, thin05, thin06, thin07, uniform00, uniform01, uniform02, uniform03, uniform04, uniform05, uniform06
Case Name Status Exec Time Memory
binary00 AC 71 ms 5760 KB
binary01 AC 112 ms 6400 KB
binary02 AC 155 ms 10880 KB
binary03 AC 164 ms 11136 KB
hand00 AC 99 ms 14848 KB
line00 AC 67 ms 12032 KB
line01 AC 91 ms 12416 KB
line02 AC 133 ms 16640 KB
random00 AC 66 ms 5632 KB
random01 AC 71 ms 5760 KB
random02 AC 86 ms 5888 KB
random03 AC 111 ms 6400 KB
random04 AC 148 ms 8960 KB
random05 AC 153 ms 10624 KB
random06 AC 164 ms 10880 KB
random07 AC 164 ms 11008 KB
random08 AC 158 ms 11136 KB
sample01 AC 3 ms 3328 KB
sample02 AC 3 ms 3328 KB
sample03 AC 3 ms 3328 KB
star00 AC 53 ms 4600 KB
star01 AC 93 ms 5240 KB
star02 AC 136 ms 9848 KB
ternary00 AC 67 ms 5248 KB
ternary01 AC 108 ms 5888 KB
ternary02 AC 154 ms 10368 KB
ternary03 AC 156 ms 10496 KB
thin00 AC 73 ms 8064 KB
thin01 AC 70 ms 6784 KB
thin02 AC 116 ms 8704 KB
thin03 AC 112 ms 7424 KB
thin04 AC 163 ms 13184 KB
thin05 AC 160 ms 11904 KB
thin06 AC 165 ms 13440 KB
thin07 AC 161 ms 12160 KB
uniform00 AC 148 ms 10240 KB
uniform01 AC 152 ms 10624 KB
uniform02 AC 148 ms 10368 KB
uniform03 AC 151 ms 10240 KB
uniform04 AC 147 ms 10112 KB
uniform05 AC 156 ms 9984 KB
uniform06 AC 142 ms 9856 KB