Submission #435797

# Submission time Handle Problem Language Result Execution time Memory
435797 2021-06-23T18:05:03 Z TLP39 Fountain Parks (IOI21_parks) C++17
30 / 100
360 ms 87056 KB
#include "parks.h"
#include<bits/stdc++.h>
using namespace std;
typedef pair<pair<int,int>,int> pp;
int n;
///bench position
int bench_lr[200010]={},bench_ba[200010]={};
///above,below,left,right;
int a[200010],b[200010],l[200010],r[200010];
///sort to get a b l r
vector<pp> fo,fo2;
///go back to find links: traceback above and below (get the x coor)
int trace_a[200010],trace_b[200010];
///to get potential connected links (that shouldn't collide)
int cur_y[200010],cur_x[200010];
vector<pair<int,bool>> link;
vector<int> adj[200010];
int chosen[200010]={};
///check if every fountains are connected
vector<int> adj2[200010];

void changebench(int x)
{
    if(x<0) return;
    if(a[x]<0) return;
    int temp=x;
    while(temp>=0 && a[temp]>=0 && bench_lr[temp]==-1)
    {
        bench_lr[temp]=1;
        temp=r[x];
    }
}

void dfs(int v,int col)
{
    chosen[v]=col;
    for(int i=0;i<adj[v].size();i++)
    {
        if(!chosen[adj[v][i]]) dfs(adj[v][i],3-col);
    }
}

int cou=0;
bool visited[200010]={};
void dfs_check(int v)
{
    cou++;
    for(int i=0;i<adj2[v].size();i++)
    {
        if(visited[adj2[v][i]]) continue;
        visited[adj2[v][i]]=true;
        dfs_check(adj2[v][i]);
    }
}

int construct_roads(std::vector<int> x, std::vector<int> y) {
    if (x.size() == 1) {
	build({}, {}, {}, {});
        return 1;
    }
    n=x.size();
    for(int i=0;i<n;i++)
    {
        fo.push_back({{x[i],y[i]},i});
        fo2.push_back({{y[i],x[i]},i});
        a[i]=-1;
        b[i]=-1;
        l[i]=-1;
        r[i]=-1;
    }
    for(int i=0;i<200010;i++) cur_x[i]=cur_y[i]=-2;
    sort(fo.begin(),fo.end());
    sort(fo2.begin(),fo2.end());
    for(int i=0;i<n-1;i++)
    {
        if(fo[i].first.first==fo[i+1].first.first && fo[i].first.second==fo[i+1].first.second-2)
        {
            a[fo[i].second]=fo[i+1].second;
            b[fo[i+1].second]=fo[i].second;
            bench_lr[fo[i].second]=-1;
        }
    }
    for(int i=0;i<n-1;i++)
    {
        if(fo2[i].first.first==fo2[i+1].first.first && fo2[i].first.second==fo2[i+1].first.second-2)
        {
            r[fo2[i].second]=fo2[i+1].second;
            l[fo2[i+1].second]=fo2[i].second;
        }
    }
    for(int i=0;i<n;i++)
    {
        if(a[i]>=0) adj2[i].push_back(a[i]);
        if(b[i]>=0) adj2[i].push_back(b[i]);
        if(l[i]>=0) adj2[i].push_back(l[i]);
        if(r[i]>=0) adj2[i].push_back(r[i]);
    }
    visited[0]=true;
    dfs_check(0);
    if(cou<n) return 0;
    int temp;
    for(int i=0;i<n-1;i++)
    {
        temp=fo[i].second;
        if(a[temp]<0)
        {
            trace_a[temp]=x[temp]+1;
        }
        else
        {
            if(l[temp]<0)
            {
                trace_a[temp]=x[temp]-1;
            }
            else
            {
                trace_a[temp]=trace_a[l[temp]];
            }
        }
        if(b[temp]<0)
        {
            trace_b[temp]=x[temp]+1;
        }
        else
        {
            if(l[temp]<0) trace_b[temp]=x[temp]-1;
            else trace_b[temp]=trace_b[l[temp]];
        }
    }
    int temp2;
    for(int i=0;i<n-1;i++)
    {
        temp=fo[i].second;
        if(r[temp]<0) continue;
        if(b[temp]<0 || b[r[temp]]<0)
        {
            link.push_back({temp,false});
            if(cur_y[y[temp]-1]>=0)
            {
                temp2=link[cur_y[y[temp]-1]].first;
                if(trace_b[temp]<=x[temp2]+1)
                {
                    adj[link.size()-1].push_back(cur_y[y[temp]-1]);
                    adj[cur_y[y[temp]-1]].push_back(link.size()-1);
                }
            }
            cur_y[y[temp]-1]=link.size()-1;
            cur_x[x[temp]+1]=link.size()-1;
        }
        if(a[temp]<0 || a[r[temp]]<0)
        {
            link.push_back({temp,true});
            if(cur_y[y[temp]+1]>=0)
            {
                temp2=link[cur_y[y[temp]+1]].first;
                if(trace_a[temp]<=x[temp2]+1)
                {
                    adj[link.size()-1].push_back(cur_y[y[temp]+1]);
                    adj[cur_y[y[temp]+1]].push_back(link.size()-1);
                }
            }
            cur_y[y[temp]+1]=link.size()-1;
            adj[link.size()-1].push_back(cur_x[x[temp]+1]);
            adj[cur_x[x[temp]+1]].push_back(link.size()-1);
            cur_x[x[temp]+1]=link.size()-1;
        }
    }
    for(int i=0;i<link.size();i++)
    {
        if(chosen[i]) continue;
        dfs(i,1);
    }
    for(int i=0;i<link.size();i++)
    {
        if(chosen[i]==1)
        {
            temp=link[i].first;
            if(link[i].second)
            {
                bench_ba[temp]=1;
                changebench(r[temp]);
            }
            else
            {
                bench_ba[temp]=-1;
                changebench(b[r[temp]]);
            }
        }
    }
    vector<int> u,v,aa,bb;
    for(int i=0;i<n;i++)
    {
        if(bench_lr[i])
        {
            u.push_back(i);
            v.push_back(a[i]);
            aa.push_back(x[i]+bench_lr[i]);
            bb.push_back(y[i]+1);
        }
        if(bench_ba[i])
        {
            u.push_back(i);
            v.push_back(r[i]);
            aa.push_back(x[i]+1);
            bb.push_back(y[i]+bench_ba[i]);
        }
    }
    build(u,v,aa,bb);
    return 1;
}

Compilation message

parks.cpp: In function 'void dfs(int, 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<adj[v].size();i++)
      |                 ~^~~~~~~~~~~~~~
parks.cpp: In function 'void dfs_check(int)':
parks.cpp:48:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |     for(int i=0;i<adj2[v].size();i++)
      |                 ~^~~~~~~~~~~~~~~
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:168:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, bool> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  168 |     for(int i=0;i<link.size();i++)
      |                 ~^~~~~~~~~~~~
parks.cpp:173:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, bool> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  173 |     for(int i=0;i<link.size();i++)
      |                 ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 9 ms 11212 KB Output is correct
3 Correct 8 ms 11212 KB Output is correct
4 Correct 8 ms 11212 KB Output is correct
5 Correct 8 ms 11212 KB Output is correct
6 Correct 8 ms 11212 KB Output is correct
7 Correct 8 ms 11228 KB Output is correct
8 Correct 8 ms 11196 KB Output is correct
9 Correct 139 ms 31820 KB Output is correct
10 Correct 19 ms 13516 KB Output is correct
11 Correct 57 ms 22532 KB Output is correct
12 Correct 21 ms 14484 KB Output is correct
13 Correct 28 ms 16692 KB Output is correct
14 Correct 9 ms 11288 KB Output is correct
15 Correct 9 ms 11408 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 9 ms 11212 KB Output is correct
3 Correct 8 ms 11212 KB Output is correct
4 Correct 8 ms 11212 KB Output is correct
5 Correct 8 ms 11212 KB Output is correct
6 Correct 8 ms 11212 KB Output is correct
7 Correct 8 ms 11228 KB Output is correct
8 Correct 8 ms 11196 KB Output is correct
9 Correct 139 ms 31820 KB Output is correct
10 Correct 19 ms 13516 KB Output is correct
11 Correct 57 ms 22532 KB Output is correct
12 Correct 21 ms 14484 KB Output is correct
13 Correct 28 ms 16692 KB Output is correct
14 Correct 9 ms 11288 KB Output is correct
15 Correct 9 ms 11408 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
17 Correct 9 ms 11300 KB Output is correct
18 Correct 8 ms 11212 KB Output is correct
19 Correct 8 ms 11196 KB Output is correct
20 Correct 8 ms 11212 KB Output is correct
21 Correct 9 ms 11212 KB Output is correct
22 Correct 8 ms 11212 KB Output is correct
23 Correct 295 ms 53176 KB Output is correct
24 Correct 9 ms 11212 KB Output is correct
25 Correct 10 ms 11468 KB Output is correct
26 Correct 10 ms 11596 KB Output is correct
27 Correct 10 ms 11592 KB Output is correct
28 Correct 109 ms 28252 KB Output is correct
29 Correct 169 ms 36044 KB Output is correct
30 Correct 252 ms 45496 KB Output is correct
31 Correct 311 ms 53252 KB Output is correct
32 Correct 11 ms 11212 KB Output is correct
33 Correct 9 ms 11212 KB Output is correct
34 Correct 9 ms 11300 KB Output is correct
35 Correct 8 ms 11212 KB Output is correct
36 Correct 9 ms 11272 KB Output is correct
37 Correct 9 ms 11212 KB Output is correct
38 Correct 10 ms 11288 KB Output is correct
39 Correct 10 ms 11272 KB Output is correct
40 Correct 9 ms 11240 KB Output is correct
41 Correct 8 ms 11212 KB Output is correct
42 Correct 11 ms 11212 KB Output is correct
43 Correct 10 ms 11408 KB Output is correct
44 Correct 11 ms 11468 KB Output is correct
45 Correct 146 ms 33400 KB Output is correct
46 Correct 227 ms 44660 KB Output is correct
47 Correct 218 ms 44336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 9 ms 11212 KB Output is correct
3 Correct 8 ms 11212 KB Output is correct
4 Correct 8 ms 11212 KB Output is correct
5 Correct 8 ms 11212 KB Output is correct
6 Correct 8 ms 11212 KB Output is correct
7 Correct 8 ms 11228 KB Output is correct
8 Correct 8 ms 11196 KB Output is correct
9 Correct 139 ms 31820 KB Output is correct
10 Correct 19 ms 13516 KB Output is correct
11 Correct 57 ms 22532 KB Output is correct
12 Correct 21 ms 14484 KB Output is correct
13 Correct 28 ms 16692 KB Output is correct
14 Correct 9 ms 11288 KB Output is correct
15 Correct 9 ms 11408 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
17 Correct 9 ms 11300 KB Output is correct
18 Correct 8 ms 11212 KB Output is correct
19 Correct 8 ms 11196 KB Output is correct
20 Correct 8 ms 11212 KB Output is correct
21 Correct 9 ms 11212 KB Output is correct
22 Correct 8 ms 11212 KB Output is correct
23 Correct 295 ms 53176 KB Output is correct
24 Correct 9 ms 11212 KB Output is correct
25 Correct 10 ms 11468 KB Output is correct
26 Correct 10 ms 11596 KB Output is correct
27 Correct 10 ms 11592 KB Output is correct
28 Correct 109 ms 28252 KB Output is correct
29 Correct 169 ms 36044 KB Output is correct
30 Correct 252 ms 45496 KB Output is correct
31 Correct 311 ms 53252 KB Output is correct
32 Correct 11 ms 11212 KB Output is correct
33 Correct 9 ms 11212 KB Output is correct
34 Correct 9 ms 11300 KB Output is correct
35 Correct 8 ms 11212 KB Output is correct
36 Correct 9 ms 11272 KB Output is correct
37 Correct 9 ms 11212 KB Output is correct
38 Correct 10 ms 11288 KB Output is correct
39 Correct 10 ms 11272 KB Output is correct
40 Correct 9 ms 11240 KB Output is correct
41 Correct 8 ms 11212 KB Output is correct
42 Correct 11 ms 11212 KB Output is correct
43 Correct 10 ms 11408 KB Output is correct
44 Correct 11 ms 11468 KB Output is correct
45 Correct 146 ms 33400 KB Output is correct
46 Correct 227 ms 44660 KB Output is correct
47 Correct 218 ms 44336 KB Output is correct
48 Correct 9 ms 11212 KB Output is correct
49 Correct 10 ms 11212 KB Output is correct
50 Correct 9 ms 11212 KB Output is correct
51 Correct 9 ms 11212 KB Output is correct
52 Correct 10 ms 11268 KB Output is correct
53 Correct 8 ms 11212 KB Output is correct
54 Correct 9 ms 11256 KB Output is correct
55 Correct 315 ms 50888 KB Output is correct
56 Correct 9 ms 11212 KB Output is correct
57 Correct 11 ms 11656 KB Output is correct
58 Correct 14 ms 12564 KB Output is correct
59 Correct 16 ms 12236 KB Output is correct
60 Correct 135 ms 32024 KB Output is correct
61 Correct 189 ms 40296 KB Output is correct
62 Correct 224 ms 44560 KB Output is correct
63 Correct 280 ms 52436 KB Output is correct
64 Correct 9 ms 11212 KB Output is correct
65 Correct 8 ms 11212 KB Output is correct
66 Correct 11 ms 11216 KB Output is correct
67 Correct 259 ms 50504 KB Output is correct
68 Correct 268 ms 50692 KB Output is correct
69 Correct 269 ms 51620 KB Output is correct
70 Correct 12 ms 11596 KB Output is correct
71 Correct 16 ms 12048 KB Output is correct
72 Correct 135 ms 32964 KB Output is correct
73 Correct 217 ms 45192 KB Output is correct
74 Correct 300 ms 55040 KB Output is correct
75 Correct 320 ms 62080 KB Output is correct
76 Correct 271 ms 53196 KB Output is correct
77 Correct 11 ms 11852 KB Output is correct
78 Correct 13 ms 12096 KB Output is correct
79 Correct 141 ms 34012 KB Output is correct
80 Correct 231 ms 46028 KB Output is correct
81 Correct 342 ms 57460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 9 ms 11212 KB Output is correct
3 Correct 8 ms 11212 KB Output is correct
4 Correct 8 ms 11212 KB Output is correct
5 Correct 8 ms 11212 KB Output is correct
6 Correct 8 ms 11212 KB Output is correct
7 Correct 8 ms 11228 KB Output is correct
8 Correct 8 ms 11196 KB Output is correct
9 Correct 139 ms 31820 KB Output is correct
10 Correct 19 ms 13516 KB Output is correct
11 Correct 57 ms 22532 KB Output is correct
12 Correct 21 ms 14484 KB Output is correct
13 Correct 28 ms 16692 KB Output is correct
14 Correct 9 ms 11288 KB Output is correct
15 Correct 9 ms 11408 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
17 Correct 9 ms 11220 KB Output is correct
18 Correct 9 ms 11212 KB Output is correct
19 Correct 9 ms 11212 KB Output is correct
20 Correct 339 ms 63492 KB Output is correct
21 Correct 319 ms 61224 KB Output is correct
22 Correct 360 ms 60160 KB Output is correct
23 Runtime error 242 ms 82936 KB Execution killed with signal 11
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 9 ms 11212 KB Output is correct
3 Correct 8 ms 11212 KB Output is correct
4 Correct 8 ms 11212 KB Output is correct
5 Correct 8 ms 11212 KB Output is correct
6 Correct 8 ms 11212 KB Output is correct
7 Correct 8 ms 11228 KB Output is correct
8 Correct 8 ms 11196 KB Output is correct
9 Correct 139 ms 31820 KB Output is correct
10 Correct 19 ms 13516 KB Output is correct
11 Correct 57 ms 22532 KB Output is correct
12 Correct 21 ms 14484 KB Output is correct
13 Correct 28 ms 16692 KB Output is correct
14 Correct 9 ms 11288 KB Output is correct
15 Correct 9 ms 11408 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
17 Correct 338 ms 63016 KB Output is correct
18 Correct 319 ms 62208 KB Output is correct
19 Correct 352 ms 63324 KB Output is correct
20 Runtime error 325 ms 87056 KB Execution killed with signal 11
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 9 ms 11212 KB Output is correct
3 Correct 8 ms 11212 KB Output is correct
4 Correct 8 ms 11212 KB Output is correct
5 Correct 8 ms 11212 KB Output is correct
6 Correct 8 ms 11212 KB Output is correct
7 Correct 8 ms 11228 KB Output is correct
8 Correct 8 ms 11196 KB Output is correct
9 Correct 139 ms 31820 KB Output is correct
10 Correct 19 ms 13516 KB Output is correct
11 Correct 57 ms 22532 KB Output is correct
12 Correct 21 ms 14484 KB Output is correct
13 Correct 28 ms 16692 KB Output is correct
14 Correct 9 ms 11288 KB Output is correct
15 Correct 9 ms 11408 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
17 Correct 9 ms 11300 KB Output is correct
18 Correct 8 ms 11212 KB Output is correct
19 Correct 8 ms 11196 KB Output is correct
20 Correct 8 ms 11212 KB Output is correct
21 Correct 9 ms 11212 KB Output is correct
22 Correct 8 ms 11212 KB Output is correct
23 Correct 295 ms 53176 KB Output is correct
24 Correct 9 ms 11212 KB Output is correct
25 Correct 10 ms 11468 KB Output is correct
26 Correct 10 ms 11596 KB Output is correct
27 Correct 10 ms 11592 KB Output is correct
28 Correct 109 ms 28252 KB Output is correct
29 Correct 169 ms 36044 KB Output is correct
30 Correct 252 ms 45496 KB Output is correct
31 Correct 311 ms 53252 KB Output is correct
32 Correct 11 ms 11212 KB Output is correct
33 Correct 9 ms 11212 KB Output is correct
34 Correct 9 ms 11300 KB Output is correct
35 Correct 8 ms 11212 KB Output is correct
36 Correct 9 ms 11272 KB Output is correct
37 Correct 9 ms 11212 KB Output is correct
38 Correct 10 ms 11288 KB Output is correct
39 Correct 10 ms 11272 KB Output is correct
40 Correct 9 ms 11240 KB Output is correct
41 Correct 8 ms 11212 KB Output is correct
42 Correct 11 ms 11212 KB Output is correct
43 Correct 10 ms 11408 KB Output is correct
44 Correct 11 ms 11468 KB Output is correct
45 Correct 146 ms 33400 KB Output is correct
46 Correct 227 ms 44660 KB Output is correct
47 Correct 218 ms 44336 KB Output is correct
48 Correct 9 ms 11212 KB Output is correct
49 Correct 10 ms 11212 KB Output is correct
50 Correct 9 ms 11212 KB Output is correct
51 Correct 9 ms 11212 KB Output is correct
52 Correct 10 ms 11268 KB Output is correct
53 Correct 8 ms 11212 KB Output is correct
54 Correct 9 ms 11256 KB Output is correct
55 Correct 315 ms 50888 KB Output is correct
56 Correct 9 ms 11212 KB Output is correct
57 Correct 11 ms 11656 KB Output is correct
58 Correct 14 ms 12564 KB Output is correct
59 Correct 16 ms 12236 KB Output is correct
60 Correct 135 ms 32024 KB Output is correct
61 Correct 189 ms 40296 KB Output is correct
62 Correct 224 ms 44560 KB Output is correct
63 Correct 280 ms 52436 KB Output is correct
64 Correct 9 ms 11212 KB Output is correct
65 Correct 8 ms 11212 KB Output is correct
66 Correct 11 ms 11216 KB Output is correct
67 Correct 259 ms 50504 KB Output is correct
68 Correct 268 ms 50692 KB Output is correct
69 Correct 269 ms 51620 KB Output is correct
70 Correct 12 ms 11596 KB Output is correct
71 Correct 16 ms 12048 KB Output is correct
72 Correct 135 ms 32964 KB Output is correct
73 Correct 217 ms 45192 KB Output is correct
74 Correct 300 ms 55040 KB Output is correct
75 Correct 320 ms 62080 KB Output is correct
76 Correct 271 ms 53196 KB Output is correct
77 Correct 11 ms 11852 KB Output is correct
78 Correct 13 ms 12096 KB Output is correct
79 Correct 141 ms 34012 KB Output is correct
80 Correct 231 ms 46028 KB Output is correct
81 Correct 342 ms 57460 KB Output is correct
82 Correct 9 ms 11220 KB Output is correct
83 Correct 9 ms 11212 KB Output is correct
84 Correct 9 ms 11212 KB Output is correct
85 Correct 339 ms 63492 KB Output is correct
86 Correct 319 ms 61224 KB Output is correct
87 Correct 360 ms 60160 KB Output is correct
88 Runtime error 242 ms 82936 KB Execution killed with signal 11
89 Halted 0 ms 0 KB -