Submission #1040821

# Submission time Handle Problem Language Result Execution time Memory
1040821 2024-08-01T10:09:30 Z Dan4Life Fountain Parks (IOI21_parks) C++17
55 / 100
311 ms 122072 KB
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
using vi = vector<int>;
using ll = long long;
using ar2 = array<int,2>;
using ar3 = array<int,3>;

const int mxN = (int)2e5+10;
int n;
struct Edge{
	int fa, fb, x, y;
	Edge(int a=0, int b=0, int _x=-1, int _y=-1){
		fa=a, fb=b, x=_x, y=_y;
	}
};
vector<Edge> edges;
vector<ar3> fountains, origFountains;
vector<int> adj[mxN];
set<ar2> usedB;

template<int SZ> struct DSU{
	int p[SZ];
	vector<int> comp[SZ];
	void init(int n){
		for(int i = 0; i < n; i++){
			p[i]=i; comp[i].clear();
			comp[i].pb(i);
		}
	}
	
	int findSet(int i){return p[i]==i?i:p[i]=findSet(p[i]);}
	bool isSameSet(int i, int j) {return findSet(i)==findSet(j);}
	void unionSet(int i, int j){
		int x = findSet(i), y = findSet(j);
		if(x==y) return;
		adj[i].pb(j); adj[j].pb(i);
		if(sz(comp[x]) < sz(comp[y])) swap(x,y);
		p[y]=x; for(auto u : comp[y]) comp[x].pb(u);
	}
	int findSz(int i) { return sz(comp[findSet(i)]); }
};
DSU<mxN> dsu;

set<int> horEdge[mxN], verEdge[mxN];

void dfs(int s, int p, bool turn){
	if(p!=-1){
		if(origFountains[s][0]==origFountains[p][0]){
			int x = origFountains[s][0], y = min(origFountains[s][1],origFountains[p][1])+1;
			int x1 = x-1, x2 = x+1;
			if(!turn) verEdge[x].insert(y-1);
			else{
				bool ok = 0;
				if(horEdge[y-1].count(x-2) and usedB.count({x1,y-2})) ok=1;
				if(horEdge[y+1].count(x-2) and usedB.count({x1,y+2})) ok=1;
				if(ok) swap(x1,x2);
				
				if(usedB.count({x1,y})) swap(x1,x2);
				if(usedB.count({x1,y})){ dsu.init(n); return; }
				edges.pb(Edge(s,p,x1,y)); usedB.insert({x1,y});
				verEdge[x].erase(y-1);
			}
		}
		else{
			int x = min(origFountains[s][0],origFountains[p][0])+1, y = origFountains[s][1];
			int y1 = y-1, y2 = y+1;
			if(!turn) horEdge[y].insert(x-1);
			else{
				bool ok = 0;
				if(verEdge[x-1].count(y-2) and usedB.count({x-2,y1})) ok=1;
				if(verEdge[x+1].count(y-2) and usedB.count({x+2,y1})) ok=1;
				if(ok) swap(y1,y2);
				
				if(usedB.count({x,y1})) swap(y1,y2);
				if(usedB.count({x,y1})){ dsu.init(n); return; }
				edges.pb(Edge(s,p,x,y1)); usedB.insert({x,y1});
				horEdge[y].erase(x-1);
			}
		}
	}
	for(auto u : adj[s]){
		if(u==p) continue;
		dfs(u,s,turn);
	}
}

int construct_roads(vi x, vi y) {
	n = sz(x); dsu.init(n);
	if (n==1){ build({}, {}, {}, {}); return 1; }
	int mnX = *min_element(all(x));
	int mxX = *max_element(all(x));
	for(int i = 0; i < n; i++){
		fountains.pb({x[i],y[i],i});
		origFountains.pb({x[i],y[i],i});
	}
	
	if(mnX==mxX){
		sort(all(fountains),[&](ar3 a, ar3 b){ return a[1]<b[1]; });
		for(int i = 1; i < sz(fountains); i++){
			if(fountains[i][1]-fountains[i-1][1]==2){
				auto [x1,y1,a] = fountains[i-1];
				auto [x2,y2,b] = fountains[i];
				dsu.unionSet(a,b);
				edges.pb(Edge(a,b,x1-1,y1+1));
			}
		}
	}
	else if(mxX-mnX==2){
		sort(all(fountains),[&](ar3 a, ar3 b){ 
			if(a[0]!=b[0]) return a[0]<b[0];
			return a[1]<b[1]; 
		});
		set<ar2> S; S.clear();
		for(int i = sz(fountains)-1; i>=0; i--){
			if(fountains[i][0]!=mxX) break;
			S.insert({fountains[i][1], fountains[i][2]});
		}
		for(int i = 1; i < sz(fountains); i++){
			if(fountains[i][0]!=fountains[i-1][0]) continue;
			if(fountains[i][1]-fountains[i-1][1]==2){
				auto [x1,y1,a] = fountains[i-1];
				auto [x2,y2,b] = fountains[i];
				dsu.unionSet(a,b);
				if(x1==mnX) edges.pb(Edge(a,b,x1-1,y1+1));
				else edges.pb(Edge(a,b,x1+1,y1+1));
			}
		}
		
		for(int i = 0; i < sz(fountains); i++){
			auto [x1,y1,a] = fountains[i];
			if(x1!=mnX) break;
			auto itr = S.lower_bound({y1,-1});
			if(itr==end(S) or (*itr)[0]!=y1) continue;
			int b = (*itr)[1];
			dsu.unionSet(a,b);
			edges.pb(Edge(a,b,x1+1,y1-1));
		}
	}
	else if(mxX-mnX==4 and 0){ // currently false for now
		sort(all(fountains),[&](ar3 a, ar3 b){ 
			if(a[0]!=b[0]) return a[0]<b[0];
			return a[1]<b[1]; 
		});
	}
	else{
		sort(all(fountains),[&](ar3 a, ar3 b){ 
			if(a[0]!=b[0]) return a[0]<b[0];
			return a[1]<b[1]; 
		});
		set<ar2> S[mxN];
		for(int i = sz(fountains)-1; i>=0; i--)
			S[fountains[i][0]].insert({fountains[i][1], fountains[i][2]});
		for(int i = 1; i < sz(fountains); i++){
			if(fountains[i][0]!=fountains[i-1][0]) continue;
			if(fountains[i][1]-fountains[i-1][1]==2){
				auto [x1,y1,a] = fountains[i-1];
				auto [x2,y2,b] = fountains[i];
				dsu.unionSet(a,b);
			}
		}
		for(int i = 0; i < sz(fountains); i++){
			auto [x1,y1,a] = fountains[i];
			auto itr = S[x1+2].lower_bound({y1,-1});
			if(itr==end(S[x1+2]) or (*itr)[0]!=y1) continue;
			int b = (*itr)[1];
			dsu.unionSet(a,b);
		}
		for(int i = 0; i < n; i++){
			if(sz(adj[i])!=1) continue;
			dfs(i,-1, 0); dfs(i,-1, 1); break;
		}
	}
	if(dsu.findSz(0)!=n) return 0;
    vi U, V, A, B;
    for(auto edge : edges){
		U.pb(edge.fa), V.pb(edge.fb);
		A.pb(edge.x), B.pb(edge.y);
	}
	build(U,V,A,B); return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 38748 KB Output is correct
2 Correct 8 ms 38748 KB Output is correct
3 Correct 8 ms 38744 KB Output is correct
4 Correct 8 ms 38644 KB Output is correct
5 Correct 8 ms 38748 KB Output is correct
6 Correct 9 ms 38748 KB Output is correct
7 Correct 8 ms 38748 KB Output is correct
8 Correct 9 ms 38748 KB Output is correct
9 Correct 59 ms 57128 KB Output is correct
10 Correct 12 ms 40536 KB Output is correct
11 Correct 28 ms 48532 KB Output is correct
12 Correct 13 ms 41560 KB Output is correct
13 Correct 18 ms 44104 KB Output is correct
14 Correct 9 ms 38748 KB Output is correct
15 Correct 9 ms 38812 KB Output is correct
16 Correct 70 ms 57032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 38748 KB Output is correct
2 Correct 8 ms 38748 KB Output is correct
3 Correct 8 ms 38744 KB Output is correct
4 Correct 8 ms 38644 KB Output is correct
5 Correct 8 ms 38748 KB Output is correct
6 Correct 9 ms 38748 KB Output is correct
7 Correct 8 ms 38748 KB Output is correct
8 Correct 9 ms 38748 KB Output is correct
9 Correct 59 ms 57128 KB Output is correct
10 Correct 12 ms 40536 KB Output is correct
11 Correct 28 ms 48532 KB Output is correct
12 Correct 13 ms 41560 KB Output is correct
13 Correct 18 ms 44104 KB Output is correct
14 Correct 9 ms 38748 KB Output is correct
15 Correct 9 ms 38812 KB Output is correct
16 Correct 70 ms 57032 KB Output is correct
17 Correct 8 ms 38748 KB Output is correct
18 Correct 8 ms 38604 KB Output is correct
19 Correct 8 ms 38744 KB Output is correct
20 Correct 8 ms 39000 KB Output is correct
21 Correct 8 ms 38600 KB Output is correct
22 Correct 8 ms 38748 KB Output is correct
23 Correct 161 ms 89264 KB Output is correct
24 Correct 9 ms 38744 KB Output is correct
25 Correct 9 ms 39004 KB Output is correct
26 Correct 9 ms 39260 KB Output is correct
27 Correct 10 ms 39364 KB Output is correct
28 Correct 71 ms 57800 KB Output is correct
29 Correct 92 ms 67456 KB Output is correct
30 Correct 129 ms 77336 KB Output is correct
31 Correct 167 ms 88324 KB Output is correct
32 Correct 8 ms 38748 KB Output is correct
33 Correct 8 ms 38748 KB Output is correct
34 Correct 8 ms 38748 KB Output is correct
35 Correct 9 ms 38748 KB Output is correct
36 Correct 8 ms 38748 KB Output is correct
37 Correct 8 ms 38740 KB Output is correct
38 Correct 8 ms 38640 KB Output is correct
39 Correct 8 ms 38748 KB Output is correct
40 Correct 8 ms 38748 KB Output is correct
41 Correct 8 ms 38748 KB Output is correct
42 Correct 8 ms 38692 KB Output is correct
43 Correct 9 ms 39004 KB Output is correct
44 Correct 10 ms 39260 KB Output is correct
45 Correct 82 ms 57900 KB Output is correct
46 Correct 128 ms 67516 KB Output is correct
47 Correct 104 ms 69564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 38748 KB Output is correct
2 Correct 8 ms 38748 KB Output is correct
3 Correct 8 ms 38744 KB Output is correct
4 Correct 8 ms 38644 KB Output is correct
5 Correct 8 ms 38748 KB Output is correct
6 Correct 9 ms 38748 KB Output is correct
7 Correct 8 ms 38748 KB Output is correct
8 Correct 9 ms 38748 KB Output is correct
9 Correct 59 ms 57128 KB Output is correct
10 Correct 12 ms 40536 KB Output is correct
11 Correct 28 ms 48532 KB Output is correct
12 Correct 13 ms 41560 KB Output is correct
13 Correct 18 ms 44104 KB Output is correct
14 Correct 9 ms 38748 KB Output is correct
15 Correct 9 ms 38812 KB Output is correct
16 Correct 70 ms 57032 KB Output is correct
17 Correct 8 ms 38748 KB Output is correct
18 Correct 8 ms 38604 KB Output is correct
19 Correct 8 ms 38744 KB Output is correct
20 Correct 8 ms 39000 KB Output is correct
21 Correct 8 ms 38600 KB Output is correct
22 Correct 8 ms 38748 KB Output is correct
23 Correct 161 ms 89264 KB Output is correct
24 Correct 9 ms 38744 KB Output is correct
25 Correct 9 ms 39004 KB Output is correct
26 Correct 9 ms 39260 KB Output is correct
27 Correct 10 ms 39364 KB Output is correct
28 Correct 71 ms 57800 KB Output is correct
29 Correct 92 ms 67456 KB Output is correct
30 Correct 129 ms 77336 KB Output is correct
31 Correct 167 ms 88324 KB Output is correct
32 Correct 8 ms 38748 KB Output is correct
33 Correct 8 ms 38748 KB Output is correct
34 Correct 8 ms 38748 KB Output is correct
35 Correct 9 ms 38748 KB Output is correct
36 Correct 8 ms 38748 KB Output is correct
37 Correct 8 ms 38740 KB Output is correct
38 Correct 8 ms 38640 KB Output is correct
39 Correct 8 ms 38748 KB Output is correct
40 Correct 8 ms 38748 KB Output is correct
41 Correct 8 ms 38748 KB Output is correct
42 Correct 8 ms 38692 KB Output is correct
43 Correct 9 ms 39004 KB Output is correct
44 Correct 10 ms 39260 KB Output is correct
45 Correct 82 ms 57900 KB Output is correct
46 Correct 128 ms 67516 KB Output is correct
47 Correct 104 ms 69564 KB Output is correct
48 Correct 8 ms 38748 KB Output is correct
49 Correct 9 ms 38748 KB Output is correct
50 Correct 8 ms 38748 KB Output is correct
51 Correct 9 ms 38580 KB Output is correct
52 Correct 10 ms 38748 KB Output is correct
53 Correct 11 ms 38704 KB Output is correct
54 Correct 10 ms 38748 KB Output is correct
55 Correct 295 ms 109728 KB Output is correct
56 Correct 9 ms 38748 KB Output is correct
57 Incorrect 12 ms 39164 KB Solution announced impossible, but it is possible.
58 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 38748 KB Output is correct
2 Correct 8 ms 38748 KB Output is correct
3 Correct 8 ms 38744 KB Output is correct
4 Correct 8 ms 38644 KB Output is correct
5 Correct 8 ms 38748 KB Output is correct
6 Correct 9 ms 38748 KB Output is correct
7 Correct 8 ms 38748 KB Output is correct
8 Correct 9 ms 38748 KB Output is correct
9 Correct 59 ms 57128 KB Output is correct
10 Correct 12 ms 40536 KB Output is correct
11 Correct 28 ms 48532 KB Output is correct
12 Correct 13 ms 41560 KB Output is correct
13 Correct 18 ms 44104 KB Output is correct
14 Correct 9 ms 38748 KB Output is correct
15 Correct 9 ms 38812 KB Output is correct
16 Correct 70 ms 57032 KB Output is correct
17 Correct 9 ms 38744 KB Output is correct
18 Correct 8 ms 38748 KB Output is correct
19 Correct 9 ms 38748 KB Output is correct
20 Correct 260 ms 119820 KB Output is correct
21 Correct 271 ms 121352 KB Output is correct
22 Correct 283 ms 121356 KB Output is correct
23 Correct 206 ms 110836 KB Output is correct
24 Correct 74 ms 64208 KB Output is correct
25 Correct 123 ms 71220 KB Output is correct
26 Correct 102 ms 70812 KB Output is correct
27 Correct 215 ms 89104 KB Output is correct
28 Correct 219 ms 89608 KB Output is correct
29 Correct 222 ms 89612 KB Output is correct
30 Correct 234 ms 89352 KB Output is correct
31 Correct 9 ms 38748 KB Output is correct
32 Correct 28 ms 43232 KB Output is correct
33 Correct 36 ms 51768 KB Output is correct
34 Correct 264 ms 121128 KB Output is correct
35 Correct 16 ms 40920 KB Output is correct
36 Correct 41 ms 48192 KB Output is correct
37 Correct 72 ms 56892 KB Output is correct
38 Correct 114 ms 60348 KB Output is correct
39 Correct 151 ms 67540 KB Output is correct
40 Correct 198 ms 79312 KB Output is correct
41 Correct 252 ms 86516 KB Output is correct
42 Correct 311 ms 92168 KB Output is correct
43 Correct 8 ms 38748 KB Output is correct
44 Correct 8 ms 38644 KB Output is correct
45 Correct 8 ms 38748 KB Output is correct
46 Correct 12 ms 38780 KB Output is correct
47 Correct 8 ms 38748 KB Output is correct
48 Correct 8 ms 38748 KB Output is correct
49 Correct 8 ms 38748 KB Output is correct
50 Correct 8 ms 38748 KB Output is correct
51 Correct 9 ms 38744 KB Output is correct
52 Correct 8 ms 39000 KB Output is correct
53 Correct 13 ms 38748 KB Output is correct
54 Correct 10 ms 39004 KB Output is correct
55 Correct 10 ms 39260 KB Output is correct
56 Correct 87 ms 58664 KB Output is correct
57 Correct 104 ms 71112 KB Output is correct
58 Correct 106 ms 70840 KB Output is correct
59 Correct 10 ms 39000 KB Output is correct
60 Correct 8 ms 38748 KB Output is correct
61 Correct 8 ms 38800 KB Output is correct
62 Correct 280 ms 120368 KB Output is correct
63 Correct 281 ms 120088 KB Output is correct
64 Correct 280 ms 120612 KB Output is correct
65 Correct 11 ms 39512 KB Output is correct
66 Correct 13 ms 40284 KB Output is correct
67 Correct 139 ms 68696 KB Output is correct
68 Correct 225 ms 93848 KB Output is correct
69 Correct 302 ms 104984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 38748 KB Output is correct
2 Correct 8 ms 38748 KB Output is correct
3 Correct 8 ms 38744 KB Output is correct
4 Correct 8 ms 38644 KB Output is correct
5 Correct 8 ms 38748 KB Output is correct
6 Correct 9 ms 38748 KB Output is correct
7 Correct 8 ms 38748 KB Output is correct
8 Correct 9 ms 38748 KB Output is correct
9 Correct 59 ms 57128 KB Output is correct
10 Correct 12 ms 40536 KB Output is correct
11 Correct 28 ms 48532 KB Output is correct
12 Correct 13 ms 41560 KB Output is correct
13 Correct 18 ms 44104 KB Output is correct
14 Correct 9 ms 38748 KB Output is correct
15 Correct 9 ms 38812 KB Output is correct
16 Correct 70 ms 57032 KB Output is correct
17 Correct 269 ms 119308 KB Output is correct
18 Correct 265 ms 113160 KB Output is correct
19 Correct 286 ms 122072 KB Output is correct
20 Correct 216 ms 88872 KB Output is correct
21 Correct 198 ms 83404 KB Output is correct
22 Correct 9 ms 38744 KB Output is correct
23 Correct 44 ms 47308 KB Output is correct
24 Correct 23 ms 43092 KB Output is correct
25 Correct 58 ms 51776 KB Output is correct
26 Correct 84 ms 59708 KB Output is correct
27 Correct 147 ms 64528 KB Output is correct
28 Correct 198 ms 71088 KB Output is correct
29 Correct 208 ms 79768 KB Output is correct
30 Correct 285 ms 85456 KB Output is correct
31 Correct 296 ms 90628 KB Output is correct
32 Correct 276 ms 103180 KB Output is correct
33 Correct 290 ms 120332 KB Output is correct
34 Correct 12 ms 40028 KB Output is correct
35 Correct 15 ms 40820 KB Output is correct
36 Correct 134 ms 70036 KB Output is correct
37 Correct 202 ms 87844 KB Output is correct
38 Correct 294 ms 103432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 38748 KB Output is correct
2 Correct 8 ms 38748 KB Output is correct
3 Correct 8 ms 38744 KB Output is correct
4 Correct 8 ms 38644 KB Output is correct
5 Correct 8 ms 38748 KB Output is correct
6 Correct 9 ms 38748 KB Output is correct
7 Correct 8 ms 38748 KB Output is correct
8 Correct 9 ms 38748 KB Output is correct
9 Correct 59 ms 57128 KB Output is correct
10 Correct 12 ms 40536 KB Output is correct
11 Correct 28 ms 48532 KB Output is correct
12 Correct 13 ms 41560 KB Output is correct
13 Correct 18 ms 44104 KB Output is correct
14 Correct 9 ms 38748 KB Output is correct
15 Correct 9 ms 38812 KB Output is correct
16 Correct 70 ms 57032 KB Output is correct
17 Correct 8 ms 38748 KB Output is correct
18 Correct 8 ms 38604 KB Output is correct
19 Correct 8 ms 38744 KB Output is correct
20 Correct 8 ms 39000 KB Output is correct
21 Correct 8 ms 38600 KB Output is correct
22 Correct 8 ms 38748 KB Output is correct
23 Correct 161 ms 89264 KB Output is correct
24 Correct 9 ms 38744 KB Output is correct
25 Correct 9 ms 39004 KB Output is correct
26 Correct 9 ms 39260 KB Output is correct
27 Correct 10 ms 39364 KB Output is correct
28 Correct 71 ms 57800 KB Output is correct
29 Correct 92 ms 67456 KB Output is correct
30 Correct 129 ms 77336 KB Output is correct
31 Correct 167 ms 88324 KB Output is correct
32 Correct 8 ms 38748 KB Output is correct
33 Correct 8 ms 38748 KB Output is correct
34 Correct 8 ms 38748 KB Output is correct
35 Correct 9 ms 38748 KB Output is correct
36 Correct 8 ms 38748 KB Output is correct
37 Correct 8 ms 38740 KB Output is correct
38 Correct 8 ms 38640 KB Output is correct
39 Correct 8 ms 38748 KB Output is correct
40 Correct 8 ms 38748 KB Output is correct
41 Correct 8 ms 38748 KB Output is correct
42 Correct 8 ms 38692 KB Output is correct
43 Correct 9 ms 39004 KB Output is correct
44 Correct 10 ms 39260 KB Output is correct
45 Correct 82 ms 57900 KB Output is correct
46 Correct 128 ms 67516 KB Output is correct
47 Correct 104 ms 69564 KB Output is correct
48 Correct 8 ms 38748 KB Output is correct
49 Correct 9 ms 38748 KB Output is correct
50 Correct 8 ms 38748 KB Output is correct
51 Correct 9 ms 38580 KB Output is correct
52 Correct 10 ms 38748 KB Output is correct
53 Correct 11 ms 38704 KB Output is correct
54 Correct 10 ms 38748 KB Output is correct
55 Correct 295 ms 109728 KB Output is correct
56 Correct 9 ms 38748 KB Output is correct
57 Incorrect 12 ms 39164 KB Solution announced impossible, but it is possible.
58 Halted 0 ms 0 KB -