제출 #1105626

#제출 시각아이디문제언어결과실행 시간메모리
1105626koukirocksThe Xana coup (BOI21_xanadu)C++17
10 / 100
118 ms59220 KiB
#include <bits/stdc++.h> #define speed ios_base::sync_with_stdio(0); cin.tie(0) #define all(x) (x).begin(),(x).end() #define F first #define S second //#pragma GCC optimize("O3,unroll-loops") //#pragma GCC target("avx,avx2") //#pragma GCC target("popcnt") using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair<int,int> pii; typedef pair<ll,ll> pll; const ll MAX=1e5+10,P=1e9+7; const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f; const ldb eps=1e-6; const ldb PI=acos(-1.0); const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; template<typename T> using vvector = vector<vector<T>>; vvector<int> dfs(int v,int p,vvector<int> &G,vector<int> &a) { vvector<int> ans(2,vector<int>(2)); vvector<int> rec(2,vector<int>(2)); rec[0][0]=0; rec[0][1]=INF; rec[1][0]=0; rec[1][1]=INF; for (int i:G[v]) { if (i==p) continue; vvector<int> now = dfs(i,v,G,a); vvector<int> nrec(2,vector<int>(2)); nrec[0][0]=min(rec[0][1]+now[0][1],rec[0][0]+now[0][0]); nrec[0][1]=min(rec[0][1]+now[0][0],rec[0][0]+now[0][1]); nrec[1][0]=min(rec[1][1]+now[1][1],rec[1][0]+now[1][0]); nrec[1][1]=min(rec[1][1]+now[1][0],rec[1][0]+now[1][1]); rec=nrec; } if (a[v]) { ans[0][0] = rec[0][1]; ans[0][1] = 1+rec[1][0]; ans[1][0] = rec[0][0]; ans[1][1] = 1+rec[1][1]; } else { ans[0][0] = rec[0][0]; ans[0][1] = 1+rec[1][1]; ans[1][0] = rec[0][1]; ans[1][1] = 1+rec[1][0]; } // cout<<v<<" "<<rec[0][0]<<" "<<rec[0][1]<<" "<<rec[1][0]<<" "<<rec[1][1]<<" v rec\n"; // cout<<v<<" "<<ans[0][0]<<" "<<ans[0][1]<<" "<<ans[1][0]<<" "<<ans[1][1]<<" v ans\n"; return ans; } int main() { speed; int n; cin>>n; vvector<int> G(n+1); for (int i=0;i<n-1;i++) { int a,b; cin>>a>>b; G[a].push_back(b); G[b].push_back(a); } vector<int> a(n+1); for (int i=1;i<=n;i++) { cin>>a[i]; } vvector<int> ans = dfs(1,0,G,a); int fin = min(ans[0][0],ans[0][1]); if (fin>=INF) cout<<"impossible\n"; else cout<<fin<<"\n"; return 0; }
#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...