답안 #560700

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
560700 2022-05-12T00:22:48 Z AlperenT 분수 공원 (IOI21_parks) C++17
30 / 100
589 ms 50436 KB
#include "parks.h"
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 2e5 + 5;
 
struct DSU{
    int par[N], setsize[N], setcnt;
 
    void reset(int n){
        for(int i = 0; i < n; i++) par[i] = i, setsize[i] = 1;
        setcnt = n;
    }
 
    int setfind(int a){
        if(a == par[a]) return a;
        else return par[a] = setfind(par[a]);
    }
 
    void setunion(int a, int b){
        a = setfind(a), b = setfind(b);
 
        if(a != b){
            if(setsize[b] > setsize[a]) swap(a, b);
            par[b] = par[a];
            setsize[a] += setsize[b];
            setcnt--;
        }
    }
};
 
DSU dsu;
 
struct Point{
    int x, y;
 
    bool operator < (const Point &sc) const{
        if(x == sc.x) return y < sc.y;
        else return x < sc.x;
    }
};
 
int construct_roads(vector<int> xvec, vector<int> yvec){
    int n = xvec.size();
 
    dsu.reset(n);
 
    map<Point, int> nodes;
    map<Point, Point> edges;
    map<Point, bool> vis;
 
    for(int i = 0; i < n; i++) nodes[{xvec[i], yvec[i]}] = i;
 
    if(*max_element(xvec.begin(), xvec.end()) <= 2){    // subtask 1
        for(auto p : nodes){
            Point po = p.first;
            if(nodes.find({2, po.y + 2}) != nodes.end()){
                edges[{p.second, nodes[{2, po.y + 2}]}] = {1, po.y + 1};
                dsu.setunion(p.second, nodes[{2, po.y + 2}]);
            }
        }
    }
    else if(*max_element(xvec.begin(), xvec.end()) <= 4){    // subtask 2
        vector<int> x2, x4;
 
        for(int i = 0; i < n; i++){
            if(xvec[i] == 2) x2.push_back(yvec[i]);
            else if(xvec[i] == 4) x4.push_back(yvec[i]);
        } 
 
        sort(x2.begin(), x2.end());
        sort(x4.begin(), x4.end());
 
        for(auto y : x2){
            if(nodes.find({2, y + 2}) != nodes.end()){
                edges[{nodes[{2, y}], nodes[{2, y + 2}]}] = {1, y + 1};
                dsu.setunion(nodes[{2, y}], nodes[{2, y + 2}]);
            }
        }
 
        for(auto y : x4){
            if(nodes.find({4, y + 2}) != nodes.end()){
                edges[{nodes[{4, y}], nodes[{4, y + 2}]}] = {5, y + 1};
                dsu.setunion(nodes[{4, y}], nodes[{4, y + 2}]);
            }
 
            if(nodes.find({2, y}) != nodes.end()){
                edges[{nodes[{4, y}], nodes[{2, y}]}] = {3, y + 1};
                dsu.setunion(nodes[{4, y}], nodes[{2, y}]);
            }
        }
    }
    else if(*max_element(xvec.begin(), xvec.end()) <= 6){    // subtask 3
        vector<int> x2, x4, x6;
 
        for(int i = 0; i < n; i++){
            if(xvec[i] == 2) x2.push_back(yvec[i]);
            else if(xvec[i] == 4) x4.push_back(yvec[i]);
            else if(xvec[i] == 6) x6.push_back(yvec[i]);
        } 
 
        sort(x2.begin(), x2.end());
        sort(x4.begin(), x4.end());
        sort(x6.begin(), x6.end());
 
        for(auto y : x2){
            if(nodes.find({2, y + 2}) != nodes.end()){
                edges[{nodes[{2, y}], nodes[{2, y + 2}]}] = {1, y + 1};
                dsu.setunion(nodes[{2, y}], nodes[{2, y + 2}]);
            }
        }
 
        for(auto y : x6){
            if(nodes.find({6, y + 2}) != nodes.end()){
                edges[{nodes[{6, y}], nodes[{6, y + 2}]}] = {7, y + 1};
                dsu.setunion(nodes[{6, y}], nodes[{6, y + 2}]);
            }
        }
 
        for(auto y : x4){
            if(nodes.find({4, y + 2}) != nodes.end()){
                dsu.setunion(nodes[{4, y}], nodes[{4, y + 2}]);
            }
        }
 
        for(auto y : x4){
            if(nodes.find({6, y}) != nodes.end() && dsu.setfind(nodes[{4, y}]) != dsu.setfind(nodes[{6, y}])){
                edges[{nodes[{4, y}], nodes[{6, y}]}] = {5, y - 1};
                vis[{5, y - 1}] = true;
                dsu.setunion(nodes[{4, y}], nodes[{6, y}]);
 
                if(nodes.find({2, y}) != nodes.end() && dsu.setfind(nodes[{4, y}]) != dsu.setfind(nodes[{2, y}])){
                    edges[{nodes[{2, y}], nodes[{4, y}]}] = {3, y + 1};
                    vis[{3, y + 1}] = true;
                    dsu.setunion(nodes[{2, y}], nodes[{4, y}]);
                }
            }
            else if(nodes.find({2, y}) != nodes.end() && dsu.setfind(nodes[{4, y}]) != dsu.setfind(nodes[{2, y}])){
                edges[{nodes[{2, y}], nodes[{4, y}]}] = {3, y - 1};
                vis[{3, y - 1}] = true;
                dsu.setunion(nodes[{2, y}], nodes[{4, y}]);
            }
        }
 
        for(auto y : x4){
            if(nodes.find({4, y + 2}) != nodes.end()){
                if(!vis[{3, y + 1}]){
                    edges[{nodes[{4, y}], nodes[{4, y + 2}]}] = {3, y + 1};
                }
                else if(!vis[{5, y + 1}]){
                    edges[{nodes[{4, y}], nodes[{4, y + 2}]}] = {5, y + 1};
                }
                // else assert(false);
            }
        }
    }
    else{    // subtask 4, 5
        queue<Point> que;

        Point fpo = nodes.begin()->first;

        if(nodes.find({fpo.x + 2, fpo.y}) != nodes.end()){
            edges[{nodes.begin()->second, nodes[{fpo.x + 2, fpo.y}]}] = {fpo.x + 1, fpo.y - 1};
            dsu.setunion(nodes.begin()->second, nodes[{fpo.x + 2, fpo.y}]);
            que.push({fpo.x + 1, fpo.y - 1});
        }
        else if(nodes.find({fpo.x, fpo.y + 2}) != nodes.end()){
            edges[{nodes.begin()->second, nodes[{fpo.x, fpo.y + 2}]}] = {fpo.x - 1, fpo.y + 1};
            dsu.setunion(nodes.begin()->second, nodes[{fpo.x, fpo.y + 2}]);
            que.push({fpo.x - 1, fpo.y + 1});
        }

        while(!que.empty()){
            Point p = que.front(); que.pop();

            if(nodes.find({p.x - 1, p.y - 1}) != nodes.end() && nodes.find({p.x - 1, p.y + 1}) != nodes.end() && !vis[{p.x - 2, p.y}] && dsu.setfind(nodes[{p.x - 1, p.y - 1}]) != dsu.setfind(nodes[{p.x - 1, p.y + 1}])){
                edges[{nodes[{p.x - 1, p.y - 1}], nodes[{p.x - 1, p.y + 1}]}] = {p.x - 2, p.y};
                dsu.setunion(nodes[{p.x - 1, p.y - 1}], nodes[{p.x - 1, p.y + 1}]);
                vis[{p.x - 2, p.y}] = true;
                que.push({p.x - 2, p.y});
            }
            if(nodes.find({p.x - 1, p.y - 1}) != nodes.end() && nodes.find({p.x + 1, p.y - 1}) != nodes.end() && !vis[{p.x, p.y - 2}] && dsu.setfind(nodes[{p.x - 1, p.y - 1}]) != dsu.setfind(nodes[{p.x + 1, p.y - 1}])){
                edges[{nodes[{p.x - 1, p.y - 1}], nodes[{p.x + 1, p.y - 1}]}] = {p.x, p.y - 2};
                dsu.setunion(nodes[{p.x - 1, p.y - 1}], nodes[{p.x + 1, p.y - 1}]);
                vis[{p.x, p.y - 2}] = true;
                que.push({p.x, p.y - 2});
            }
            if(nodes.find({p.x + 1, p.y - 1}) != nodes.end() && nodes.find({p.x + 1, p.y + 1}) != nodes.end() && !vis[{p.x + 2, p.y}] && dsu.setfind(nodes[{p.x + 1, p.y - 1}]) != dsu.setfind(nodes[{p.x + 1, p.y + 1}])){
                edges[{nodes[{p.x + 1, p.y - 1}], nodes[{p.x + 1, p.y + 1}]}] = {p.x + 2, p.y};
                dsu.setunion(nodes[{p.x + 1, p.y - 1}], nodes[{p.x + 1, p.y + 1}]);
                vis[{p.x + 2, p.y}] = true;
                que.push({p.x + 2, p.y});
            }
            if(nodes.find({p.x + 1, p.y + 1}) != nodes.end() && nodes.find({p.x - 1, p.y + 1}) != nodes.end() && !vis[{p.x, p.y + 2}] && dsu.setfind(nodes[{p.x + 1, p.y + 1}]) != dsu.setfind(nodes[{p.x - 1, p.y + 1}])){
                edges[{nodes[{p.x + 1, p.y + 1}], nodes[{p.x - 1, p.y + 1}]}] = {p.x, p.y + 2};
                dsu.setunion(nodes[{p.x + 1, p.y + 1}], nodes[{p.x - 1, p.y + 1}]);
                vis[{p.x, p.y + 2}] = true;
                que.push({p.x, p.y + 2});
            }
        }
    }
 
    if(dsu.setcnt == 1){
        vector<int> vecs[4];
 
        for(auto p : edges){
            vecs[0].push_back(p.first.x);
            vecs[1].push_back(p.first.y);
            vecs[2].push_back(p.second.x);
            vecs[3].push_back(p.second.y);
        }
 
        build(vecs[0], vecs[1], vecs[2], vecs[3]);
 
        return 1;
    }
    else return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 158 ms 19988 KB Output is correct
10 Correct 11 ms 2520 KB Output is correct
11 Correct 71 ms 10956 KB Output is correct
12 Correct 17 ms 3476 KB Output is correct
13 Correct 43 ms 6876 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 165 ms 20020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 158 ms 19988 KB Output is correct
10 Correct 11 ms 2520 KB Output is correct
11 Correct 71 ms 10956 KB Output is correct
12 Correct 17 ms 3476 KB Output is correct
13 Correct 43 ms 6876 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 165 ms 20020 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 589 ms 50436 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 3 ms 852 KB Output is correct
27 Correct 5 ms 984 KB Output is correct
28 Correct 179 ms 20584 KB Output is correct
29 Correct 305 ms 30476 KB Output is correct
30 Correct 436 ms 40608 KB Output is correct
31 Correct 586 ms 50428 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 2 ms 596 KB Output is correct
44 Correct 3 ms 724 KB Output is correct
45 Correct 182 ms 20192 KB Output is correct
46 Correct 304 ms 29784 KB Output is correct
47 Correct 306 ms 29672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 158 ms 19988 KB Output is correct
10 Correct 11 ms 2520 KB Output is correct
11 Correct 71 ms 10956 KB Output is correct
12 Correct 17 ms 3476 KB Output is correct
13 Correct 43 ms 6876 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 165 ms 20020 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 589 ms 50436 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 3 ms 852 KB Output is correct
27 Correct 5 ms 984 KB Output is correct
28 Correct 179 ms 20584 KB Output is correct
29 Correct 305 ms 30476 KB Output is correct
30 Correct 436 ms 40608 KB Output is correct
31 Correct 586 ms 50428 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 2 ms 596 KB Output is correct
44 Correct 3 ms 724 KB Output is correct
45 Correct 182 ms 20192 KB Output is correct
46 Correct 304 ms 29784 KB Output is correct
47 Correct 306 ms 29672 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 0 ms 212 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 550 ms 44352 KB Output is correct
56 Correct 1 ms 312 KB Output is correct
57 Correct 3 ms 724 KB Output is correct
58 Correct 9 ms 1748 KB Output is correct
59 Correct 11 ms 1964 KB Output is correct
60 Correct 223 ms 22328 KB Output is correct
61 Correct 316 ms 29612 KB Output is correct
62 Correct 406 ms 36120 KB Output is correct
63 Correct 536 ms 44136 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 0 ms 212 KB Output is correct
67 Correct 455 ms 40004 KB Output is correct
68 Correct 463 ms 39980 KB Output is correct
69 Correct 446 ms 39776 KB Output is correct
70 Correct 5 ms 980 KB Output is correct
71 Correct 11 ms 1792 KB Output is correct
72 Correct 227 ms 23772 KB Output is correct
73 Correct 385 ms 34540 KB Output is correct
74 Correct 556 ms 46460 KB Output is correct
75 Correct 523 ms 42512 KB Output is correct
76 Correct 470 ms 40048 KB Output is correct
77 Correct 5 ms 1108 KB Output is correct
78 Correct 10 ms 1876 KB Output is correct
79 Correct 227 ms 22996 KB Output is correct
80 Correct 380 ms 33820 KB Output is correct
81 Correct 554 ms 45500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 158 ms 19988 KB Output is correct
10 Correct 11 ms 2520 KB Output is correct
11 Correct 71 ms 10956 KB Output is correct
12 Correct 17 ms 3476 KB Output is correct
13 Correct 43 ms 6876 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 165 ms 20020 KB Output is correct
17 Incorrect 0 ms 212 KB Solution announced impossible, but it is possible.
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 158 ms 19988 KB Output is correct
10 Correct 11 ms 2520 KB Output is correct
11 Correct 71 ms 10956 KB Output is correct
12 Correct 17 ms 3476 KB Output is correct
13 Correct 43 ms 6876 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 165 ms 20020 KB Output is correct
17 Incorrect 153 ms 17484 KB Solution announced impossible, but it is possible.
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 158 ms 19988 KB Output is correct
10 Correct 11 ms 2520 KB Output is correct
11 Correct 71 ms 10956 KB Output is correct
12 Correct 17 ms 3476 KB Output is correct
13 Correct 43 ms 6876 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 165 ms 20020 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 589 ms 50436 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 2 ms 596 KB Output is correct
26 Correct 3 ms 852 KB Output is correct
27 Correct 5 ms 984 KB Output is correct
28 Correct 179 ms 20584 KB Output is correct
29 Correct 305 ms 30476 KB Output is correct
30 Correct 436 ms 40608 KB Output is correct
31 Correct 586 ms 50428 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 0 ms 212 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 2 ms 596 KB Output is correct
44 Correct 3 ms 724 KB Output is correct
45 Correct 182 ms 20192 KB Output is correct
46 Correct 304 ms 29784 KB Output is correct
47 Correct 306 ms 29672 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 0 ms 212 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Correct 0 ms 212 KB Output is correct
55 Correct 550 ms 44352 KB Output is correct
56 Correct 1 ms 312 KB Output is correct
57 Correct 3 ms 724 KB Output is correct
58 Correct 9 ms 1748 KB Output is correct
59 Correct 11 ms 1964 KB Output is correct
60 Correct 223 ms 22328 KB Output is correct
61 Correct 316 ms 29612 KB Output is correct
62 Correct 406 ms 36120 KB Output is correct
63 Correct 536 ms 44136 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 0 ms 212 KB Output is correct
67 Correct 455 ms 40004 KB Output is correct
68 Correct 463 ms 39980 KB Output is correct
69 Correct 446 ms 39776 KB Output is correct
70 Correct 5 ms 980 KB Output is correct
71 Correct 11 ms 1792 KB Output is correct
72 Correct 227 ms 23772 KB Output is correct
73 Correct 385 ms 34540 KB Output is correct
74 Correct 556 ms 46460 KB Output is correct
75 Correct 523 ms 42512 KB Output is correct
76 Correct 470 ms 40048 KB Output is correct
77 Correct 5 ms 1108 KB Output is correct
78 Correct 10 ms 1876 KB Output is correct
79 Correct 227 ms 22996 KB Output is correct
80 Correct 380 ms 33820 KB Output is correct
81 Correct 554 ms 45500 KB Output is correct
82 Incorrect 0 ms 212 KB Solution announced impossible, but it is possible.
83 Halted 0 ms 0 KB -