제출 #880957

#제출 시각아이디문제언어결과실행 시간메모리
880957vjudge1Love Polygon (BOI18_polygon)C++17
0 / 100
64 ms23220 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long 
#define pb push_back
#define pii array<int, 2>

const int N = 1e5 + 4;

int m, out[N];
bool vis[N];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    vector<string> all;
    vector<array<string, 2>> edges;
    cin >> m;
    for (int i = 0; i < m; ++i) {
        string v, u;
        cin >> v >> u;
        edges.pb({v, u});
        all.pb(v);
        all.pb(u);
    }

    unordered_map<string, int> match;
    int last = 1;
    for (string s : all) {
        if (match[s] == 0) {
            match[s] = last++;
        }
    }

    for (auto [v, u] : edges) {
        out[match[v]] = match[u];
    }

    int ans = 0, odd = 0;
    for (int i = 1; i < last; ++i) {
        if (vis[i] == false) {
            int cur = i, cnt = 0;
            while (vis[cur] == false) {
                vis[cur] = true;
                cur = out[cur];
                ++cnt;
            }
            if (cnt != 2) {
                ans += cnt / 2;
                odd += cnt % 2;
            }
        }
    }

    if (odd % 2 == 1) {
        cout << -1;
    }
    else {
        cout << ans + odd / 2;
    }

    return 0;
}

#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…