답안 #435782

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
435782 2021-06-23T17:53:56 Z TLP39 분수 공원 (IOI21_parks) C++17
30 / 100
327 ms 87072 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(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++)
      |                 ~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 8 ms 11212 KB Output is correct
3 Correct 7 ms 11260 KB Output is correct
4 Correct 7 ms 11212 KB Output is correct
5 Correct 8 ms 11272 KB Output is correct
6 Correct 7 ms 11212 KB Output is correct
7 Correct 8 ms 11192 KB Output is correct
8 Correct 7 ms 11212 KB Output is correct
9 Correct 123 ms 31744 KB Output is correct
10 Correct 20 ms 13496 KB Output is correct
11 Correct 52 ms 22412 KB Output is correct
12 Correct 24 ms 14448 KB Output is correct
13 Correct 29 ms 16708 KB Output is correct
14 Correct 8 ms 11340 KB Output is correct
15 Correct 8 ms 11468 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 8 ms 11212 KB Output is correct
3 Correct 7 ms 11260 KB Output is correct
4 Correct 7 ms 11212 KB Output is correct
5 Correct 8 ms 11272 KB Output is correct
6 Correct 7 ms 11212 KB Output is correct
7 Correct 8 ms 11192 KB Output is correct
8 Correct 7 ms 11212 KB Output is correct
9 Correct 123 ms 31744 KB Output is correct
10 Correct 20 ms 13496 KB Output is correct
11 Correct 52 ms 22412 KB Output is correct
12 Correct 24 ms 14448 KB Output is correct
13 Correct 29 ms 16708 KB Output is correct
14 Correct 8 ms 11340 KB Output is correct
15 Correct 8 ms 11468 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
17 Correct 7 ms 11212 KB Output is correct
18 Correct 7 ms 11212 KB Output is correct
19 Correct 7 ms 11212 KB Output is correct
20 Correct 8 ms 11212 KB Output is correct
21 Correct 9 ms 11212 KB Output is correct
22 Correct 9 ms 11212 KB Output is correct
23 Correct 286 ms 53224 KB Output is correct
24 Correct 8 ms 11212 KB Output is correct
25 Correct 10 ms 11472 KB Output is correct
26 Correct 9 ms 11532 KB Output is correct
27 Correct 10 ms 11596 KB Output is correct
28 Correct 99 ms 28240 KB Output is correct
29 Correct 169 ms 36056 KB Output is correct
30 Correct 216 ms 45520 KB Output is correct
31 Correct 285 ms 53380 KB Output is correct
32 Correct 8 ms 11212 KB Output is correct
33 Correct 8 ms 11212 KB Output is correct
34 Correct 7 ms 11212 KB Output is correct
35 Correct 7 ms 11212 KB Output is correct
36 Correct 7 ms 11252 KB Output is correct
37 Correct 8 ms 11212 KB Output is correct
38 Correct 8 ms 11212 KB Output is correct
39 Correct 8 ms 11212 KB Output is correct
40 Correct 9 ms 11300 KB Output is correct
41 Correct 8 ms 11244 KB Output is correct
42 Correct 7 ms 11212 KB Output is correct
43 Correct 8 ms 11468 KB Output is correct
44 Correct 9 ms 11468 KB Output is correct
45 Correct 128 ms 33492 KB Output is correct
46 Correct 210 ms 44608 KB Output is correct
47 Correct 207 ms 44376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 8 ms 11212 KB Output is correct
3 Correct 7 ms 11260 KB Output is correct
4 Correct 7 ms 11212 KB Output is correct
5 Correct 8 ms 11272 KB Output is correct
6 Correct 7 ms 11212 KB Output is correct
7 Correct 8 ms 11192 KB Output is correct
8 Correct 7 ms 11212 KB Output is correct
9 Correct 123 ms 31744 KB Output is correct
10 Correct 20 ms 13496 KB Output is correct
11 Correct 52 ms 22412 KB Output is correct
12 Correct 24 ms 14448 KB Output is correct
13 Correct 29 ms 16708 KB Output is correct
14 Correct 8 ms 11340 KB Output is correct
15 Correct 8 ms 11468 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
17 Correct 7 ms 11212 KB Output is correct
18 Correct 7 ms 11212 KB Output is correct
19 Correct 7 ms 11212 KB Output is correct
20 Correct 8 ms 11212 KB Output is correct
21 Correct 9 ms 11212 KB Output is correct
22 Correct 9 ms 11212 KB Output is correct
23 Correct 286 ms 53224 KB Output is correct
24 Correct 8 ms 11212 KB Output is correct
25 Correct 10 ms 11472 KB Output is correct
26 Correct 9 ms 11532 KB Output is correct
27 Correct 10 ms 11596 KB Output is correct
28 Correct 99 ms 28240 KB Output is correct
29 Correct 169 ms 36056 KB Output is correct
30 Correct 216 ms 45520 KB Output is correct
31 Correct 285 ms 53380 KB Output is correct
32 Correct 8 ms 11212 KB Output is correct
33 Correct 8 ms 11212 KB Output is correct
34 Correct 7 ms 11212 KB Output is correct
35 Correct 7 ms 11212 KB Output is correct
36 Correct 7 ms 11252 KB Output is correct
37 Correct 8 ms 11212 KB Output is correct
38 Correct 8 ms 11212 KB Output is correct
39 Correct 8 ms 11212 KB Output is correct
40 Correct 9 ms 11300 KB Output is correct
41 Correct 8 ms 11244 KB Output is correct
42 Correct 7 ms 11212 KB Output is correct
43 Correct 8 ms 11468 KB Output is correct
44 Correct 9 ms 11468 KB Output is correct
45 Correct 128 ms 33492 KB Output is correct
46 Correct 210 ms 44608 KB Output is correct
47 Correct 207 ms 44376 KB Output is correct
48 Correct 8 ms 11224 KB Output is correct
49 Correct 7 ms 11212 KB Output is correct
50 Correct 7 ms 11212 KB Output is correct
51 Correct 9 ms 11280 KB Output is correct
52 Correct 9 ms 11212 KB Output is correct
53 Correct 8 ms 11212 KB Output is correct
54 Correct 8 ms 11212 KB Output is correct
55 Correct 288 ms 51008 KB Output is correct
56 Correct 8 ms 11212 KB Output is correct
57 Correct 10 ms 11596 KB Output is correct
58 Correct 14 ms 12620 KB Output is correct
59 Correct 14 ms 12224 KB Output is correct
60 Correct 135 ms 31964 KB Output is correct
61 Correct 184 ms 40404 KB Output is correct
62 Correct 225 ms 44564 KB Output is correct
63 Correct 280 ms 52224 KB Output is correct
64 Correct 7 ms 11212 KB Output is correct
65 Correct 7 ms 11212 KB Output is correct
66 Correct 8 ms 11232 KB Output is correct
67 Correct 256 ms 50472 KB Output is correct
68 Correct 265 ms 50612 KB Output is correct
69 Correct 273 ms 51592 KB Output is correct
70 Correct 10 ms 11596 KB Output is correct
71 Correct 12 ms 12084 KB Output is correct
72 Correct 139 ms 32984 KB Output is correct
73 Correct 220 ms 45096 KB Output is correct
74 Correct 288 ms 55040 KB Output is correct
75 Correct 304 ms 62080 KB Output is correct
76 Correct 274 ms 53212 KB Output is correct
77 Correct 11 ms 11852 KB Output is correct
78 Correct 12 ms 12108 KB Output is correct
79 Correct 139 ms 33956 KB Output is correct
80 Correct 227 ms 46060 KB Output is correct
81 Correct 307 ms 57556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 8 ms 11212 KB Output is correct
3 Correct 7 ms 11260 KB Output is correct
4 Correct 7 ms 11212 KB Output is correct
5 Correct 8 ms 11272 KB Output is correct
6 Correct 7 ms 11212 KB Output is correct
7 Correct 8 ms 11192 KB Output is correct
8 Correct 7 ms 11212 KB Output is correct
9 Correct 123 ms 31744 KB Output is correct
10 Correct 20 ms 13496 KB Output is correct
11 Correct 52 ms 22412 KB Output is correct
12 Correct 24 ms 14448 KB Output is correct
13 Correct 29 ms 16708 KB Output is correct
14 Correct 8 ms 11340 KB Output is correct
15 Correct 8 ms 11468 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
17 Correct 7 ms 11212 KB Output is correct
18 Correct 7 ms 11212 KB Output is correct
19 Correct 7 ms 11212 KB Output is correct
20 Correct 324 ms 63472 KB Output is correct
21 Correct 320 ms 61240 KB Output is correct
22 Correct 319 ms 60240 KB Output is correct
23 Runtime error 221 ms 83008 KB Execution killed with signal 11
24 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 8 ms 11212 KB Output is correct
3 Correct 7 ms 11260 KB Output is correct
4 Correct 7 ms 11212 KB Output is correct
5 Correct 8 ms 11272 KB Output is correct
6 Correct 7 ms 11212 KB Output is correct
7 Correct 8 ms 11192 KB Output is correct
8 Correct 7 ms 11212 KB Output is correct
9 Correct 123 ms 31744 KB Output is correct
10 Correct 20 ms 13496 KB Output is correct
11 Correct 52 ms 22412 KB Output is correct
12 Correct 24 ms 14448 KB Output is correct
13 Correct 29 ms 16708 KB Output is correct
14 Correct 8 ms 11340 KB Output is correct
15 Correct 8 ms 11468 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
17 Correct 320 ms 62956 KB Output is correct
18 Correct 302 ms 62212 KB Output is correct
19 Correct 327 ms 63240 KB Output is correct
20 Runtime error 265 ms 87072 KB Execution killed with signal 11
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9676 KB Output is correct
2 Correct 8 ms 11212 KB Output is correct
3 Correct 7 ms 11260 KB Output is correct
4 Correct 7 ms 11212 KB Output is correct
5 Correct 8 ms 11272 KB Output is correct
6 Correct 7 ms 11212 KB Output is correct
7 Correct 8 ms 11192 KB Output is correct
8 Correct 7 ms 11212 KB Output is correct
9 Correct 123 ms 31744 KB Output is correct
10 Correct 20 ms 13496 KB Output is correct
11 Correct 52 ms 22412 KB Output is correct
12 Correct 24 ms 14448 KB Output is correct
13 Correct 29 ms 16708 KB Output is correct
14 Correct 8 ms 11340 KB Output is correct
15 Correct 8 ms 11468 KB Output is correct
16 Correct 116 ms 29916 KB Output is correct
17 Correct 7 ms 11212 KB Output is correct
18 Correct 7 ms 11212 KB Output is correct
19 Correct 7 ms 11212 KB Output is correct
20 Correct 8 ms 11212 KB Output is correct
21 Correct 9 ms 11212 KB Output is correct
22 Correct 9 ms 11212 KB Output is correct
23 Correct 286 ms 53224 KB Output is correct
24 Correct 8 ms 11212 KB Output is correct
25 Correct 10 ms 11472 KB Output is correct
26 Correct 9 ms 11532 KB Output is correct
27 Correct 10 ms 11596 KB Output is correct
28 Correct 99 ms 28240 KB Output is correct
29 Correct 169 ms 36056 KB Output is correct
30 Correct 216 ms 45520 KB Output is correct
31 Correct 285 ms 53380 KB Output is correct
32 Correct 8 ms 11212 KB Output is correct
33 Correct 8 ms 11212 KB Output is correct
34 Correct 7 ms 11212 KB Output is correct
35 Correct 7 ms 11212 KB Output is correct
36 Correct 7 ms 11252 KB Output is correct
37 Correct 8 ms 11212 KB Output is correct
38 Correct 8 ms 11212 KB Output is correct
39 Correct 8 ms 11212 KB Output is correct
40 Correct 9 ms 11300 KB Output is correct
41 Correct 8 ms 11244 KB Output is correct
42 Correct 7 ms 11212 KB Output is correct
43 Correct 8 ms 11468 KB Output is correct
44 Correct 9 ms 11468 KB Output is correct
45 Correct 128 ms 33492 KB Output is correct
46 Correct 210 ms 44608 KB Output is correct
47 Correct 207 ms 44376 KB Output is correct
48 Correct 8 ms 11224 KB Output is correct
49 Correct 7 ms 11212 KB Output is correct
50 Correct 7 ms 11212 KB Output is correct
51 Correct 9 ms 11280 KB Output is correct
52 Correct 9 ms 11212 KB Output is correct
53 Correct 8 ms 11212 KB Output is correct
54 Correct 8 ms 11212 KB Output is correct
55 Correct 288 ms 51008 KB Output is correct
56 Correct 8 ms 11212 KB Output is correct
57 Correct 10 ms 11596 KB Output is correct
58 Correct 14 ms 12620 KB Output is correct
59 Correct 14 ms 12224 KB Output is correct
60 Correct 135 ms 31964 KB Output is correct
61 Correct 184 ms 40404 KB Output is correct
62 Correct 225 ms 44564 KB Output is correct
63 Correct 280 ms 52224 KB Output is correct
64 Correct 7 ms 11212 KB Output is correct
65 Correct 7 ms 11212 KB Output is correct
66 Correct 8 ms 11232 KB Output is correct
67 Correct 256 ms 50472 KB Output is correct
68 Correct 265 ms 50612 KB Output is correct
69 Correct 273 ms 51592 KB Output is correct
70 Correct 10 ms 11596 KB Output is correct
71 Correct 12 ms 12084 KB Output is correct
72 Correct 139 ms 32984 KB Output is correct
73 Correct 220 ms 45096 KB Output is correct
74 Correct 288 ms 55040 KB Output is correct
75 Correct 304 ms 62080 KB Output is correct
76 Correct 274 ms 53212 KB Output is correct
77 Correct 11 ms 11852 KB Output is correct
78 Correct 12 ms 12108 KB Output is correct
79 Correct 139 ms 33956 KB Output is correct
80 Correct 227 ms 46060 KB Output is correct
81 Correct 307 ms 57556 KB Output is correct
82 Correct 7 ms 11212 KB Output is correct
83 Correct 7 ms 11212 KB Output is correct
84 Correct 7 ms 11212 KB Output is correct
85 Correct 324 ms 63472 KB Output is correct
86 Correct 320 ms 61240 KB Output is correct
87 Correct 319 ms 60240 KB Output is correct
88 Runtime error 221 ms 83008 KB Execution killed with signal 11
89 Halted 0 ms 0 KB -