QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#775411 | #9110. Zayin and Tree | wallcrack# | AC ✓ | 288ms | 40624kb | C++14 | 1.2kb | 2024-11-23 15:46:57 | 2024-11-23 15:46:57 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
vector<int>eg[1000010];
int v, u, ans;
int n, a[1000010], dis[1000010];
const int maxn=1e6+10;
int f[maxn];
void dfs(int u,int fa)
{
for(auto &v:eg[u])
{
if(v==fa)
continue;
dfs(v,u);
f[u]=min(f[u],f[v]+1);
}
f[u]=min(f[u],1-a[u]);
}
void dfs1(int u,int fa,int dis)
{
ans=min(ans,min(f[u],dis)+a[u]);
multiset<int>st;
for(auto &v:eg[u])
{
if(v==fa) continue;
st.insert(f[v]+1);
}
st.insert(INT_MAX);
for(auto &v:eg[u])
{
if(v==fa) continue;
st.erase(st.find(f[v]+1));
int res=*st.begin();
st.insert(f[v]+1);
int nowdis=min(dis,res);
nowdis=min(nowdis,1-a[u]);
dfs1(v,u,nowdis+1);
}
}
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i];
eg[i].clear();
f[i]=INT_MAX;
}
for(int i = 1; i < n; ++i){
cin >> v >> u;
eg[u].emplace_back(v);
eg[v].emplace_back(u);
}
ans=INT_MAX;
dfs(1,0);
dfs1(1,0,1e9);
cout << ans << "\n";
}
signed main(){
ios::sync_with_stdio(false);
int _ = 1;
cin >> _;
while(_--){
solve();
}
}
/*
2
5
4 5 3 4 2
1 2
2 3
3 4
3 5
5
4 4 1 1 2
1 2
2 3
3 4
3 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 288ms
memory: 40624kb
input:
3009 5 4 5 3 4 2 1 2 2 3 3 4 3 5 5 4 4 1 1 2 1 2 2 3 3 4 3 5 10 5 8 1 0 8 7 5 2 0 4 2 4 3 8 3 9 1 2 1 3 3 6 4 5 5 7 6 10 10 6 8 8 4 8 0 6 6 0 2 7 10 1 7 2 9 2 3 3 4 1 5 1 6 6 8 1 2 10 9 0 4 0 4 6 0 2 0 0 1 5 1 3 1 7 2 6 1 2 1 9 1 4 5 8 7 10 10 8 8 1 2 7 4 8 6 0 8 1 6 1 7 1 5 7 9 1 3 1 2 2 10 3 4 1 8...
output:
0 -1 -6 -6 -7 -6 -7 -4 -3 -7 -5 -6 -5 -4 -6 -3 -4 -7 -4 -4 -6 -6 -6 -5 -4 -5 -6 -6 -7 -7 -5 -7 -6 -6 -7 -6 -5 -5 -4 -6 -6 -5 -6 -6 -6 -6 -3 -6 -3 -6 -4 -6 -7 -6 -7 -6 -6 -5 -7 -6 -4 -7 -3 -5 -5 -6 -4 -5 -7 -6 -5 -5 -4 -3 -5 -3 -4 -2 -6 -5 -7 -4 -5 -5 -7 -7 -4 -6 -5 -4 -6 -5 -5 -6 -3 -6 -7 -7 -7 -6 -...
result:
ok 3009 lines