Submission #443496

# Submission time Handle Problem Language Result Execution time Memory
443496 2021-07-10T15:35:41 Z urd05 Fountain Parks (IOI21_parks) C++17
15 / 100
3500 ms 131916 KB
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> P;
map<P,int> pt; //점->인덱스
map<P,int> ed; //인덱스쌍->간선인덱스
set<P> vis;
vector<P> edge;
vector<int> adj[400000];
map<P,P> mp;
bool chk[400000];
P ret[400000];
bool ckk[400000];

int p[200000];

int find(int a) {
    return p[a]<0?a:p[a]=find(p[a]);
}

void merge(int a,int b) {
    a=find(a);
    b=find(b);
    if (a==b) {
        return;
    }
    p[a]+=p[b];
    p[b]=a;
}

int construct_roads(vector<int> x, vector<int> y) {
    vector<int> uu;
    vector<int> vv;
    vector<int> aa;
    vector<int> bb;
    memset(p,-1,sizeof(p));
    int n=x.size();
    for(int i=0;i<n;i++) {
        pt[P(x[i],y[i])]=i;
    }
    int m=0;
    for(int i=0;i<n;i++) {
        if (pt.find(P(x[i]-2,y[i]))!=pt.end()) {
            int ind=pt[P(x[i]-2,y[i])];
            if (ed.find(P(ind,i))!=ed.end()) {
                ed[P(i,ind)]=ed[P(ind,i)];
            }
            else {
                ed[P(i,ind)]=m++;
                edge.push_back(P(i,ind));
            }
        }
        if (pt.find(P(x[i]+2,y[i]))!=pt.end()) {
            int ind=pt[P(x[i]+2,y[i])];
            if (ed.find(P(ind,i))!=ed.end()) {
                ed[P(i,ind)]=ed[P(ind,i)];
            }
            else {
                ed[P(i,ind)]=m++;
                edge.push_back(P(i,ind));
            }
        }
        if (pt.find(P(x[i],y[i]-2))!=pt.end()) {
            int ind=pt[P(x[i],y[i]-2)];
            if (ed.find(P(ind,i))!=ed.end()) {
                ed[P(i,ind)]=ed[P(ind,i)];
            }
            else {
                ed[P(i,ind)]=m++;
                edge.push_back(P(i,ind));
            }
        }
        if (pt.find(P(x[i],y[i]+2))!=pt.end()) {
            int ind=pt[P(x[i],y[i]+2)];
            if (ed.find(P(ind,i))!=ed.end()) {
                ed[P(i,ind)]=ed[P(ind,i)];
            }
            else {
                ed[P(i,ind)]=m++;
                edge.push_back(P(i,ind));
            }
        }
    }
    for(int i=0;i<edge.size();i++) {
        merge(edge[i].first,edge[i].second);
    }
    if (-p[find(0)]!=n) {
        return 0;
    }
    for(int i=0;i<edge.size();i++) {
        if (x[edge[i].first]==2&&x[edge[i].second]==2) {
            uu.push_back(edge[i].first);
            vv.push_back(edge[i].second);
            aa.push_back(1);
            bb.push_back((y[edge[i].first]+y[edge[i].second])/2);
            chk[i]=true;
            ckk[i]=true;
        }
        else if (x[edge[i].first]==6&&x[edge[i].second]==6) {
            uu.push_back(edge[i].first);
            vv.push_back(edge[i].second);
            aa.push_back(7);
            bb.push_back((y[edge[i].first]+y[edge[i].second])/2);
            chk[i]=true;
            ckk[i]=true;
        }
        else if (y[edge[i].first]==y[edge[i].second]) {
            int pind1=-1;
            int pind2=-1;
            if (pt.find(P(x[edge[i].first],y[edge[i].first]-2))!=pt.end()) {
                pind1=pt[P(x[edge[i].first],y[edge[i].first]-2)];
            }
            if (pt.find(P(x[edge[i].second],y[edge[i].second]-2))!=pt.end()) {
                pind2=pt[P(x[edge[i].second],y[edge[i].second]-2)];
            }
            if (pind1!=-1&&pind2!=-1) {
                chk[i]=true;
                ckk[i]=true;
            }
        }
    }
    for(int i=0;i<edge.size();i++) {
        if (x[edge[i].first]==x[edge[i].second]) {
            int nx=x[edge[i].first]-1;
            int ny=(y[edge[i].first]+y[edge[i].second])/2;
            if (vis.find(P(nx,ny))==vis.end()) {
                bool flag=false;
                if (pt.find(P(nx-1,ny+1))!=pt.end()) {
                    int pind=pt[P(nx-1,ny+1)];
                    int eind=ed[P(pind,pt[P(nx+1,ny+1)])];
                    if (!ckk[eind]) {
                    adj[eind].push_back(i);
                    adj[i].push_back(eind);
                    mp[P(i,eind)]=P(nx,ny);
                    mp[P(eind,i)]=P(nx,ny);
                    flag=true;
                    }
                }
                if (pt.find(P(nx-1,ny-1))!=pt.end()) {
                    int pind=pt[P(nx-1,ny-1)];
                    int eind=ed[P(pind,pt[P(nx+1,ny-1)])];
                    if (!ckk[eind]) {
                    adj[eind].push_back(i);
                    adj[i].push_back(eind);
                    mp[P(i,eind)]=P(nx,ny);
                    mp[P(eind,i)]=P(nx,ny);
                    flag=true;
                    }
                }
                if (!flag) {
                    adj[i].push_back(i);
                    mp[P(i,i)]=P(nx,ny);
                }
                vis.insert(P(nx,ny));
            }
            nx=x[edge[i].first]+1;
            if (vis.find(P(nx,ny))==vis.end()) {
                bool flag=false;
                if (pt.find(P(nx+1,ny+1))!=pt.end()) {
                    int pind=pt[P(nx+1,ny+1)];
                    int eind=ed[P(pind,pt[P(nx-1,ny+1)])];
                    if (!ckk[eind]) {
                    adj[eind].push_back(i);
                    adj[i].push_back(eind);
                    mp[P(i,eind)]=P(nx,ny);
                    mp[P(eind,i)]=P(nx,ny);
                    flag=true;
                    }
                }
                if (pt.find(P(nx+1,ny-1))!=pt.end()) {
                    int pind=pt[P(nx+1,ny-1)];
                    int eind=ed[P(pind,pt[P(nx-1,ny-1)])];
                    if (!ckk[eind]) {
                    adj[eind].push_back(i);
                    adj[i].push_back(eind);
                    mp[P(i,eind)]=P(nx,ny);
                    mp[P(eind,i)]=P(nx,ny);
                    flag=true;
                    }
                }
                if (!flag) {
                    adj[i].push_back(i);
                    mp[P(i,i)]=P(nx,ny);
                }
                vis.insert(P(nx,ny));
            }
        }
        else {
            int nx=(x[edge[i].first]+x[edge[i].second])/2;
            int ny=y[edge[i].first]-1;
            if (vis.find(P(nx,ny))==vis.end()) {
                bool flag=false;
                if (pt.find(P(nx-1,ny-1))!=pt.end()) {
                    int pind=pt[P(nx-1,ny-1)];
                    int eind=ed[P(pind,pt[P(nx-1,ny+1)])];
                    if (!ckk[eind]) {
                    adj[eind].push_back(i);
                    adj[i].push_back(eind);
                    mp[P(i,eind)]=P(nx,ny);
                    mp[P(eind,i)]=P(nx,ny);
                    flag=true;
                    }
                }
                if (pt.find(P(nx+1,ny-1))!=pt.end()) {
                    int pind=pt[P(nx+1,ny-1)];
                    int eind=ed[P(pind,pt[P(nx+1,ny+1)])];
                    if (!ckk[eind]) {
                    adj[eind].push_back(i);
                    adj[i].push_back(eind);
                    mp[P(i,eind)]=P(nx,ny);
                    mp[P(eind,i)]=P(nx,ny);
                    flag=true;
                    }
                }
                if (!flag) {
                    adj[i].push_back(i);
                    mp[P(i,i)]=P(nx,ny);
                }
                vis.insert(P(nx,ny));
            }
            ny=y[edge[i].first]+1;
            if (vis.find(P(nx,ny))==vis.end()) {
                bool flag=false;
                if (pt.find(P(nx-1,ny+1))!=pt.end()) {
                    int pind=pt[P(nx-1,ny+1)];
                    int eind=ed[P(pind,pt[P(nx-1,ny-1)])];
                    if (!ckk[eind]) {
                    adj[eind].push_back(i);
                    adj[i].push_back(eind);
                    mp[P(i,eind)]=P(nx,ny);
                    mp[P(eind,i)]=P(nx,ny);
                    flag=true;
                    }
                }
                if (pt.find(P(nx+1,ny+1))!=pt.end()) {
                    int pind=pt[P(nx+1,ny+1)];
                    int eind=ed[P(pind,pt[P(nx+1,ny-1)])];
                    if (!ckk[eind]) {
                    adj[eind].push_back(i);
                    adj[i].push_back(eind);
                    mp[P(i,eind)]=P(nx,ny);
                    mp[P(eind,i)]=P(nx,ny);
                    flag=true;
                    }
                }
                if (!flag) {
                    adj[i].push_back(i);
                    mp[P(i,i)]=P(nx,ny);
                }
                vis.insert(P(nx,ny));
            }
        }
    }
    /*for(int i=0;i<m;i++) {
        printf("%d\n",adj[i].size());
        for(int j=0;j<adj[i].size();j++) {
            printf("%d ",adj[i][j]);
        }
        printf("\n");
    }*/
    for(int i=0;i<m;i++) {
        if (chk[i]) {
            continue;
        }
        if (adj[i][0]==i||adj[i][1]==i) {

        }
        else {
            continue;
        }
        ret[i]=mp[P(i,i)];
        int now;
        if (adj[i][0]==i) {
            now=adj[i][1];
        }
        else {
            now=adj[i][0];
        }
        chk[i]=true;
        int prev=i;
        while (1) {
            chk[now]=true;
            if (adj[now][0]==now||adj[now][1]==now) {
                ret[now]=mp[P(now,now)];
                break;
            }
            if (adj[now][0]==prev) {
                ret[now]=mp[P(now,adj[now][1])];
                prev=now;
                now=adj[now][1];
            }
            else {
                ret[now]=mp[P(now,adj[now][0])];
                prev=now;
                now=adj[now][0];
            }
        }
    }
    for(int i=0;i<m;i++) {
        if (chk[i]) {
            continue;
        }
        ret[i]=mp[P(i,adj[i][0])];
        int prev=i;
        int now=adj[i][0];
        chk[i]=true;
        while (now!=i) {
            chk[now]=true;
            if (adj[now][0]==prev) {
                ret[now]=mp[P(now,adj[now][1])];
                prev=now;
                now=adj[now][1];
            }
            else {
                ret[now]=mp[P(now,adj[now][0])];
                prev=now;
                now=adj[now][0];
            }
        }
    }
    for(int i=0;i<m;i++) {
            if (ckk[i]) {
                continue;
            }
        uu.push_back(edge[i].first);
        vv.push_back(edge[i].second);
        aa.push_back(ret[i].first);
        bb.push_back(ret[i].second);
    }
    build(uu,vv,aa,bb);
    return 1;
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:85: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]
   85 |     for(int i=0;i<edge.size();i++) {
      |                 ~^~~~~~~~~~~~
parks.cpp:91: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]
   91 |     for(int i=0;i<edge.size();i++) {
      |                 ~^~~~~~~~~~~~
parks.cpp:123: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]
  123 |     for(int i=0;i<edge.size();i++) {
      |                 ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10444 KB Output is correct
2 Correct 7 ms 10388 KB Output is correct
3 Correct 7 ms 10444 KB Output is correct
4 Correct 7 ms 10444 KB Output is correct
5 Correct 7 ms 10444 KB Output is correct
6 Correct 7 ms 10444 KB Output is correct
7 Correct 7 ms 10444 KB Output is correct
8 Correct 7 ms 10444 KB Output is correct
9 Correct 625 ms 56656 KB Output is correct
10 Correct 40 ms 15172 KB Output is correct
11 Correct 220 ms 35284 KB Output is correct
12 Correct 58 ms 17428 KB Output is correct
13 Correct 93 ms 19756 KB Output is correct
14 Correct 8 ms 10656 KB Output is correct
15 Correct 10 ms 10828 KB Output is correct
16 Correct 626 ms 56644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10444 KB Output is correct
2 Correct 7 ms 10388 KB Output is correct
3 Correct 7 ms 10444 KB Output is correct
4 Correct 7 ms 10444 KB Output is correct
5 Correct 7 ms 10444 KB Output is correct
6 Correct 7 ms 10444 KB Output is correct
7 Correct 7 ms 10444 KB Output is correct
8 Correct 7 ms 10444 KB Output is correct
9 Correct 625 ms 56656 KB Output is correct
10 Correct 40 ms 15172 KB Output is correct
11 Correct 220 ms 35284 KB Output is correct
12 Correct 58 ms 17428 KB Output is correct
13 Correct 93 ms 19756 KB Output is correct
14 Correct 8 ms 10656 KB Output is correct
15 Correct 10 ms 10828 KB Output is correct
16 Correct 626 ms 56644 KB Output is correct
17 Correct 7 ms 10444 KB Output is correct
18 Correct 7 ms 10444 KB Output is correct
19 Correct 7 ms 10444 KB Output is correct
20 Correct 7 ms 10444 KB Output is correct
21 Correct 7 ms 10444 KB Output is correct
22 Correct 7 ms 10444 KB Output is correct
23 Correct 2898 ms 131916 KB Output is correct
24 Correct 7 ms 10444 KB Output is correct
25 Correct 13 ms 11188 KB Output is correct
26 Correct 13 ms 11288 KB Output is correct
27 Correct 16 ms 11560 KB Output is correct
28 Correct 889 ms 59176 KB Output is correct
29 Correct 1513 ms 83192 KB Output is correct
30 Correct 2123 ms 108056 KB Output is correct
31 Correct 2913 ms 131916 KB Output is correct
32 Correct 7 ms 10444 KB Output is correct
33 Correct 8 ms 10408 KB Output is correct
34 Correct 7 ms 10444 KB Output is correct
35 Correct 7 ms 10444 KB Output is correct
36 Correct 7 ms 10444 KB Output is correct
37 Correct 7 ms 10488 KB Output is correct
38 Correct 7 ms 10444 KB Output is correct
39 Correct 7 ms 10444 KB Output is correct
40 Correct 7 ms 10444 KB Output is correct
41 Correct 7 ms 10444 KB Output is correct
42 Correct 7 ms 10444 KB Output is correct
43 Correct 10 ms 10828 KB Output is correct
44 Correct 12 ms 11092 KB Output is correct
45 Correct 936 ms 59272 KB Output is correct
46 Correct 1510 ms 81956 KB Output is correct
47 Correct 1510 ms 81856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10444 KB Output is correct
2 Correct 7 ms 10388 KB Output is correct
3 Correct 7 ms 10444 KB Output is correct
4 Correct 7 ms 10444 KB Output is correct
5 Correct 7 ms 10444 KB Output is correct
6 Correct 7 ms 10444 KB Output is correct
7 Correct 7 ms 10444 KB Output is correct
8 Correct 7 ms 10444 KB Output is correct
9 Correct 625 ms 56656 KB Output is correct
10 Correct 40 ms 15172 KB Output is correct
11 Correct 220 ms 35284 KB Output is correct
12 Correct 58 ms 17428 KB Output is correct
13 Correct 93 ms 19756 KB Output is correct
14 Correct 8 ms 10656 KB Output is correct
15 Correct 10 ms 10828 KB Output is correct
16 Correct 626 ms 56644 KB Output is correct
17 Correct 7 ms 10444 KB Output is correct
18 Correct 7 ms 10444 KB Output is correct
19 Correct 7 ms 10444 KB Output is correct
20 Correct 7 ms 10444 KB Output is correct
21 Correct 7 ms 10444 KB Output is correct
22 Correct 7 ms 10444 KB Output is correct
23 Correct 2898 ms 131916 KB Output is correct
24 Correct 7 ms 10444 KB Output is correct
25 Correct 13 ms 11188 KB Output is correct
26 Correct 13 ms 11288 KB Output is correct
27 Correct 16 ms 11560 KB Output is correct
28 Correct 889 ms 59176 KB Output is correct
29 Correct 1513 ms 83192 KB Output is correct
30 Correct 2123 ms 108056 KB Output is correct
31 Correct 2913 ms 131916 KB Output is correct
32 Correct 7 ms 10444 KB Output is correct
33 Correct 8 ms 10408 KB Output is correct
34 Correct 7 ms 10444 KB Output is correct
35 Correct 7 ms 10444 KB Output is correct
36 Correct 7 ms 10444 KB Output is correct
37 Correct 7 ms 10488 KB Output is correct
38 Correct 7 ms 10444 KB Output is correct
39 Correct 7 ms 10444 KB Output is correct
40 Correct 7 ms 10444 KB Output is correct
41 Correct 7 ms 10444 KB Output is correct
42 Correct 7 ms 10444 KB Output is correct
43 Correct 10 ms 10828 KB Output is correct
44 Correct 12 ms 11092 KB Output is correct
45 Correct 936 ms 59272 KB Output is correct
46 Correct 1510 ms 81956 KB Output is correct
47 Correct 1510 ms 81856 KB Output is correct
48 Correct 7 ms 10444 KB Output is correct
49 Correct 6 ms 10444 KB Output is correct
50 Correct 7 ms 10408 KB Output is correct
51 Execution timed out 3560 ms 10444 KB Time limit exceeded
52 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10444 KB Output is correct
2 Correct 7 ms 10388 KB Output is correct
3 Correct 7 ms 10444 KB Output is correct
4 Correct 7 ms 10444 KB Output is correct
5 Correct 7 ms 10444 KB Output is correct
6 Correct 7 ms 10444 KB Output is correct
7 Correct 7 ms 10444 KB Output is correct
8 Correct 7 ms 10444 KB Output is correct
9 Correct 625 ms 56656 KB Output is correct
10 Correct 40 ms 15172 KB Output is correct
11 Correct 220 ms 35284 KB Output is correct
12 Correct 58 ms 17428 KB Output is correct
13 Correct 93 ms 19756 KB Output is correct
14 Correct 8 ms 10656 KB Output is correct
15 Correct 10 ms 10828 KB Output is correct
16 Correct 626 ms 56644 KB Output is correct
17 Correct 7 ms 10444 KB Output is correct
18 Correct 7 ms 10444 KB Output is correct
19 Correct 7 ms 10444 KB Output is correct
20 Incorrect 2542 ms 108908 KB Tree @(7, 199997) appears more than once: for edges on positions 0 and 1440
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10444 KB Output is correct
2 Correct 7 ms 10388 KB Output is correct
3 Correct 7 ms 10444 KB Output is correct
4 Correct 7 ms 10444 KB Output is correct
5 Correct 7 ms 10444 KB Output is correct
6 Correct 7 ms 10444 KB Output is correct
7 Correct 7 ms 10444 KB Output is correct
8 Correct 7 ms 10444 KB Output is correct
9 Correct 625 ms 56656 KB Output is correct
10 Correct 40 ms 15172 KB Output is correct
11 Correct 220 ms 35284 KB Output is correct
12 Correct 58 ms 17428 KB Output is correct
13 Correct 93 ms 19756 KB Output is correct
14 Correct 8 ms 10656 KB Output is correct
15 Correct 10 ms 10828 KB Output is correct
16 Correct 626 ms 56644 KB Output is correct
17 Correct 1822 ms 106560 KB Output is correct
18 Correct 1822 ms 107036 KB Output is correct
19 Incorrect 2613 ms 108880 KB Tree @(7, 50005) appears more than once: for edges on positions 1 and 29824
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 10444 KB Output is correct
2 Correct 7 ms 10388 KB Output is correct
3 Correct 7 ms 10444 KB Output is correct
4 Correct 7 ms 10444 KB Output is correct
5 Correct 7 ms 10444 KB Output is correct
6 Correct 7 ms 10444 KB Output is correct
7 Correct 7 ms 10444 KB Output is correct
8 Correct 7 ms 10444 KB Output is correct
9 Correct 625 ms 56656 KB Output is correct
10 Correct 40 ms 15172 KB Output is correct
11 Correct 220 ms 35284 KB Output is correct
12 Correct 58 ms 17428 KB Output is correct
13 Correct 93 ms 19756 KB Output is correct
14 Correct 8 ms 10656 KB Output is correct
15 Correct 10 ms 10828 KB Output is correct
16 Correct 626 ms 56644 KB Output is correct
17 Correct 7 ms 10444 KB Output is correct
18 Correct 7 ms 10444 KB Output is correct
19 Correct 7 ms 10444 KB Output is correct
20 Correct 7 ms 10444 KB Output is correct
21 Correct 7 ms 10444 KB Output is correct
22 Correct 7 ms 10444 KB Output is correct
23 Correct 2898 ms 131916 KB Output is correct
24 Correct 7 ms 10444 KB Output is correct
25 Correct 13 ms 11188 KB Output is correct
26 Correct 13 ms 11288 KB Output is correct
27 Correct 16 ms 11560 KB Output is correct
28 Correct 889 ms 59176 KB Output is correct
29 Correct 1513 ms 83192 KB Output is correct
30 Correct 2123 ms 108056 KB Output is correct
31 Correct 2913 ms 131916 KB Output is correct
32 Correct 7 ms 10444 KB Output is correct
33 Correct 8 ms 10408 KB Output is correct
34 Correct 7 ms 10444 KB Output is correct
35 Correct 7 ms 10444 KB Output is correct
36 Correct 7 ms 10444 KB Output is correct
37 Correct 7 ms 10488 KB Output is correct
38 Correct 7 ms 10444 KB Output is correct
39 Correct 7 ms 10444 KB Output is correct
40 Correct 7 ms 10444 KB Output is correct
41 Correct 7 ms 10444 KB Output is correct
42 Correct 7 ms 10444 KB Output is correct
43 Correct 10 ms 10828 KB Output is correct
44 Correct 12 ms 11092 KB Output is correct
45 Correct 936 ms 59272 KB Output is correct
46 Correct 1510 ms 81956 KB Output is correct
47 Correct 1510 ms 81856 KB Output is correct
48 Correct 7 ms 10444 KB Output is correct
49 Correct 6 ms 10444 KB Output is correct
50 Correct 7 ms 10408 KB Output is correct
51 Execution timed out 3560 ms 10444 KB Time limit exceeded
52 Halted 0 ms 0 KB -