답안 #1039730

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1039730 2024-07-31T08:10:59 Z Alihan_8 분수 공원 (IOI21_parks) C++17
55 / 100
2708 ms 132488 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]});
		}
	}
	
	Dsu ds(n);
	
	auto shuffle = [&](auto &a){
		int n = a.size();
		
		set <int> st;
		
		for ( int i = 0; i < n; i++ ){
			st.insert(i);
		}
		
		auto nxt = a;
		
		for ( int i = 0; i < n; i++ ){
			auto it = st.lower_bound(rnd(0, n - 1));
			
			if ( it == st.end() ) --it;
			
			nxt[i] = a[*it];
			
			st.erase(it);
		} swap(nxt, a);
	};
	
	shuffle(E);
	
	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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 397 ms 45852 KB Output is correct
10 Correct 24 ms 4800 KB Output is correct
11 Correct 148 ms 24840 KB Output is correct
12 Correct 34 ms 7144 KB Output is correct
13 Correct 88 ms 10896 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 3 ms 856 KB Output is correct
16 Correct 411 ms 45960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 397 ms 45852 KB Output is correct
10 Correct 24 ms 4800 KB Output is correct
11 Correct 148 ms 24840 KB Output is correct
12 Correct 34 ms 7144 KB Output is correct
13 Correct 88 ms 10896 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 3 ms 856 KB Output is correct
16 Correct 411 ms 45960 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2660 ms 109684 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 5 ms 1112 KB Output is correct
26 Correct 6 ms 1172 KB Output is correct
27 Correct 8 ms 1372 KB Output is correct
28 Correct 723 ms 44460 KB Output is correct
29 Correct 1243 ms 65728 KB Output is correct
30 Correct 1805 ms 88592 KB Output is correct
31 Correct 2708 ms 109604 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 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 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 5 ms 1116 KB Output is correct
45 Correct 631 ms 53604 KB Output is correct
46 Correct 1111 ms 78300 KB Output is correct
47 Correct 1065 ms 78336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 397 ms 45852 KB Output is correct
10 Correct 24 ms 4800 KB Output is correct
11 Correct 148 ms 24840 KB Output is correct
12 Correct 34 ms 7144 KB Output is correct
13 Correct 88 ms 10896 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 3 ms 856 KB Output is correct
16 Correct 411 ms 45960 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2660 ms 109684 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 5 ms 1112 KB Output is correct
26 Correct 6 ms 1172 KB Output is correct
27 Correct 8 ms 1372 KB Output is correct
28 Correct 723 ms 44460 KB Output is correct
29 Correct 1243 ms 65728 KB Output is correct
30 Correct 1805 ms 88592 KB Output is correct
31 Correct 2708 ms 109604 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 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 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 5 ms 1116 KB Output is correct
45 Correct 631 ms 53604 KB Output is correct
46 Correct 1111 ms 78300 KB Output is correct
47 Correct 1065 ms 78336 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 0 ms 344 KB Output is correct
51 Correct 0 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 1 ms 348 KB Output is correct
55 Incorrect 2345 ms 94304 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 397 ms 45852 KB Output is correct
10 Correct 24 ms 4800 KB Output is correct
11 Correct 148 ms 24840 KB Output is correct
12 Correct 34 ms 7144 KB Output is correct
13 Correct 88 ms 10896 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 3 ms 856 KB Output is correct
16 Correct 411 ms 45960 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 2029 ms 132488 KB Output is correct
21 Correct 1890 ms 124368 KB Output is correct
22 Correct 1699 ms 124800 KB Output is correct
23 Correct 755 ms 82768 KB Output is correct
24 Correct 159 ms 18264 KB Output is correct
25 Correct 703 ms 48132 KB Output is correct
26 Correct 605 ms 48092 KB Output is correct
27 Correct 814 ms 91628 KB Output is correct
28 Correct 837 ms 91648 KB Output is correct
29 Correct 1036 ms 91648 KB Output is correct
30 Correct 1069 ms 91660 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 58 ms 8180 KB Output is correct
33 Correct 111 ms 18640 KB Output is correct
34 Correct 1789 ms 131584 KB Output is correct
35 Correct 20 ms 2620 KB Output is correct
36 Correct 136 ms 12208 KB Output is correct
37 Correct 295 ms 24228 KB Output is correct
38 Correct 500 ms 45024 KB Output is correct
39 Correct 771 ms 61820 KB Output is correct
40 Correct 1042 ms 79340 KB Output is correct
41 Correct 1293 ms 95256 KB Output is correct
42 Correct 1658 ms 111892 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 1 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 344 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 4 ms 860 KB Output is correct
55 Correct 4 ms 1116 KB Output is correct
56 Correct 569 ms 53532 KB Output is correct
57 Correct 893 ms 78300 KB Output is correct
58 Correct 873 ms 78296 KB Output is correct
59 Correct 1 ms 344 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 827 ms 91484 KB Output is correct
63 Correct 791 ms 91520 KB Output is correct
64 Correct 788 ms 91020 KB Output is correct
65 Correct 7 ms 1372 KB Output is correct
66 Correct 14 ms 2140 KB Output is correct
67 Correct 598 ms 54124 KB Output is correct
68 Correct 971 ms 81544 KB Output is correct
69 Correct 1340 ms 108200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 397 ms 45852 KB Output is correct
10 Correct 24 ms 4800 KB Output is correct
11 Correct 148 ms 24840 KB Output is correct
12 Correct 34 ms 7144 KB Output is correct
13 Correct 88 ms 10896 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 3 ms 856 KB Output is correct
16 Correct 411 ms 45960 KB Output is correct
17 Correct 903 ms 96128 KB Output is correct
18 Correct 892 ms 96016 KB Output is correct
19 Correct 1738 ms 126848 KB Output is correct
20 Correct 1667 ms 109500 KB Output is correct
21 Correct 1092 ms 88532 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 152 ms 17260 KB Output is correct
24 Correct 49 ms 5204 KB Output is correct
25 Correct 214 ms 17408 KB Output is correct
26 Correct 393 ms 30656 KB Output is correct
27 Correct 691 ms 55932 KB Output is correct
28 Correct 882 ms 69272 KB Output is correct
29 Correct 1137 ms 83640 KB Output is correct
30 Correct 1406 ms 97764 KB Output is correct
31 Correct 1685 ms 111324 KB Output is correct
32 Correct 1446 ms 105996 KB Output is correct
33 Correct 725 ms 91420 KB Output is correct
34 Correct 9 ms 1536 KB Output is correct
35 Correct 17 ms 2360 KB Output is correct
36 Correct 589 ms 54396 KB Output is correct
37 Correct 1010 ms 81288 KB Output is correct
38 Correct 1444 ms 108500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 397 ms 45852 KB Output is correct
10 Correct 24 ms 4800 KB Output is correct
11 Correct 148 ms 24840 KB Output is correct
12 Correct 34 ms 7144 KB Output is correct
13 Correct 88 ms 10896 KB Output is correct
14 Correct 2 ms 600 KB Output is correct
15 Correct 3 ms 856 KB Output is correct
16 Correct 411 ms 45960 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 2660 ms 109684 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 5 ms 1112 KB Output is correct
26 Correct 6 ms 1172 KB Output is correct
27 Correct 8 ms 1372 KB Output is correct
28 Correct 723 ms 44460 KB Output is correct
29 Correct 1243 ms 65728 KB Output is correct
30 Correct 1805 ms 88592 KB Output is correct
31 Correct 2708 ms 109604 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 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 1 ms 344 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 3 ms 860 KB Output is correct
44 Correct 5 ms 1116 KB Output is correct
45 Correct 631 ms 53604 KB Output is correct
46 Correct 1111 ms 78300 KB Output is correct
47 Correct 1065 ms 78336 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 0 ms 344 KB Output is correct
51 Correct 0 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 1 ms 348 KB Output is correct
55 Incorrect 2345 ms 94304 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -