Submission #788482

# Submission time Handle Problem Language Result Execution time Memory
788482 2023-07-20T09:20:14 Z alexander707070 Fountain Parks (IOI21_parks) C++17
55 / 100
1444 ms 181520 KB
#include<bits/stdc++.h>
#include "parks.h"
#define MAXN 800007
using namespace std;

struct bench{
    int from,to;
    int a,b;
};

int n,curr,benches,br,k,comp[MAXN],ans[MAXN],dsu[MAXN];
bool li[MAXN];
vector<int> v[MAXN],r[MAXN];
stack<int> st;

vector<bench> sol;
vector<int> A,B,C,D;
vector< pair<int,int> > ors;

map< pair<int,int>, int> mp;
map< pair<int,int>, vector<int> > bro;

int op(int x){
    if(x<400000)return x+400000;
    return x-400000;
}

void add(int x,int y,int z){
    for(int i=0;i<bro[{x,y}].size();i++){
        ors.push_back({op(bro[{x,y}][i]),op(z)});
    }
    bro[{x,y}].push_back(z);
}

void dfs(int x){
    li[x]=true;
    for(int i=0;i<v[x].size();i++){
        if(!li[v[x][i]])dfs(v[x][i]);
    }
    st.push(x);
}

void scc(int x){
    comp[x]=k;
    li[x]=true;

    for(int i=0;i<r[x].size();i++){
        if(!li[r[x][i]])scc(r[x][i]);
    }
}

int root(int x){
    if(dsu[x]==x)return x;
    dsu[x]=root(dsu[x]);
    return dsu[x];
}

void mergev(int x,int y){
    dsu[root(x)]=root(y);
}

int construct_roads(vector<int> x,vector<int> y){
    n=int(x.size());

    for(int i=0;i<n;i++){
        dsu[i]=i;
        mp[{x[i],y[i]}]=i+1;
    }

    br=0;
    for(int i=0;i<n;i++){
        curr=mp[{x[i]-2,y[i]}];
        if(curr!=0 and root(i)!=root(curr-1)){
            curr--; mergev(i,curr);

            add(x[i]-1,y[i]-1,br);
            add(x[i]-1,y[i]+1,br+1);
            ors.push_back({br,br+1});
            
            br+=2;
        }

        curr=mp[{x[i],y[i]-2}];
        if(curr!=0 and root(i)!=root(curr-1)){
            curr--; mergev(i,curr);

            add(x[i]-1,y[i]-1,br);
            add(x[i]+1,y[i]-1,br+1);
            ors.push_back({br,br+1});

            br+=2;
        }
    }

    if(br!=2*n-2)return 0;

    for(int i=0;i<ors.size();i++){
        v[op(ors[i].first)].push_back(ors[i].second);
        r[ors[i].second].push_back(op(ors[i].first));

        v[op(ors[i].second)].push_back(ors[i].first);
        r[ors[i].first].push_back(op(ors[i].second));
    }

    for(int i=0;i<800000;i++){
        if(!li[i])dfs(i);
    }
    for(int i=0;i<800000;i++)li[i]=false;
    while(!st.empty()){
        if(!li[st.top()]){
            k++; scc(st.top());
        }
        st.pop();
    }

    for(int i=0;i<br;i++){
        if(comp[i]==comp[op(i)])return 0;
        if(comp[i]<comp[op(i)])ans[i]=0;
        else ans[i]=1;
    }

    for(int i=0;i<n;i++)dsu[i]=i;

    br=0;
    for(int i=0;i<n;i++){
        curr=mp[{x[i]-2,y[i]}];
        if(curr!=0 and root(i)!=root(curr-1)){
            curr--; mergev(i,curr);
        
            if(ans[br]==0 and ans[br+1]==0)return -1;
            if(ans[br]==1){
                sol.push_back({curr,i,x[i]-1,y[i]-1});
            }else{
                sol.push_back({curr,i,x[i]-1,y[i]+1});
            }
            
            br+=2;
        }

        curr=mp[{x[i],y[i]-2}];
        if(curr!=0 and root(i)!=root(curr-1)){
            curr--; mergev(i,curr);

            if(ans[br]==0 and ans[br+1]==0)return -1;
            if(ans[br]==1){
                sol.push_back({curr,i,x[i]-1,y[i]-1});
            }else{
                sol.push_back({curr,i,x[i]+1,y[i]-1});
            }

            br+=2;
        }
    }
    
    for(int i=0;i<sol.size();i++){
        A.push_back(sol[i].from);
        B.push_back(sol[i].to);
        C.push_back(sol[i].a);
        D.push_back(sol[i].b);
    }

    build(A,B,C,D);
    return 1;
}

/*
int main(){
    cout<<construct_roads({4, 4, 6, 4, 2}, {4, 6, 4, 2, 4});
}
*/




Compilation message

parks.cpp: In function 'void add(int, int, int)':
parks.cpp:29:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |     for(int i=0;i<bro[{x,y}].size();i++){
      |                 ~^~~~~~~~~~~~~~~~~~
parks.cpp: In function 'void dfs(int)':
parks.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for(int i=0;i<v[x].size();i++){
      |                 ~^~~~~~~~~~~~
parks.cpp: In function 'void scc(int)':
parks.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     for(int i=0;i<r[x].size();i++){
      |                 ~^~~~~~~~~~~~
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:97:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |     for(int i=0;i<ors.size();i++){
      |                 ~^~~~~~~~~~~
parks.cpp:155:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<bench>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |     for(int i=0;i<sol.size();i++){
      |                 ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 26 ms 45000 KB Output is correct
2 Correct 28 ms 44972 KB Output is correct
3 Correct 18 ms 37844 KB Output is correct
4 Correct 30 ms 45020 KB Output is correct
5 Correct 31 ms 45052 KB Output is correct
6 Correct 18 ms 37844 KB Output is correct
7 Correct 19 ms 37808 KB Output is correct
8 Correct 17 ms 37844 KB Output is correct
9 Correct 518 ms 101108 KB Output is correct
10 Correct 43 ms 50260 KB Output is correct
11 Correct 180 ms 73944 KB Output is correct
12 Correct 55 ms 52960 KB Output is correct
13 Correct 75 ms 54508 KB Output is correct
14 Correct 20 ms 38144 KB Output is correct
15 Correct 20 ms 38532 KB Output is correct
16 Correct 545 ms 100948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 45000 KB Output is correct
2 Correct 28 ms 44972 KB Output is correct
3 Correct 18 ms 37844 KB Output is correct
4 Correct 30 ms 45020 KB Output is correct
5 Correct 31 ms 45052 KB Output is correct
6 Correct 18 ms 37844 KB Output is correct
7 Correct 19 ms 37808 KB Output is correct
8 Correct 17 ms 37844 KB Output is correct
9 Correct 518 ms 101108 KB Output is correct
10 Correct 43 ms 50260 KB Output is correct
11 Correct 180 ms 73944 KB Output is correct
12 Correct 55 ms 52960 KB Output is correct
13 Correct 75 ms 54508 KB Output is correct
14 Correct 20 ms 38144 KB Output is correct
15 Correct 20 ms 38532 KB Output is correct
16 Correct 545 ms 100948 KB Output is correct
17 Correct 27 ms 45048 KB Output is correct
18 Correct 27 ms 45084 KB Output is correct
19 Correct 28 ms 45028 KB Output is correct
20 Correct 28 ms 45056 KB Output is correct
21 Correct 18 ms 37884 KB Output is correct
22 Correct 29 ms 45004 KB Output is correct
23 Correct 1235 ms 154640 KB Output is correct
24 Correct 35 ms 45072 KB Output is correct
25 Correct 31 ms 45576 KB Output is correct
26 Correct 23 ms 38656 KB Output is correct
27 Correct 25 ms 38980 KB Output is correct
28 Correct 354 ms 87764 KB Output is correct
29 Correct 647 ms 109800 KB Output is correct
30 Correct 1026 ms 133288 KB Output is correct
31 Correct 1257 ms 155512 KB Output is correct
32 Correct 33 ms 45012 KB Output is correct
33 Correct 28 ms 45012 KB Output is correct
34 Correct 30 ms 45012 KB Output is correct
35 Correct 19 ms 37812 KB Output is correct
36 Correct 19 ms 37884 KB Output is correct
37 Correct 29 ms 45012 KB Output is correct
38 Correct 27 ms 45068 KB Output is correct
39 Correct 27 ms 45052 KB Output is correct
40 Correct 29 ms 45056 KB Output is correct
41 Correct 18 ms 37844 KB Output is correct
42 Correct 28 ms 45052 KB Output is correct
43 Correct 20 ms 38484 KB Output is correct
44 Correct 22 ms 38696 KB Output is correct
45 Correct 506 ms 102108 KB Output is correct
46 Correct 852 ms 129880 KB Output is correct
47 Correct 875 ms 129932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 45000 KB Output is correct
2 Correct 28 ms 44972 KB Output is correct
3 Correct 18 ms 37844 KB Output is correct
4 Correct 30 ms 45020 KB Output is correct
5 Correct 31 ms 45052 KB Output is correct
6 Correct 18 ms 37844 KB Output is correct
7 Correct 19 ms 37808 KB Output is correct
8 Correct 17 ms 37844 KB Output is correct
9 Correct 518 ms 101108 KB Output is correct
10 Correct 43 ms 50260 KB Output is correct
11 Correct 180 ms 73944 KB Output is correct
12 Correct 55 ms 52960 KB Output is correct
13 Correct 75 ms 54508 KB Output is correct
14 Correct 20 ms 38144 KB Output is correct
15 Correct 20 ms 38532 KB Output is correct
16 Correct 545 ms 100948 KB Output is correct
17 Correct 27 ms 45048 KB Output is correct
18 Correct 27 ms 45084 KB Output is correct
19 Correct 28 ms 45028 KB Output is correct
20 Correct 28 ms 45056 KB Output is correct
21 Correct 18 ms 37884 KB Output is correct
22 Correct 29 ms 45004 KB Output is correct
23 Correct 1235 ms 154640 KB Output is correct
24 Correct 35 ms 45072 KB Output is correct
25 Correct 31 ms 45576 KB Output is correct
26 Correct 23 ms 38656 KB Output is correct
27 Correct 25 ms 38980 KB Output is correct
28 Correct 354 ms 87764 KB Output is correct
29 Correct 647 ms 109800 KB Output is correct
30 Correct 1026 ms 133288 KB Output is correct
31 Correct 1257 ms 155512 KB Output is correct
32 Correct 33 ms 45012 KB Output is correct
33 Correct 28 ms 45012 KB Output is correct
34 Correct 30 ms 45012 KB Output is correct
35 Correct 19 ms 37812 KB Output is correct
36 Correct 19 ms 37884 KB Output is correct
37 Correct 29 ms 45012 KB Output is correct
38 Correct 27 ms 45068 KB Output is correct
39 Correct 27 ms 45052 KB Output is correct
40 Correct 29 ms 45056 KB Output is correct
41 Correct 18 ms 37844 KB Output is correct
42 Correct 28 ms 45052 KB Output is correct
43 Correct 20 ms 38484 KB Output is correct
44 Correct 22 ms 38696 KB Output is correct
45 Correct 506 ms 102108 KB Output is correct
46 Correct 852 ms 129880 KB Output is correct
47 Correct 875 ms 129932 KB Output is correct
48 Correct 28 ms 45012 KB Output is correct
49 Correct 29 ms 45028 KB Output is correct
50 Correct 34 ms 44956 KB Output is correct
51 Correct 29 ms 45012 KB Output is correct
52 Correct 29 ms 45056 KB Output is correct
53 Correct 30 ms 45056 KB Output is correct
54 Correct 29 ms 45012 KB Output is correct
55 Incorrect 1054 ms 137856 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 45000 KB Output is correct
2 Correct 28 ms 44972 KB Output is correct
3 Correct 18 ms 37844 KB Output is correct
4 Correct 30 ms 45020 KB Output is correct
5 Correct 31 ms 45052 KB Output is correct
6 Correct 18 ms 37844 KB Output is correct
7 Correct 19 ms 37808 KB Output is correct
8 Correct 17 ms 37844 KB Output is correct
9 Correct 518 ms 101108 KB Output is correct
10 Correct 43 ms 50260 KB Output is correct
11 Correct 180 ms 73944 KB Output is correct
12 Correct 55 ms 52960 KB Output is correct
13 Correct 75 ms 54508 KB Output is correct
14 Correct 20 ms 38144 KB Output is correct
15 Correct 20 ms 38532 KB Output is correct
16 Correct 545 ms 100948 KB Output is correct
17 Correct 28 ms 45056 KB Output is correct
18 Correct 29 ms 45008 KB Output is correct
19 Correct 19 ms 37848 KB Output is correct
20 Correct 1072 ms 178308 KB Output is correct
21 Correct 1391 ms 171312 KB Output is correct
22 Correct 1110 ms 166400 KB Output is correct
23 Correct 870 ms 142936 KB Output is correct
24 Correct 393 ms 79484 KB Output is correct
25 Correct 713 ms 112288 KB Output is correct
26 Correct 696 ms 112280 KB Output is correct
27 Correct 1212 ms 160332 KB Output is correct
28 Correct 1203 ms 160280 KB Output is correct
29 Correct 1108 ms 160320 KB Output is correct
30 Correct 1124 ms 160416 KB Output is correct
31 Correct 27 ms 45004 KB Output is correct
32 Correct 72 ms 52696 KB Output is correct
33 Correct 122 ms 58660 KB Output is correct
34 Correct 1362 ms 181520 KB Output is correct
35 Correct 34 ms 40664 KB Output is correct
36 Correct 129 ms 51392 KB Output is correct
37 Correct 271 ms 64700 KB Output is correct
38 Correct 377 ms 89836 KB Output is correct
39 Correct 603 ms 106972 KB Output is correct
40 Correct 868 ms 125444 KB Output is correct
41 Correct 1113 ms 142968 KB Output is correct
42 Correct 1414 ms 160756 KB Output is correct
43 Correct 29 ms 45016 KB Output is correct
44 Correct 30 ms 44980 KB Output is correct
45 Correct 30 ms 45040 KB Output is correct
46 Correct 21 ms 37832 KB Output is correct
47 Correct 20 ms 37888 KB Output is correct
48 Correct 29 ms 45012 KB Output is correct
49 Correct 29 ms 45012 KB Output is correct
50 Correct 28 ms 45060 KB Output is correct
51 Correct 30 ms 45012 KB Output is correct
52 Correct 19 ms 37812 KB Output is correct
53 Correct 30 ms 45008 KB Output is correct
54 Correct 26 ms 38432 KB Output is correct
55 Correct 23 ms 38704 KB Output is correct
56 Correct 528 ms 101300 KB Output is correct
57 Correct 929 ms 128912 KB Output is correct
58 Correct 903 ms 128964 KB Output is correct
59 Correct 21 ms 37824 KB Output is correct
60 Correct 28 ms 45060 KB Output is correct
61 Correct 20 ms 37888 KB Output is correct
62 Correct 1224 ms 160356 KB Output is correct
63 Correct 1255 ms 160392 KB Output is correct
64 Correct 1210 ms 159752 KB Output is correct
65 Correct 24 ms 38996 KB Output is correct
66 Correct 34 ms 40204 KB Output is correct
67 Correct 583 ms 101016 KB Output is correct
68 Correct 896 ms 131540 KB Output is correct
69 Correct 1401 ms 161040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 45000 KB Output is correct
2 Correct 28 ms 44972 KB Output is correct
3 Correct 18 ms 37844 KB Output is correct
4 Correct 30 ms 45020 KB Output is correct
5 Correct 31 ms 45052 KB Output is correct
6 Correct 18 ms 37844 KB Output is correct
7 Correct 19 ms 37808 KB Output is correct
8 Correct 17 ms 37844 KB Output is correct
9 Correct 518 ms 101108 KB Output is correct
10 Correct 43 ms 50260 KB Output is correct
11 Correct 180 ms 73944 KB Output is correct
12 Correct 55 ms 52960 KB Output is correct
13 Correct 75 ms 54508 KB Output is correct
14 Correct 20 ms 38144 KB Output is correct
15 Correct 20 ms 38532 KB Output is correct
16 Correct 545 ms 100948 KB Output is correct
17 Correct 1101 ms 160916 KB Output is correct
18 Correct 1163 ms 161004 KB Output is correct
19 Correct 1286 ms 179548 KB Output is correct
20 Correct 1237 ms 153704 KB Output is correct
21 Correct 1053 ms 144788 KB Output is correct
22 Correct 28 ms 45012 KB Output is correct
23 Correct 134 ms 61416 KB Output is correct
24 Correct 54 ms 43520 KB Output is correct
25 Correct 171 ms 57076 KB Output is correct
26 Correct 346 ms 70704 KB Output is correct
27 Correct 536 ms 99924 KB Output is correct
28 Correct 702 ms 114324 KB Output is correct
29 Correct 961 ms 130200 KB Output is correct
30 Correct 1119 ms 143712 KB Output is correct
31 Correct 1333 ms 158596 KB Output is correct
32 Correct 1270 ms 158056 KB Output is correct
33 Correct 1209 ms 160200 KB Output is correct
34 Correct 24 ms 39380 KB Output is correct
35 Correct 31 ms 40368 KB Output is correct
36 Correct 599 ms 100264 KB Output is correct
37 Correct 1047 ms 130300 KB Output is correct
38 Correct 1444 ms 159752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 45000 KB Output is correct
2 Correct 28 ms 44972 KB Output is correct
3 Correct 18 ms 37844 KB Output is correct
4 Correct 30 ms 45020 KB Output is correct
5 Correct 31 ms 45052 KB Output is correct
6 Correct 18 ms 37844 KB Output is correct
7 Correct 19 ms 37808 KB Output is correct
8 Correct 17 ms 37844 KB Output is correct
9 Correct 518 ms 101108 KB Output is correct
10 Correct 43 ms 50260 KB Output is correct
11 Correct 180 ms 73944 KB Output is correct
12 Correct 55 ms 52960 KB Output is correct
13 Correct 75 ms 54508 KB Output is correct
14 Correct 20 ms 38144 KB Output is correct
15 Correct 20 ms 38532 KB Output is correct
16 Correct 545 ms 100948 KB Output is correct
17 Correct 27 ms 45048 KB Output is correct
18 Correct 27 ms 45084 KB Output is correct
19 Correct 28 ms 45028 KB Output is correct
20 Correct 28 ms 45056 KB Output is correct
21 Correct 18 ms 37884 KB Output is correct
22 Correct 29 ms 45004 KB Output is correct
23 Correct 1235 ms 154640 KB Output is correct
24 Correct 35 ms 45072 KB Output is correct
25 Correct 31 ms 45576 KB Output is correct
26 Correct 23 ms 38656 KB Output is correct
27 Correct 25 ms 38980 KB Output is correct
28 Correct 354 ms 87764 KB Output is correct
29 Correct 647 ms 109800 KB Output is correct
30 Correct 1026 ms 133288 KB Output is correct
31 Correct 1257 ms 155512 KB Output is correct
32 Correct 33 ms 45012 KB Output is correct
33 Correct 28 ms 45012 KB Output is correct
34 Correct 30 ms 45012 KB Output is correct
35 Correct 19 ms 37812 KB Output is correct
36 Correct 19 ms 37884 KB Output is correct
37 Correct 29 ms 45012 KB Output is correct
38 Correct 27 ms 45068 KB Output is correct
39 Correct 27 ms 45052 KB Output is correct
40 Correct 29 ms 45056 KB Output is correct
41 Correct 18 ms 37844 KB Output is correct
42 Correct 28 ms 45052 KB Output is correct
43 Correct 20 ms 38484 KB Output is correct
44 Correct 22 ms 38696 KB Output is correct
45 Correct 506 ms 102108 KB Output is correct
46 Correct 852 ms 129880 KB Output is correct
47 Correct 875 ms 129932 KB Output is correct
48 Correct 28 ms 45012 KB Output is correct
49 Correct 29 ms 45028 KB Output is correct
50 Correct 34 ms 44956 KB Output is correct
51 Correct 29 ms 45012 KB Output is correct
52 Correct 29 ms 45056 KB Output is correct
53 Correct 30 ms 45056 KB Output is correct
54 Correct 29 ms 45012 KB Output is correct
55 Incorrect 1054 ms 137856 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -