Submission #802735

# Submission time Handle Problem Language Result Execution time Memory
802735 2023-08-02T13:47:32 Z SamAnd Fountain Parks (IOI21_parks) C++17
55 / 100
1703 ms 135544 KB
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define m_p make_pair
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
const int N = 400005;

int n;
vector<int> g[N];

bool c[N];
vector<int> uu, vv;
void dfs(int x)
{
    c[x] = true;
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        if (c[h])
            continue;
        uu.push_back(x);
        vv.push_back(g[x][i]);
        dfs(h);
    }
}

vector<pair<int, int> > rs;
vector<int> gg[N], gi[N];
void dfs(int x, vector<int>& a, vector<int>& b)
{
    c[x] = true;
    for (int i = 0; i < gg[x].size(); ++i)
    {
        int h = gg[x][i];
        if (a[gi[x][i]] == -1)
        {
            a[gi[x][i]] = rs[x].fi;
            b[gi[x][i]] = rs[x].se;
        }
        if (!c[h])
            dfs(h, a, b);
    }
}

int construct_roads(std::vector<int> x, std::vector<int> y) {
    std::vector<int> u, v, a, b;

    n = sz(x);
    vector<pair<int, int> > p;
    map<pair<int, int>, int> mp;
    for (int i = 0; i < n; ++i)
    {
        p.push_back(m_p(x[i], y[i]));
        mp[m_p(x[i], y[i])] = i;
    }

    int z = 0;
    map<pair<int, int>, int> s;
    for (int i = 0; i < n; ++i)
    {
        int x = p[i].fi;
        int y = p[i].se;
        if (mp.find(m_p(x + 2, y)) != mp.end())
        {
            u.push_back(mp[m_p(x, y)]);
            v.push_back(mp[m_p(x + 2, y)]);
            if (s.find(m_p(x + 1, y + 1)) == s.end())
                s[m_p(x + 1, y + 1)] = z++;
            if (s.find(m_p(x + 1, y - 1)) == s.end())
                s[m_p(x + 1, y - 1)] = z++;
            g[u.back()].push_back(v.back());
            g[v.back()].push_back(u.back());
            gg[s[m_p(x + 1, y + 1)]].push_back(s[m_p(x + 1, y - 1)]);
            gg[s[m_p(x + 1, y - 1)]].push_back(s[m_p(x + 1, y + 1)]);
            gi[s[m_p(x + 1, y + 1)]].push_back(sz(u) - 1);
            gi[s[m_p(x + 1, y - 1)]].push_back(sz(u) - 1);
        }
        if (mp.find(m_p(x, y + 2)) != mp.end())
        {
            u.push_back(mp[m_p(x, y)]);
            v.push_back(mp[m_p(x, y + 2)]);
            if (s.find(m_p(x + 1, y + 1)) == s.end())
                s[m_p(x + 1, y + 1)] = z++;
            if (s.find(m_p(x - 1, y + 1)) == s.end())
                s[m_p(x - 1, y + 1)] = z++;
            g[u.back()].push_back(v.back());
            g[v.back()].push_back(u.back());
            gg[s[m_p(x + 1, y + 1)]].push_back(s[m_p(x - 1, y + 1)]);
            gg[s[m_p(x - 1, y + 1)]].push_back(s[m_p(x + 1, y + 1)]);
            gi[s[m_p(x + 1, y + 1)]].push_back(sz(u) - 1);
            gi[s[m_p(x - 1, y + 1)]].push_back(sz(u) - 1);
        }
    }

    rs.assign(z, m_p(0, 0));
    for (auto it = s.begin(); it != s.end(); ++it)
    {
        rs[it->se] = it->fi;
    }

    dfs(0);
    for (int i = 0; i < n; ++i)
    {
        if (!c[i])
            return 0;
    }

    int maxu = 0;
    for (int i = 0; i < n; ++i)
    {
        maxu = max(maxu, x[i]);
    }

    memset(c, false, sizeof c);
    a.assign(sz(u), -1);
    b.assign(sz(u), -1);

    if (maxu <= 6)
    {
        u = uu;
        v = vv;
        a.assign(sz(u), -1);
        b.assign(sz(u), -1);
        vector<pair<pair<int, int>, int> > p;
        for (int i = 0; i < sz(u); ++i)
        {
            int mx = (x[u[i]] + x[v[i]]) / 2;
            int my = (y[u[i]] + y[v[i]]) / 2;
            p.push_back(m_p(m_p(my, mx), i));
        }
        sort(all(p));
        for (int i = 0; i < sz(u); ++i)
        {
            uu[i] = u[p[i].se];
            vv[i] = v[p[i].se];
        }
        u = uu;
        v = vv;
        for (int i = 0; i < sz(u); ++i)
        {
            int mx = (x[u[i]] + x[v[i]]) / 2;
            int my = (y[u[i]] + y[v[i]]) / 2;
            if (mx % 2 == 0)
            {
                if (!c[s[m_p(mx - 1, my)]])
                {
                    a[i] = mx - 1;
                    b[i] = my;
                    c[s[m_p(mx - 1, my)]] = true;
                }
                else
                {
                    assert(!c[s[m_p(mx + 1, my)]]);
                    a[i] = mx + 1;
                    b[i] = my;
                    c[s[m_p(mx + 1, my)]] = true;
                }
            }
            else
            {
                assert(my % 2 == 0);
                if (!c[s[m_p(mx, my - 1)]])
                {
                    a[i] = mx;
                    b[i] = my - 1;
                    c[s[m_p(mx, my - 1)]] = true;
                }
                else
                {
                    assert(!c[s[m_p(mx, my + 1)]]);
                    a[i] = mx;
                    b[i] = my + 1;
                    c[s[m_p(mx, my + 1)]] = true;
                }
            }
        }
    }
    else
    {
        for (int i = 0; i < z; ++i)
        {
            if (!c[i] && sz(gg[i]) == 1)
            {
                dfs(i, a, b);
            }
        }
        for (int i = 0; i < z; ++i)
        {
            if (!c[i])
            {
                dfs(i, a, b);
            }
        }
    }

    build(u, v, a, b);
    return 1;
}

/*
3
2 2
4 4
2 4

3
2 2
4 2
2 4

3
4 2
2 2
4 4

3
4 4
4 2
2 4

9
4 2
4 4
2 2
2 4
2 6
4 6
2 8
4 8
4 10
*/

Compilation message

parks.cpp: In function 'void dfs(int)':
parks.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
parks.cpp: In function 'void dfs(int, std::vector<int>&, std::vector<int>&)':
parks.cpp:35:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |     for (int i = 0; i < gg[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 28800 KB Output is correct
2 Correct 14 ms 28776 KB Output is correct
3 Correct 14 ms 28372 KB Output is correct
4 Correct 13 ms 28756 KB Output is correct
5 Correct 14 ms 28756 KB Output is correct
6 Correct 14 ms 28436 KB Output is correct
7 Correct 15 ms 28432 KB Output is correct
8 Correct 15 ms 28408 KB Output is correct
9 Correct 529 ms 80744 KB Output is correct
10 Correct 37 ms 34120 KB Output is correct
11 Correct 175 ms 56976 KB Output is correct
12 Correct 53 ms 36772 KB Output is correct
13 Correct 129 ms 48468 KB Output is correct
14 Correct 16 ms 28884 KB Output is correct
15 Correct 18 ms 29232 KB Output is correct
16 Correct 493 ms 77588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 28800 KB Output is correct
2 Correct 14 ms 28776 KB Output is correct
3 Correct 14 ms 28372 KB Output is correct
4 Correct 13 ms 28756 KB Output is correct
5 Correct 14 ms 28756 KB Output is correct
6 Correct 14 ms 28436 KB Output is correct
7 Correct 15 ms 28432 KB Output is correct
8 Correct 15 ms 28408 KB Output is correct
9 Correct 529 ms 80744 KB Output is correct
10 Correct 37 ms 34120 KB Output is correct
11 Correct 175 ms 56976 KB Output is correct
12 Correct 53 ms 36772 KB Output is correct
13 Correct 129 ms 48468 KB Output is correct
14 Correct 16 ms 28884 KB Output is correct
15 Correct 18 ms 29232 KB Output is correct
16 Correct 493 ms 77588 KB Output is correct
17 Correct 16 ms 28756 KB Output is correct
18 Correct 16 ms 28756 KB Output is correct
19 Correct 16 ms 28764 KB Output is correct
20 Correct 14 ms 28800 KB Output is correct
21 Correct 14 ms 28372 KB Output is correct
22 Correct 15 ms 28800 KB Output is correct
23 Correct 1599 ms 115524 KB Output is correct
24 Correct 15 ms 28840 KB Output is correct
25 Correct 19 ms 29416 KB Output is correct
26 Correct 24 ms 29516 KB Output is correct
27 Correct 25 ms 29820 KB Output is correct
28 Correct 519 ms 64904 KB Output is correct
29 Correct 871 ms 81788 KB Output is correct
30 Correct 1277 ms 101816 KB Output is correct
31 Correct 1602 ms 116012 KB Output is correct
32 Correct 15 ms 28756 KB Output is correct
33 Correct 14 ms 28844 KB Output is correct
34 Correct 14 ms 28864 KB Output is correct
35 Correct 14 ms 28588 KB Output is correct
36 Correct 14 ms 28372 KB Output is correct
37 Correct 14 ms 28860 KB Output is correct
38 Correct 14 ms 28832 KB Output is correct
39 Correct 15 ms 28868 KB Output is correct
40 Correct 15 ms 28864 KB Output is correct
41 Correct 16 ms 28484 KB Output is correct
42 Correct 15 ms 28756 KB Output is correct
43 Correct 18 ms 29140 KB Output is correct
44 Correct 21 ms 29396 KB Output is correct
45 Correct 557 ms 71004 KB Output is correct
46 Correct 843 ms 90112 KB Output is correct
47 Correct 881 ms 89676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 28800 KB Output is correct
2 Correct 14 ms 28776 KB Output is correct
3 Correct 14 ms 28372 KB Output is correct
4 Correct 13 ms 28756 KB Output is correct
5 Correct 14 ms 28756 KB Output is correct
6 Correct 14 ms 28436 KB Output is correct
7 Correct 15 ms 28432 KB Output is correct
8 Correct 15 ms 28408 KB Output is correct
9 Correct 529 ms 80744 KB Output is correct
10 Correct 37 ms 34120 KB Output is correct
11 Correct 175 ms 56976 KB Output is correct
12 Correct 53 ms 36772 KB Output is correct
13 Correct 129 ms 48468 KB Output is correct
14 Correct 16 ms 28884 KB Output is correct
15 Correct 18 ms 29232 KB Output is correct
16 Correct 493 ms 77588 KB Output is correct
17 Correct 16 ms 28756 KB Output is correct
18 Correct 16 ms 28756 KB Output is correct
19 Correct 16 ms 28764 KB Output is correct
20 Correct 14 ms 28800 KB Output is correct
21 Correct 14 ms 28372 KB Output is correct
22 Correct 15 ms 28800 KB Output is correct
23 Correct 1599 ms 115524 KB Output is correct
24 Correct 15 ms 28840 KB Output is correct
25 Correct 19 ms 29416 KB Output is correct
26 Correct 24 ms 29516 KB Output is correct
27 Correct 25 ms 29820 KB Output is correct
28 Correct 519 ms 64904 KB Output is correct
29 Correct 871 ms 81788 KB Output is correct
30 Correct 1277 ms 101816 KB Output is correct
31 Correct 1602 ms 116012 KB Output is correct
32 Correct 15 ms 28756 KB Output is correct
33 Correct 14 ms 28844 KB Output is correct
34 Correct 14 ms 28864 KB Output is correct
35 Correct 14 ms 28588 KB Output is correct
36 Correct 14 ms 28372 KB Output is correct
37 Correct 14 ms 28860 KB Output is correct
38 Correct 14 ms 28832 KB Output is correct
39 Correct 15 ms 28868 KB Output is correct
40 Correct 15 ms 28864 KB Output is correct
41 Correct 16 ms 28484 KB Output is correct
42 Correct 15 ms 28756 KB Output is correct
43 Correct 18 ms 29140 KB Output is correct
44 Correct 21 ms 29396 KB Output is correct
45 Correct 557 ms 71004 KB Output is correct
46 Correct 843 ms 90112 KB Output is correct
47 Correct 881 ms 89676 KB Output is correct
48 Correct 15 ms 28836 KB Output is correct
49 Correct 15 ms 28796 KB Output is correct
50 Correct 16 ms 28748 KB Output is correct
51 Correct 15 ms 28756 KB Output is correct
52 Correct 14 ms 28788 KB Output is correct
53 Correct 16 ms 28756 KB Output is correct
54 Correct 15 ms 28880 KB Output is correct
55 Correct 1703 ms 109724 KB Output is correct
56 Correct 15 ms 28756 KB Output is correct
57 Runtime error 48 ms 59732 KB Execution killed with signal 6
58 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 18 ms 28800 KB Output is correct
2 Correct 14 ms 28776 KB Output is correct
3 Correct 14 ms 28372 KB Output is correct
4 Correct 13 ms 28756 KB Output is correct
5 Correct 14 ms 28756 KB Output is correct
6 Correct 14 ms 28436 KB Output is correct
7 Correct 15 ms 28432 KB Output is correct
8 Correct 15 ms 28408 KB Output is correct
9 Correct 529 ms 80744 KB Output is correct
10 Correct 37 ms 34120 KB Output is correct
11 Correct 175 ms 56976 KB Output is correct
12 Correct 53 ms 36772 KB Output is correct
13 Correct 129 ms 48468 KB Output is correct
14 Correct 16 ms 28884 KB Output is correct
15 Correct 18 ms 29232 KB Output is correct
16 Correct 493 ms 77588 KB Output is correct
17 Correct 15 ms 28844 KB Output is correct
18 Correct 15 ms 28812 KB Output is correct
19 Correct 14 ms 28400 KB Output is correct
20 Correct 1073 ms 108116 KB Output is correct
21 Correct 1213 ms 104580 KB Output is correct
22 Correct 1049 ms 102812 KB Output is correct
23 Correct 1020 ms 113400 KB Output is correct
24 Correct 276 ms 45628 KB Output is correct
25 Correct 1161 ms 106772 KB Output is correct
26 Correct 1134 ms 106992 KB Output is correct
27 Correct 1238 ms 118248 KB Output is correct
28 Correct 1233 ms 118136 KB Output is correct
29 Correct 1237 ms 118248 KB Output is correct
30 Correct 1213 ms 118168 KB Output is correct
31 Correct 14 ms 28756 KB Output is correct
32 Correct 56 ms 34348 KB Output is correct
33 Correct 94 ms 37052 KB Output is correct
34 Correct 1137 ms 108216 KB Output is correct
35 Correct 39 ms 31336 KB Output is correct
36 Correct 196 ms 42920 KB Output is correct
37 Correct 501 ms 57304 KB Output is correct
38 Correct 398 ms 56360 KB Output is correct
39 Correct 560 ms 66648 KB Output is correct
40 Correct 805 ms 77340 KB Output is correct
41 Correct 1070 ms 86936 KB Output is correct
42 Correct 1272 ms 97584 KB Output is correct
43 Correct 16 ms 28756 KB Output is correct
44 Correct 14 ms 28800 KB Output is correct
45 Correct 15 ms 28848 KB Output is correct
46 Correct 14 ms 28396 KB Output is correct
47 Correct 15 ms 28372 KB Output is correct
48 Correct 14 ms 28764 KB Output is correct
49 Correct 15 ms 28756 KB Output is correct
50 Correct 15 ms 28756 KB Output is correct
51 Correct 14 ms 28756 KB Output is correct
52 Correct 17 ms 28416 KB Output is correct
53 Correct 17 ms 28788 KB Output is correct
54 Correct 19 ms 29120 KB Output is correct
55 Correct 20 ms 29396 KB Output is correct
56 Correct 575 ms 70188 KB Output is correct
57 Correct 822 ms 90192 KB Output is correct
58 Correct 863 ms 89588 KB Output is correct
59 Correct 16 ms 28480 KB Output is correct
60 Correct 18 ms 28780 KB Output is correct
61 Correct 15 ms 28372 KB Output is correct
62 Correct 1296 ms 130952 KB Output is correct
63 Correct 1236 ms 131292 KB Output is correct
64 Correct 1269 ms 132448 KB Output is correct
65 Correct 22 ms 29776 KB Output is correct
66 Correct 34 ms 30988 KB Output is correct
67 Correct 561 ms 68092 KB Output is correct
68 Correct 940 ms 89380 KB Output is correct
69 Correct 1296 ms 107560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 28800 KB Output is correct
2 Correct 14 ms 28776 KB Output is correct
3 Correct 14 ms 28372 KB Output is correct
4 Correct 13 ms 28756 KB Output is correct
5 Correct 14 ms 28756 KB Output is correct
6 Correct 14 ms 28436 KB Output is correct
7 Correct 15 ms 28432 KB Output is correct
8 Correct 15 ms 28408 KB Output is correct
9 Correct 529 ms 80744 KB Output is correct
10 Correct 37 ms 34120 KB Output is correct
11 Correct 175 ms 56976 KB Output is correct
12 Correct 53 ms 36772 KB Output is correct
13 Correct 129 ms 48468 KB Output is correct
14 Correct 16 ms 28884 KB Output is correct
15 Correct 18 ms 29232 KB Output is correct
16 Correct 493 ms 77588 KB Output is correct
17 Correct 1150 ms 134348 KB Output is correct
18 Correct 1156 ms 128180 KB Output is correct
19 Correct 1181 ms 108080 KB Output is correct
20 Correct 1423 ms 107992 KB Output is correct
21 Correct 1229 ms 107792 KB Output is correct
22 Correct 14 ms 28756 KB Output is correct
23 Correct 123 ms 40520 KB Output is correct
24 Correct 73 ms 34848 KB Output is correct
25 Correct 340 ms 49908 KB Output is correct
26 Correct 660 ms 65240 KB Output is correct
27 Correct 622 ms 68772 KB Output is correct
28 Correct 802 ms 76208 KB Output is correct
29 Correct 996 ms 89028 KB Output is correct
30 Correct 1292 ms 98476 KB Output is correct
31 Correct 1518 ms 107844 KB Output is correct
32 Correct 1515 ms 118188 KB Output is correct
33 Correct 1299 ms 135544 KB Output is correct
34 Correct 28 ms 30292 KB Output is correct
35 Correct 36 ms 31552 KB Output is correct
36 Correct 634 ms 70388 KB Output is correct
37 Correct 1052 ms 91504 KB Output is correct
38 Correct 1501 ms 113444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 28800 KB Output is correct
2 Correct 14 ms 28776 KB Output is correct
3 Correct 14 ms 28372 KB Output is correct
4 Correct 13 ms 28756 KB Output is correct
5 Correct 14 ms 28756 KB Output is correct
6 Correct 14 ms 28436 KB Output is correct
7 Correct 15 ms 28432 KB Output is correct
8 Correct 15 ms 28408 KB Output is correct
9 Correct 529 ms 80744 KB Output is correct
10 Correct 37 ms 34120 KB Output is correct
11 Correct 175 ms 56976 KB Output is correct
12 Correct 53 ms 36772 KB Output is correct
13 Correct 129 ms 48468 KB Output is correct
14 Correct 16 ms 28884 KB Output is correct
15 Correct 18 ms 29232 KB Output is correct
16 Correct 493 ms 77588 KB Output is correct
17 Correct 16 ms 28756 KB Output is correct
18 Correct 16 ms 28756 KB Output is correct
19 Correct 16 ms 28764 KB Output is correct
20 Correct 14 ms 28800 KB Output is correct
21 Correct 14 ms 28372 KB Output is correct
22 Correct 15 ms 28800 KB Output is correct
23 Correct 1599 ms 115524 KB Output is correct
24 Correct 15 ms 28840 KB Output is correct
25 Correct 19 ms 29416 KB Output is correct
26 Correct 24 ms 29516 KB Output is correct
27 Correct 25 ms 29820 KB Output is correct
28 Correct 519 ms 64904 KB Output is correct
29 Correct 871 ms 81788 KB Output is correct
30 Correct 1277 ms 101816 KB Output is correct
31 Correct 1602 ms 116012 KB Output is correct
32 Correct 15 ms 28756 KB Output is correct
33 Correct 14 ms 28844 KB Output is correct
34 Correct 14 ms 28864 KB Output is correct
35 Correct 14 ms 28588 KB Output is correct
36 Correct 14 ms 28372 KB Output is correct
37 Correct 14 ms 28860 KB Output is correct
38 Correct 14 ms 28832 KB Output is correct
39 Correct 15 ms 28868 KB Output is correct
40 Correct 15 ms 28864 KB Output is correct
41 Correct 16 ms 28484 KB Output is correct
42 Correct 15 ms 28756 KB Output is correct
43 Correct 18 ms 29140 KB Output is correct
44 Correct 21 ms 29396 KB Output is correct
45 Correct 557 ms 71004 KB Output is correct
46 Correct 843 ms 90112 KB Output is correct
47 Correct 881 ms 89676 KB Output is correct
48 Correct 15 ms 28836 KB Output is correct
49 Correct 15 ms 28796 KB Output is correct
50 Correct 16 ms 28748 KB Output is correct
51 Correct 15 ms 28756 KB Output is correct
52 Correct 14 ms 28788 KB Output is correct
53 Correct 16 ms 28756 KB Output is correct
54 Correct 15 ms 28880 KB Output is correct
55 Correct 1703 ms 109724 KB Output is correct
56 Correct 15 ms 28756 KB Output is correct
57 Runtime error 48 ms 59732 KB Execution killed with signal 6
58 Halted 0 ms 0 KB -