답안 #944245

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944245 2024-03-12T12:31:58 Z LucaIlie Love Polygon (BOI18_polygon) C++17
29 / 100
137 ms 24720 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], dp[MAX_N + 1][2];
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 );

        dp[u][false] += dp[v][true];
        sizee[u] += sizee[v];
    }

    dp[u][true] = dp[u][false] + 1;
    for ( int v: adj[u] ) {
        if ( isOnCycle[v] )
            continue;

        dp[u][true] = min( dp[u][true], dp[u][false] - dp[v][true] + dp[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 );
    }

    int ans = 0;
    for ( int v = 1; v <= n; v++ ) {
        if ( !isOnCycle[v] )
            continue;

        vector <int> cycle;
        int u = v;
        do {
            cycle.push_back( u );
            u = parent[u];
        } while ( u != v );

        if ( cycle.size() == 1 ) {
            ans += dp[cycle[0]][true];
            continue;
        }
        if ( cycle.size() == 2 ) {
            ans += dp[cycle[0]][false] + dp[cycle[1]][false];
            continue;
        }
        exit( 1 );
    }

    cout << ans << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Runtime error 1 ms 4700 KB Execution failed because the return code was nonzero
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 2 ms 4720 KB Output is correct
3 Runtime error 1 ms 4700 KB Execution failed because the return code was nonzero
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 16784 KB Output is correct
2 Correct 116 ms 18884 KB Output is correct
3 Correct 90 ms 17764 KB Output is correct
4 Correct 93 ms 15876 KB Output is correct
5 Correct 119 ms 24720 KB Output is correct
6 Correct 137 ms 16432 KB Output is correct
7 Correct 100 ms 16068 KB Output is correct
8 Correct 100 ms 16428 KB Output is correct
9 Correct 97 ms 15556 KB Output is correct
10 Correct 109 ms 14908 KB Output is correct
11 Correct 1 ms 4696 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 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Runtime error 1 ms 4700 KB Execution failed because the return code was nonzero
5 Halted 0 ms 0 KB -