Submission #1123137

#TimeUsernameProblemLanguageResultExecution timeMemory
1123137razivoThe Xana coup (BOI21_xanadu)C++20
100 / 100
187 ms40804 KiB
#include <iostream> #include <vector> #include <algorithm> using namespace std; long long n; vector<vector<long long>> g(100000); vector<long long> are(100000); vector<vector<vector<long long>>> on(100000,vector<vector<long long>>(2,vector<long long>(2,0))); pair<long long,long long> beo(vector<pair<long long,long long>> &u) { vector<pair<long long,long long>> d; d.reserve(u.size()); long long sum = 0; for (long long i = 0; i < u.size(); ++i) { d.emplace_back(u[i].second-u[i].first,i); sum+=u[i].first; } if(d.size()>0)sort(d.begin(),d.end()); long long be=sum,bo=2e5; for (long long i = 0; i < d.size(); ++i) { sum+=d[i].first; if(i%2==1) be=min(be,sum); else bo = min(bo,sum); } return {be,bo}; } long long check(bool ch,bool me, bool p, vector<vector<vector<long long>>> &t) { vector<pair<long long,long long>> d; long long v = me ? 1 : 0; for (auto & i : t) { d.emplace_back(i[0][v],i[1][v]); } auto [e,o] = beo(d); bool a = me^p^ch; if(a) return o; else return e; } void dfs(long long cur, long long per) { vector<vector<vector<long long>>> t; for (long long i: g[cur]) { if(i==per) continue; dfs(i,cur); t.push_back(on[i]); } on[cur][0][0]=check(are[cur],false,false,t); on[cur][1][0]=check(are[cur],true,false,t)+1; on[cur][0][1]=check(are[cur],false,true,t); on[cur][1][1]=check(are[cur],true,true,t)+1; } int main() { cin>>n; for (long long i = 0; i < n-1; ++i) { long long x; long long y; cin>>x>>y; x--; y--; g[x].push_back(y); g[y].push_back(x); } for (long long i = 0; i < n; ++i) { long long a; cin>>a; are[i]=a; } dfs(0,-1); long long res = min(on[0][0][0],on[0][1][0]); if(res>n) { cout<<"impossible"<<endl; }else { cout<<res<<endl; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...