Submission #944261

#TimeUsernameProblemLanguageResultExecution timeMemory
944261LucaIlieLove Polygon (BOI18_polygon)C++17
100 / 100
164 ms25620 KiB
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5; int n; bool vis[MAX_N + 1], isOnCycle[MAX_N + 1]; int repr[MAX_N + 1], parent[MAX_N + 1], sizee[MAX_N + 1], minChanges[MAX_N + 1][2], dp1[MAX_N + 1], dp2[MAX_N + 1]; vector <int> adj[MAX_N + 1]; unordered_map <string, int> nameToId; void findCycles( int u, int r ) { repr[u] = r; for ( int v: adj[u] ) { if ( repr[v] == r ) { isOnCycle[u] = true; return; } if ( repr[v] == 0 ) findCycles( v, r ); if ( isOnCycle[v] ) { isOnCycle[u] = true; return; } } } void detectCycles() { for ( int v = 1; v <= n; v++ ) vis[v] = false; for ( int v = 1; v <= n; v++ ) { if ( vis[v] ) continue; findCycles( v, v ); } } void calc( int u ) { sizee[u] = 1; for ( int v: adj[u] ) { if ( isOnCycle[v] ) continue; calc( v ); minChanges[u][false] += minChanges[v][true]; sizee[u] += sizee[v]; } minChanges[u][true] = minChanges[u][false] + 1; for ( int v: adj[u] ) { if ( isOnCycle[v] ) continue; minChanges[u][true] = min( minChanges[u][true], minChanges[u][false] - minChanges[v][true] + minChanges[v][false] + 1 ); } } int main() { cin >> n; int id = 0; for ( int i = 0; i < n; i++ ) { string s, t; cin >> s >> t; nameToId[s] = (nameToId[s] == 0 ? ++id : nameToId[s]); nameToId[t] = (nameToId[t] == 0 ? ++id : nameToId[t]); parent[nameToId[s]] = nameToId[t]; adj[nameToId[t]].push_back( nameToId[s] ); //cout << nameToId[s] << " " << nameToId[t] << "\n"; } if ( n % 2 == 1 ) { cout << -1 << "\n"; return 0; } detectCycles(); for ( int u = 1; u <= n; u++ ) { if ( !isOnCycle[u] ) continue; calc( u ); } for ( int v = 1; v <= n; v++ ) vis[v] = false; int ans = 0; for ( int v = 1; v <= n; v++ ) { if ( !isOnCycle[v] || vis[v] ) continue; vector <int> cycle; cycle.push_back( v ); int u = v; do { vis[u] = true; u = parent[u]; cycle.push_back( u ); } while ( u != v ); if ( cycle.size() == 2 ) { ans += minChanges[cycle[0]][true]; continue; } if ( cycle.size() == 3 ) { ans += minChanges[cycle[0]][false] + minChanges[cycle[1]][false]; continue; } dp1[1] = 0; dp2[1] = minChanges[cycle[1]][true]; dp1[0] = n; //printf( "%d %d %d\n", cycle[1], dp1[1], dp2[1] ); for ( int i = 2; i < cycle.size(); i++ ) { dp1[i] = min( dp1[i - 1] + minChanges[cycle[i]][true], dp1[i - 2] + minChanges[cycle[i - 1]][false] + minChanges[cycle[i]][false] + 1 ); dp2[i] = min( dp2[i - 1] + minChanges[cycle[i]][true], dp2[i - 2] + minChanges[cycle[i - 1]][false] + minChanges[cycle[i]][false] + 1 ); //printf( "%d %d %d\n", cycle[i], dp1[i], dp2[i] ); } ans += min( dp2[cycle.size() - 1], dp1[cycle.size() - 2] + minChanges[cycle.back()][false] + minChanges[cycle[1]][false] + 1 ); //printf( "baa %d %d\n", dp2[cycle.size() - 1], dp1[cycle.size() - 2] + minChanges[cycle.back()][false] + minChanges[cycle[1]][false] + 1 ); } cout << ans << "\n"; return 0; }

Compilation message (stderr)

polygon.cpp: In function 'int main()':
polygon.cpp:117:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |         for ( int i = 2; i < cycle.size(); i++ ) {
      |                          ~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...