Submission #712502

# Submission time Handle Problem Language Result Execution time Memory
712502 2023-03-18T22:56:56 Z danikoynov Fountain Parks (IOI21_parks) C++17
55 / 100
1824 ms 164288 KB
#include "parks.h"
#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 10;
int N, par[maxn];
int find_leader(int v)
{
    return (v == par[v]) ? v : (par[v] = find_leader(par[v]));
}

bool unite(int v, int u)
{
    v = find_leader(v);
    u = find_leader(u);
    if (v == u)
        return false;
    par[u] = v;
    return true;
}

struct edge
{
    int v, u, a, b;
    pair < pair < int, int >, pair < int, int > > db;
};

vector < edge > edges;
map < pair < int, int >, int > mp;
map < pair < int, int >, set < int > > pos;
void add_edge(pair < int, int > p, pair < int, int > q, int i, int j)
{
    edge cur;
    cur.v = i;
    cur.u = j;
    edges.push_back(cur);
    if (p.first > q.first)
        swap(p, q);
    if (p.second > q.second)
        swap(p, q);
    pair < int, int > curd;
    if (p.first == q.first)
    {
        pos[make_pair(p.first - 1, p.second + 1)].insert((int)edges.size() - 1);
        pos[ {p.first + 1,p.second + 1}].insert((int)edges.size() - 1);

        edges.back().db = {{p.first - 1, p.second + 1}, {p.first + 1, p.second + 1}};
    }
    else
    {
        pos[ {p.first + 1,p.second - 1}].insert((int)edges.size() - 1);
        pos[ {p.first + 1, p.second + 1}].insert((int)edges.size() - 1);
        edges.back().db = {{p.first + 1, p.second + 1}, {p.first + 1, p.second - 1}};
    }

}

vector < int > from, to, cx, cy;

int construct_roads(std::vector<int> x, std::vector<int> y)
{
    N = x.size();

    for (int i = 0; i < N; i ++)
    {
        par[i] = i;
        mp[ {x[i], y[i]}] = i + 1;
    }

    for (int i = 0; i < N; i ++)
    {
        for (int dx = -2; dx <= 2; dx ++)
        {
            if (abs(dx) < 2)
                continue;
            pair < int, int > nb = {x[i], y[i]};
            nb.first += dx;
            if (mp[nb] != 0)
            {
                if (unite(mp[nb] - 1, i))
                {
                    add_edge({x[i], y[i]}, nb, i, mp[nb] - 1);
                }
            }

            nb.first -= dx;
            nb.second += dx;
            if (mp[nb] != 0)
            {
                if (unite(mp[nb] - 1, i))
                {
                    add_edge({x[i], y[i]}, nb, i, mp[nb] - 1);
                }
            }
        }
    }
    int val = find_leader(0);
    for (int i = 1; i < N; i ++)
        if (find_leader(i) != val)
        return 0;
    /**for (int i = 0; i < edges.size(); i ++)
    {
        cout << edges[i].v << " " << edges[i].u << " " << edges[i].db.first.first << " " << edges[i].db.first.second << " " << edges[i].db.second.first << " " << edges[i].db.second.second << endl;
        //if (pos[edges[i].db])
    }*/
    set < pair < int, int > > q[5];
    for (pair < pair < int, int >, set < int > > cur : pos)
    {
        q[cur.second.size()].insert(cur.first);
    }

    for (int step = 0; step < edges.size(); step ++)
    {
        int pt = 1;
        while(q[pt].empty())
        {
           /// cout << "pt " << pt << endl;
            pt ++;
        }

        pair < int, int > cur = *q[pt].begin();
        int index = *pos[cur].begin();


        edges[index].a = cur.first;
        edges[index].b = cur.second;
        pair < int, int > d1 = edges[index].db.first;
        pair < int, int > d2 = edges[index].db.second;

        if (pos[d1].find(index) != pos[d1].end())
        {
            q[pos[d1].size()].erase(d1);
            pos[d1].erase(index);
            if (d1 != cur)
            q[pos[d1].size()].insert(d1);
        }
        if (pos[d2].find(index) != pos[d2].end())
        {
            q[pos[d2].size()].erase(d2);
            pos[d2].erase(index);
            if (d2 != cur)
            q[pos[d2].size()].insert(d2);
        }




    }

    from.resize(edges.size());
    to.resize(edges.size());
    cx.resize(edges.size());
    cy.resize(edges.size());

    for (int i = 0; i < edges.size(); i ++)
    {
        from[i] = edges[i].v;
        to[i] = edges[i].u;
        cx[i] = edges[i].a;
        cy[i] = edges[i].b;
    }

    build(from, to, cx, cy);
    /**for (int i = 0; i < edges.size(); i ++)
    {
        cout << edges[i].v << " " << edges[i].u << endl;
    }*/
    return 1;
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:113:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |     for (int step = 0; step < edges.size(); step ++)
      |                        ~~~~~^~~~~~~~~~~~~~
parks.cpp:156:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  156 |     for (int i = 0; i < edges.size(); i ++)
      |                     ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 729 ms 63856 KB Output is correct
10 Correct 46 ms 6724 KB Output is correct
11 Correct 255 ms 35084 KB Output is correct
12 Correct 63 ms 9976 KB Output is correct
13 Correct 103 ms 23924 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 5 ms 1364 KB Output is correct
16 Correct 749 ms 63812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 729 ms 63856 KB Output is correct
10 Correct 46 ms 6724 KB Output is correct
11 Correct 255 ms 35084 KB Output is correct
12 Correct 63 ms 9976 KB Output is correct
13 Correct 103 ms 23924 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 5 ms 1364 KB Output is correct
16 Correct 749 ms 63812 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 312 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1602 ms 100868 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 5 ms 852 KB Output is correct
26 Correct 7 ms 1364 KB Output is correct
27 Correct 8 ms 1876 KB Output is correct
28 Correct 553 ms 40360 KB Output is correct
29 Correct 1048 ms 60760 KB Output is correct
30 Correct 1221 ms 80888 KB Output is correct
31 Correct 1716 ms 101000 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 308 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 308 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 4 ms 1096 KB Output is correct
44 Correct 6 ms 1620 KB Output is correct
45 Correct 721 ms 54316 KB Output is correct
46 Correct 1118 ms 78696 KB Output is correct
47 Correct 1263 ms 78732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 729 ms 63856 KB Output is correct
10 Correct 46 ms 6724 KB Output is correct
11 Correct 255 ms 35084 KB Output is correct
12 Correct 63 ms 9976 KB Output is correct
13 Correct 103 ms 23924 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 5 ms 1364 KB Output is correct
16 Correct 749 ms 63812 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 312 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1602 ms 100868 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 5 ms 852 KB Output is correct
26 Correct 7 ms 1364 KB Output is correct
27 Correct 8 ms 1876 KB Output is correct
28 Correct 553 ms 40360 KB Output is correct
29 Correct 1048 ms 60760 KB Output is correct
30 Correct 1221 ms 80888 KB Output is correct
31 Correct 1716 ms 101000 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 308 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 308 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 4 ms 1096 KB Output is correct
44 Correct 6 ms 1620 KB Output is correct
45 Correct 721 ms 54316 KB Output is correct
46 Correct 1118 ms 78696 KB Output is correct
47 Correct 1263 ms 78732 KB Output is correct
48 Correct 1 ms 256 KB Output is correct
49 Correct 1 ms 308 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 312 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 308 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Runtime error 1623 ms 164288 KB Execution killed with signal 11
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 729 ms 63856 KB Output is correct
10 Correct 46 ms 6724 KB Output is correct
11 Correct 255 ms 35084 KB Output is correct
12 Correct 63 ms 9976 KB Output is correct
13 Correct 103 ms 23924 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 5 ms 1364 KB Output is correct
16 Correct 749 ms 63812 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1318 ms 88544 KB Output is correct
21 Correct 1425 ms 88100 KB Output is correct
22 Correct 1330 ms 97724 KB Output is correct
23 Correct 1410 ms 116228 KB Output is correct
24 Correct 797 ms 43628 KB Output is correct
25 Correct 1136 ms 93564 KB Output is correct
26 Correct 1113 ms 93400 KB Output is correct
27 Correct 1701 ms 124192 KB Output is correct
28 Correct 1620 ms 124292 KB Output is correct
29 Correct 1657 ms 124256 KB Output is correct
30 Correct 1704 ms 124352 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 60 ms 7772 KB Output is correct
33 Correct 179 ms 22204 KB Output is correct
34 Correct 1296 ms 88568 KB Output is correct
35 Correct 29 ms 4068 KB Output is correct
36 Correct 206 ms 19012 KB Output is correct
37 Correct 500 ms 37932 KB Output is correct
38 Correct 557 ms 39932 KB Output is correct
39 Correct 804 ms 54504 KB Output is correct
40 Correct 1027 ms 69508 KB Output is correct
41 Correct 1372 ms 84508 KB Output is correct
42 Correct 1575 ms 99212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 308 KB Output is correct
48 Correct 0 ms 312 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 312 KB Output is correct
54 Correct 4 ms 1108 KB Output is correct
55 Correct 10 ms 1608 KB Output is correct
56 Correct 765 ms 54264 KB Output is correct
57 Correct 1256 ms 78692 KB Output is correct
58 Correct 1157 ms 78588 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 1634 ms 126020 KB Output is correct
63 Correct 1793 ms 126040 KB Output is correct
64 Correct 1824 ms 125388 KB Output is correct
65 Correct 8 ms 1876 KB Output is correct
66 Correct 21 ms 3596 KB Output is correct
67 Correct 732 ms 52516 KB Output is correct
68 Correct 1219 ms 78932 KB Output is correct
69 Correct 1699 ms 105248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 729 ms 63856 KB Output is correct
10 Correct 46 ms 6724 KB Output is correct
11 Correct 255 ms 35084 KB Output is correct
12 Correct 63 ms 9976 KB Output is correct
13 Correct 103 ms 23924 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 5 ms 1364 KB Output is correct
16 Correct 749 ms 63812 KB Output is correct
17 Correct 1590 ms 135320 KB Output is correct
18 Correct 1606 ms 136784 KB Output is correct
19 Correct 1369 ms 89256 KB Output is correct
20 Correct 1486 ms 91944 KB Output is correct
21 Correct 1401 ms 103780 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 163 ms 16564 KB Output is correct
24 Correct 64 ms 7760 KB Output is correct
25 Correct 306 ms 26984 KB Output is correct
26 Correct 642 ms 44620 KB Output is correct
27 Correct 784 ms 49352 KB Output is correct
28 Correct 935 ms 61776 KB Output is correct
29 Correct 1183 ms 73916 KB Output is correct
30 Correct 1341 ms 85656 KB Output is correct
31 Correct 1586 ms 98252 KB Output is correct
32 Correct 1665 ms 105536 KB Output is correct
33 Correct 1707 ms 126116 KB Output is correct
34 Correct 12 ms 2336 KB Output is correct
35 Correct 22 ms 3904 KB Output is correct
36 Correct 686 ms 52092 KB Output is correct
37 Correct 1145 ms 78400 KB Output is correct
38 Correct 1699 ms 104580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 312 KB Output is correct
8 Correct 1 ms 312 KB Output is correct
9 Correct 729 ms 63856 KB Output is correct
10 Correct 46 ms 6724 KB Output is correct
11 Correct 255 ms 35084 KB Output is correct
12 Correct 63 ms 9976 KB Output is correct
13 Correct 103 ms 23924 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 5 ms 1364 KB Output is correct
16 Correct 749 ms 63812 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 312 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1602 ms 100868 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 5 ms 852 KB Output is correct
26 Correct 7 ms 1364 KB Output is correct
27 Correct 8 ms 1876 KB Output is correct
28 Correct 553 ms 40360 KB Output is correct
29 Correct 1048 ms 60760 KB Output is correct
30 Correct 1221 ms 80888 KB Output is correct
31 Correct 1716 ms 101000 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 308 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 308 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 4 ms 1096 KB Output is correct
44 Correct 6 ms 1620 KB Output is correct
45 Correct 721 ms 54316 KB Output is correct
46 Correct 1118 ms 78696 KB Output is correct
47 Correct 1263 ms 78732 KB Output is correct
48 Correct 1 ms 256 KB Output is correct
49 Correct 1 ms 308 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 312 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 1 ms 308 KB Output is correct
54 Correct 1 ms 212 KB Output is correct
55 Runtime error 1623 ms 164288 KB Execution killed with signal 11
56 Halted 0 ms 0 KB -