Submission #467843

#TimeUsernameProblemLanguageResultExecution timeMemory
467843OzyLove Polygon (BOI18_polygon)C++17
54 / 100
342 ms26036 KiB
#include <iostream> #include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i = (a); i <= (b); i++) #define repa(i,a,b) for(int i = (a); i >= (b); i--) #define lli long long int #define debug(a) cout << #a << " = " << a << endl #define debugsl(a) cout << #a << " = " << a << ", " #define MAX 100000 pair<lli,lli> ult; lli n,a,b,cont,res,cant; lli dp[2][MAX+2],dir[MAX+2],visitados[MAX+2]; string A,B; map<string, lli> mapa; vector<lli> hijos[MAX+2]; void dfs(lli pos, lli marca) { visitados[pos] = marca; dp[0][pos] = 0; dp[1][pos] = 0; lli MIN = INT_MAX; lli sum = 0; lli x; bool tengoH = false; for(auto h : hijos[pos]) { if (visitados[h] == marca) continue; tengoH = true; dfs(h,marca); sum += dp[1][h]; x = dp[0][h] - dp[1][h]; if (x < MIN) MIN = x; } if (!tengoH) { dp[0][pos] = 0; dp[1][pos] = 1; return; } dp[0][pos] = sum; sum += MIN; dp[1][pos] = sum + 1; } pair<lli,lli> busca(lli pos) { while (visitados[pos] == 0) { visitados[pos] = 1; if (visitados[dir[pos]] == 1) { if (pos != dir[pos] && dir[dir[pos]] == pos) res--; return {pos,dir[pos]}; } pos = dir[pos]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; cont = 1; rep(i,1,n) { cin >> A >> B; if (mapa.find(A) == mapa.end()) mapa[A] = cont++; if (mapa.find(B) == mapa.end()) mapa[B] = cont++; a = mapa[A]; b = mapa[B]; dir[a] = b; hijos[b].push_back(a); hijos[a].push_back(b); } if (n&1) { cout << "-1"; return 0; } res = 0; rep(i,1,n) if (visitados[i] == 0) { ult = busca(i); dfs(ult.first, 2); a = dp[1][ult.first]; if (ult.second != ult.first) { dfs(ult.second, 3); a = min(a, dp[1][ult.second]); } res += a; } cout << res; }

Compilation message (stderr)

polygon.cpp: In function 'std::pair<long long int, long long int> busca(long long int)':
polygon.cpp:63:1: warning: control reaches end of non-void function [-Wreturn-type]
   63 | }
      | ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...