Submission #491625

#TimeUsernameProblemLanguageResultExecution timeMemory
491625blueLogičari (COCI21_logicari)C++17
110 / 110
93 ms26588 KiB
#include <iostream> #include <vector> #include <queue> using namespace std; const long long max_n = 100'000; long long n; const long long INF = 1'000'000; long long dp[1+max_n][4]; vector<long long> children[1+max_n]; vector<long long> edge[1+max_n]; vector<long long> degree(1+max_n, 0); void compute(long long u) { // cerr << "compute " << u << '\n'; vector<long long> sm(4, 0); long long min13 = INF; long long min02 = INF; for(long long v: children[u]) { for(long long k = 0; k < 4; k++) { sm[k] += dp[v][k]; } min13 = min(min13, dp[v][1] - dp[v][3]); min02 = min(min02, dp[v][0] - dp[v][2]); } // cerr << sm[0] << ' ' << sm[1] << ' ' << sm[2] << ' ' << sm[3] << ' ' << min13 << ' ' << min02 << '\n'; dp[u][0] = 1 + sm[3] + min13; dp[u][1] = 1 + sm[3]; dp[u][2] = sm[2] + min02; dp[u][3] = sm[2]; for(long long k = 0; k < 4; k++) if(dp[u][k] >= INF) dp[u][k] = INF; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for(long long e = 1; e <= n; e++) { long long a, b; cin >> a >> b; edge[a].push_back(b); edge[b].push_back(a); degree[a]++; degree[b]++; } queue<long long> tbv; for(long long i = 1; i <= n; i++) { if(degree[i] == 1) tbv.push(i); } vector<bool> visit(1+n, 0); while(!tbv.empty()) { long long u = tbv.front(); // cerr << "bfs " << u << '\n'; tbv.pop(); visit[u] = 1; for(long long v: edge[u]) { if(degree[v] == 1) continue; children[v].push_back(u); degree[v]--; if(degree[v] == 1) tbv.push(v); } compute(u); } long long s = 1; while(visit[s]) s++; vector<long long> chain; // cerr << "s = " << s << '\n'; // cerr << "check\n"; while(1) { chain.push_back(s); visit[s] = 1; long long nv = -1; for(long long v: edge[s]) { if(visit[v]) continue; nv = v; break; } compute(s); // cerr << s << ' ' << nv << '\n'; if(nv == -1) break; s = nv; } // for(long long i = 1; i <= n; i++) // { // cerr << i << ": "; // for(long long j = 0; j < 4; j++) cerr << dp[i][j] << ' '; // cerr << '\n'; // } // for(long long c: chain) cerr << c << ' '; // cerr << '\n'; long long q = (long long)chain.size(); //current index, current state, state -1 long long dp2[q][4][4]; /* 0 = satisfied blue 1 = unsatisfied blue 2 = satisfied red 3 = unsatisfied red */ for(long long x = 0; x < 4; x++) for(long long y = 0; y < 4; y++) dp2[0][x][y] = INF; dp2[0][0][3] = dp[chain[0]][0]; dp2[0][0][1] = dp[chain[0]][1]; dp2[0][1][3] = dp[chain[0]][1]; dp2[0][2][2] = dp[chain[0]][2]; dp2[0][2][0] = dp[chain[0]][3]; dp2[0][3][2] = dp[chain[0]][3]; for(long long i = 1; i < q; i++) { for(long long y = 0; y < 4; y++) { dp2[i][0][y] = min(dp[chain[i]][0] + dp2[i-1][3][y], dp[chain[i]][1] + dp2[i-1][1][y]); dp2[i][1][y] = dp[chain[i]][1] + dp2[i-1][3][y]; dp2[i][2][y] = min(dp[chain[i]][2] + dp2[i-1][2][y], dp[chain[i]][3] + dp2[i-1][0][y]); dp2[i][3][y] = dp[chain[i]][3] + dp2[i-1][2][y]; } } long long ans = INF; for(long long x = 0; x < 4; x++) ans = min(ans, dp2[q-1][x][x]); if(ans == INF) ans = -1; cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...