답안 #446949

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
446949 2021-07-24T04:54:03 Z blue 분수 공원 (IOI21_parks) C++17
25 / 100
3500 ms 112716 KB
#include "parks.h"
#include <vector>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;

const int maxN = 200'000;
const int MX = 500'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> 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21068 KB Output is correct
2 Correct 11 ms 21024 KB Output is correct
3 Correct 11 ms 20988 KB Output is correct
4 Correct 10 ms 21056 KB Output is correct
5 Correct 11 ms 20940 KB Output is correct
6 Correct 11 ms 21020 KB Output is correct
7 Correct 11 ms 20984 KB Output is correct
8 Correct 11 ms 20984 KB Output is correct
9 Correct 373 ms 62916 KB Output is correct
10 Correct 31 ms 25412 KB Output is correct
11 Correct 143 ms 43940 KB Output is correct
12 Correct 43 ms 27680 KB Output is correct
13 Correct 61 ms 30276 KB Output is correct
14 Correct 12 ms 21172 KB Output is correct
15 Correct 12 ms 21420 KB Output is correct
16 Correct 380 ms 61508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21068 KB Output is correct
2 Correct 11 ms 21024 KB Output is correct
3 Correct 11 ms 20988 KB Output is correct
4 Correct 10 ms 21056 KB Output is correct
5 Correct 11 ms 20940 KB Output is correct
6 Correct 11 ms 21020 KB Output is correct
7 Correct 11 ms 20984 KB Output is correct
8 Correct 11 ms 20984 KB Output is correct
9 Correct 373 ms 62916 KB Output is correct
10 Correct 31 ms 25412 KB Output is correct
11 Correct 143 ms 43940 KB Output is correct
12 Correct 43 ms 27680 KB Output is correct
13 Correct 61 ms 30276 KB Output is correct
14 Correct 12 ms 21172 KB Output is correct
15 Correct 12 ms 21420 KB Output is correct
16 Correct 380 ms 61508 KB Output is correct
17 Runtime error 29 ms 42476 KB Execution killed with signal 11
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21068 KB Output is correct
2 Correct 11 ms 21024 KB Output is correct
3 Correct 11 ms 20988 KB Output is correct
4 Correct 10 ms 21056 KB Output is correct
5 Correct 11 ms 20940 KB Output is correct
6 Correct 11 ms 21020 KB Output is correct
7 Correct 11 ms 20984 KB Output is correct
8 Correct 11 ms 20984 KB Output is correct
9 Correct 373 ms 62916 KB Output is correct
10 Correct 31 ms 25412 KB Output is correct
11 Correct 143 ms 43940 KB Output is correct
12 Correct 43 ms 27680 KB Output is correct
13 Correct 61 ms 30276 KB Output is correct
14 Correct 12 ms 21172 KB Output is correct
15 Correct 12 ms 21420 KB Output is correct
16 Correct 380 ms 61508 KB Output is correct
17 Runtime error 29 ms 42476 KB Execution killed with signal 11
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21068 KB Output is correct
2 Correct 11 ms 21024 KB Output is correct
3 Correct 11 ms 20988 KB Output is correct
4 Correct 10 ms 21056 KB Output is correct
5 Correct 11 ms 20940 KB Output is correct
6 Correct 11 ms 21020 KB Output is correct
7 Correct 11 ms 20984 KB Output is correct
8 Correct 11 ms 20984 KB Output is correct
9 Correct 373 ms 62916 KB Output is correct
10 Correct 31 ms 25412 KB Output is correct
11 Correct 143 ms 43940 KB Output is correct
12 Correct 43 ms 27680 KB Output is correct
13 Correct 61 ms 30276 KB Output is correct
14 Correct 12 ms 21172 KB Output is correct
15 Correct 12 ms 21420 KB Output is correct
16 Correct 380 ms 61508 KB Output is correct
17 Correct 11 ms 21012 KB Output is correct
18 Correct 12 ms 21068 KB Output is correct
19 Correct 11 ms 21068 KB Output is correct
20 Correct 935 ms 104948 KB Output is correct
21 Correct 943 ms 103284 KB Output is correct
22 Correct 1012 ms 102740 KB Output is correct
23 Correct 759 ms 90032 KB Output is correct
24 Correct 262 ms 38608 KB Output is correct
25 Correct 555 ms 57428 KB Output is correct
26 Correct 526 ms 57540 KB Output is correct
27 Correct 945 ms 98192 KB Output is correct
28 Correct 952 ms 98048 KB Output is correct
29 Correct 1123 ms 98120 KB Output is correct
30 Correct 995 ms 98060 KB Output is correct
31 Correct 11 ms 21068 KB Output is correct
32 Correct 48 ms 27160 KB Output is correct
33 Correct 93 ms 29896 KB Output is correct
34 Correct 899 ms 103928 KB Output is correct
35 Correct 23 ms 22860 KB Output is correct
36 Correct 97 ms 30228 KB Output is correct
37 Correct 238 ms 39248 KB Output is correct
38 Correct 304 ms 52168 KB Output is correct
39 Correct 471 ms 63756 KB Output is correct
40 Correct 672 ms 75248 KB Output is correct
41 Correct 829 ms 86960 KB Output is correct
42 Correct 975 ms 98892 KB Output is correct
43 Correct 11 ms 20960 KB Output is correct
44 Correct 11 ms 21052 KB Output is correct
45 Correct 11 ms 21004 KB Output is correct
46 Correct 12 ms 20956 KB Output is correct
47 Correct 11 ms 20940 KB Output is correct
48 Correct 11 ms 21068 KB Output is correct
49 Correct 11 ms 21052 KB Output is correct
50 Correct 12 ms 20940 KB Output is correct
51 Correct 11 ms 21036 KB Output is correct
52 Correct 11 ms 20964 KB Output is correct
53 Correct 11 ms 21068 KB Output is correct
54 Correct 12 ms 21452 KB Output is correct
55 Correct 13 ms 21580 KB Output is correct
56 Correct 380 ms 61724 KB Output is correct
57 Correct 610 ms 79896 KB Output is correct
58 Correct 594 ms 79688 KB Output is correct
59 Correct 11 ms 21068 KB Output is correct
60 Correct 11 ms 20968 KB Output is correct
61 Correct 11 ms 21056 KB Output is correct
62 Correct 916 ms 102532 KB Output is correct
63 Correct 943 ms 102412 KB Output is correct
64 Correct 917 ms 102756 KB Output is correct
65 Correct 14 ms 21708 KB Output is correct
66 Correct 19 ms 22552 KB Output is correct
67 Correct 404 ms 60588 KB Output is correct
68 Correct 656 ms 81104 KB Output is correct
69 Correct 952 ms 100120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21068 KB Output is correct
2 Correct 11 ms 21024 KB Output is correct
3 Correct 11 ms 20988 KB Output is correct
4 Correct 10 ms 21056 KB Output is correct
5 Correct 11 ms 20940 KB Output is correct
6 Correct 11 ms 21020 KB Output is correct
7 Correct 11 ms 20984 KB Output is correct
8 Correct 11 ms 20984 KB Output is correct
9 Correct 373 ms 62916 KB Output is correct
10 Correct 31 ms 25412 KB Output is correct
11 Correct 143 ms 43940 KB Output is correct
12 Correct 43 ms 27680 KB Output is correct
13 Correct 61 ms 30276 KB Output is correct
14 Correct 12 ms 21172 KB Output is correct
15 Correct 12 ms 21420 KB Output is correct
16 Correct 380 ms 61508 KB Output is correct
17 Correct 957 ms 104132 KB Output is correct
18 Correct 998 ms 103744 KB Output is correct
19 Correct 939 ms 104492 KB Output is correct
20 Correct 1185 ms 112716 KB Output is correct
21 Correct 977 ms 94148 KB Output is correct
22 Correct 11 ms 20940 KB Output is correct
23 Correct 108 ms 34564 KB Output is correct
24 Correct 40 ms 25028 KB Output is correct
25 Correct 151 ms 34660 KB Output is correct
26 Correct 338 ms 44228 KB Output is correct
27 Correct 475 ms 64932 KB Output is correct
28 Correct 651 ms 76264 KB Output is correct
29 Correct 804 ms 87028 KB Output is correct
30 Correct 986 ms 97772 KB Output is correct
31 Correct 1186 ms 109124 KB Output is correct
32 Execution timed out 3548 ms 103516 KB Time limit exceeded
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21068 KB Output is correct
2 Correct 11 ms 21024 KB Output is correct
3 Correct 11 ms 20988 KB Output is correct
4 Correct 10 ms 21056 KB Output is correct
5 Correct 11 ms 20940 KB Output is correct
6 Correct 11 ms 21020 KB Output is correct
7 Correct 11 ms 20984 KB Output is correct
8 Correct 11 ms 20984 KB Output is correct
9 Correct 373 ms 62916 KB Output is correct
10 Correct 31 ms 25412 KB Output is correct
11 Correct 143 ms 43940 KB Output is correct
12 Correct 43 ms 27680 KB Output is correct
13 Correct 61 ms 30276 KB Output is correct
14 Correct 12 ms 21172 KB Output is correct
15 Correct 12 ms 21420 KB Output is correct
16 Correct 380 ms 61508 KB Output is correct
17 Runtime error 29 ms 42476 KB Execution killed with signal 11
18 Halted 0 ms 0 KB -