Submission #490603

# Submission time Handle Problem Language Result Execution time Memory
490603 2021-11-28T09:41:08 Z radal Love Polygon (BOI18_polygon) C++14
100 / 100
481 ms 55004 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
#pragma GCC target("avx2,fma")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
typedef pair<long double,long double> pld;
const long long int N = 2e5+10,mod = 1e9+7,inf = 1e9,sq = 700,sig = 26;
inline int mkay(int a,int b){
    if (a+b >= mod) return a+b-mod;
    if (a+b < 0) return a+b+mod;
    return a+b;
}
inline int poww(int n,int k){
    int c = 1;
    while (k){
        if (k&1) c = (1ll*c*n)%mod;
        n = (1ll*n*n)%mod;
        k >>= 1;
    }
    return c;
}
map<string,int> mp;
vector<int> in[N],out[N];
int o,ans;
bool vis[N];
int mark[N],comp[N],p[N];
bool b[N];
void sfd(int v){
    for (int u : in[v]){
        if (u == v || mark[u] == 2) continue;
        sfd(u);
        if (!vis[v] && !vis[u]){
            vis[v] = 1;
            p[v] = u;
            p[u] = v;
            vis[u] = 1;
            ans++;
            o--;
        }
    }
    if (!vis[v])
        o++;
    else if (mark[v] == 2)
        b[comp[v]] = 1;
}
void dfs(int v,int t){
    comp[v] = t;
    for (int u : out[v]) if (!comp[u]) dfs(u,t);
    for (int u : in[v]) if (!comp[u]) dfs(u,t);
}
int main(){
    ios :: sync_with_stdio(0); cin.tie(0);cout.tie(0);
    int n;
    cin >> n;
    if(n&1){
        cout << -1;
        return 0;
    }
    set<string> st;
    int sz = 0;
    rep(i,1,n+1){
        string s,t;
        cin >> s >> t;
        if (st.find(s) == st.end()){
            sz++;
            mp[s] = sz;
            st.insert(s);
        }
        if (st.find(t) == st.end()){
            sz++;
            mp[t] = sz;
            st.insert(t);
        }
        in[mp[t]].pb(mp[s]);
        out[mp[s]].pb(mp[t]);
    }
    int t = 0;
    rep(i,1,n+1){
        if(comp[i]) continue;
        int v = i;
        while (mark[v] == 0){
            mark[v] = 1;
            v = out[v][0];
        }
        int u = v;
        while (mark[u] != 2){
            mark[u] = 2;
            u = out[u][0];
        }
        t++;
        dfs(u,t);
    }

    rep(i,1,n+1)
        if (!vis[i] && mark[i] == 2)
            sfd(i);
    rep(i,1,n+1){
        if (mark[i] != 2) continue;
        if (out[out[i][0]][0] == i && i != out[i][0]){
            if (i > out[i][0]) continue;
            int v = out[i][0];
            if (!vis[i] && !vis[v])
                o -= 2;
            else if (vis[i] && vis[v])
                continue;
            else
                ans--;
            continue;
        }
        if (!b[comp[i]]){
            int s = 1;
            int v = out[i][0];
            while(v != i){
                s++;
                v = out[v][0];
                vis[v] = 1;
            }
            if (s == 2) o -= 2;
            else{
                if (s&1) o -= (s-1);
                else o -= s;
                ans += s/2;
            }
            b[comp[i]] = 1;
            continue;
        }
        if (!vis[i] || vis[out[i][0]]) continue;
        int v = out[i][0];
        while (vis[v] != 1){
            if (!vis[out[v][0]]){
                vis[v] = 1;
                vis[out[v][0]] = 1;
                p[v] = out[v][0];
                p[out[v][0]] = v;
                ans ++;
                o -= 2;
                v = out[out[v][0]][0];
            }
            else break;
        }
    }
    cout << o+ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 6 ms 9708 KB Output is correct
4 Correct 6 ms 9668 KB Output is correct
5 Correct 5 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 6 ms 9712 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 5 ms 9644 KB Output is correct
10 Correct 5 ms 9676 KB Output is correct
11 Correct 6 ms 9708 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 6 ms 9712 KB Output is correct
14 Correct 5 ms 9708 KB Output is correct
15 Correct 7 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 6 ms 9712 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 456 ms 52300 KB Output is correct
5 Correct 376 ms 34616 KB Output is correct
6 Correct 481 ms 55004 KB Output is correct
7 Correct 5 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 364 ms 34244 KB Output is correct
2 Correct 393 ms 36384 KB Output is correct
3 Correct 283 ms 34628 KB Output is correct
4 Correct 5 ms 9676 KB Output is correct
5 Correct 381 ms 42204 KB Output is correct
6 Correct 367 ms 33196 KB Output is correct
7 Correct 344 ms 33616 KB Output is correct
8 Correct 313 ms 33820 KB Output is correct
9 Correct 321 ms 32864 KB Output is correct
10 Correct 239 ms 31444 KB Output is correct
11 Correct 5 ms 9676 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 5 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 6 ms 9708 KB Output is correct
4 Correct 6 ms 9668 KB Output is correct
5 Correct 5 ms 9676 KB Output is correct
6 Correct 5 ms 9676 KB Output is correct
7 Correct 6 ms 9712 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 5 ms 9644 KB Output is correct
10 Correct 5 ms 9676 KB Output is correct
11 Correct 6 ms 9708 KB Output is correct
12 Correct 5 ms 9676 KB Output is correct
13 Correct 6 ms 9712 KB Output is correct
14 Correct 5 ms 9708 KB Output is correct
15 Correct 7 ms 9676 KB Output is correct
16 Correct 5 ms 9676 KB Output is correct
17 Correct 6 ms 9712 KB Output is correct
18 Correct 5 ms 9676 KB Output is correct
19 Correct 456 ms 52300 KB Output is correct
20 Correct 376 ms 34616 KB Output is correct
21 Correct 481 ms 55004 KB Output is correct
22 Correct 5 ms 9676 KB Output is correct
23 Correct 364 ms 34244 KB Output is correct
24 Correct 393 ms 36384 KB Output is correct
25 Correct 283 ms 34628 KB Output is correct
26 Correct 5 ms 9676 KB Output is correct
27 Correct 381 ms 42204 KB Output is correct
28 Correct 367 ms 33196 KB Output is correct
29 Correct 344 ms 33616 KB Output is correct
30 Correct 313 ms 33820 KB Output is correct
31 Correct 321 ms 32864 KB Output is correct
32 Correct 239 ms 31444 KB Output is correct
33 Correct 5 ms 9676 KB Output is correct
34 Correct 5 ms 9676 KB Output is correct
35 Correct 5 ms 9676 KB Output is correct
36 Correct 5 ms 9676 KB Output is correct
37 Correct 397 ms 34756 KB Output is correct
38 Correct 354 ms 37012 KB Output is correct
39 Correct 351 ms 33724 KB Output is correct
40 Correct 372 ms 33336 KB Output is correct
41 Correct 347 ms 33348 KB Output is correct
42 Correct 357 ms 33800 KB Output is correct
43 Correct 364 ms 33828 KB Output is correct
44 Correct 375 ms 33928 KB Output is correct
45 Correct 363 ms 33892 KB Output is correct
46 Correct 384 ms 33864 KB Output is correct
47 Correct 333 ms 33028 KB Output is correct
48 Correct 383 ms 34244 KB Output is correct
49 Correct 392 ms 36188 KB Output is correct
50 Correct 284 ms 34676 KB Output is correct
51 Correct 7 ms 9804 KB Output is correct
52 Correct 405 ms 42208 KB Output is correct
53 Correct 361 ms 33220 KB Output is correct
54 Correct 342 ms 33616 KB Output is correct
55 Correct 322 ms 34040 KB Output is correct
56 Correct 330 ms 32648 KB Output is correct
57 Correct 224 ms 31376 KB Output is correct
58 Correct 5 ms 9676 KB Output is correct
59 Correct 5 ms 9716 KB Output is correct
60 Correct 5 ms 9676 KB Output is correct
61 Correct 5 ms 9676 KB Output is correct
62 Correct 5 ms 9676 KB Output is correct
63 Correct 5 ms 9676 KB Output is correct
64 Correct 5 ms 9676 KB Output is correct
65 Correct 416 ms 52276 KB Output is correct
66 Correct 368 ms 34628 KB Output is correct
67 Correct 409 ms 54896 KB Output is correct
68 Correct 5 ms 9720 KB Output is correct
69 Correct 5 ms 9716 KB Output is correct
70 Correct 5 ms 9676 KB Output is correct
71 Correct 5 ms 9676 KB Output is correct
72 Correct 5 ms 9712 KB Output is correct
73 Correct 5 ms 9676 KB Output is correct
74 Correct 5 ms 9712 KB Output is correct
75 Correct 6 ms 9648 KB Output is correct
76 Correct 6 ms 9676 KB Output is correct
77 Correct 5 ms 9676 KB Output is correct
78 Correct 5 ms 9676 KB Output is correct
79 Correct 5 ms 9676 KB Output is correct
80 Correct 5 ms 9676 KB Output is correct
81 Correct 5 ms 9676 KB Output is correct
82 Correct 5 ms 9676 KB Output is correct
83 Correct 5 ms 9676 KB Output is correct