Submission #941708

# Submission time Handle Problem Language Result Execution time Memory
941708 2024-03-09T10:28:09 Z phoenix0423 Love Polygon (BOI18_polygon) C++17
100 / 100
126 ms 12344 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
const int maxn = 2e5 + 5;
int to[maxn];
int indeg[maxn];
map<string, int> mp;
int cnt = 0;

signed main(void){
    fastio;
    int n;
    cin>>n;
    int ans = 0;
    for(int i = 0; i < n; i++){
        string a, b;
        cin>>a>>b;
        int id1 = (mp[a] ? mp[a] : mp[a] = ++cnt), id2 = (mp[b] ? mp[b] : mp[b] = ++cnt);
        to[id1] = id2;
        indeg[id2] ++;
    }
    if(n % 2){
        cout<<-1<<"\n";
        return 0;
    }
    vector<int> formed(n + 1);
    for(int i = 1; i <= n; i++){
        if(formed[i]) continue;
        if(to[i] != i && to[to[i]] == i){
            formed[i] = formed[to[i]] = 1;
        }
    }
    queue<int> q;
    for(int i = 1; i <= n; i++){
        if(!indeg[i]) q.push(i);
    }
    int left = 0;
    while(!q.empty()){
        int pos = q.front(); q.pop();
        if(formed[to[pos]]){
            left++;
            continue;
        }
        ans++;
        indeg[to[to[pos]]] --;
        if(!formed[to[to[pos]]] && !indeg[to[to[pos]]]) q.push(to[to[pos]]);
        formed[pos] = formed[to[pos]] = 1;
    }
    vector<int> vis(n + 1);
    for(int i = 1; i <= n; i++){
        if(!formed[i] && indeg[i] && !vis[i]){
            int tmp = to[i], len = 1;
            vis[i] = 1;
            while(tmp != i){
                vis[tmp] = 1, len++, tmp = to[tmp];
            }
            ans += len / 2, left += len % 2;
        }
    }
    cout<<ans + left<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 115 ms 11860 KB Output is correct
5 Correct 122 ms 11860 KB Output is correct
6 Correct 113 ms 11864 KB Output is correct
7 Correct 112 ms 11204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 11860 KB Output is correct
2 Correct 118 ms 11908 KB Output is correct
3 Correct 99 ms 11860 KB Output is correct
4 Correct 109 ms 11096 KB Output is correct
5 Correct 111 ms 11856 KB Output is correct
6 Correct 111 ms 12116 KB Output is correct
7 Correct 112 ms 12080 KB Output is correct
8 Correct 108 ms 12000 KB Output is correct
9 Correct 104 ms 12112 KB Output is correct
10 Correct 81 ms 11960 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 115 ms 11860 KB Output is correct
20 Correct 122 ms 11860 KB Output is correct
21 Correct 113 ms 11864 KB Output is correct
22 Correct 112 ms 11204 KB Output is correct
23 Correct 116 ms 11860 KB Output is correct
24 Correct 118 ms 11908 KB Output is correct
25 Correct 99 ms 11860 KB Output is correct
26 Correct 109 ms 11096 KB Output is correct
27 Correct 111 ms 11856 KB Output is correct
28 Correct 111 ms 12116 KB Output is correct
29 Correct 112 ms 12080 KB Output is correct
30 Correct 108 ms 12000 KB Output is correct
31 Correct 104 ms 12112 KB Output is correct
32 Correct 81 ms 11960 KB Output is correct
33 Correct 1 ms 344 KB Output is correct
34 Correct 0 ms 344 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 119 ms 11856 KB Output is correct
38 Correct 112 ms 12180 KB Output is correct
39 Correct 126 ms 12344 KB Output is correct
40 Correct 111 ms 12112 KB Output is correct
41 Correct 112 ms 12012 KB Output is correct
42 Correct 111 ms 11972 KB Output is correct
43 Correct 126 ms 11936 KB Output is correct
44 Correct 112 ms 11944 KB Output is correct
45 Correct 111 ms 12116 KB Output is correct
46 Correct 112 ms 12116 KB Output is correct
47 Correct 107 ms 12236 KB Output is correct
48 Correct 124 ms 11860 KB Output is correct
49 Correct 113 ms 11860 KB Output is correct
50 Correct 100 ms 11856 KB Output is correct
51 Correct 109 ms 10976 KB Output is correct
52 Correct 118 ms 11884 KB Output is correct
53 Correct 113 ms 12072 KB Output is correct
54 Correct 112 ms 12148 KB Output is correct
55 Correct 109 ms 12116 KB Output is correct
56 Correct 106 ms 12168 KB Output is correct
57 Correct 83 ms 12012 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 464 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 118 ms 11860 KB Output is correct
66 Correct 115 ms 11840 KB Output is correct
67 Correct 113 ms 11884 KB Output is correct
68 Correct 110 ms 11092 KB Output is correct
69 Correct 0 ms 348 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 1 ms 348 KB Output is correct
72 Correct 1 ms 348 KB Output is correct
73 Correct 0 ms 348 KB Output is correct
74 Correct 0 ms 348 KB Output is correct
75 Correct 0 ms 348 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 0 ms 348 KB Output is correct
81 Correct 1 ms 348 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct