답안 #485533

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485533 2021-11-08T04:33:24 Z sam571128 Love Polygon (BOI18_polygon) C++17
25 / 100
269 ms 23684 KB
#include <bits/stdc++.h>

#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int N = 1e5+5;
int visited[N], arr[N], sz[N], visited2[N], check[N];
vector<int> adj[N];

void dfs(int u){
    if(arr[u]==u) check[u] = 1;
    sz[u] = 1;
    visited[u] = 1;
    for(auto v : adj[u]){
        if(visited[v]) continue;
        dfs(v);
        sz[u] += sz[v];
        check[u] |= check[v];
    }
}

bool dfs2(int u){
    if(adj[u].size() >= 2) return true;
    visited2[u] = 1;
    for(auto v : adj[u]){
        if(visited2[v]) continue;
        if(dfs2(v)) return true;
    }
    return false;
}

int idx;
map<string,int> m;

int assign(string x){
    if(m.find(x)==m.end()){
        m[x] = idx++;
    }
    return m[x];
}

signed main(){
    fastio
    int n;
    cin >> n;

    for(int i = 0;i < n;i++){
        string a,b;
        cin >> a >> b;
        int x = assign(a);
        int y = assign(b);
        arr[x] = y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

    if(n%2){
        cout << -1 << "\n";
        return 0;
    }

    
    int ans = 0;
    for(int i = 0; i < n; i++){
        if(!visited[i]){
            dfs(i); 
            bool tmp = false;
            if(check[i]) tmp |= dfs2(i);
            if(tmp) ans++;
            if(sz[i]>2){
                ans += sz[i]/2; 
                if(sz[i]%2&&!tmp) ans++;
            }

        }
    }
    cout << ans << "\n";
}   
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2636 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 2 ms 2672 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 269 ms 23376 KB Output is correct
5 Correct 228 ms 17604 KB Output is correct
6 Correct 217 ms 23684 KB Output is correct
7 Correct 210 ms 15164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 224 ms 17236 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2636 KB Output isn't correct
2 Halted 0 ms 0 KB -