답안 #393299

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
393299 2021-04-23T07:01:13 Z jainbot27 Love Polygon (BOI18_polygon) C++17
100 / 100
83 ms 16216 KB
#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int) x.size()
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)

#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define ROF(i, a, b) for(int i=(b-1); i>=(a); i--)
#define F0R(i, n) FOR(i, 0, n)
#define R0F(i, n) ROF(i, 0, n)
#define trav(x, y) for(auto&x:y)

using ll=long long;
using ld=long double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using pli=pair<ll, int>;
using vi=vector<int>;
using vl=vector<ll>;
using vpii=vector<pii>;

template<class T> inline bool ckmin(T&a, const T&b) {return b<a?a=b,1:0;}
template<class T> inline bool ckmax(T&a, const T&b) {return b>a?a=b,1:0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const char nl='\n';
const int mxN=1e5+10;
const int MOD=1e9+7;
const ll infLL=1e18;
const ld eps=1e-6;

int n; 
string s[mxN], t[mxN];
unordered_map<string, int> mp;
int a[mxN];
int deg[mxN];
int par[mxN], sz[mxN];
bool alive[mxN];
int used[mxN];
queue<int> q;
int ans=0;

int Find(int u){
    return par[u]=(par[u]==u?par[u]:Find(par[u]));
}
bool comb(int e1, int e2){
    e1=Find(e1); e2=Find(e2); 
    if(e1==e2) return 0;
    if(sz[e1]<sz[e2]) swap(e1, e2);
    sz[e1]+=sz[e2]; sz[e2]=0;
    par[e2]=e1;
    return 1;
}

int32_t main(){
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    if(n&1) {cout << "-1\n"; return 0;}
    F0R(i, n) par[i]=i, sz[i]=1;
    F0R(i, n)
        cin >> s[i] >> t[i], mp[s[i]]=i;
    F0R(i, n){
        a[i]=mp[t[i]];
        deg[a[i]]++;
        alive[i]=1;
    }    
    F0R(i, n){
        if(alive[i]&&a[i]!=i&&a[a[i]]==i){
            alive[a[i]]=0;
            alive[i]=0;
            deg[a[i]]--;
            deg[i]--;
        }
    }
    F0R(i, n)
        if(deg[i]==0){
            q.push(i);
        }       
    while(!q.empty()){
        auto T=q.front(); q.pop();
        assert(deg[T]==0);
        if(alive[T]==0) continue;
        if(alive[a[T]]==0) continue;
        alive[T]=0;
        alive[a[T]]=0;
        ans++;
        if(alive[a[a[T]]]) {
            deg[a[a[T]]]--;
            if(deg[a[a[T]]]==0)
                q.push(a[a[T]]);
        }
    }
    F0R(i, n){
        if(alive[i]&&alive[a[i]]){
            comb(a[i], i);
        }
    }
    F0R(i, n){
        if(alive[i]&&!used[Find(i)]){
            used[Find(i)]=1;
            ans+=(sz[Find(i)]+1)/2;
        }
    }
    cout << ans << nl;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6604 KB Output is correct
2 Correct 4 ms 6604 KB Output is correct
3 Correct 4 ms 6604 KB Output is correct
4 Correct 4 ms 6604 KB Output is correct
5 Correct 4 ms 6476 KB Output is correct
6 Correct 4 ms 6476 KB Output is correct
7 Correct 4 ms 6604 KB Output is correct
8 Correct 4 ms 6604 KB Output is correct
9 Correct 4 ms 6604 KB Output is correct
10 Correct 4 ms 6604 KB Output is correct
11 Correct 4 ms 6604 KB Output is correct
12 Correct 4 ms 6604 KB Output is correct
13 Correct 4 ms 6604 KB Output is correct
14 Correct 4 ms 6604 KB Output is correct
15 Correct 4 ms 6604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6604 KB Output is correct
2 Correct 4 ms 6604 KB Output is correct
3 Correct 4 ms 6604 KB Output is correct
4 Correct 76 ms 15452 KB Output is correct
5 Correct 75 ms 15860 KB Output is correct
6 Correct 73 ms 15420 KB Output is correct
7 Correct 4 ms 6476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 16012 KB Output is correct
2 Correct 82 ms 15848 KB Output is correct
3 Correct 68 ms 15772 KB Output is correct
4 Correct 4 ms 6476 KB Output is correct
5 Correct 70 ms 15464 KB Output is correct
6 Correct 71 ms 16048 KB Output is correct
7 Correct 72 ms 16076 KB Output is correct
8 Correct 73 ms 15960 KB Output is correct
9 Correct 65 ms 16184 KB Output is correct
10 Correct 56 ms 15860 KB Output is correct
11 Correct 4 ms 6604 KB Output is correct
12 Correct 4 ms 6604 KB Output is correct
13 Correct 4 ms 6604 KB Output is correct
14 Correct 4 ms 6604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6604 KB Output is correct
2 Correct 4 ms 6604 KB Output is correct
3 Correct 4 ms 6604 KB Output is correct
4 Correct 4 ms 6604 KB Output is correct
5 Correct 4 ms 6476 KB Output is correct
6 Correct 4 ms 6476 KB Output is correct
7 Correct 4 ms 6604 KB Output is correct
8 Correct 4 ms 6604 KB Output is correct
9 Correct 4 ms 6604 KB Output is correct
10 Correct 4 ms 6604 KB Output is correct
11 Correct 4 ms 6604 KB Output is correct
12 Correct 4 ms 6604 KB Output is correct
13 Correct 4 ms 6604 KB Output is correct
14 Correct 4 ms 6604 KB Output is correct
15 Correct 4 ms 6604 KB Output is correct
16 Correct 4 ms 6604 KB Output is correct
17 Correct 4 ms 6604 KB Output is correct
18 Correct 4 ms 6604 KB Output is correct
19 Correct 76 ms 15452 KB Output is correct
20 Correct 75 ms 15860 KB Output is correct
21 Correct 73 ms 15420 KB Output is correct
22 Correct 4 ms 6476 KB Output is correct
23 Correct 78 ms 16012 KB Output is correct
24 Correct 82 ms 15848 KB Output is correct
25 Correct 68 ms 15772 KB Output is correct
26 Correct 4 ms 6476 KB Output is correct
27 Correct 70 ms 15464 KB Output is correct
28 Correct 71 ms 16048 KB Output is correct
29 Correct 72 ms 16076 KB Output is correct
30 Correct 73 ms 15960 KB Output is correct
31 Correct 65 ms 16184 KB Output is correct
32 Correct 56 ms 15860 KB Output is correct
33 Correct 4 ms 6604 KB Output is correct
34 Correct 4 ms 6604 KB Output is correct
35 Correct 4 ms 6604 KB Output is correct
36 Correct 4 ms 6604 KB Output is correct
37 Correct 73 ms 15900 KB Output is correct
38 Correct 79 ms 16012 KB Output is correct
39 Correct 83 ms 15952 KB Output is correct
40 Correct 74 ms 16024 KB Output is correct
41 Correct 70 ms 16092 KB Output is correct
42 Correct 68 ms 15972 KB Output is correct
43 Correct 69 ms 16044 KB Output is correct
44 Correct 71 ms 16040 KB Output is correct
45 Correct 75 ms 15980 KB Output is correct
46 Correct 72 ms 15964 KB Output is correct
47 Correct 68 ms 16152 KB Output is correct
48 Correct 74 ms 15976 KB Output is correct
49 Correct 80 ms 15908 KB Output is correct
50 Correct 67 ms 15880 KB Output is correct
51 Correct 5 ms 6592 KB Output is correct
52 Correct 68 ms 15456 KB Output is correct
53 Correct 69 ms 16076 KB Output is correct
54 Correct 72 ms 16040 KB Output is correct
55 Correct 68 ms 15928 KB Output is correct
56 Correct 70 ms 16216 KB Output is correct
57 Correct 55 ms 15876 KB Output is correct
58 Correct 4 ms 6604 KB Output is correct
59 Correct 4 ms 6476 KB Output is correct
60 Correct 4 ms 6604 KB Output is correct
61 Correct 4 ms 6604 KB Output is correct
62 Correct 4 ms 6604 KB Output is correct
63 Correct 4 ms 6476 KB Output is correct
64 Correct 4 ms 6604 KB Output is correct
65 Correct 80 ms 15488 KB Output is correct
66 Correct 74 ms 15852 KB Output is correct
67 Correct 82 ms 15484 KB Output is correct
68 Correct 4 ms 6476 KB Output is correct
69 Correct 4 ms 6604 KB Output is correct
70 Correct 4 ms 6604 KB Output is correct
71 Correct 4 ms 6604 KB Output is correct
72 Correct 4 ms 6604 KB Output is correct
73 Correct 4 ms 6476 KB Output is correct
74 Correct 4 ms 6476 KB Output is correct
75 Correct 4 ms 6604 KB Output is correct
76 Correct 4 ms 6604 KB Output is correct
77 Correct 4 ms 6532 KB Output is correct
78 Correct 4 ms 6604 KB Output is correct
79 Correct 4 ms 6604 KB Output is correct
80 Correct 4 ms 6604 KB Output is correct
81 Correct 4 ms 6604 KB Output is correct
82 Correct 4 ms 6604 KB Output is correct
83 Correct 4 ms 6604 KB Output is correct