답안 #944226

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
944226 2024-03-12T11:47:02 Z LucaIlie Love Polygon (BOI18_polygon) C++17
25 / 100
140 ms 24632 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5;
int n;
bool vis[MAX_N + 1], isOnCycle[MAX_N + 1], isUnpaired[MAX_N + 1];
int repr[MAX_N + 1], parent[MAX_N + 1], sizee[MAX_N + 1], dp[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 ) {
    int odd = 0;
    sizee[u] = 1;
    for ( int v: adj[u] ) {
        if ( isOnCycle[v] )
            continue;

        calc( v );

        dp[u] += dp[v];
        sizee[u] += sizee[v];
        if ( sizee[v] % 2 == 1 )
            odd++;
    }
    dp[u] += odd;
}

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();

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

        calc( u );
        ans += dp[u];
        if ( sizee[u] % 2 == 1 )
            isUnpaired[u] = true;
    }

    for ( int v = 1; v <= n; v++ ) {
        if ( parent[v] != v && isUnpaired[v] ) {
            vector<int> cycle;
            int u = v;
            do {
                cycle.push_back( u );
                u = parent[u];
            } while ( u != v );

            if ( cycle.size() == 2 )
                isUnpaired[cycle[0]] = isUnpaired[cycle[1]] = false;
            if ( cycle.size() <= 2 )
                continue;

            for ( int i = 0; i < cycle.size(); i++ ) {
                if ( isUnpaired[cycle[i]] && isUnpaired[cycle[(i + 1) % cycle.size()]] ) {
                    isUnpaired[cycle[i]] = isUnpaired[cycle[(i + 1) % cycle.size()]] = false;
                    ans++;
                }
            }
        }
    }
    for ( int v = 1; v <= n; v++ ) {
        if ( isUnpaired[parent[v]] )
            ans++;
    }

    cout << ans << "\n";

    return 0;
}

Compilation message

polygon.cpp: In function 'int main()':
polygon.cpp:103:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |             for ( int i = 0; i < cycle.size(); i++ ) {
      |                              ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Incorrect 1 ms 2652 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 140 ms 21572 KB Output is correct
5 Correct 106 ms 17348 KB Output is correct
6 Correct 116 ms 24632 KB Output is correct
7 Correct 97 ms 16044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 104 ms 14536 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Incorrect 1 ms 2652 KB Output isn't correct
4 Halted 0 ms 0 KB -