이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e5+10;
int n, a[maxn];
vector<int> g[maxn], mark[maxn][2][2], dp[maxn][2][2];
int sol(int u, int qtdtr, int tr, int id, int ant) {
if(mark[u][qtdtr][tr][id]) return dp[u][qtdtr][tr][id];
mark[u][qtdtr][tr][id] = 1;
if(id == (int) g[u].size()) {
if((qtdtr^a[u]) == 0) dp[u][qtdtr][tr][id] = 0;
else dp[u][qtdtr][tr][id] = inf;
// cout << u << " " << qtdtr << " " << tr << " " << id << " " << ant << " " << dp[u][qtdtr][tr][id] << endl;
return dp[u][qtdtr][tr][id];
}
int v = g[u][id];
if(v == ant) return dp[u][qtdtr][tr][id] = sol(u,qtdtr,tr,id+1,ant);
int ans = inf;
dp[u][qtdtr][tr][id] = inf;
//nao troco o v
dp[u][qtdtr][tr][id] = min(dp[u][qtdtr][tr][id],sol(u,qtdtr,tr,id+1,ant)+sol(v,tr,0,0,u));
//troco o v
dp[u][qtdtr][tr][id] = min(dp[u][qtdtr][tr][id],sol(u,qtdtr^1,tr,id+1,ant)+sol(v,tr^1,1,0,u)+1);
// cout << u << " " << qtdtr << " " << tr << " " << id << " " << dp[u][qtdtr][tr][id] << endl;
return dp[u][qtdtr][tr][id];
}
void solve() {
cin >> n;
for(int i = 1; i <= n-1; i++) {
int u,v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) {
for(int j1 = 0; j1 <= 1; j1++) {
for(int j2 = 0; j2 <= 1; j2++) {
mark[i][j1][j2].pb({});
dp[i][j1][j2].pb({});
for(int k = 0; k <= (int) g[i].size()+3; k++) {
mark[i][j1][j2].pb(0);
dp[i][j1][j2].pb(0);
}
}
}
}
int ans = min(sol(1,0,0,0,0),sol(1,1,1,0,0)+1);
if(ans >= n+1) cout << "impossible" << endl;
else cout << ans << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp: In function 'long long int sol(long long int, long long int, long long int, long long int, long long int)':
xanadu.cpp:32:9: warning: unused variable 'ans' [-Wunused-variable]
32 | int ans = inf;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |