Submission #930090

# Submission time Handle Problem Language Result Execution time Memory
930090 2024-02-18T13:03:19 Z Karoot Love Polygon (BOI18_polygon) C++17
100 / 100
249 ms 20272 KB
#include <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>

#define all(x)  (x).begin(), (x).end()
#define rall(x)  (x).rbegin(), (x).rend()

using namespace std;

typedef long long ll;

ll linf = 1e15+1;

inline void scoobydoobydoo(){
    ios::sync_with_stdio(false);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
}

const int MAXN = 2e5+1;
int indeg[MAXN];
int parent[MAXN];
bool don[MAXN];

int counter = 0;

map<int, string> M;
map<string, int> person;
vector<int> single;

vector<int> ans;

int main(){
    scoobydoobydoo();
    int n; cin >> n;
    for (int i = 0; i < n; i++){
        string a, b; cin >> a >> b;
        if (person.find(a) == person.end()){
            person[a] = counter++;
            M[person[a]] = a;
        }
        if (person.find(b) == person.end()){
            person[b] = counter++;
            M[person[b]] = b;
        }
        parent[person[a]] = person[b];
    }

    if (n%2){
        cout << -1 << endl;
        return 0;
    }


    //filter relationships
    for (int i = 0; i < counter; i++){
        if (!don[i] && parent[parent[i]] == i && parent[i] != i){
            don[i] = true;
            don[parent[i]] = true;
        }
    }

    for (int i = 0; i < counter; i++){
        if (don[i])continue;
        if (don[parent[i]] || parent[i] == i)parent[i] = -1;
        else indeg[parent[i]]++;
    }

    queue<int> q;

    for (int i = 0; i < counter; i++){
        if (don[i])continue;
        if (parent[i] == -1 && indeg[i] == 0)single.push_back(i);
        else if (indeg[i] == 0)q.push(i);
    }

    while (!q.empty()){
        int node = q.front();
        int pare = parent[node];
        q.pop();
        if (don[pare]){
            single.push_back(node);
            don[node] = true;
            continue;
        }
        ans.push_back(pare);
        indeg[parent[pare]]--;
        don[node] = true;
        don[pare] = true;
        if (parent[parent[pare]] == -1 && indeg[parent[pare]] == 0)single.push_back(parent[pare]);
        else if (indeg[parent[pare]] == 0)q.push(parent[pare]);
    }

    //cout << (int)ans.size() << endl;
    //cout << single.size() << endl;

    for (int i = 0; i < counter; i++){
        if (!don[i] && indeg[i] == 1 && parent[i] != -1){
            don[i] = true;
            //cout << "inhere: " << i << endl;
            if (don[parent[i]]){
                don[i] = true;
                single.push_back(i);
                continue;
            }
            don[parent[i]] = true;
            indeg[parent[parent[i]]]--;
            ans.push_back(parent[i]);
            if (indeg[parent[parent[i]]] == 0)q.push(parent[parent[i]]);

            while (!q.empty()){
                int node = q.front();
                int pare = parent[node];
                q.pop();
                if (don[node])continue;
                if (don[pare]){
                    single.push_back(node);
                    don[node] = true;
                    continue;
                }
                ans.push_back(pare);
                indeg[parent[pare]]--;
                don[node] = true;
                don[pare] = true;
                if ((parent[parent[pare]] == -1 || don[parent[parent[pare]]]) && indeg[parent[pare]] == 0 && !don[parent[pare]] && parent[parent[pare]] != i)single.push_back(parent[pare]);
                else if (indeg[parent[pare]] == 0)q.push(parent[pare]);
            }
        }
    }

    /*
10 
a a
b c
c d
d e
e f
f b
g c
h g
i c
j g
    
    
    
    */


    //pair singles

    /*cout << "ordinary" << endl;
    for (auto& e : ans)cout << e << " ";
    cout << endl;
    

    cout << "singles" << endl;*/
    
    for (int i = 0; i < single.size(); i++){
        ans.push_back(single[i]);
        //cout << single[i] << " ";
    }
    //cout << endl;


    cout << ans.size() << endl;





    return 0;
}

Compilation message

polygon.cpp: In function 'int main()':
polygon.cpp:165:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  165 |     for (int i = 0; i < single.size(); i++){
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 428 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 249 ms 19516 KB Output is correct
5 Correct 230 ms 19484 KB Output is correct
6 Correct 192 ms 19540 KB Output is correct
7 Correct 226 ms 18640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 19612 KB Output is correct
2 Correct 200 ms 19516 KB Output is correct
3 Correct 189 ms 19784 KB Output is correct
4 Correct 194 ms 18428 KB Output is correct
5 Correct 200 ms 19404 KB Output is correct
6 Correct 218 ms 19776 KB Output is correct
7 Correct 204 ms 19648 KB Output is correct
8 Correct 182 ms 20032 KB Output is correct
9 Correct 179 ms 20236 KB Output is correct
10 Correct 149 ms 20168 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 428 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 468 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 249 ms 19516 KB Output is correct
20 Correct 230 ms 19484 KB Output is correct
21 Correct 192 ms 19540 KB Output is correct
22 Correct 226 ms 18640 KB Output is correct
23 Correct 193 ms 19612 KB Output is correct
24 Correct 200 ms 19516 KB Output is correct
25 Correct 189 ms 19784 KB Output is correct
26 Correct 194 ms 18428 KB Output is correct
27 Correct 200 ms 19404 KB Output is correct
28 Correct 218 ms 19776 KB Output is correct
29 Correct 204 ms 19648 KB Output is correct
30 Correct 182 ms 20032 KB Output is correct
31 Correct 179 ms 20236 KB Output is correct
32 Correct 149 ms 20168 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 199 ms 19536 KB Output is correct
38 Correct 202 ms 19816 KB Output is correct
39 Correct 199 ms 19608 KB Output is correct
40 Correct 193 ms 19712 KB Output is correct
41 Correct 188 ms 20044 KB Output is correct
42 Correct 190 ms 19692 KB Output is correct
43 Correct 188 ms 19580 KB Output is correct
44 Correct 210 ms 19640 KB Output is correct
45 Correct 185 ms 19664 KB Output is correct
46 Correct 205 ms 19560 KB Output is correct
47 Correct 211 ms 20272 KB Output is correct
48 Correct 183 ms 19504 KB Output is correct
49 Correct 196 ms 19464 KB Output is correct
50 Correct 168 ms 19744 KB Output is correct
51 Correct 180 ms 18516 KB Output is correct
52 Correct 199 ms 19488 KB Output is correct
53 Correct 187 ms 19672 KB Output is correct
54 Correct 197 ms 19688 KB Output is correct
55 Correct 191 ms 20052 KB Output is correct
56 Correct 181 ms 20136 KB Output is correct
57 Correct 155 ms 19980 KB Output is correct
58 Correct 1 ms 468 KB Output is correct
59 Correct 1 ms 344 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 1 ms 344 KB Output is correct
63 Correct 1 ms 348 KB Output is correct
64 Correct 0 ms 472 KB Output is correct
65 Correct 190 ms 19428 KB Output is correct
66 Correct 205 ms 19504 KB Output is correct
67 Correct 212 ms 19676 KB Output is correct
68 Correct 183 ms 18516 KB Output is correct
69 Correct 1 ms 600 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 344 KB Output is correct
73 Correct 0 ms 348 KB Output is correct
74 Correct 0 ms 348 KB Output is correct
75 Correct 1 ms 348 KB Output is correct
76 Correct 1 ms 348 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 0 ms 344 KB Output is correct
79 Correct 1 ms 468 KB Output is correct
80 Correct 1 ms 348 KB Output is correct
81 Correct 0 ms 348 KB Output is correct
82 Correct 1 ms 344 KB Output is correct
83 Correct 1 ms 344 KB Output is correct