Submission #696921

#TimeUsernameProblemLanguageResultExecution timeMemory
696921Tien_NoobThe Xana coup (BOI21_xanadu)C++17
100 / 100
68 ms23172 KiB
//Make CSP great again //Vengeance #include <bits/stdc++.h> #define TASK "TESTCODE" using namespace std; const int N = 1e5; int dp[N + 1][2][2]; vector<int> adj[N + 1]; int a[N + 1], n; void read() { cin >> n; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; ++ i) { cin >> a[i]; } } void DFS(int u, int p) { memset(dp[u], 0x3f, sizeof(dp[u])); dp[u][a[u]][0] = 0; dp[u][1 ^ a[u]][1] = 1; for (int v : adj[u]) { if (v == p) { continue; } DFS(v, u); int nw[2][2]; memset(nw, 0x3f, sizeof(nw)); for (int i1 = 0; i1 < 2; ++ i1) { for (int j1 = 0; j1 < 2; ++j1) { for (int i2 = 0; i2 < 2; ++ i2) { for (int j2 = 0; j2 < 2; ++ j2) { int t = j1 ^ j2; if (i2 ^ j1) { continue; } nw[i1 ^ j2][j1] = min(nw[i1 ^ j2][j1], dp[u][i1][j1] + dp[v][i2][j2]); } } } } for (int i1 = 0; i1 < 2; ++ i1) { for (int j1 = 0; j1 < 2; ++ j1) { dp[u][i1][j1] = nw[i1][j1]; } } } /*cerr << u << '\n'; for (int i1 = 0; i1 < 2; ++ i1) { for (int j1 = 0; j1 < 2; ++ j1) { cerr << dp[u][i1][j1] << ' '; } } cerr << '\n';*/ } void solve() { DFS(1, 0); int res = min(dp[1][0][0], dp[1][0][1]); if (res <= n) { cout << res; return ; } cout << "impossible"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); if (fopen(TASK".INP", "r")) { freopen(TASK".INP", "r", stdin); //freopen(TASK".OUT", "w", stdout); } int t = 1; bool typetest = false; if (typetest) { cin >> t; } for (int __ = 1; __ <= t; ++ __) { //cout << "Case " << __ << ": "; read(); solve(); } }

Compilation message (stderr)

xanadu.cpp: In function 'void DFS(int, int)':
xanadu.cpp:47:29: warning: unused variable 't' [-Wunused-variable]
   47 |                         int t = j1 ^ j2;
      |                             ^
xanadu.cpp: In function 'int main()':
xanadu.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...