Submission #645934

# Submission time Handle Problem Language Result Execution time Memory
645934 2022-09-28T10:11:35 Z jamezzz Fountain Parks (IOI21_parks) C++17
25 / 100
750 ms 116308 KB
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define maxn 200005
#define pb push_back

typedef pair<int,int> ii;

int dx[2]={2,0},dy[2]={0,2};
bool vis[maxn],bad[maxn],weird[maxn];
vector<ii> squares;
map<ii,int> fid,sid,eid,use;
vector<int> AL[maxn],par[maxn];
int p[maxn],rnk[maxn];

int fp(int i){return (p[i]==i)?i:p[i]=fp(p[i]);}
void join(int x,int y){
	x=fp(x),y=fp(y);
	if(x==y)return;
	if(rnk[x]<rnk[y])p[x]=y;
	else p[y]=x;
	if(rnk[x]==rnk[y])++rnk[x];
}

inline bool fin(int x,int y){
	return fid.find(ii(x,y))!=fid.end();
}

inline bool sin(int x,int y){
	return sid.find(ii(x,y))!=sid.end();
}

inline bool ein(int x,int y){
	return eid.find(ii(x,y))!=eid.end();
}

void dfs(int u,int p){
	//printf("dfs: %d %d\n",u,p);
	if(p!=-1){
		auto[x,y]=squares[u];
		ii f1,f2;
		if(p==par[u][0]){
			if((x+y)%4==0){//vertical
				f1={x-1,y-1},f2={x-1,y+1};
			}
			else{//horizontal
				f1={x-1,y-1},f2={x+1,y-1};
			}
		}
		else{
			if((x+y)%4==0){//vertical
				f1={x+1,y-1},f2={x+1,y+1};
			}
			else{//horizontal
				f1={x-1,y+1},f2={x+1,y+1};
			}
		}
		int i1=fid[f1],i2=fid[f2];
		if(ein(i1,i2))bad[eid[{i1,i2}]]=true;
		else bad[eid[{i2,i1}]]=true;
	}
	for(int v:AL[u]){
		if(vis[v])continue;
		vis[v]=true;
		dfs(v,u);
	}
}

int construct_roads(vector<int> x,vector<int> y){
    int n=x.size();
    for(int i=0;i<n;++i){
		fid[{x[i],y[i]}]=i;
	}
	int scnt=0,ecnt=0;
	for(int i=0;i<n;++i){
		if(fin(x[i]+2,y[i])&&fin(x[i],y[i]+2)&&fin(x[i]+2,y[i]+2)){
			sid[{x[i]+1,y[i]+1}]=scnt++;
			squares.push_back({x[i]+1,y[i]+1});
		}
		for(int j=0;j<2;++j){
			int nx=x[i]+dx[j],ny=y[i]+dy[j];
			if(fin(nx,ny))eid[{i,fid[{nx,ny}]}]=ecnt++;
		}
	}
	for(auto it=sid.begin();it!=sid.end();++it){
		auto[x,y]=it->fi;
		if((x+y)%4==0){//vertical
			for(int dx:{-2,2}){
				if(sin(x+dx,y)){
					AL[sid[{x+dx,y}]].push_back(it->se);
					par[it->se].push_back(sid[{x+dx,y}]);
				}
				else{
					AL[scnt].push_back(it->se);
					par[it->se].push_back(scnt);
				}
			}
		}
		else{//horizontal
			for(int dy:{-2,2}){
				if(sin(x,y+dy)){
					AL[sid[{x,y+dy}]].push_back(it->se);
					par[it->se].push_back(sid[{x,y+dy}]);
				}
				else{
					AL[scnt].push_back(it->se);
					par[it->se].push_back(scnt);
				}
			}
		}
	}
	vis[scnt]=true;
	dfs(scnt,-1);
	for(auto it=sid.begin();it!=sid.end();++it){
		auto[x,y]=it->fi;
		if((x+y)%4==0){//vertical
			int cnt=0;
			for(int dx:{-1,1}){
				ii f1={x+dx,y-1},f2={x+dx,y+1};
				int i1=fid[f1],i2=fid[f2];
				if(ein(i1,i2)&&!bad[eid[{i1,i2}]])++cnt;
				else if(ein(i2,i1)&&!bad[eid[{i2,i1}]])++cnt;
			}
			if(cnt==2)assert(false);
		}
		else{//horizontal
			int cnt=0;
			for(int dy:{-1,1}){
				ii f1={x-1,y+dy},f2={x+1,y+dy};
				int i1=fid[f1],i2=fid[f2];
				if(ein(i1,i2)&&!bad[eid[{i1,i2}]])++cnt;
				else if(ein(i2,i1)&&!bad[eid[{i2,i1}]])++cnt;
			}
			if(cnt==2)assert(false);
		}
	}
	for(int i=0;i<n;++i)p[i]=i;
	vector<int> u,v,a,b;
	for(auto it=eid.begin();it!=eid.end();++it){
		if(bad[it->se])continue;
		auto[i,j]=it->fi;
		if(fp(i)==fp(j))continue;
		join(i,j);
		int x1=x[i],x2=x[j],y1=y[i],y2=y[j];
		int bx,by;
		if(x1==x2){
			if((x1-1+(y1+y2)/2)%4==0)bx=x1-1,by=(y1+y2)/2;
			else bx=x1+1,by=(y1+y2)/2;
		}
		else{
			if(((x1+x2)/2+y1-1)%4!=0)bx=(x1+x2)/2,by=y1-1;
			else bx=(x1+x2)/2,by=y1+1;
		}
		u.pb(i),v.pb(j),a.pb(bx),b.pb(by);
		int idk=use[{bx,by}];
		assert(idk==0);
		use[{bx,by}]=true;
	}
	for(int i=0;i<n;++i){
		if(fp(i)!=fp(0))return 0;
	}
	build(u,v,a,b);
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9704 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9700 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 5 ms 9596 KB Output is correct
9 Correct 218 ms 36272 KB Output is correct
10 Correct 20 ms 12512 KB Output is correct
11 Correct 96 ms 23964 KB Output is correct
12 Correct 28 ms 13788 KB Output is correct
13 Correct 67 ms 19828 KB Output is correct
14 Correct 7 ms 9940 KB Output is correct
15 Correct 8 ms 10092 KB Output is correct
16 Correct 210 ms 36244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9704 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9700 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 5 ms 9596 KB Output is correct
9 Correct 218 ms 36272 KB Output is correct
10 Correct 20 ms 12512 KB Output is correct
11 Correct 96 ms 23964 KB Output is correct
12 Correct 28 ms 13788 KB Output is correct
13 Correct 67 ms 19828 KB Output is correct
14 Correct 7 ms 9940 KB Output is correct
15 Correct 8 ms 10092 KB Output is correct
16 Correct 210 ms 36244 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 6 ms 9700 KB Output is correct
19 Correct 7 ms 9708 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 6 ms 9700 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Runtime error 718 ms 116308 KB Execution killed with signal 6
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9704 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9700 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 5 ms 9596 KB Output is correct
9 Correct 218 ms 36272 KB Output is correct
10 Correct 20 ms 12512 KB Output is correct
11 Correct 96 ms 23964 KB Output is correct
12 Correct 28 ms 13788 KB Output is correct
13 Correct 67 ms 19828 KB Output is correct
14 Correct 7 ms 9940 KB Output is correct
15 Correct 8 ms 10092 KB Output is correct
16 Correct 210 ms 36244 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 6 ms 9700 KB Output is correct
19 Correct 7 ms 9708 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 6 ms 9700 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Runtime error 718 ms 116308 KB Execution killed with signal 6
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9704 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9700 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 5 ms 9596 KB Output is correct
9 Correct 218 ms 36272 KB Output is correct
10 Correct 20 ms 12512 KB Output is correct
11 Correct 96 ms 23964 KB Output is correct
12 Correct 28 ms 13788 KB Output is correct
13 Correct 67 ms 19828 KB Output is correct
14 Correct 7 ms 9940 KB Output is correct
15 Correct 8 ms 10092 KB Output is correct
16 Correct 210 ms 36244 KB Output is correct
17 Correct 5 ms 9616 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9692 KB Output is correct
20 Correct 697 ms 63944 KB Output is correct
21 Correct 651 ms 63776 KB Output is correct
22 Correct 595 ms 63708 KB Output is correct
23 Correct 499 ms 55312 KB Output is correct
24 Correct 313 ms 27752 KB Output is correct
25 Correct 655 ms 56688 KB Output is correct
26 Correct 537 ms 56684 KB Output is correct
27 Correct 618 ms 62928 KB Output is correct
28 Correct 598 ms 62972 KB Output is correct
29 Correct 657 ms 62884 KB Output is correct
30 Correct 750 ms 62932 KB Output is correct
31 Correct 5 ms 9684 KB Output is correct
32 Correct 35 ms 13756 KB Output is correct
33 Correct 103 ms 19164 KB Output is correct
34 Correct 593 ms 63840 KB Output is correct
35 Correct 24 ms 12104 KB Output is correct
36 Correct 103 ms 21668 KB Output is correct
37 Correct 230 ms 33636 KB Output is correct
38 Correct 203 ms 31416 KB Output is correct
39 Correct 302 ms 39540 KB Output is correct
40 Correct 418 ms 47872 KB Output is correct
41 Correct 535 ms 55788 KB Output is correct
42 Correct 692 ms 63912 KB Output is correct
43 Correct 5 ms 9684 KB Output is correct
44 Correct 6 ms 9700 KB Output is correct
45 Correct 5 ms 9708 KB Output is correct
46 Correct 6 ms 9684 KB Output is correct
47 Correct 6 ms 9700 KB Output is correct
48 Correct 5 ms 9684 KB Output is correct
49 Correct 7 ms 9712 KB Output is correct
50 Correct 5 ms 9708 KB Output is correct
51 Correct 6 ms 9684 KB Output is correct
52 Correct 6 ms 9700 KB Output is correct
53 Correct 5 ms 9684 KB Output is correct
54 Correct 7 ms 10068 KB Output is correct
55 Correct 9 ms 10324 KB Output is correct
56 Correct 233 ms 36380 KB Output is correct
57 Correct 392 ms 48324 KB Output is correct
58 Correct 392 ms 48296 KB Output is correct
59 Correct 5 ms 9704 KB Output is correct
60 Correct 5 ms 9684 KB Output is correct
61 Correct 6 ms 9684 KB Output is correct
62 Correct 515 ms 62988 KB Output is correct
63 Correct 531 ms 63032 KB Output is correct
64 Correct 538 ms 62696 KB Output is correct
65 Correct 10 ms 10580 KB Output is correct
66 Correct 17 ms 11476 KB Output is correct
67 Correct 242 ms 36216 KB Output is correct
68 Correct 445 ms 49700 KB Output is correct
69 Correct 613 ms 63172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9704 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9700 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 5 ms 9596 KB Output is correct
9 Correct 218 ms 36272 KB Output is correct
10 Correct 20 ms 12512 KB Output is correct
11 Correct 96 ms 23964 KB Output is correct
12 Correct 28 ms 13788 KB Output is correct
13 Correct 67 ms 19828 KB Output is correct
14 Correct 7 ms 9940 KB Output is correct
15 Correct 8 ms 10092 KB Output is correct
16 Correct 210 ms 36244 KB Output is correct
17 Correct 578 ms 63336 KB Output is correct
18 Correct 580 ms 63436 KB Output is correct
19 Correct 596 ms 63748 KB Output is correct
20 Correct 668 ms 65840 KB Output is correct
21 Correct 537 ms 57624 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 73 ms 18620 KB Output is correct
24 Correct 41 ms 14700 KB Output is correct
25 Correct 157 ms 27224 KB Output is correct
26 Correct 316 ms 39788 KB Output is correct
27 Correct 278 ms 38028 KB Output is correct
28 Correct 361 ms 45232 KB Output is correct
29 Correct 467 ms 52412 KB Output is correct
30 Correct 551 ms 59156 KB Output is correct
31 Correct 705 ms 66308 KB Output is correct
32 Correct 611 ms 65604 KB Output is correct
33 Correct 507 ms 62912 KB Output is correct
34 Correct 11 ms 10864 KB Output is correct
35 Correct 19 ms 11884 KB Output is correct
36 Correct 243 ms 36888 KB Output is correct
37 Correct 429 ms 50632 KB Output is correct
38 Incorrect 593 ms 58124 KB Solution announced impossible, but it is possible.
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9704 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 6 ms 9700 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 5 ms 9596 KB Output is correct
9 Correct 218 ms 36272 KB Output is correct
10 Correct 20 ms 12512 KB Output is correct
11 Correct 96 ms 23964 KB Output is correct
12 Correct 28 ms 13788 KB Output is correct
13 Correct 67 ms 19828 KB Output is correct
14 Correct 7 ms 9940 KB Output is correct
15 Correct 8 ms 10092 KB Output is correct
16 Correct 210 ms 36244 KB Output is correct
17 Correct 5 ms 9684 KB Output is correct
18 Correct 6 ms 9700 KB Output is correct
19 Correct 7 ms 9708 KB Output is correct
20 Correct 6 ms 9684 KB Output is correct
21 Correct 6 ms 9700 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Runtime error 718 ms 116308 KB Execution killed with signal 6
24 Halted 0 ms 0 KB -