답안 #440879

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
440879 2021-07-03T12:21:38 Z Haruto810198 Love Polygon (BOI18_polygon) C++17
100 / 100
235 ms 10948 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define double long double

#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())

#define vi vector<int>
#define pii pair<int,int>

#define pb push_back
#define eb emplace_back
#define mkp make_pair

const int INF = 2147483647;
const int LNF = INF*INF;
const int MOD = 1000000007;
const int mod = 998244353;

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")

const int MAX = 100010;

int n;
map<string, int> mp;
int edge[MAX];
int indeg[MAX];
bool exist[MAX];

vi qu;
int res;

int query(string str){
    if(mp.find(str) == mp.end()){
        mp[str] = szof(mp) + 1;
    }
    return mp[str];
}

signed main(){

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    FOR(i, 1, n, 1){
        string su, sv;
        cin>>su>>sv;
        int u = query(su);
        int v = query(sv);
        edge[u] = v;
        indeg[v]++;
    }

    if(n%2 == 1){
        cout<<-1<<'\n';
        return 0;
    }

    FOR(i, 1, n, 1){
        exist[i] = 1;
        if(indeg[i] == 0){
            qu.pb(i);
        }
    }

    res = n;

    FOR(i, 1, n, 1){

        if(exist[i]==0) continue;

        if(edge[edge[i]] == i and edge[i] != i){
            res -= 2;
            exist[i] = 0;
            exist[edge[i]] = 0;
            indeg[i]--;
            indeg[edge[i]]--;
        }

    }

    while(!qu.empty()){

        int u = qu.back();
        qu.pop_back();
        if(exist[u]==0) continue;

        int v = edge[u];
        if(exist[v]==0) continue;
        int w = edge[v];

        exist[u] = 0;
        exist[v] = 0;
        indeg[v]--;
        indeg[w]--;
        res--;

        if(indeg[w] == 0 and exist[w] == 1) qu.pb(w);

    }

    FOR(i, 1, n, 1){

        if(exist[i]==0) continue;

        int ptr = i;
        int cnt = 0;
        while(exist[ptr] == 1){
            exist[ptr] = 0;
            ptr = edge[ptr];
            cnt++;
        }

        if(ptr == i){
            if(cnt == 2){
                res -= 2;
            }
            else{
                res -= cnt / 2;
            }
        }

    }

    cout<<res<<'\n';

    /*
    FOR(i, 1, n, 1){
        cerr<<exist[i]<<" ";
    }
    cerr<<endl;
    */

    return 0;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 235 ms 9664 KB Output is correct
5 Correct 218 ms 9708 KB Output is correct
6 Correct 214 ms 9716 KB Output is correct
7 Correct 215 ms 9668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 221 ms 10088 KB Output is correct
2 Correct 220 ms 9968 KB Output is correct
3 Correct 175 ms 9668 KB Output is correct
4 Correct 211 ms 9668 KB Output is correct
5 Correct 222 ms 9796 KB Output is correct
6 Correct 218 ms 10312 KB Output is correct
7 Correct 219 ms 10364 KB Output is correct
8 Correct 202 ms 10440 KB Output is correct
9 Correct 204 ms 10836 KB Output is correct
10 Correct 170 ms 10104 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 235 ms 9664 KB Output is correct
20 Correct 218 ms 9708 KB Output is correct
21 Correct 214 ms 9716 KB Output is correct
22 Correct 215 ms 9668 KB Output is correct
23 Correct 221 ms 10088 KB Output is correct
24 Correct 220 ms 9968 KB Output is correct
25 Correct 175 ms 9668 KB Output is correct
26 Correct 211 ms 9668 KB Output is correct
27 Correct 222 ms 9796 KB Output is correct
28 Correct 218 ms 10312 KB Output is correct
29 Correct 219 ms 10364 KB Output is correct
30 Correct 202 ms 10440 KB Output is correct
31 Correct 204 ms 10836 KB Output is correct
32 Correct 170 ms 10104 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 0 ms 216 KB Output is correct
37 Correct 215 ms 9992 KB Output is correct
38 Correct 214 ms 10416 KB Output is correct
39 Correct 223 ms 10364 KB Output is correct
40 Correct 229 ms 10352 KB Output is correct
41 Correct 231 ms 10532 KB Output is correct
42 Correct 224 ms 10376 KB Output is correct
43 Correct 218 ms 10440 KB Output is correct
44 Correct 232 ms 10420 KB Output is correct
45 Correct 220 ms 10480 KB Output is correct
46 Correct 214 ms 10328 KB Output is correct
47 Correct 204 ms 10948 KB Output is correct
48 Correct 215 ms 10088 KB Output is correct
49 Correct 220 ms 9968 KB Output is correct
50 Correct 178 ms 9704 KB Output is correct
51 Correct 214 ms 9604 KB Output is correct
52 Correct 216 ms 9728 KB Output is correct
53 Correct 226 ms 10312 KB Output is correct
54 Correct 220 ms 10420 KB Output is correct
55 Correct 206 ms 10360 KB Output is correct
56 Correct 198 ms 10904 KB Output is correct
57 Correct 163 ms 10060 KB Output is correct
58 Correct 0 ms 216 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 0 ms 204 KB Output is correct
61 Correct 0 ms 204 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 0 ms 204 KB Output is correct
64 Correct 0 ms 204 KB Output is correct
65 Correct 219 ms 9660 KB Output is correct
66 Correct 215 ms 9668 KB Output is correct
67 Correct 225 ms 9668 KB Output is correct
68 Correct 217 ms 9652 KB Output is correct
69 Correct 0 ms 204 KB Output is correct
70 Correct 0 ms 204 KB Output is correct
71 Correct 0 ms 204 KB Output is correct
72 Correct 0 ms 204 KB Output is correct
73 Correct 0 ms 204 KB Output is correct
74 Correct 0 ms 204 KB Output is correct
75 Correct 0 ms 204 KB Output is correct
76 Correct 0 ms 204 KB Output is correct
77 Correct 1 ms 204 KB Output is correct
78 Correct 0 ms 204 KB Output is correct
79 Correct 0 ms 204 KB Output is correct
80 Correct 0 ms 204 KB Output is correct
81 Correct 0 ms 204 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 0 ms 204 KB Output is correct