답안 #376999

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
376999 2021-03-12T16:50:53 Z lertwqen243 Love Polygon (BOI18_polygon) C++14
25 / 100
190 ms 30340 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;

int n,love[N];
int ind;
unordered_map<string,int>id;
vector <int> g[N],adj[N];
bool mrk[N];
vector<int>ord,roots;
void dfs(int v){
    mrk[v]=1;
    for (int i=0;i<(int)g[v].size();i++){
        int u=g[v][i];
        if (!mrk[u]){
            adj[v].push_back(u); 
            dfs(u);
        }
    }
    ord.push_back(v);
    return;
}
int used[N];
int max_maching;
bool mark[N];
void maching_dfs(int v, int par){
    for (int i=0;i<(int)adj[v].size();i++){
        int u=adj[v][i];
        maching_dfs(u,v);
    }
    if (!used[v] && !used[par] && par!=0){
        max_maching++;
        used[v]=used[par]=1;
    }
}
inline int solve1(int root){
    max_maching = 0;
    for (int i=0;i<(int)ord.size();i++){
        int v=ord[i];
        used[v]=0;
    }
    maching_dfs(root,0);
    return max_maching;
}
inline int solve2(int root){
    if (love[root]==root) return 0;
    for (int i=0;i<(int)ord.size();i++){
        int v=ord[i];
        used[v]=0;
    }
    used[root]=used[love[root]]=1;
    max_maching=0;
    maching_dfs(root,0);
    return max_maching+1+(int)(love[love[root]]==root);
}
int main() {
    ios_base::sync_with_stdio(0),cin.tie(0);cout.tie(0);
    cin>>n;
    if (n%2){
        cout<<"-1\n";
        return 0;
    }
    for (int i=1;i<=n;i++){
        string a,b;
        cin>>a>>b;
        if (id.count(a)==0){
            id[a]=++ind;
        }
        if (id.count(b)==0){
            id[b]=++ind;
        }
        int u=id[a];
        int v=id[b];
        love[u]=v;
        g[v].push_back(u);
    }
    for (int i=1;i<=n;i++){
        mrk[i]=0;
        if(love[i]==i) roots.push_back(i);
    }
    int ans=n;
    for (int i=1;i<=n;i++){
        if (mrk[i]) continue;
        int root=i;
        mark[i]=1;
        if (!mark[love[root]]){
            root=love[root];
            mark[root]=1;
        }   
        ord.clear();dfs(root);
        ans-=max(solve1(root),solve2(root));
    }
    cout<<ans<<'\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5100 KB Output is correct
2 Correct 4 ms 5228 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Incorrect 4 ms 5100 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 3 ms 5100 KB Output is correct
4 Correct 167 ms 28816 KB Output is correct
5 Correct 158 ms 21048 KB Output is correct
6 Correct 190 ms 30340 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 154 ms 19984 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5100 KB Output is correct
2 Correct 4 ms 5228 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5100 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Incorrect 4 ms 5100 KB Output isn't correct
12 Halted 0 ms 0 KB -