이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e13;
//#define int long long
int n, a[MAXN];
vector<int> g[MAXN];
ll dp[MAXN][2][2];
void dfs(int u, int par){
dp[u][a[u]][0]=0;
dp[u][a[u]][1]=0;
Forv(v, g[u]) if(v ^ par){
dfs(v, u);
ll dpu10=dp[u][1][0];
ll dpu11=dp[u][1][1];
ll dpu00=dp[u][0][0];
ll dpu01=dp[u][0][1];
dp[u][0][0]=min(dp[u][0][0]+dp[v][0][0], dpu10+dp[v][1][1]+1);
dp[u][1][0]=min(dp[u][1][0]+dp[v][0][0], dpu00+dp[v][1][1]+1);
dp[u][0][1]=min(dp[u][0][1]+dp[v][1][0], dpu11+dp[v][0][1]+1);
dp[u][1][1]=min(dp[u][1][1]+dp[v][1][0], dpu01+dp[v][0][1]+1);
}
}
void Solve() {
cin >> n;
For(i, 1, n - 1){
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
For(i, 1, n) cin >> a[i];
For(i,0,n)For(j,0,1)For(k,0,1)dp[i][j][k]=oo;
dfs(1, 0);
if (min(dp[1][0][0],dp[1][1][1]+1) < oo)cout<<min(dp[1][0][0],dp[1][1][1]+1)<<'\n';
else cout <<"impossible";
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("BOI21_xanadu.inp", "r")) {
freopen("BOI21_xanadu.inp", "r", stdin);
freopen("BOI21_xanadu.out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t --) Solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
xanadu.cpp: In function 'int main()':
xanadu.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen("BOI21_xanadu.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
xanadu.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen("BOI21_xanadu.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |