답안 #1058369

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1058369 2024-08-14T09:39:09 Z vjudge1 분수 공원 (IOI21_parks) C++17
15 / 100
2147 ms 164020 KB
#include "parks.h"
#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;
using ii = pair<int, int>;

int DX[] = {-1, 0, 1, 0};
int DY[] = {0, 1, 0, -1};

struct DSU {
    int nrc;
    vi e;
    DSU(int n) : nrc(n), e(n, -1) {}
    int repr(int u) {
        while(e[u] >= 0) u = e[u];
        return u;
    }

    bool same(int u, int v) { return repr(u) == repr(v); }

    void join(int u, int v) {
        u = repr(u);
        v = repr(v);
        if(u == v) return;
        --nrc;
        if(e[u] >= e[v]) swap(u, v);
        e[u] += e[v];
        e[v] = u;
    }
};

vi solve_assign(int m, int len, vector<vector<ii> > &G0, bool &ok) {
    /// will return a vector of len elements, with the id of the bench assigned to path i
    vector<set<ii> > G(m);
    for(int i = 0; i < m; ++i) {
        for(auto it : G0[i]) G[i].insert(it);
    }
   // printf("m=%d len=%d\n", m, len);
   // for(int i = 0; i < m; ++i) {
   //     printf("G[%d]: ", i);
   //     for(auto [a, b] : G[i]) printf("(%d, %d) ", a, b);
   //     printf("\n");
   // }
   // printf("\n");
    vi Re(len, -1);
    
    set<int> noduri_singure;
    for(int i = 0; i < m; ++i) {
        if(G[i].size() == 1) {
            noduri_singure.insert(i);
        }
    }
    while(!noduri_singure.empty()) {
        auto u = *noduri_singure.begin();
        noduri_singure.erase(u);
        if(G[u].empty()) continue;

        /// am muchia u<->v, care se refera la drumul cul
        auto [v, cul] = *G[u].begin(); 

        Re[cul] = u;
        G[v].erase({u, cul});
        G[u].clear();
        if(G[v].size() == 1) noduri_singure.insert(v);
    }


    ///fiecare nod are outdeg > 1 acum
    ///ar trebuii sa fi ramas doar cu un ciclu

    vi viz(m, 0);
    function<void(int, int)> dfs = [&](int u, int p) {
        for(auto [v, cul] : G[u]) {
            if(v == p) continue;
            if(!viz[v]) {
                viz[v] = 1;
                Re[cul] = v;
                dfs(v, u);
            }
            if(viz[u]) break;
        }
    };
    for(int i = 0; i < m; ++i) {
        if(G[i].size() > 1) {
            dfs(i, -1);
            break;
        }
    }
  //  printf("Re: ");
  //  for(auto it : Re) {
  //      printf("%d ", it);
  //  }
  //  printf("\n");
    for(auto it : Re) if(it == -1) ok = 0;
    return Re;
}

int construct_roads(vi x, vi y) {
    int n = (int)x.size();
    map<ii, int> Id;
    for(int i = 0; i < n; ++i)
        Id[ii{x[i], y[i]}] = i;
    auto get_bs_coord = [&](int u, int v) -> pair<ii, ii> {
        int x1 = x[u], y1 = y[u], x2 = x[v], y2 = y[v];
        int xcm = (x1 + x2) / 2, ycm = (y1 + y2) / 2;
        int dx = 2 - abs(x1 - x2), dy = 2 - abs(y1 - y2);
        assert(dx >= 0 && dy >= 0); // alaturate
        return make_pair(ii{xcm + dx / 2, ycm + dy / 2}, ii{xcm - dx / 2, ycm - dy / 2});
    };
    vector<ii> B;
    for(int i = 0; i < n; ++i) {
        for(int d = 0; d < 4; ++d) {
            int nx = x[i] + DX[d] * 2, ny = y[i] + DY[d] * 2;
            if(Id.count({nx, ny})) {
                int u = Id[ii{nx, ny}];
                auto [a, b] = get_bs_coord(i, u);
                assert(a.first & 1);
                assert(a.second & 1);
                assert(b.first & 1);
                assert(b.second & 1);
                B.push_back(a);
                B.push_back(b);
            }
        }
    }

    sort(B.begin(), B.end());
    B.resize(unique(B.begin(), B.end()) - B.begin());

    map<ii, int> BenchId;
    int m = (int)B.size();

    vector<vi> L(n); /// graful pe fantani
    vector<ii> E; // edges
    set<ii> SetEdges;
    vector<vector<ii> > G(m); /// graful pe banci
    for(int i = 0; i < B.size(); ++i)
        BenchId[B[i]] = i;
    
    auto get_bs_id = [&](int u, int v) {
        auto [a, b] = get_bs_coord(u, v);
        return ii{BenchId[a], BenchId[b]};
    };

    vector<ii> DupaY;
    for(int i = 0; i < n; ++i)
        DupaY.push_back({y[i], i});
    sort(DupaY.begin(), DupaY.end());

    vi Speciale = {DupaY[0].second};

    DSU UF(n);

    auto add_edge = [&](int u, int v) {
        if(SetEdges.count({u, v})) return;
        if(UF.same(u, v)) return;
        SetEdges.insert({u, v});
        SetEdges.insert({v, u});
        auto [a, b] = get_bs_id(u, v);
        G[a].push_back({b, E.size()});
        G[b].push_back({a, E.size()});
        L[u].push_back(v);
        L[v].push_back(u);
        E.push_back({u, v});
        UF.join(u, v);
    };

    for(int i = 1; i < n; ++i) {
        int u = DupaY[i - 1].second, v = DupaY[i].second;
        if(y[u] != y[v] || abs(x[u] - x[v]) > 2) {
            Speciale.push_back(v);
            Speciale.push_back(u);
        }
        else add_edge(u, v);
    }

    mt19937 rng(1);
    auto nr = [&](int a, int b) { return uniform_int_distribution<int>(a, b)(rng); };

    shuffle(Speciale.begin(), Speciale.end(), rng);
    for(auto it : Speciale) {
        for(int d = 0; d < 4; ++d) {
            int nx = x[it] + DX[d] * 2, ny = y[it] + DY[d] * 2;
            if(Id.count({nx, ny})) {
                int u = Id[ii{nx, ny}];
                add_edge(it, u);
            }
        }
    }

    if(UF.nrc != 1) return 0;

   // printf("muchile pe fantani:\n");
   // for(auto [u, v] : E) {
   //     printf("%d %d\n", u, v);
   // }
   // printf("\n");

    bool ok = 1;
    auto Re = solve_assign(m, E.size(), G, ok);
    if(!ok) return 0;

    int len = E.size();
    vi U(len), V(len), RA(len), RB(len);
    for(int i = 0; i < len; ++i) {
        tie(U[i], V[i]) = E[i];
        int id = Re[i];
        assert(id >= 0 && id < B.size());
        tie(RA[i], RB[i]) = B[id];
    }

    build(U, V, RA, RB);
    return 1;
}

Compilation message

parks.cpp: In function 'int construct_roads(vi, vi)':
parks.cpp:139:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |     for(int i = 0; i < B.size(); ++i)
      |                    ~~^~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from parks.cpp:2:
parks.cpp:210:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  210 |         assert(id >= 0 && id < B.size());
      |                           ~~~^~~~~~~~~~
parks.cpp:180:10: warning: variable 'nr' set but not used [-Wunused-but-set-variable]
  180 |     auto nr = [&](int a, int b) { return uniform_int_distribution<int>(a, b)(rng); };
      |          ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 704 ms 81724 KB Output is correct
10 Correct 37 ms 8404 KB Output is correct
11 Correct 257 ms 43788 KB Output is correct
12 Correct 55 ms 12496 KB Output is correct
13 Correct 169 ms 23240 KB Output is correct
14 Correct 2 ms 860 KB Output is correct
15 Correct 5 ms 1372 KB Output is correct
16 Correct 662 ms 80860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 704 ms 81724 KB Output is correct
10 Correct 37 ms 8404 KB Output is correct
11 Correct 257 ms 43788 KB Output is correct
12 Correct 55 ms 12496 KB Output is correct
13 Correct 169 ms 23240 KB Output is correct
14 Correct 2 ms 860 KB Output is correct
15 Correct 5 ms 1372 KB Output is correct
16 Correct 662 ms 80860 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1292 ms 132800 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 3 ms 1116 KB Output is correct
26 Correct 8 ms 1736 KB Output is correct
27 Correct 11 ms 2264 KB Output is correct
28 Correct 418 ms 52884 KB Output is correct
29 Correct 674 ms 80380 KB Output is correct
30 Correct 992 ms 105916 KB Output is correct
31 Correct 1310 ms 132704 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 596 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 5 ms 1116 KB Output is correct
44 Correct 7 ms 1628 KB Output is correct
45 Correct 559 ms 64980 KB Output is correct
46 Correct 940 ms 95332 KB Output is correct
47 Correct 984 ms 95412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 704 ms 81724 KB Output is correct
10 Correct 37 ms 8404 KB Output is correct
11 Correct 257 ms 43788 KB Output is correct
12 Correct 55 ms 12496 KB Output is correct
13 Correct 169 ms 23240 KB Output is correct
14 Correct 2 ms 860 KB Output is correct
15 Correct 5 ms 1372 KB Output is correct
16 Correct 662 ms 80860 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1292 ms 132800 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 3 ms 1116 KB Output is correct
26 Correct 8 ms 1736 KB Output is correct
27 Correct 11 ms 2264 KB Output is correct
28 Correct 418 ms 52884 KB Output is correct
29 Correct 674 ms 80380 KB Output is correct
30 Correct 992 ms 105916 KB Output is correct
31 Correct 1310 ms 132704 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 596 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 5 ms 1116 KB Output is correct
44 Correct 7 ms 1628 KB Output is correct
45 Correct 559 ms 64980 KB Output is correct
46 Correct 940 ms 95332 KB Output is correct
47 Correct 984 ms 95412 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 344 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Incorrect 1377 ms 128044 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 704 ms 81724 KB Output is correct
10 Correct 37 ms 8404 KB Output is correct
11 Correct 257 ms 43788 KB Output is correct
12 Correct 55 ms 12496 KB Output is correct
13 Correct 169 ms 23240 KB Output is correct
14 Correct 2 ms 860 KB Output is correct
15 Correct 5 ms 1372 KB Output is correct
16 Correct 662 ms 80860 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1112 ms 109708 KB Output is correct
21 Correct 1302 ms 112312 KB Output is correct
22 Correct 1325 ms 111348 KB Output is correct
23 Correct 1338 ms 138676 KB Output is correct
24 Correct 440 ms 27008 KB Output is correct
25 Correct 1726 ms 105816 KB Output is correct
26 Correct 1566 ms 106936 KB Output is correct
27 Correct 1788 ms 163772 KB Output is correct
28 Correct 1706 ms 162756 KB Output is correct
29 Correct 2112 ms 162744 KB Output is correct
30 Correct 2147 ms 164020 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Incorrect 63 ms 9340 KB Solution announced impossible, but it is possible.
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 704 ms 81724 KB Output is correct
10 Correct 37 ms 8404 KB Output is correct
11 Correct 257 ms 43788 KB Output is correct
12 Correct 55 ms 12496 KB Output is correct
13 Correct 169 ms 23240 KB Output is correct
14 Correct 2 ms 860 KB Output is correct
15 Correct 5 ms 1372 KB Output is correct
16 Correct 662 ms 80860 KB Output is correct
17 Correct 1648 ms 162956 KB Output is correct
18 Correct 1620 ms 163516 KB Output is correct
19 Correct 1340 ms 109936 KB Output is correct
20 Incorrect 1731 ms 125932 KB Solution announced impossible, but it is possible.
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 704 ms 81724 KB Output is correct
10 Correct 37 ms 8404 KB Output is correct
11 Correct 257 ms 43788 KB Output is correct
12 Correct 55 ms 12496 KB Output is correct
13 Correct 169 ms 23240 KB Output is correct
14 Correct 2 ms 860 KB Output is correct
15 Correct 5 ms 1372 KB Output is correct
16 Correct 662 ms 80860 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1292 ms 132800 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 3 ms 1116 KB Output is correct
26 Correct 8 ms 1736 KB Output is correct
27 Correct 11 ms 2264 KB Output is correct
28 Correct 418 ms 52884 KB Output is correct
29 Correct 674 ms 80380 KB Output is correct
30 Correct 992 ms 105916 KB Output is correct
31 Correct 1310 ms 132704 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 0 ms 596 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 5 ms 1116 KB Output is correct
44 Correct 7 ms 1628 KB Output is correct
45 Correct 559 ms 64980 KB Output is correct
46 Correct 940 ms 95332 KB Output is correct
47 Correct 984 ms 95412 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 344 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Incorrect 1377 ms 128044 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -