Submission #1039731

# Submission time Handle Problem Language Result Execution time Memory
1039731 2024-07-31T08:13:05 Z Alihan_8 Fountain Parks (IOI21_parks) C++17
55 / 100
1551 ms 131352 KB
#include "parks.h"
 
#include <bits/stdc++.h>
 
using namespace std;
 
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
//#define int long long
 
using i64 = long long;
 
template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}
 
template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}
 
struct Dsu{
	vector <int> fa;
	
	int n;
	
	Dsu(int n) : n(n) {
		fa.resize(n);
		iota(all(fa), 0);
	}
	
	int get(int u){
		return u ^ fa[u] ? fa[u] = get(fa[u]) : u;
	}
	
	bool merge(int u, int v){
		u = get(u), v = get(v);
		
		if ( u == v ) return false;
		
		fa[v] = u;
		
		return true;
	}
	
};

mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
#define rnd(l, r) uniform_int_distribution <int> (l, r)(rng)
 
int dx[] = {2, -2, 0, 0};
int dy[] = {0, 0, -2, 2};
 
int construct_roads(std::vector<int> x, std::vector<int> y) {
    int n = x.size();
    
    map <int,map<int,int>> id;
    
    for ( int i = 0; i < n; i++ ){
		id[x[i]][y[i]] = i;
	}
	
	vector <ar<int,2>> e, E;
	
	map <ar<int,2>,int> mp;
	
	vector <vector<int>> G(n);
	
	for ( int i = 0; i < n; i++ ){
		for ( int j = 0; j < 4; j++ ){
			int u = x[i] + dx[j], v = y[i] + dy[j];
			
			if ( !id[u].count(v) ) continue;
			
			E.pb({i, id[u][v]});
		}
	}
	
	auto f = [&](int i, int j){
		return !(y[i] == y[j]);
	};
	
	sort(all(E), [&](auto &u, auto &v){
		auto [i, j] = u;
		auto [x, y] = v;
		
		return f(i, j) < f(x, y);
	});
	
	Dsu ds(n);
	
	for ( auto &[u, v]: E ){
		if ( ds.merge(u, v) ){
			e.pb({u, v});
				
			mp[{v, u}] = mp[{u, v}] = (int)e.size() - 1;
		}
	}
	
	if ( (int)e.size() != n - 1 ){
		return 0;
	}
	
	//~ for ( int i = 0; i + 1 < n; i++ ){
		//~ cout << e[i][0] << ' ' << e[i][1] << " " << i << ln;
	//~ } cout << ln;
	
	vector <vector<int>> adj(n * 2), rev(n * 2);
	
	auto add = [&](int u, int v){
		//~ cout << u << " " << v << ln;
		
		adj[u].pb(v);
		rev[v].pb(u);
	};
	
	for ( int i = 0; i < n - 1; i++ ){
		auto [u, v] = e[i];
		
		if ( x[u] == x[v] ){ // vertical
			if ( y[u] > y[v] ) swap(u, v);
			
			{ // 0
				int a = -1, b = -1;
				
				if ( id[x[u] + 2].count(y[u]) ){
					int j = id[x[u] + 2][y[u]]; a = j;
					
					if ( mp.count({u, j}) ){
						int k = mp[{u, j}];
						
						add(i, k);
					}
				}
				
				if ( id[x[u] + 2].count(y[v]) ){
					int j = id[x[u] + 2][y[v]]; b = j;
					
					if ( mp.count({j, v}) ){
						int k = mp[{j, v}];
						
						add(i, k + n);
					}
				}
				
				if ( mp.count({a, b}) ){
					add(i, mp[{a, b}]);
				}
			}
			
			{ // 1
				int a = -1, b = -1;
					
				if ( id[x[u] - 2].count(y[u]) ){
					int j = id[x[u] - 2][y[u]]; a = j;
					
					if ( mp.count({u, j}) ){
						int k = mp[{u, j}];
						
						add(i + n, k);
					}
				}
				
				if ( id[x[u] - 2].count(y[v]) ){
					int j = id[x[u] - 2][y[v]]; b = j;
					
					if ( mp.count({j, v}) ){
						int k = mp[{j, v}];
						
						add(i + n, k + n);
					}
				}
				
				if ( mp.count({a, b}) ){
					add(i + n, mp[{a, b}] + n);
				}
			}
		} else{ // horizontal
			if ( x[u] > x[v] ) swap(u, v);
			
			{ // 0
				int a = -1, b = -1;
					
				if ( id[x[u]].count(y[u] - 2) ){
					int j = id[x[u]][y[u] - 2]; a = j;
					
					if ( mp.count({u, j}) ){
						int k = mp[{u, j}];
						
						add(i, k + n);
					}
				}
				
				if ( id[x[v]].count(y[v] - 2) ){
					int j = id[x[v]][y[v] - 2]; b = j;
					
					if ( mp.count({j, v}) ){
						int k = mp[{j, v}];
						
						add(i, k);
					}
				}
				
				if ( mp.count({a, b}) ){
					add(i, mp[{a, b}]);
				}
			}
			{ // 1
				int a = -1, b = -1;
					
				if ( id[x[u]].count(y[u] + 2) ){
					int j = id[x[u]][y[u] + 2]; a = j;
					
					if ( mp.count({u, j}) ){
						int k = mp[{u, j}];
						
						add(i + n, k + n);
					}
				}
				
				if ( id[x[v]].count(y[v] + 2) ){
					int j = id[x[v]][y[v] + 2]; b = j;
					
					if ( mp.count({j, v}) ){
						int k = mp[{j, v}];
						
						add(i + n, k);
					}
				}
				
				if ( mp.count({a, b}) ){
					add(i + n, mp[{a, b}] + n);
				}
			}
		}
	}
	
	vector <int> ord, us(n * 2);
	
	auto dfs = [&](auto dfs, int u) -> void{
		if ( us[u] ) return;
		
		us[u] = true;
		
		for ( auto &v: adj[u] ){
			dfs(dfs, v);
		}
		
		ord.pb(u);
	};
	
	for ( int i = 0; i < n * 2; i++ ){
		dfs(dfs, i);
	}
	
	reverse(all(ord));
	
	vector <int> c(n * 2, -1);
	
	int ct = 0;
	
	auto trav = [&](auto trav, int u) -> void{
		if ( c[u] != -1 ) return;
		
		c[u] = ct;
		
		for ( auto &v: rev[u] ){
			trav(trav, v);
		}
	};
	
	for ( auto &u: ord ){
		if ( c[u] != -1 ) continue;
		
		trav(trav, u);
		
		ct++;
	}
	
	vector <int> t(n - 1);
	
	for ( int i = 0; i + 1 < n; i++ ){
		if ( c[i] == c[i + n] ){
			return 0;
		}
		
		t[i] = (c[i] < c[i + n]);
	}
	
	vector <int> U, V, A, B;
	
	set <pair<int,int>> st;
	
	for ( int i = 0; i + 1 < n; i++ ){
		auto [u, v] = e[i];
		
		U.pb(u), V.pb(v);
		
		if ( x[u] == x[v] ){
			if ( y[u] > y[v] ) swap(u, v);
			
			A.pb(x[u] + (t[i] ? -1 : 1));
			B.pb(y[u] + 1);
		} else{
			if ( x[u] > x[v] ) swap(u, v);
			
			A.pb(x[u] + 1);
			B.pb(y[u] + (t[i] ? 1 : -1));
		}
		
		assert(!st.count({A.back(), B.back()}));
		
		st.insert({A.back(), B.back()});
	}
	
	build(U, V, A, B);
	
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 238 ms 45832 KB Output is correct
10 Correct 13 ms 4832 KB Output is correct
11 Correct 77 ms 24960 KB Output is correct
12 Correct 19 ms 7136 KB Output is correct
13 Correct 36 ms 10820 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 860 KB Output is correct
16 Correct 222 ms 45764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 238 ms 45832 KB Output is correct
10 Correct 13 ms 4832 KB Output is correct
11 Correct 77 ms 24960 KB Output is correct
12 Correct 19 ms 7136 KB Output is correct
13 Correct 36 ms 10820 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 860 KB Output is correct
16 Correct 222 ms 45764 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1464 ms 115816 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 4 ms 1116 KB Output is correct
26 Correct 4 ms 1116 KB Output is correct
27 Correct 5 ms 1400 KB Output is correct
28 Correct 458 ms 46348 KB Output is correct
29 Correct 744 ms 69004 KB Output is correct
30 Correct 1064 ms 93480 KB Output is correct
31 Correct 1491 ms 114952 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 2 ms 868 KB Output is correct
44 Correct 3 ms 1072 KB Output is correct
45 Correct 423 ms 53568 KB Output is correct
46 Correct 681 ms 79044 KB Output is correct
47 Correct 675 ms 79652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 238 ms 45832 KB Output is correct
10 Correct 13 ms 4832 KB Output is correct
11 Correct 77 ms 24960 KB Output is correct
12 Correct 19 ms 7136 KB Output is correct
13 Correct 36 ms 10820 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 860 KB Output is correct
16 Correct 222 ms 45764 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1464 ms 115816 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 4 ms 1116 KB Output is correct
26 Correct 4 ms 1116 KB Output is correct
27 Correct 5 ms 1400 KB Output is correct
28 Correct 458 ms 46348 KB Output is correct
29 Correct 744 ms 69004 KB Output is correct
30 Correct 1064 ms 93480 KB Output is correct
31 Correct 1491 ms 114952 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 2 ms 868 KB Output is correct
44 Correct 3 ms 1072 KB Output is correct
45 Correct 423 ms 53568 KB Output is correct
46 Correct 681 ms 79044 KB Output is correct
47 Correct 675 ms 79652 KB Output is correct
48 Correct 0 ms 600 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Incorrect 1545 ms 104968 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 238 ms 45832 KB Output is correct
10 Correct 13 ms 4832 KB Output is correct
11 Correct 77 ms 24960 KB Output is correct
12 Correct 19 ms 7136 KB Output is correct
13 Correct 36 ms 10820 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 860 KB Output is correct
16 Correct 222 ms 45764 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1442 ms 131352 KB Output is correct
21 Correct 1404 ms 124488 KB Output is correct
22 Correct 1445 ms 124836 KB Output is correct
23 Correct 504 ms 83904 KB Output is correct
24 Correct 164 ms 18516 KB Output is correct
25 Correct 458 ms 48840 KB Output is correct
26 Correct 335 ms 48752 KB Output is correct
27 Correct 541 ms 92020 KB Output is correct
28 Correct 526 ms 91824 KB Output is correct
29 Correct 730 ms 92504 KB Output is correct
30 Correct 723 ms 92292 KB Output is correct
31 Correct 0 ms 344 KB Output is correct
32 Correct 44 ms 8140 KB Output is correct
33 Correct 126 ms 18596 KB Output is correct
34 Correct 1374 ms 130340 KB Output is correct
35 Correct 14 ms 2780 KB Output is correct
36 Correct 92 ms 12328 KB Output is correct
37 Correct 198 ms 24384 KB Output is correct
38 Correct 426 ms 44996 KB Output is correct
39 Correct 604 ms 61764 KB Output is correct
40 Correct 841 ms 81024 KB Output is correct
41 Correct 1073 ms 95976 KB Output is correct
42 Correct 1303 ms 111896 KB Output is correct
43 Correct 0 ms 344 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 2 ms 860 KB Output is correct
55 Correct 3 ms 1116 KB Output is correct
56 Correct 420 ms 53660 KB Output is correct
57 Correct 673 ms 80212 KB Output is correct
58 Correct 709 ms 79556 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 500 ms 92204 KB Output is correct
63 Correct 511 ms 91320 KB Output is correct
64 Correct 558 ms 90900 KB Output is correct
65 Correct 5 ms 1372 KB Output is correct
66 Correct 16 ms 2140 KB Output is correct
67 Correct 526 ms 54188 KB Output is correct
68 Correct 947 ms 81928 KB Output is correct
69 Correct 1306 ms 108732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 238 ms 45832 KB Output is correct
10 Correct 13 ms 4832 KB Output is correct
11 Correct 77 ms 24960 KB Output is correct
12 Correct 19 ms 7136 KB Output is correct
13 Correct 36 ms 10820 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 860 KB Output is correct
16 Correct 222 ms 45764 KB Output is correct
17 Correct 731 ms 96164 KB Output is correct
18 Correct 661 ms 96072 KB Output is correct
19 Correct 1394 ms 126444 KB Output is correct
20 Correct 1439 ms 107068 KB Output is correct
21 Correct 1016 ms 88972 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 129 ms 17420 KB Output is correct
24 Correct 31 ms 5064 KB Output is correct
25 Correct 143 ms 17396 KB Output is correct
26 Correct 267 ms 30340 KB Output is correct
27 Correct 627 ms 55320 KB Output is correct
28 Correct 859 ms 69920 KB Output is correct
29 Correct 1031 ms 83132 KB Output is correct
30 Correct 1276 ms 96768 KB Output is correct
31 Correct 1551 ms 110012 KB Output is correct
32 Correct 1251 ms 107264 KB Output is correct
33 Correct 616 ms 92136 KB Output is correct
34 Correct 6 ms 1628 KB Output is correct
35 Correct 14 ms 2520 KB Output is correct
36 Correct 510 ms 54696 KB Output is correct
37 Correct 909 ms 82088 KB Output is correct
38 Correct 1343 ms 108868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 238 ms 45832 KB Output is correct
10 Correct 13 ms 4832 KB Output is correct
11 Correct 77 ms 24960 KB Output is correct
12 Correct 19 ms 7136 KB Output is correct
13 Correct 36 ms 10820 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 860 KB Output is correct
16 Correct 222 ms 45764 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1464 ms 115816 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 4 ms 1116 KB Output is correct
26 Correct 4 ms 1116 KB Output is correct
27 Correct 5 ms 1400 KB Output is correct
28 Correct 458 ms 46348 KB Output is correct
29 Correct 744 ms 69004 KB Output is correct
30 Correct 1064 ms 93480 KB Output is correct
31 Correct 1491 ms 114952 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 2 ms 868 KB Output is correct
44 Correct 3 ms 1072 KB Output is correct
45 Correct 423 ms 53568 KB Output is correct
46 Correct 681 ms 79044 KB Output is correct
47 Correct 675 ms 79652 KB Output is correct
48 Correct 0 ms 600 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Incorrect 1545 ms 104968 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -