답안 #535572

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535572 2022-03-10T14:01:56 Z perchuts Love Polygon (BOI18_polygon) C++17
54 / 100
287 ms 22708 KB
#include <bits/stdc++.h>
#define maxn (int)(1e5+51)
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define ll long long
#define pb push_back
#define ull unsigned long long
#define ii pair<int,int>
#define iii tuple<int,int,int>
#define inf 2000000001
#define mod 1000000007 //998244353
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

using namespace std;

template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }

map<string,int>mp;
int par[maxn], cmp[maxn], qnt[maxn], ans;
vector<int>g[maxn];
int vis[maxn], mark[maxn];

void dfs(int u,int p){
    vis[u] = 2;
    for(auto v:g[u]){
        if(vis[v]==2)continue;
        dfs(v,u);
    }
    if(!mark[u])ans++, mark[p] = 1;
}

void dfs(int u){
    mark[u] = 1;
    ckmax(vis[u],1);
    for(auto v:g[u]){
        if(!mark[v])dfs(v);
    }
}

int main(){_
    int n;cin>>n;
    if(n&1){
        cout<<-1<<endl;
        return 0;
    }
    int q = 0;
    for(int i=1;i<=n;++i){
        string a,b;cin>>a>>b;
        if(mp.find(a)==mp.end())mp[a]=++q;
        if(mp.find(b)==mp.end())mp[b]=++q;
        int u = mp[a], v = mp[b];
        par[u] = v;
        g[v].pb(u);
    }   
    int components = 0;
    for(int i=1;i<=n;++i){
        if(!vis[i]){
            int cur = i, cnt = 1;
            while(!vis[cur])vis[cur] = 1, cur = par[cur];
            int start = cur;
            ++components, cmp[cur] = components, vis[cur] = 2, cur = par[cur];
            while(cur!=start)
                cmp[cur] = components,++cnt, vis[cur] = 2, cur = par[cur];
            qnt[components] = cnt&1;
            if(cnt!=2)ans += (cnt+1)/2;
            dfs(start);
        }
    }   
    for(int i=1;i<=n;++i)mark[i] = 0;

    for(int i=1;i<=n;++i){
        if(vis[i]==1){
            int root = i;
            while(vis[root]==1)root = par[root];
            dfs(root,root);
            if(mark[root])ans -= qnt[cmp[root]], qnt[cmp[root]] = 0;
        }       
    }        
    cout<<ans<<endl;
    

}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2696 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Incorrect 1 ms 2644 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2756 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 240 ms 19984 KB Output is correct
5 Correct 243 ms 17356 KB Output is correct
6 Correct 226 ms 20428 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 255 ms 16604 KB Output is correct
2 Correct 251 ms 17928 KB Output is correct
3 Correct 163 ms 17688 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 287 ms 22708 KB Output is correct
6 Correct 225 ms 15564 KB Output is correct
7 Correct 204 ms 15688 KB Output is correct
8 Correct 243 ms 16340 KB Output is correct
9 Correct 192 ms 14856 KB Output is correct
10 Correct 146 ms 14124 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2696 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Incorrect 1 ms 2644 KB Output isn't correct
5 Halted 0 ms 0 KB -