Submission #1039697

# Submission time Handle Problem Language Result Execution time Memory
1039697 2024-07-31T07:54:06 Z Alihan_8 Fountain Parks (IOI21_parks) C++17
55 / 100
1852 ms 127644 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;
	}
	
};

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;
	
	Dsu ds(n);
	
	map <ar<int,2>,int> mp;
	
	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;
			
			if ( ds.merge(id[u][v], i) ){
				e.pb({id[u][v], i});
				
				mp[{id[u][v], i}] = mp[{i, id[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 348 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 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 274 ms 42676 KB Output is correct
10 Correct 18 ms 4696 KB Output is correct
11 Correct 81 ms 23116 KB Output is correct
12 Correct 21 ms 6588 KB Output is correct
13 Correct 41 ms 9420 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 282 ms 42648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 274 ms 42676 KB Output is correct
10 Correct 18 ms 4696 KB Output is correct
11 Correct 81 ms 23116 KB Output is correct
12 Correct 21 ms 6588 KB Output is correct
13 Correct 41 ms 9420 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 282 ms 42648 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 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 1391 ms 102224 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 4 ms 860 KB Output is correct
26 Correct 4 ms 860 KB Output is correct
27 Correct 6 ms 1272 KB Output is correct
28 Correct 479 ms 41032 KB Output is correct
29 Correct 703 ms 61272 KB Output is correct
30 Correct 1028 ms 81940 KB Output is correct
31 Correct 1530 ms 102076 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 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 440 KB Output is correct
40 Correct 0 ms 436 KB Output is correct
41 Correct 0 ms 432 KB Output is correct
42 Correct 0 ms 432 KB Output is correct
43 Correct 3 ms 604 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 523 ms 50500 KB Output is correct
46 Correct 922 ms 73880 KB Output is correct
47 Correct 821 ms 73792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 274 ms 42676 KB Output is correct
10 Correct 18 ms 4696 KB Output is correct
11 Correct 81 ms 23116 KB Output is correct
12 Correct 21 ms 6588 KB Output is correct
13 Correct 41 ms 9420 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 282 ms 42648 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 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 1391 ms 102224 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 4 ms 860 KB Output is correct
26 Correct 4 ms 860 KB Output is correct
27 Correct 6 ms 1272 KB Output is correct
28 Correct 479 ms 41032 KB Output is correct
29 Correct 703 ms 61272 KB Output is correct
30 Correct 1028 ms 81940 KB Output is correct
31 Correct 1530 ms 102076 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 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 440 KB Output is correct
40 Correct 0 ms 436 KB Output is correct
41 Correct 0 ms 432 KB Output is correct
42 Correct 0 ms 432 KB Output is correct
43 Correct 3 ms 604 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 523 ms 50500 KB Output is correct
46 Correct 922 ms 73880 KB Output is correct
47 Correct 821 ms 73792 KB Output is correct
48 Correct 1 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 1 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Incorrect 1495 ms 85288 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 274 ms 42676 KB Output is correct
10 Correct 18 ms 4696 KB Output is correct
11 Correct 81 ms 23116 KB Output is correct
12 Correct 21 ms 6588 KB Output is correct
13 Correct 41 ms 9420 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 282 ms 42648 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1640 ms 127644 KB Output is correct
21 Correct 1655 ms 117680 KB Output is correct
22 Correct 1550 ms 117904 KB Output is correct
23 Correct 584 ms 77628 KB Output is correct
24 Correct 161 ms 15444 KB Output is correct
25 Correct 532 ms 41924 KB Output is correct
26 Correct 440 ms 41920 KB Output is correct
27 Correct 654 ms 85272 KB Output is correct
28 Correct 692 ms 85360 KB Output is correct
29 Correct 847 ms 85312 KB Output is correct
30 Correct 855 ms 85312 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 51 ms 7720 KB Output is correct
33 Correct 108 ms 17492 KB Output is correct
34 Correct 1532 ms 126552 KB Output is correct
35 Correct 17 ms 2396 KB Output is correct
36 Correct 105 ms 10952 KB Output is correct
37 Correct 210 ms 21724 KB Output is correct
38 Correct 416 ms 42896 KB Output is correct
39 Correct 637 ms 58696 KB Output is correct
40 Correct 810 ms 75640 KB Output is correct
41 Correct 1134 ms 90740 KB Output is correct
42 Correct 1364 ms 106560 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 600 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 428 KB Output is correct
51 Correct 0 ms 344 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 4 ms 1048 KB Output is correct
56 Correct 475 ms 50500 KB Output is correct
57 Correct 750 ms 73812 KB Output is correct
58 Correct 841 ms 73792 KB Output is correct
59 Correct 1 ms 600 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1 ms 348 KB Output is correct
62 Correct 677 ms 85176 KB Output is correct
63 Correct 740 ms 85244 KB Output is correct
64 Correct 709 ms 84824 KB Output is correct
65 Correct 6 ms 1116 KB Output is correct
66 Correct 12 ms 1884 KB Output is correct
67 Correct 538 ms 51272 KB Output is correct
68 Correct 921 ms 76896 KB Output is correct
69 Correct 1437 ms 101972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 274 ms 42676 KB Output is correct
10 Correct 18 ms 4696 KB Output is correct
11 Correct 81 ms 23116 KB Output is correct
12 Correct 21 ms 6588 KB Output is correct
13 Correct 41 ms 9420 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 282 ms 42648 KB Output is correct
17 Correct 880 ms 90388 KB Output is correct
18 Correct 899 ms 90536 KB Output is correct
19 Correct 1852 ms 121660 KB Output is correct
20 Correct 1472 ms 102728 KB Output is correct
21 Correct 1018 ms 82748 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 143 ms 16464 KB Output is correct
24 Correct 36 ms 4464 KB Output is correct
25 Correct 157 ms 15308 KB Output is correct
26 Correct 309 ms 25796 KB Output is correct
27 Correct 604 ms 52744 KB Output is correct
28 Correct 849 ms 65868 KB Output is correct
29 Correct 998 ms 79624 KB Output is correct
30 Correct 1111 ms 92228 KB Output is correct
31 Correct 1333 ms 105280 KB Output is correct
32 Correct 1039 ms 99136 KB Output is correct
33 Correct 602 ms 85124 KB Output is correct
34 Correct 8 ms 1372 KB Output is correct
35 Correct 13 ms 2236 KB Output is correct
36 Correct 479 ms 51016 KB Output is correct
37 Correct 778 ms 76604 KB Output is correct
38 Correct 1160 ms 101456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 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 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 274 ms 42676 KB Output is correct
10 Correct 18 ms 4696 KB Output is correct
11 Correct 81 ms 23116 KB Output is correct
12 Correct 21 ms 6588 KB Output is correct
13 Correct 41 ms 9420 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 282 ms 42648 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 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 1391 ms 102224 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 4 ms 860 KB Output is correct
26 Correct 4 ms 860 KB Output is correct
27 Correct 6 ms 1272 KB Output is correct
28 Correct 479 ms 41032 KB Output is correct
29 Correct 703 ms 61272 KB Output is correct
30 Correct 1028 ms 81940 KB Output is correct
31 Correct 1530 ms 102076 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 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 440 KB Output is correct
40 Correct 0 ms 436 KB Output is correct
41 Correct 0 ms 432 KB Output is correct
42 Correct 0 ms 432 KB Output is correct
43 Correct 3 ms 604 KB Output is correct
44 Correct 4 ms 860 KB Output is correct
45 Correct 523 ms 50500 KB Output is correct
46 Correct 922 ms 73880 KB Output is correct
47 Correct 821 ms 73792 KB Output is correct
48 Correct 1 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 1 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Incorrect 1495 ms 85288 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -