답안 #834495

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
834495 2023-08-22T14:56:57 Z Abrar_Al_Samit 분수 공원 (IOI21_parks) C++17
15 / 100
753 ms 57836 KB
#include <bits/stdc++.h>
#include "parks.h"
using namespace std;

const int nax = 200000;
vector<int>g[nax];
bool vis[nax];
int par[nax], sz[nax];

void trav(int v) {
    vis[v] = 1;
    for(int u : g[v]) if(!vis[u]) {
        trav(u);
    }
}

int find(int v) {
    return par[v] = (v==par[v]) ? v : find(par[v]);
}
void unite(int u, int v) {
    u = find(u), v = find(v);
    if(sz[u] < sz[v]) swap(u, v);
    sz[u] += sz[v];
    par[v] = u;
}
int construct_roads(vector<int> x, vector<int> y) {
    int n = x.size();
    map<pair<int,int>, int>ft;
    for(int i=0; i<n; ++i) {
        ft[{x[i], y[i]}] = i;
    }

    int dx[] = {2, -2, 0, 0};
    int dy[] = {0, 0, 2, -2};

    vector<int>u, v, a, b;

    map<pair<int,int>, int>ben;

    for(int i=0; i<n; ++i) {
        par[i] = i, sz[i] = 1;
    }

    vector<pair<int,int>>eds;
    for(int i=0; i<n; ++i) {
        for(int j=0; j<4; ++j) {
            int nx = x[i] + dx[j], ny = y[i] + dy[j];
            if(ft.count(make_pair(nx, ny))) {
                int she = ft[{nx, ny}];
                if(find(i) == find(she)) continue;

                g[i].push_back(she);
                g[she].push_back(i);
                unite(i, she);
                eds.emplace_back(i, she);
            }
        }
    }
    trav(0);
    for(int i=0; i<n; ++i) if(!vis[i]) {
        return 0;
    }



    for(int i=0; i<n-1; ++i) {
        auto [me, she] = eds[i];
        int x0 = x[me], y0 = y[me], x1 = x[she], y1 = y[she];
        if(x0 > x1) swap(eds[i].first, eds[i].second);
    }

    sort(eds.begin(), eds.end(), [&] (auto p, auto q) {
        if(x[p.first]==x[q.first]) {
            return max(y[p.first], y[p.second]) > max(y[q.first], y[q.second]);
        }
        return x[p.first] < x[q.first];
    });


    for(auto [me, she] : eds) {
        u.push_back(me);
        v.push_back(she);

        int x0 = x[me], y0 = y[me], x1 = x[she], y1 = y[she];

        if(x0==x1) { //vertical
            if(!ben.count(make_pair(x0-1, y0 + y1 >> 1))) {
                ben[{x0-1, y0 + y1 >> 1}] = 1;
                a.push_back(x0-1);
                b.push_back(y0 + y1 >> 1);
            } else {
                if(ben.count(make_pair(x0+1, y0 + y1 >> 1))) return 0;
                ben[{x0+1, y0 + y1 >> 1}] = 1;
                a.push_back(x0+1);
                b.push_back(y0 + y1 >> 1);
            }
        } else {
            if(!ben.count(make_pair(x0 + x1 >> 1, y0 + 1))) {
                ben[{x0 + x1 >> 1, y0 + 1}] = 1;
                a.push_back(x0 + x1 >> 1);
                b.push_back(y0 + 1);
            } else {    
                if(ben.count(make_pair(x0 + x1 >> 1, y0 - 1))) return 0;
                ben[{x0 + x1 >> 1, y0 - 1}] = 1;
                a.push_back(x0 + x1 >> 1);
                b.push_back(y0 - 1);
            }
        }
    }
    build(u, v, a, b);
    return 1;
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:68:25: warning: unused variable 'y0' [-Wunused-variable]
   68 |         int x0 = x[me], y0 = y[me], x1 = x[she], y1 = y[she];
      |                         ^~
parks.cpp:68:50: warning: unused variable 'y1' [-Wunused-variable]
   68 |         int x0 = x[me], y0 = y[me], x1 = x[she], y1 = y[she];
      |                                                  ^~
parks.cpp:87:46: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   87 |             if(!ben.count(make_pair(x0-1, y0 + y1 >> 1))) {
      |                                           ~~~^~~~
parks.cpp:88:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   88 |                 ben[{x0-1, y0 + y1 >> 1}] = 1;
      |                            ~~~^~~~
parks.cpp:90:32: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   90 |                 b.push_back(y0 + y1 >> 1);
      |                             ~~~^~~~
parks.cpp:92:49: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   92 |                 if(ben.count(make_pair(x0+1, y0 + y1 >> 1))) return 0;
      |                                              ~~~^~~~
parks.cpp:93:31: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   93 |                 ben[{x0+1, y0 + y1 >> 1}] = 1;
      |                            ~~~^~~~
parks.cpp:95:32: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   95 |                 b.push_back(y0 + y1 >> 1);
      |                             ~~~^~~~
parks.cpp:98:40: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   98 |             if(!ben.count(make_pair(x0 + x1 >> 1, y0 + 1))) {
      |                                     ~~~^~~~
parks.cpp:99:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   99 |                 ben[{x0 + x1 >> 1, y0 + 1}] = 1;
      |                      ~~~^~~~
parks.cpp:100:32: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  100 |                 a.push_back(x0 + x1 >> 1);
      |                             ~~~^~~~
parks.cpp:103:43: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  103 |                 if(ben.count(make_pair(x0 + x1 >> 1, y0 - 1))) return 0;
      |                                        ~~~^~~~
parks.cpp:104:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  104 |                 ben[{x0 + x1 >> 1, y0 - 1}] = 1;
      |                      ~~~^~~~
parks.cpp:105:32: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  105 |                 a.push_back(x0 + x1 >> 1);
      |                             ~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4920 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 197 ms 30760 KB Output is correct
10 Correct 13 ms 7684 KB Output is correct
11 Correct 78 ms 18920 KB Output is correct
12 Correct 19 ms 8992 KB Output is correct
13 Correct 40 ms 10984 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 204 ms 30096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4920 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 197 ms 30760 KB Output is correct
10 Correct 13 ms 7684 KB Output is correct
11 Correct 78 ms 18920 KB Output is correct
12 Correct 19 ms 8992 KB Output is correct
13 Correct 40 ms 10984 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 204 ms 30096 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 3 ms 5008 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 2 ms 4948 KB Output is correct
22 Correct 2 ms 4948 KB Output is correct
23 Correct 553 ms 55408 KB Output is correct
24 Correct 2 ms 4948 KB Output is correct
25 Correct 4 ms 5332 KB Output is correct
26 Correct 5 ms 5332 KB Output is correct
27 Correct 6 ms 5460 KB Output is correct
28 Correct 177 ms 25144 KB Output is correct
29 Correct 293 ms 34912 KB Output is correct
30 Correct 463 ms 45636 KB Output is correct
31 Correct 632 ms 55200 KB Output is correct
32 Correct 3 ms 4948 KB Output is correct
33 Correct 2 ms 4948 KB Output is correct
34 Correct 2 ms 4948 KB Output is correct
35 Correct 2 ms 4948 KB Output is correct
36 Correct 2 ms 4948 KB Output is correct
37 Correct 2 ms 4948 KB Output is correct
38 Correct 2 ms 5016 KB Output is correct
39 Correct 2 ms 4948 KB Output is correct
40 Correct 2 ms 4948 KB Output is correct
41 Correct 2 ms 4948 KB Output is correct
42 Correct 2 ms 4948 KB Output is correct
43 Correct 4 ms 5204 KB Output is correct
44 Correct 5 ms 5332 KB Output is correct
45 Correct 243 ms 30252 KB Output is correct
46 Correct 395 ms 42248 KB Output is correct
47 Correct 422 ms 42064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4920 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 197 ms 30760 KB Output is correct
10 Correct 13 ms 7684 KB Output is correct
11 Correct 78 ms 18920 KB Output is correct
12 Correct 19 ms 8992 KB Output is correct
13 Correct 40 ms 10984 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 204 ms 30096 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 3 ms 5008 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 2 ms 4948 KB Output is correct
22 Correct 2 ms 4948 KB Output is correct
23 Correct 553 ms 55408 KB Output is correct
24 Correct 2 ms 4948 KB Output is correct
25 Correct 4 ms 5332 KB Output is correct
26 Correct 5 ms 5332 KB Output is correct
27 Correct 6 ms 5460 KB Output is correct
28 Correct 177 ms 25144 KB Output is correct
29 Correct 293 ms 34912 KB Output is correct
30 Correct 463 ms 45636 KB Output is correct
31 Correct 632 ms 55200 KB Output is correct
32 Correct 3 ms 4948 KB Output is correct
33 Correct 2 ms 4948 KB Output is correct
34 Correct 2 ms 4948 KB Output is correct
35 Correct 2 ms 4948 KB Output is correct
36 Correct 2 ms 4948 KB Output is correct
37 Correct 2 ms 4948 KB Output is correct
38 Correct 2 ms 5016 KB Output is correct
39 Correct 2 ms 4948 KB Output is correct
40 Correct 2 ms 4948 KB Output is correct
41 Correct 2 ms 4948 KB Output is correct
42 Correct 2 ms 4948 KB Output is correct
43 Correct 4 ms 5204 KB Output is correct
44 Correct 5 ms 5332 KB Output is correct
45 Correct 243 ms 30252 KB Output is correct
46 Correct 395 ms 42248 KB Output is correct
47 Correct 422 ms 42064 KB Output is correct
48 Correct 2 ms 4948 KB Output is correct
49 Correct 2 ms 4948 KB Output is correct
50 Correct 2 ms 4948 KB Output is correct
51 Incorrect 2 ms 4948 KB Solution announced impossible, but it is possible.
52 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4920 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 197 ms 30760 KB Output is correct
10 Correct 13 ms 7684 KB Output is correct
11 Correct 78 ms 18920 KB Output is correct
12 Correct 19 ms 8992 KB Output is correct
13 Correct 40 ms 10984 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 204 ms 30096 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 537 ms 57836 KB Output is correct
21 Correct 562 ms 56976 KB Output is correct
22 Correct 583 ms 56452 KB Output is correct
23 Correct 502 ms 48604 KB Output is correct
24 Correct 283 ms 22196 KB Output is correct
25 Correct 492 ms 30240 KB Output is correct
26 Correct 429 ms 30176 KB Output is correct
27 Correct 557 ms 53504 KB Output is correct
28 Correct 640 ms 53492 KB Output is correct
29 Correct 753 ms 55040 KB Output is correct
30 Correct 660 ms 55132 KB Output is correct
31 Correct 2 ms 4948 KB Output is correct
32 Incorrect 23 ms 7100 KB Solution announced impossible, but it is possible.
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4920 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 197 ms 30760 KB Output is correct
10 Correct 13 ms 7684 KB Output is correct
11 Correct 78 ms 18920 KB Output is correct
12 Correct 19 ms 8992 KB Output is correct
13 Correct 40 ms 10984 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 204 ms 30096 KB Output is correct
17 Correct 588 ms 56876 KB Output is correct
18 Correct 591 ms 55888 KB Output is correct
19 Incorrect 521 ms 48988 KB Solution announced impossible, but it is possible.
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4920 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 197 ms 30760 KB Output is correct
10 Correct 13 ms 7684 KB Output is correct
11 Correct 78 ms 18920 KB Output is correct
12 Correct 19 ms 8992 KB Output is correct
13 Correct 40 ms 10984 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 204 ms 30096 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 3 ms 5008 KB Output is correct
20 Correct 2 ms 4948 KB Output is correct
21 Correct 2 ms 4948 KB Output is correct
22 Correct 2 ms 4948 KB Output is correct
23 Correct 553 ms 55408 KB Output is correct
24 Correct 2 ms 4948 KB Output is correct
25 Correct 4 ms 5332 KB Output is correct
26 Correct 5 ms 5332 KB Output is correct
27 Correct 6 ms 5460 KB Output is correct
28 Correct 177 ms 25144 KB Output is correct
29 Correct 293 ms 34912 KB Output is correct
30 Correct 463 ms 45636 KB Output is correct
31 Correct 632 ms 55200 KB Output is correct
32 Correct 3 ms 4948 KB Output is correct
33 Correct 2 ms 4948 KB Output is correct
34 Correct 2 ms 4948 KB Output is correct
35 Correct 2 ms 4948 KB Output is correct
36 Correct 2 ms 4948 KB Output is correct
37 Correct 2 ms 4948 KB Output is correct
38 Correct 2 ms 5016 KB Output is correct
39 Correct 2 ms 4948 KB Output is correct
40 Correct 2 ms 4948 KB Output is correct
41 Correct 2 ms 4948 KB Output is correct
42 Correct 2 ms 4948 KB Output is correct
43 Correct 4 ms 5204 KB Output is correct
44 Correct 5 ms 5332 KB Output is correct
45 Correct 243 ms 30252 KB Output is correct
46 Correct 395 ms 42248 KB Output is correct
47 Correct 422 ms 42064 KB Output is correct
48 Correct 2 ms 4948 KB Output is correct
49 Correct 2 ms 4948 KB Output is correct
50 Correct 2 ms 4948 KB Output is correct
51 Incorrect 2 ms 4948 KB Solution announced impossible, but it is possible.
52 Halted 0 ms 0 KB -