Submission #311492

# Submission time Handle Problem Language Result Execution time Memory
311492 2020-10-10T13:14:12 Z OttoTheDino Love Polygon (BOI18_polygon) C++17
100 / 100
299 ms 11576 KB
#include <bits/stdc++.h>
using namespace std;

#define rep(n)      for (int i = 0; i < n; ++i)
#define rep2(n)     for (int j = 0; j < n; ++j)
#define mp          make_pair
#define pb          push_back
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;

const int maxn = 2e5;

int loves[maxn];
bool been[maxn] = {};

int dfs (int i) {
    been[i] = 1;
    return 1+(been[loves[i]]?0:dfs(loves[i]));
}

int main() {    
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    map<string, int> decode;
    int n, num = 1, loved[maxn], ans = 0;
    string s, t;
    cin >> n;
    
    rep (n) {
        cin >> s >> t;
        if (!decode[s]) decode[s] = num++;
        if (!decode[t]) decode[t] = num++;
        loves[decode[s]] = decode[t];
        loved[decode[t]]++;
    }
    
    if (n%2==1) {
        cout << "-1\n";
        return 0;
    }
    
    queue<int> q;
    
    for (int i = 1; i <= n; ++i) {
        if (i==loves[loves[i]] && loves[i]!=i) {
            been[i] = 1;
            been[loves[i]] = 1;
        }
        else if (loved[i]==0) q.push(i);
    }
    
    while (!q.empty()) {
        int id = q.front();
        q.pop();
        been[id] = 1;
        if (!been[loves[id]]) {
            been[loves[id]] = 1;
            if (--loved[loves[loves[id]]]==0) q.push(loves[loves[id]]);
        }
        ans++;
    }
    
    for (int i = 1; i <= n; ++i) {
        if (!been[i]) ans += (dfs(i)+1)/2;
    }
    
    cout << ans << "\n";
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 279 ms 11256 KB Output is correct
5 Correct 273 ms 11256 KB Output is correct
6 Correct 271 ms 11384 KB Output is correct
7 Correct 272 ms 11256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 282 ms 9208 KB Output is correct
2 Correct 275 ms 9080 KB Output is correct
3 Correct 224 ms 9080 KB Output is correct
4 Correct 258 ms 8952 KB Output is correct
5 Correct 274 ms 9080 KB Output is correct
6 Correct 266 ms 9416 KB Output is correct
7 Correct 257 ms 9208 KB Output is correct
8 Correct 245 ms 9208 KB Output is correct
9 Correct 241 ms 9336 KB Output is correct
10 Correct 189 ms 9020 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 279 ms 11256 KB Output is correct
20 Correct 273 ms 11256 KB Output is correct
21 Correct 271 ms 11384 KB Output is correct
22 Correct 272 ms 11256 KB Output is correct
23 Correct 282 ms 9208 KB Output is correct
24 Correct 275 ms 9080 KB Output is correct
25 Correct 224 ms 9080 KB Output is correct
26 Correct 258 ms 8952 KB Output is correct
27 Correct 274 ms 9080 KB Output is correct
28 Correct 266 ms 9416 KB Output is correct
29 Correct 257 ms 9208 KB Output is correct
30 Correct 245 ms 9208 KB Output is correct
31 Correct 241 ms 9336 KB Output is correct
32 Correct 189 ms 9020 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 0 ms 384 KB Output is correct
37 Correct 291 ms 11256 KB Output is correct
38 Correct 267 ms 11404 KB Output is correct
39 Correct 277 ms 11512 KB Output is correct
40 Correct 263 ms 11484 KB Output is correct
41 Correct 261 ms 11384 KB Output is correct
42 Correct 261 ms 11384 KB Output is correct
43 Correct 287 ms 11516 KB Output is correct
44 Correct 282 ms 11368 KB Output is correct
45 Correct 262 ms 11384 KB Output is correct
46 Correct 266 ms 11512 KB Output is correct
47 Correct 252 ms 11576 KB Output is correct
48 Correct 278 ms 11512 KB Output is correct
49 Correct 299 ms 11256 KB Output is correct
50 Correct 229 ms 11256 KB Output is correct
51 Correct 263 ms 11212 KB Output is correct
52 Correct 272 ms 11256 KB Output is correct
53 Correct 266 ms 11432 KB Output is correct
54 Correct 259 ms 11512 KB Output is correct
55 Correct 251 ms 11384 KB Output is correct
56 Correct 243 ms 11512 KB Output is correct
57 Correct 185 ms 11256 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 0 ms 384 KB Output is correct
60 Correct 1 ms 384 KB Output is correct
61 Correct 0 ms 384 KB Output is correct
62 Correct 1 ms 384 KB Output is correct
63 Correct 0 ms 384 KB Output is correct
64 Correct 1 ms 384 KB Output is correct
65 Correct 272 ms 11256 KB Output is correct
66 Correct 270 ms 11384 KB Output is correct
67 Correct 270 ms 11384 KB Output is correct
68 Correct 269 ms 11128 KB Output is correct
69 Correct 1 ms 384 KB Output is correct
70 Correct 1 ms 384 KB Output is correct
71 Correct 1 ms 384 KB Output is correct
72 Correct 1 ms 384 KB Output is correct
73 Correct 0 ms 384 KB Output is correct
74 Correct 1 ms 384 KB Output is correct
75 Correct 1 ms 384 KB Output is correct
76 Correct 1 ms 384 KB Output is correct
77 Correct 0 ms 384 KB Output is correct
78 Correct 1 ms 384 KB Output is correct
79 Correct 1 ms 384 KB Output is correct
80 Correct 1 ms 384 KB Output is correct
81 Correct 1 ms 384 KB Output is correct
82 Correct 0 ms 384 KB Output is correct
83 Correct 1 ms 384 KB Output is correct