Submission #446948

# Submission time Handle Problem Language Result Execution time Memory
446948 2021-07-24T04:47:36 Z blue Fountain Parks (IOI21_parks) C++17
25 / 100
3500 ms 131824 KB
#include "parks.h"
#include <vector>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;

const int maxN = 200'000;
const int MX = 1'000'000;



vector<int> fountain_edge[maxN];
vector<int> fountain_visit(maxN, 0);
int visit_count = 0;

void fountain_dfs(int u)
{
    fountain_visit[u] = 1;
    visit_count++;
    for(int v: fountain_edge[u])
    {
        if(fountain_visit[v]) continue;
        fountain_dfs(v);
    }
}






struct point
{
    int i;
    int x;
    int y;
};

bool operator < (point A, point B)
{
    if(A.x == B.x) return A.y < B.y;
    return A.x < B.x;
};


int N;
vector<int> X, Y;
set<point> fountains;

int point_index(int x, int y)
{
    set<point>::iterator it = fountains.find(point{-1, x, y});
    if(it == fountains.end()) return -1;
    else return it->i;
}





struct path
{
    int i;
    int a;
    int b;

    path(int A, int B)
    {
        a = min(A, B);
        b = max(A, B);
    }

    path(int I, int A, int B)
    {
        i = I;
        a = min(A, B);
        b = max(A, B);
    }
};

bool operator < (path P, path Q)
{
    if(P.a == Q.a) return P.b < Q.b;
    return P.a < Q.a;
}

set<path> temp_paths;
set<path> paths;






const int X_orient = 0;
const int Y_orient = 1;

struct bench
{
    int i;
    int x;
    int y;

    int orientation()
    {
        if((x+y)%4 == 2) return X_orient;
        else return Y_orient;
    }
};

bool operator < (bench C, bench D)
{
    if(C.x == D.x) return C.y < D.y;
    return C.x < D.x;
}

set<bench> benches;

int bench_index(int x, int y)
{
    set<bench>::iterator it = benches.find(bench{-1, x, y});
    if(it == benches.end()) return -1;
    else return it->i;
}


vector<path>* bench_edge = new vector<path>[MX];











vector<int> bench_visit(MX, 0);
vector<int> path_visit(MX, 0);

vector<int> U, V, A, B;











vector<int> parent(maxN);
vector<int> siz(maxN, 1);


int getRoot(int u)
{
    int v;
    for(v = u; parent[v] != v; v = parent[v]);
    parent[u] = v;
    return v;
}

void join(int u, int v)
{
    u = getRoot(u);
    v = getRoot(v);

    if(siz[u] < siz[v]) swap(u, v);

    parent[v] = u;
}

bool connected(int u, int v)
{
    return getRoot(u) == getRoot(v);
}








int construct_roads(vector<int> x, vector<int> y)
{
    // cerr << "check\n";
    X = x;
    Y = y;

    N = X.size();

    for(int i = 0; i < N; i++)
        fountains.insert(point{i, x[i], y[i]});







    // cerr << "check 2\n";


    for(int i = 0; i < N; i++)
    {
        int I;

        I = point_index(x[i] - 2, y[i]);
        if(I != -1)
        {
            // cerr << i << ' ' << I << '\n';

            fountain_edge[i].push_back(I);
            fountain_edge[I].push_back(i);

            temp_paths.insert(path(i, I));

            // cerr << temp_paths.size() << '\n';
        }

        I = point_index(x[i], y[i] - 2);

        if(I != -1)
        {
            // cerr << i << ' ' << I << '\n';

            fountain_edge[i].push_back(I);
            fountain_edge[I].push_back(i);

            temp_paths.insert(path(i, I));

            // cerr << temp_paths.size() << '\n';
        }
    }

    // cerr << temp_paths.size() << '\n';

    // cerr << "check 3\n";






    fountain_dfs(0);
    if(visit_count != N)
    {
        return 0;
    }





    int path_id = -1;
    for(path p: temp_paths)
    {
        path_id++;
            // cerr << "P -> " << path_id << ' ' << p.a <<  ' ' << p.b << '\n';
        paths.insert(path(path_id, p.a, p.b));
    }

    // cerr << "check 4\n";







    int bench_id = -1;
    for(path p: paths)
    {
        // cerr << "p = " << p.i << ' ' << p.a << ' ' << p.b << '\n';
        int x1 = X[p.a];
        int y1 = Y[p.a];

        int x2 = X[p.b];
        int y2 = Y[p.b];


        bench new_bench;

        if(x1 == x2)
        {
            if( ((x1+1) + (y1+y2)/2) % 4 == 2 )
                new_bench = bench{-1, x1+1, (y1+y2)/2};
            else
                new_bench = bench{-1, x1-1, (y1+y2)/2};
        }
        else
        {
            if( ((x1+x2)/2 + (y1+1)) % 4 == 0 )
                new_bench = bench{-1, (x1+x2)/2, y1+1};
            else
                new_bench = bench{-1, (x1+x2)/2, y1-1};
        }

        int curr_bench_index;

        // cerr << "before I\n";

        int I = bench_index(new_bench.x, new_bench.y);

        if(I == -1)
        {
            bench_id++;
            curr_bench_index = bench_id;

            new_bench.i = curr_bench_index;

            benches.insert(new_bench);
        }
        else
        {
            curr_bench_index = I;
        }

        bench_edge[new_bench.i].push_back(p);
    }

    // cerr << "check 5\n";









    for(int i = 0; i < N; i++)
        parent[i] = i;


    for(int i = 0; i <= bench_id; i++)
    {
        sort(bench_edge[i].begin(), bench_edge[i].end(), [] (path P1, path P2)
        {
            if(X[P1.a] == X[P1.b]) return Y[P1.a] < Y[P1.b];
            else return X[P1.a] < X[P1.b];
        });
    }

    // cerr << "check 6\n";



    for(bench q: benches)
    {
        for(path z: bench_edge[q.i])
        {
            if(connected(z.a, z.b)) continue;

            U.push_back(z.a);
            V.push_back(z.b);
            A.push_back(q.x);
            B.push_back(q.y);

            join(z.a, z.b);

            break;
        }
    }

    // cerr << "check 7\n";

    build(U, V, A, B);
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 38660 KB Output is correct
2 Correct 20 ms 38632 KB Output is correct
3 Correct 20 ms 38668 KB Output is correct
4 Correct 19 ms 38660 KB Output is correct
5 Correct 24 ms 38668 KB Output is correct
6 Correct 19 ms 38612 KB Output is correct
7 Correct 20 ms 38684 KB Output is correct
8 Correct 20 ms 38604 KB Output is correct
9 Correct 417 ms 81096 KB Output is correct
10 Correct 39 ms 43032 KB Output is correct
11 Correct 153 ms 61524 KB Output is correct
12 Correct 50 ms 45252 KB Output is correct
13 Correct 64 ms 47812 KB Output is correct
14 Correct 22 ms 38860 KB Output is correct
15 Correct 21 ms 39032 KB Output is correct
16 Correct 405 ms 79656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 38660 KB Output is correct
2 Correct 20 ms 38632 KB Output is correct
3 Correct 20 ms 38668 KB Output is correct
4 Correct 19 ms 38660 KB Output is correct
5 Correct 24 ms 38668 KB Output is correct
6 Correct 19 ms 38612 KB Output is correct
7 Correct 20 ms 38684 KB Output is correct
8 Correct 20 ms 38604 KB Output is correct
9 Correct 417 ms 81096 KB Output is correct
10 Correct 39 ms 43032 KB Output is correct
11 Correct 153 ms 61524 KB Output is correct
12 Correct 50 ms 45252 KB Output is correct
13 Correct 64 ms 47812 KB Output is correct
14 Correct 22 ms 38860 KB Output is correct
15 Correct 21 ms 39032 KB Output is correct
16 Correct 405 ms 79656 KB Output is correct
17 Runtime error 55 ms 78084 KB Execution killed with signal 11
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 38660 KB Output is correct
2 Correct 20 ms 38632 KB Output is correct
3 Correct 20 ms 38668 KB Output is correct
4 Correct 19 ms 38660 KB Output is correct
5 Correct 24 ms 38668 KB Output is correct
6 Correct 19 ms 38612 KB Output is correct
7 Correct 20 ms 38684 KB Output is correct
8 Correct 20 ms 38604 KB Output is correct
9 Correct 417 ms 81096 KB Output is correct
10 Correct 39 ms 43032 KB Output is correct
11 Correct 153 ms 61524 KB Output is correct
12 Correct 50 ms 45252 KB Output is correct
13 Correct 64 ms 47812 KB Output is correct
14 Correct 22 ms 38860 KB Output is correct
15 Correct 21 ms 39032 KB Output is correct
16 Correct 405 ms 79656 KB Output is correct
17 Runtime error 55 ms 78084 KB Execution killed with signal 11
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 38660 KB Output is correct
2 Correct 20 ms 38632 KB Output is correct
3 Correct 20 ms 38668 KB Output is correct
4 Correct 19 ms 38660 KB Output is correct
5 Correct 24 ms 38668 KB Output is correct
6 Correct 19 ms 38612 KB Output is correct
7 Correct 20 ms 38684 KB Output is correct
8 Correct 20 ms 38604 KB Output is correct
9 Correct 417 ms 81096 KB Output is correct
10 Correct 39 ms 43032 KB Output is correct
11 Correct 153 ms 61524 KB Output is correct
12 Correct 50 ms 45252 KB Output is correct
13 Correct 64 ms 47812 KB Output is correct
14 Correct 22 ms 38860 KB Output is correct
15 Correct 21 ms 39032 KB Output is correct
16 Correct 405 ms 79656 KB Output is correct
17 Correct 20 ms 38596 KB Output is correct
18 Correct 20 ms 38568 KB Output is correct
19 Correct 20 ms 38604 KB Output is correct
20 Correct 974 ms 124592 KB Output is correct
21 Correct 1040 ms 123196 KB Output is correct
22 Correct 982 ms 122220 KB Output is correct
23 Correct 862 ms 109272 KB Output is correct
24 Correct 286 ms 57616 KB Output is correct
25 Correct 603 ms 76264 KB Output is correct
26 Correct 641 ms 76208 KB Output is correct
27 Correct 949 ms 117152 KB Output is correct
28 Correct 962 ms 117060 KB Output is correct
29 Correct 1181 ms 117096 KB Output is correct
30 Correct 1086 ms 117144 KB Output is correct
31 Correct 22 ms 38604 KB Output is correct
32 Correct 58 ms 44780 KB Output is correct
33 Correct 110 ms 48524 KB Output is correct
34 Correct 986 ms 123644 KB Output is correct
35 Correct 34 ms 40536 KB Output is correct
36 Correct 107 ms 48196 KB Output is correct
37 Correct 266 ms 57868 KB Output is correct
38 Correct 309 ms 70724 KB Output is correct
39 Correct 481 ms 82572 KB Output is correct
40 Correct 704 ms 94484 KB Output is correct
41 Correct 847 ms 106672 KB Output is correct
42 Correct 1046 ms 118540 KB Output is correct
43 Correct 22 ms 38676 KB Output is correct
44 Correct 25 ms 38672 KB Output is correct
45 Correct 25 ms 38576 KB Output is correct
46 Correct 21 ms 38604 KB Output is correct
47 Correct 24 ms 38672 KB Output is correct
48 Correct 22 ms 38676 KB Output is correct
49 Correct 22 ms 38604 KB Output is correct
50 Correct 23 ms 38560 KB Output is correct
51 Correct 21 ms 38624 KB Output is correct
52 Correct 21 ms 38620 KB Output is correct
53 Correct 22 ms 38676 KB Output is correct
54 Correct 25 ms 38964 KB Output is correct
55 Correct 25 ms 39244 KB Output is correct
56 Correct 420 ms 79904 KB Output is correct
57 Correct 644 ms 98748 KB Output is correct
58 Correct 646 ms 98480 KB Output is correct
59 Correct 24 ms 38576 KB Output is correct
60 Correct 24 ms 38604 KB Output is correct
61 Correct 21 ms 38676 KB Output is correct
62 Correct 964 ms 121728 KB Output is correct
63 Correct 1010 ms 121720 KB Output is correct
64 Correct 999 ms 122108 KB Output is correct
65 Correct 26 ms 39324 KB Output is correct
66 Correct 35 ms 40184 KB Output is correct
67 Correct 436 ms 79028 KB Output is correct
68 Correct 716 ms 99984 KB Output is correct
69 Correct 1033 ms 119268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 38660 KB Output is correct
2 Correct 20 ms 38632 KB Output is correct
3 Correct 20 ms 38668 KB Output is correct
4 Correct 19 ms 38660 KB Output is correct
5 Correct 24 ms 38668 KB Output is correct
6 Correct 19 ms 38612 KB Output is correct
7 Correct 20 ms 38684 KB Output is correct
8 Correct 20 ms 38604 KB Output is correct
9 Correct 417 ms 81096 KB Output is correct
10 Correct 39 ms 43032 KB Output is correct
11 Correct 153 ms 61524 KB Output is correct
12 Correct 50 ms 45252 KB Output is correct
13 Correct 64 ms 47812 KB Output is correct
14 Correct 22 ms 38860 KB Output is correct
15 Correct 21 ms 39032 KB Output is correct
16 Correct 405 ms 79656 KB Output is correct
17 Correct 963 ms 123932 KB Output is correct
18 Correct 1006 ms 123524 KB Output is correct
19 Correct 1012 ms 124356 KB Output is correct
20 Correct 1279 ms 131824 KB Output is correct
21 Correct 988 ms 113384 KB Output is correct
22 Correct 25 ms 38604 KB Output is correct
23 Correct 124 ms 52536 KB Output is correct
24 Correct 51 ms 42740 KB Output is correct
25 Correct 171 ms 53060 KB Output is correct
26 Correct 328 ms 63304 KB Output is correct
27 Correct 510 ms 83968 KB Output is correct
28 Correct 667 ms 95604 KB Output is correct
29 Correct 882 ms 106640 KB Output is correct
30 Correct 1068 ms 117548 KB Output is correct
31 Correct 1257 ms 129160 KB Output is correct
32 Execution timed out 3587 ms 123036 KB Time limit exceeded
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 38660 KB Output is correct
2 Correct 20 ms 38632 KB Output is correct
3 Correct 20 ms 38668 KB Output is correct
4 Correct 19 ms 38660 KB Output is correct
5 Correct 24 ms 38668 KB Output is correct
6 Correct 19 ms 38612 KB Output is correct
7 Correct 20 ms 38684 KB Output is correct
8 Correct 20 ms 38604 KB Output is correct
9 Correct 417 ms 81096 KB Output is correct
10 Correct 39 ms 43032 KB Output is correct
11 Correct 153 ms 61524 KB Output is correct
12 Correct 50 ms 45252 KB Output is correct
13 Correct 64 ms 47812 KB Output is correct
14 Correct 22 ms 38860 KB Output is correct
15 Correct 21 ms 39032 KB Output is correct
16 Correct 405 ms 79656 KB Output is correct
17 Runtime error 55 ms 78084 KB Execution killed with signal 11
18 Halted 0 ms 0 KB -