Submission #1091369

# Submission time Handle Problem Language Result Execution time Memory
1091369 2024-09-20T18:01:55 Z MateiKing80 Love Polygon (BOI18_polygon) C++14
100 / 100
156 ms 11928 KB
#include <bits/stdc++.h>

using namespace std;

int p[100025], n, grad[100025];
map<string, int> nume;
bool vis[100025];
queue<int> q;

int main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
        string s, t;
        cin >> s >> t;
        if(!nume.count(s))
            nume[s] = (int)nume.size();
        if(!nume.count(t))
            nume[t] = (int)nume.size();
        p[nume[s]] = nume[t];
        grad[nume[t]] ++;
    }
    if(n % 2 == 1)
    {
        cout << -1;
        return 0;
    }
    int ans = 0;
    for(int i = 0; i < n; i ++)
        if (grad[i] == 0)
            q.push(i);
    while (!q.empty())
    {
        auto k = q.front();
        q.pop();
        if(grad[k] != 0)
            continue;
        if(p[p[k]] != p[k] && p[p[p[k]]] == p[k])
            continue;
        if(p[p[k]] == p[k])
        {
            p[p[k]] = k;
            grad[k] ++;
            grad[p[k]] --;
            ans ++;
            continue;
        }
        ans ++;
        grad[p[p[k]]] --;
        if(grad[p[p[k]]] == 0)
            q.push(p[p[k]]);
        p[p[k]] = k;
        grad[k] ++;
    }
    for(int i = 0; i < n; i ++)
        if(grad[i] == 0)
        {
            ans++;
            grad[i] = 2;
            grad[p[i]] --;
        }
    for(int i = 0; i < n; i ++)
        if(grad[i] == 1 && !vis[i])
        {
            vector<int> t = {i};
            vis[i] = 1;
            int u = p[i];
            while(u != i)
            {
                t.push_back(u);
                vis[u] = 1;
                u = p[u];
            }
            if(t.size() == 2)
                continue;
            int z = t.size();
            ans += (z + 1) / 2;
        }
    cout << ans;
}
# 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 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 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 148 ms 11928 KB Output is correct
5 Correct 148 ms 11240 KB Output is correct
6 Correct 147 ms 11712 KB Output is correct
7 Correct 145 ms 11088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 153 ms 11388 KB Output is correct
2 Correct 147 ms 11244 KB Output is correct
3 Correct 136 ms 11084 KB Output is correct
4 Correct 149 ms 11024 KB Output is correct
5 Correct 156 ms 11224 KB Output is correct
6 Correct 143 ms 11356 KB Output is correct
7 Correct 148 ms 11348 KB Output is correct
8 Correct 152 ms 11348 KB Output is correct
9 Correct 147 ms 11600 KB Output is correct
10 Correct 107 ms 11600 KB Output is correct
11 Correct 0 ms 468 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 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 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 148 ms 11928 KB Output is correct
20 Correct 148 ms 11240 KB Output is correct
21 Correct 147 ms 11712 KB Output is correct
22 Correct 145 ms 11088 KB Output is correct
23 Correct 153 ms 11388 KB Output is correct
24 Correct 147 ms 11244 KB Output is correct
25 Correct 136 ms 11084 KB Output is correct
26 Correct 149 ms 11024 KB Output is correct
27 Correct 156 ms 11224 KB Output is correct
28 Correct 143 ms 11356 KB Output is correct
29 Correct 148 ms 11348 KB Output is correct
30 Correct 152 ms 11348 KB Output is correct
31 Correct 147 ms 11600 KB Output is correct
32 Correct 107 ms 11600 KB Output is correct
33 Correct 0 ms 468 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 151 ms 11196 KB Output is correct
38 Correct 147 ms 11472 KB Output is correct
39 Correct 153 ms 11304 KB Output is correct
40 Correct 151 ms 11348 KB Output is correct
41 Correct 150 ms 11344 KB Output is correct
42 Correct 144 ms 11348 KB Output is correct
43 Correct 151 ms 11344 KB Output is correct
44 Correct 144 ms 11352 KB Output is correct
45 Correct 149 ms 11348 KB Output is correct
46 Correct 149 ms 11348 KB Output is correct
47 Correct 141 ms 11600 KB Output is correct
48 Correct 144 ms 11344 KB Output is correct
49 Correct 147 ms 11248 KB Output is correct
50 Correct 131 ms 11092 KB Output is correct
51 Correct 141 ms 11088 KB Output is correct
52 Correct 146 ms 11092 KB Output is correct
53 Correct 147 ms 11348 KB Output is correct
54 Correct 146 ms 11412 KB Output is correct
55 Correct 136 ms 11368 KB Output is correct
56 Correct 135 ms 11600 KB Output is correct
57 Correct 106 ms 11464 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 600 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 428 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 147 ms 11852 KB Output is correct
66 Correct 144 ms 11088 KB Output is correct
67 Correct 150 ms 11732 KB Output is correct
68 Correct 145 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 0 ms 348 KB Output is correct
72 Correct 0 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 1 ms 344 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 0 ms 348 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct