답안 #944252

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944252 2024-03-12T12:52:05 Z LucaIlie Love Polygon (BOI18_polygon) C++17
54 / 100
138 ms 25708 KB
#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] = minChanges[cycle[1]][false];
        dp2[1] = minChanges[cycle[1]][true];
        //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]][false] + minChanges[cycle[i]][false] + 1 );
            dp2[i] = min( dp2[i - 1] + minChanges[cycle[i]][true], dp2[i - 2] + minChanges[cycle[i]][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 );
    }

    cout << ans << "\n";

    return 0;
}

Compilation message

polygon.cpp: In function 'int main()':
polygon.cpp:116:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |         for ( int i = 2; i < cycle.size(); i++ ) {
      |                          ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Incorrect 1 ms 4952 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 119 ms 24672 KB Output is correct
5 Correct 110 ms 18376 KB Output is correct
6 Correct 138 ms 25708 KB Output is correct
7 Correct 96 ms 17652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 15560 KB Output is correct
2 Correct 105 ms 17392 KB Output is correct
3 Correct 95 ms 16304 KB Output is correct
4 Correct 95 ms 14280 KB Output is correct
5 Correct 118 ms 23580 KB Output is correct
6 Correct 109 ms 14832 KB Output is correct
7 Correct 97 ms 14936 KB Output is correct
8 Correct 103 ms 15044 KB Output is correct
9 Correct 105 ms 14276 KB Output is correct
10 Correct 80 ms 13628 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4700 KB Output is correct
2 Correct 2 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Incorrect 1 ms 4952 KB Output isn't correct
5 Halted 0 ms 0 KB -