답안 #521725

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
521725 2022-02-02T20:24:06 Z Yazan_Alattar Love Polygon (BOI18_polygon) C++14
100 / 100
200 ms 24876 KB
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <utility>
#include <cmath>
#include <numeric>
#include <assert.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 200007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

set < pair <string,int> > s; int tot;
int compress(string x)
{
    auto it = s.lower_bound(make_pair(x, 0));
    if(it == s.end() || (*it).F != x){
        s.insert({x, ++tot});
        return tot;
    }
    return (*it).S;
}

bool vist[M], oncycle[M], done[M];

int get(vector <int> v)
{
    int n = v.size();
    int ret = 0;
    for(auto i : v) ret |= i;
    if(!ret) return n / 2;
    ret = 0;
    for(int i = 0; i < n; ++i){
        int j = (i + 1) % n;
        if(v[i] && !v[j]){
            int len = 0;
            while(!v[j]){
                ++len;
                j = (j + 1) % n;
            }
            ret += len / 2;
        }
    }
    return ret;
}

vector <int> adj[M];
int n, p[M], ans, mx[M];

void calc(int node)
{
    vist[node] = 1;
    for(auto i : adj[node]) if(!oncycle[i]){
        calc(i);
        mx[node] += mx[i];
        if(!done[i]) done[node] = 1;
    }
    if(done[node]) ++mx[node];
    return;
}

int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n;
    if(n % 2) return cout << -1 << endl, 0;
    for(int i = 1; i <= n; ++i){
        string x, y;
        cin >> x >> y;
        int a = compress(x);
        int b = compress(y);
        p[a] = b;
        adj[b].pb(a);
    }
    for(int i = 1; i <= n; ++i) if(!vist[i]){
        int cur = i;
        while(!vist[cur]){
            vist[cur] = 1;
            cur = p[cur];
        }
        vector <int> cycle;
        int st = cur;
        while(1){
            cycle.pb(cur);
            oncycle[cur] = 1;
            cur = p[cur];
            if(cur == st) break;
        }
        vector <int> v;
        int ret = 0;
        for(auto j : cycle){
            calc(j);
            ret += mx[j];
            v.pb(done[j]);
        }
        if(cycle.size() == 2) ret = ret - v[0] - v[1] + 2;
        else ret += get(v);
        ans += ret;
    }
    cout << n - ans << endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 5020 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 5020 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 5024 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 2 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 172 ms 19876 KB Output is correct
5 Correct 173 ms 18692 KB Output is correct
6 Correct 179 ms 20052 KB Output is correct
7 Correct 3 ms 5024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 170 ms 18432 KB Output is correct
2 Correct 171 ms 20004 KB Output is correct
3 Correct 141 ms 18628 KB Output is correct
4 Correct 3 ms 5024 KB Output is correct
5 Correct 176 ms 24876 KB Output is correct
6 Correct 168 ms 17572 KB Output is correct
7 Correct 160 ms 17568 KB Output is correct
8 Correct 162 ms 17936 KB Output is correct
9 Correct 147 ms 17112 KB Output is correct
10 Correct 115 ms 15776 KB Output is correct
11 Correct 3 ms 5012 KB Output is correct
12 Correct 3 ms 5020 KB Output is correct
13 Correct 3 ms 5008 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 3 ms 5012 KB Output is correct
3 Correct 3 ms 5020 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 2 ms 5020 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 5024 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 2 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 2 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 172 ms 19876 KB Output is correct
20 Correct 173 ms 18692 KB Output is correct
21 Correct 179 ms 20052 KB Output is correct
22 Correct 3 ms 5024 KB Output is correct
23 Correct 170 ms 18432 KB Output is correct
24 Correct 171 ms 20004 KB Output is correct
25 Correct 141 ms 18628 KB Output is correct
26 Correct 3 ms 5024 KB Output is correct
27 Correct 176 ms 24876 KB Output is correct
28 Correct 168 ms 17572 KB Output is correct
29 Correct 160 ms 17568 KB Output is correct
30 Correct 162 ms 17936 KB Output is correct
31 Correct 147 ms 17112 KB Output is correct
32 Correct 115 ms 15776 KB Output is correct
33 Correct 3 ms 5012 KB Output is correct
34 Correct 3 ms 5020 KB Output is correct
35 Correct 3 ms 5008 KB Output is correct
36 Correct 3 ms 4940 KB Output is correct
37 Correct 187 ms 18992 KB Output is correct
38 Correct 165 ms 18048 KB Output is correct
39 Correct 170 ms 17732 KB Output is correct
40 Correct 177 ms 17588 KB Output is correct
41 Correct 168 ms 17592 KB Output is correct
42 Correct 164 ms 18004 KB Output is correct
43 Correct 164 ms 18084 KB Output is correct
44 Correct 169 ms 18104 KB Output is correct
45 Correct 171 ms 17992 KB Output is correct
46 Correct 166 ms 18060 KB Output is correct
47 Correct 148 ms 17176 KB Output is correct
48 Correct 184 ms 18492 KB Output is correct
49 Correct 179 ms 19908 KB Output is correct
50 Correct 137 ms 18628 KB Output is correct
51 Correct 3 ms 5032 KB Output is correct
52 Correct 200 ms 24820 KB Output is correct
53 Correct 165 ms 17588 KB Output is correct
54 Correct 164 ms 17568 KB Output is correct
55 Correct 167 ms 17988 KB Output is correct
56 Correct 145 ms 17068 KB Output is correct
57 Correct 108 ms 15784 KB Output is correct
58 Correct 2 ms 4940 KB Output is correct
59 Correct 3 ms 4940 KB Output is correct
60 Correct 3 ms 5016 KB Output is correct
61 Correct 3 ms 4940 KB Output is correct
62 Correct 3 ms 4940 KB Output is correct
63 Correct 3 ms 5016 KB Output is correct
64 Correct 2 ms 4940 KB Output is correct
65 Correct 168 ms 19792 KB Output is correct
66 Correct 174 ms 18672 KB Output is correct
67 Correct 174 ms 20068 KB Output is correct
68 Correct 3 ms 5068 KB Output is correct
69 Correct 3 ms 4908 KB Output is correct
70 Correct 3 ms 4940 KB Output is correct
71 Correct 3 ms 4940 KB Output is correct
72 Correct 3 ms 4940 KB Output is correct
73 Correct 2 ms 4940 KB Output is correct
74 Correct 3 ms 5016 KB Output is correct
75 Correct 2 ms 5016 KB Output is correct
76 Correct 2 ms 4940 KB Output is correct
77 Correct 2 ms 4940 KB Output is correct
78 Correct 3 ms 4940 KB Output is correct
79 Correct 3 ms 4940 KB Output is correct
80 Correct 3 ms 4940 KB Output is correct
81 Correct 3 ms 5016 KB Output is correct
82 Correct 3 ms 5012 KB Output is correct
83 Correct 3 ms 4996 KB Output is correct