Submission #446415

# Submission time Handle Problem Language Result Execution time Memory
446415 2021-07-21T21:28:49 Z luciocf Fountain Parks (IOI21_parks) C++17
30 / 100
857 ms 69616 KB
#include <bits/stdc++.h>
#include "parks.h"
 
using namespace std;
 
const int maxn = 2e5+10;
 
int linha[] = {-2, 2, 0, 0};
int coluna[] = {0, 0, -2, 2};
 
struct Pt
{
    int x, y, ind;
} a[maxn];
 
struct Road
{
    int u, v, x, y;
} road[maxn];
 
bool mark[maxn];
 
struct DSU
{
    int pai[maxn], peso[maxn];
 
    void init(int n)
    {
        for (int i = 1; i <= n; i++)
            pai[i] = i;
    }
 
    int Find(int x)
    {
        if (pai[x] == x) return x;
        return pai[x] = Find(pai[x]);
    }
 
    void Join(int x, int y)
    {
        x = Find(x), y = Find(y);
        if (x == y) return;
 
        if (peso[x] < peso[y]) swap(x, y);
 
        pai[y] = x, peso[x] += peso[y]; 
    }
} dsu;
 
bool comp(Pt a, Pt b)
{
    if (a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}
 
int construct_roads(vector<int> X, vector<int> Y)
{
    int n = X.size();
 
    bool sub = 1;
    int mn = 8, mx = 0;
 
    map<pair<int, int>, int> mp;
 
    for (int i = 1; i <= n; i++)
    {
        a[i] = {X[i-1], Y[i-1], i};
 
        mp[{a[i].x, a[i].y}] = i;
 
        mx = max(mx, a[i].x);
        mn = min(mn, a[i].x);
    }
 
    if (mx > 6) sub = 0;
 
    dsu.init(n);
 
    if (sub)
    {
        map<pair<int, int>, int> bench;
 
        sort(a+1, a+n+1, comp);
 
        int ind = 0;
 
        for (int i = 1; i < n; i++)
        {
            if (dsu.Find(a[i].ind) != dsu.Find(a[i+1].ind) && a[i].x == a[i+1].x && a[i].y+2 == a[i+1].y)
            {
                dsu.Join(a[i].ind, a[i+1].ind);
 
                road[++ind] = {a[i].ind, a[i+1].ind, a[i].x-1, a[i].y+1};
 
                if (a[i].x == mx)
                {
                    road[ind].x = a[i].x+1, road[ind].y = a[i].y+1;
                    bench[{a[i].x+1, a[i].y+1}] = 1;
                }
                else if (a[i].x == mn)
                {
                    bench[{a[i].x-1, a[i].y+1}] = 1;
                } 
                else
                {
                    if (bench[{a[i].x-1, a[i].y+1}] == 1)
                    {
                        road[ind].x = a[i].x+1, road[ind].y = a[i].y+1;
                        bench[{a[i].x+1, a[i].y+1}] = 1;
                    }
                    else
                    {
                        bench[{a[i].x-1, a[i].y+1}] = 1;
                    }
                }
            }
 
            if (mp[{a[i].x+2, a[i].y}] && dsu.Find(a[i].ind) != dsu.Find(mp[{a[i].x+2, a[i].y}]))
            {
                dsu.Join(a[i].ind, mp[{a[i].x+2, a[i].y}]);
 
                if (bench[{a[i].x+1, a[i].y-1}])
                {
                    road[++ind] = {a[i].ind, mp[{a[i].x+2, a[i].y}], a[i].x+1, a[i].y+1};
                    bench[{a[i].x+1, a[i].y+1}] = 1;
                }
                else
                {
                    road[++ind] = {a[i].ind, mp[{a[i].x+2, a[i].y}], a[i].x+1, a[i].y-1};
                    bench[{a[i].x+1, a[i].y-1}] = 1;
                }
            }
        }
 
        if (ind != n-1) return 0;
 
        vector<int> U, V, A, B;
 
        for (int i = 1; i < n; i++)
        {
            U.push_back(road[i].u-1), V.push_back(road[i].v-1);
            A.push_back(road[i].x), B.push_back(road[i].y);
        }
 
        build(U, V, A, B);
        return 1;
    }
 
    map<pair<int, int>, int> bench;
 
    int ind = 0;
 
    queue<int> fila;
    fila.push(1), mark[1] = 1;
 
    while (!fila.empty())
    {
        int i = fila.front(); fila.pop();
 
        for (int j = 0; j < 4; j++)
        {
            int dx = a[i].x+linha[j], dy = a[i].y+coluna[j];
 
            if (!mp[{dx, dy}]) continue;
 
            int o = mp[{dx, dy}];
 
            if (mark[o]) continue;
 
            mark[o] = 1, fila.push(o);
 
            if (dsu.Find(i) != dsu.Find(o) && a[i].x == a[o].x)
            {
                dsu.Join(i, o);

                int mn = min(a[i].y, a[o].y);
 
                road[++ind] = {i, o, a[i].x-1, mn+1};
 
                if (bench[{a[i].x-1, mn+1}] == 1)
                {
                    road[ind].x = a[i].x+1, road[ind].y = mn+1;
                    bench[{a[i].x+1, mn+1}] = 1;
                }
                else
                {
                    bench[{a[i].x-1, mn+1}] = 1;
                }
            }
 
            if (dsu.Find(i) != dsu.Find(o) && a[i].y == a[o].y)
            {
                dsu.Join(i, o);

                int mn = min(a[i].x, a[o].x);
 
                if (bench[{mn+1, a[i].y-1}])
                {
                    road[++ind] = {i, o, mn+1, a[i].y+1};
                    bench[{mn+1, a[i].y+1}] = 1;
                }
                else
                {
                    road[++ind] = {i, o, mn+1, a[i].y-1};
                    bench[{mn+1, a[i].y-1}] = 1;
                }
            }
        }
    }
 
    if (ind != n-1) return 0;
 
    vector<int> U, V, A, B;
 
    for (int i = 1; i < n; i++)
    {
        U.push_back(road[i].u-1), V.push_back(road[i].v-1);
        A.push_back(road[i].x), B.push_back(road[i].y);
    }
 
    build(U, V, A, B);
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 181 ms 28624 KB Output is correct
10 Correct 13 ms 3276 KB Output is correct
11 Correct 78 ms 15616 KB Output is correct
12 Correct 20 ms 4728 KB Output is correct
13 Correct 45 ms 10592 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 2 ms 716 KB Output is correct
16 Correct 188 ms 28744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 181 ms 28624 KB Output is correct
10 Correct 13 ms 3276 KB Output is correct
11 Correct 78 ms 15616 KB Output is correct
12 Correct 20 ms 4728 KB Output is correct
13 Correct 45 ms 10592 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 2 ms 716 KB Output is correct
16 Correct 188 ms 28744 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 489 ms 50564 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 2 ms 588 KB Output is correct
26 Correct 4 ms 844 KB Output is correct
27 Correct 5 ms 1100 KB Output is correct
28 Correct 153 ms 20488 KB Output is correct
29 Correct 255 ms 30508 KB Output is correct
30 Correct 361 ms 40604 KB Output is correct
31 Correct 464 ms 50460 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 0 ms 204 KB Output is correct
41 Correct 0 ms 204 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 2 ms 716 KB Output is correct
44 Correct 4 ms 972 KB Output is correct
45 Correct 209 ms 27044 KB Output is correct
46 Correct 295 ms 38972 KB Output is correct
47 Correct 320 ms 39068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 181 ms 28624 KB Output is correct
10 Correct 13 ms 3276 KB Output is correct
11 Correct 78 ms 15616 KB Output is correct
12 Correct 20 ms 4728 KB Output is correct
13 Correct 45 ms 10592 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 2 ms 716 KB Output is correct
16 Correct 188 ms 28744 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 489 ms 50564 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 2 ms 588 KB Output is correct
26 Correct 4 ms 844 KB Output is correct
27 Correct 5 ms 1100 KB Output is correct
28 Correct 153 ms 20488 KB Output is correct
29 Correct 255 ms 30508 KB Output is correct
30 Correct 361 ms 40604 KB Output is correct
31 Correct 464 ms 50460 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 0 ms 204 KB Output is correct
41 Correct 0 ms 204 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 2 ms 716 KB Output is correct
44 Correct 4 ms 972 KB Output is correct
45 Correct 209 ms 27044 KB Output is correct
46 Correct 295 ms 38972 KB Output is correct
47 Correct 320 ms 39068 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 0 ms 204 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 0 ms 204 KB Output is correct
52 Correct 0 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 0 ms 204 KB Output is correct
55 Correct 515 ms 48404 KB Output is correct
56 Correct 0 ms 204 KB Output is correct
57 Correct 3 ms 716 KB Output is correct
58 Correct 10 ms 1876 KB Output is correct
59 Correct 12 ms 2256 KB Output is correct
60 Correct 200 ms 24504 KB Output is correct
61 Correct 284 ms 32780 KB Output is correct
62 Correct 357 ms 40028 KB Output is correct
63 Correct 453 ms 48548 KB Output is correct
64 Correct 0 ms 204 KB Output is correct
65 Correct 0 ms 204 KB Output is correct
66 Correct 0 ms 204 KB Output is correct
67 Correct 450 ms 56748 KB Output is correct
68 Correct 464 ms 56732 KB Output is correct
69 Correct 425 ms 56472 KB Output is correct
70 Correct 5 ms 1100 KB Output is correct
71 Correct 9 ms 1996 KB Output is correct
72 Correct 188 ms 26728 KB Output is correct
73 Correct 318 ms 39992 KB Output is correct
74 Correct 440 ms 53124 KB Output is correct
75 Correct 448 ms 51740 KB Output is correct
76 Correct 418 ms 56836 KB Output is correct
77 Correct 6 ms 1356 KB Output is correct
78 Correct 10 ms 2180 KB Output is correct
79 Correct 194 ms 26532 KB Output is correct
80 Correct 311 ms 39596 KB Output is correct
81 Correct 492 ms 52596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 181 ms 28624 KB Output is correct
10 Correct 13 ms 3276 KB Output is correct
11 Correct 78 ms 15616 KB Output is correct
12 Correct 20 ms 4728 KB Output is correct
13 Correct 45 ms 10592 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 2 ms 716 KB Output is correct
16 Correct 188 ms 28744 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 567 ms 57180 KB Output is correct
21 Correct 604 ms 57028 KB Output is correct
22 Correct 623 ms 59392 KB Output is correct
23 Correct 571 ms 61032 KB Output is correct
24 Correct 188 ms 20804 KB Output is correct
25 Correct 181 ms 20928 KB Output is correct
26 Correct 181 ms 20932 KB Output is correct
27 Correct 830 ms 58668 KB Output is correct
28 Incorrect 857 ms 58652 KB Tree @(863, 999) appears more than once: for edges on positions 500 and 503
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 181 ms 28624 KB Output is correct
10 Correct 13 ms 3276 KB Output is correct
11 Correct 78 ms 15616 KB Output is correct
12 Correct 20 ms 4728 KB Output is correct
13 Correct 45 ms 10592 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 2 ms 716 KB Output is correct
16 Correct 188 ms 28744 KB Output is correct
17 Correct 704 ms 69568 KB Output is correct
18 Incorrect 732 ms 69616 KB Tree @(50003, 50001) appears more than once: for edges on positions 39329 and 39330
19 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 181 ms 28624 KB Output is correct
10 Correct 13 ms 3276 KB Output is correct
11 Correct 78 ms 15616 KB Output is correct
12 Correct 20 ms 4728 KB Output is correct
13 Correct 45 ms 10592 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 2 ms 716 KB Output is correct
16 Correct 188 ms 28744 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 489 ms 50564 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 2 ms 588 KB Output is correct
26 Correct 4 ms 844 KB Output is correct
27 Correct 5 ms 1100 KB Output is correct
28 Correct 153 ms 20488 KB Output is correct
29 Correct 255 ms 30508 KB Output is correct
30 Correct 361 ms 40604 KB Output is correct
31 Correct 464 ms 50460 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 0 ms 204 KB Output is correct
41 Correct 0 ms 204 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 2 ms 716 KB Output is correct
44 Correct 4 ms 972 KB Output is correct
45 Correct 209 ms 27044 KB Output is correct
46 Correct 295 ms 38972 KB Output is correct
47 Correct 320 ms 39068 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 0 ms 204 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 0 ms 204 KB Output is correct
52 Correct 0 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 0 ms 204 KB Output is correct
55 Correct 515 ms 48404 KB Output is correct
56 Correct 0 ms 204 KB Output is correct
57 Correct 3 ms 716 KB Output is correct
58 Correct 10 ms 1876 KB Output is correct
59 Correct 12 ms 2256 KB Output is correct
60 Correct 200 ms 24504 KB Output is correct
61 Correct 284 ms 32780 KB Output is correct
62 Correct 357 ms 40028 KB Output is correct
63 Correct 453 ms 48548 KB Output is correct
64 Correct 0 ms 204 KB Output is correct
65 Correct 0 ms 204 KB Output is correct
66 Correct 0 ms 204 KB Output is correct
67 Correct 450 ms 56748 KB Output is correct
68 Correct 464 ms 56732 KB Output is correct
69 Correct 425 ms 56472 KB Output is correct
70 Correct 5 ms 1100 KB Output is correct
71 Correct 9 ms 1996 KB Output is correct
72 Correct 188 ms 26728 KB Output is correct
73 Correct 318 ms 39992 KB Output is correct
74 Correct 440 ms 53124 KB Output is correct
75 Correct 448 ms 51740 KB Output is correct
76 Correct 418 ms 56836 KB Output is correct
77 Correct 6 ms 1356 KB Output is correct
78 Correct 10 ms 2180 KB Output is correct
79 Correct 194 ms 26532 KB Output is correct
80 Correct 311 ms 39596 KB Output is correct
81 Correct 492 ms 52596 KB Output is correct
82 Correct 0 ms 204 KB Output is correct
83 Correct 0 ms 204 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 567 ms 57180 KB Output is correct
86 Correct 604 ms 57028 KB Output is correct
87 Correct 623 ms 59392 KB Output is correct
88 Correct 571 ms 61032 KB Output is correct
89 Correct 188 ms 20804 KB Output is correct
90 Correct 181 ms 20928 KB Output is correct
91 Correct 181 ms 20932 KB Output is correct
92 Correct 830 ms 58668 KB Output is correct
93 Incorrect 857 ms 58652 KB Tree @(863, 999) appears more than once: for edges on positions 500 and 503
94 Halted 0 ms 0 KB -