답안 #828114

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
828114 2023-08-17T05:19:38 Z tranxuanbach Simurgh (IOI17_simurgh) C++17
51 / 100
87 ms 5496 KB
#include "simurgh.h"

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)a.size())

const int N = 5e2 + 5, M = N * (N - 1) / 2;

struct disjoint_set{
	int par[N];

	void reset(int n = N){
		fill(par, par + n, -1);
	}

	disjoint_set(){
		reset();
	}

	int root(int x){
		return (par[x] < 0 ? x : (par[x] = root(par[x])));
	}

	bool merge(int x, int y){
		if ((x = root(x)) == (y = root(y))){
			return false;
		}
		if (par[x] > par[y]){
			swap(x, y);
		}
		par[x] += par[y];
		par[y] = x;
		return true;
	}
} dsu;

struct weighted_disjoint_set{
	pair <int, int> par[M];

	void reset(int n = M){
		fill(par, par + n, pair{-1, 0});
	}

	weighted_disjoint_set(){
		reset();
	}

	pair <int, int> root(int x){
		if (par[x].fi < 0){
			return pair{x, 0};
		}
		pair <int, int> ans = root(par[x].fi);
		ans.se ^= par[x].se;
		return (par[x] = ans);
	}

	bool share(int x, int y){
		return (root(x).fi == root(y).fi);
	}

	int dist(int x, int y){
		return (root(x).se ^ root(y).se);
	}

	bool merge(int x, int y, int val){
		auto [tx, vx] = root(x);
		auto [ty, vy] = root(y);
		if (tx == ty){
			return false;
		}
		val ^= vx ^ vy;
		if (par[tx].fi > par[ty].fi){
			swap(tx, ty);
		}
		par[tx].fi += par[ty].fi;
		par[ty] = pair{tx, val};
		return true;
	}
} wdsu;

int n, m;
pair <int, int> edge[M];
int adj[N][N];
vector <int> vadj[N];

vector <int> find_spanning_tree(){
	vector <int> ans;
	For(i, 0, m){
		auto [u, v] = edge[i];
		if (dsu.merge(u, v)){
			ans.emplace_back(i);
		}
	}
	return ans;
}

namespace subtask123{
	bool check(){
		return n <= 240;
	}

	int msk[N];
	vector <int> vadj_tree[N];
	int known_edge[M];

	void dfs_msk(int u, int p, int val){
		msk[u] = val;
		for (auto v: vadj_tree[u]){
			if (v == p){
				continue;
			}
			dfs_msk(v, u, val);
		}
	}

	vector <int> solve(){
		vector <int> mst = find_spanning_tree();
		// cout << "mst" << endl;
		// for (auto i: mst){
		// 	cout << i << ' ';
		// }
		// cout << endl;
		int count_mst = count_common_roads(mst);
		for (auto i: mst){
			auto &[u, v] = edge[i];
			vadj_tree[u].emplace_back(v);
			vadj_tree[v].emplace_back(u);
		}
		fill(known_edge, known_edge + m, -1);

		for (auto id: mst){
			vector <int> rem_mst;
			for (auto i: mst){
				if (i != id){
					rem_mst.emplace_back(i);
				}
			}
			auto query = [&](int i){
				rem_mst.emplace_back(i);
				int ans = count_common_roads(rem_mst);
				rem_mst.pop_back();
				return ans;
			};

			auto &[s, t] = edge[id];
			dfs_msk(s, t, s);
			dfs_msk(t, s, t);
			// cout << "id " << id << endl;
			// For(u, 0, n){
			// 	cout << msk[u] << ' ';
			// }
			// cout << endl;

			For(i, 0, m){
				if (i == id){
					continue;
				}
				auto &[u, v] = edge[i];
				if (msk[u] == msk[v]){
					continue;
				}
				if (wdsu.share(i, id)){
					// cout << "share " << id << ' ' << i << ' ' << wdsu.dist(i, id) << endl;
					if (known_edge[id] == -1 and wdsu.dist(i, id)){
						int cur = query(i);
						if (cur < count_mst){
							known_edge[id] = 1;
						}
						else{
							known_edge[id] = 0;
						}
					}
					continue;
				}
				// cout << "new connection " << id << ' ' << i << endl;
				int cur = query(i);
				// cout << cur << ' ' << count_mst << endl;
				if (cur < count_mst){
					known_edge[id] = 1;
					wdsu.merge(i, id, 1);
				}
				else if (cur > count_mst){
					known_edge[id] = 0;
					wdsu.merge(i, id, 1);
				}
				else{
					wdsu.merge(i, id, 0);
				}
			}
			if (known_edge[id] == -1){
				known_edge[id] = 1;
			}
			// cout << "known_edge " << id << ' ' << known_edge[id] << endl;
		}
		For(i, 0, m){
			if (known_edge[i] != -1){
				auto [j, w] = wdsu.root(i);
				known_edge[j] = known_edge[i] ^ w;
			}
		}
		For(i, 0, m){
			if (known_edge[i] == -1){
				auto [j, w] = wdsu.root(i);
				known_edge[i] = known_edge[j] ^ w;
			}
		}
		vector <int> ans;
		For(i, 0, m){
			if (known_edge[i]){
				ans.emplace_back(i);
			}
		}
		return ans;
	}
}

vector <int> find_roads(int _n, vector <int> _u, vector <int> _v){
	n = _n;
	m = isz(_u);
	For(u, 0, n){
		For(v, 0, n){
			adj[u][v] = -1;
		}
	}
	For(i, 0, m){
		int u = _u[i], v = _v[i];
		edge[i] = pair{u, v};
		adj[u][v] = adj[v][u] = i;
		vadj[u].emplace_back(i);
		vadj[v].emplace_back(i);
	}

	if (subtask123::check()){
		return subtask123::solve();
	}
	return {};
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1364 KB correct
2 Correct 1 ms 1364 KB correct
3 Correct 1 ms 1344 KB correct
4 Correct 1 ms 1340 KB correct
5 Correct 1 ms 1356 KB correct
6 Correct 1 ms 1364 KB correct
7 Correct 1 ms 1236 KB correct
8 Correct 1 ms 1236 KB correct
9 Correct 1 ms 1236 KB correct
10 Correct 1 ms 1236 KB correct
11 Correct 1 ms 1236 KB correct
12 Correct 1 ms 1236 KB correct
13 Correct 1 ms 1344 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1364 KB correct
2 Correct 1 ms 1364 KB correct
3 Correct 1 ms 1344 KB correct
4 Correct 1 ms 1340 KB correct
5 Correct 1 ms 1356 KB correct
6 Correct 1 ms 1364 KB correct
7 Correct 1 ms 1236 KB correct
8 Correct 1 ms 1236 KB correct
9 Correct 1 ms 1236 KB correct
10 Correct 1 ms 1236 KB correct
11 Correct 1 ms 1236 KB correct
12 Correct 1 ms 1236 KB correct
13 Correct 1 ms 1344 KB correct
14 Correct 2 ms 1472 KB correct
15 Correct 2 ms 1492 KB correct
16 Correct 2 ms 1492 KB correct
17 Correct 2 ms 1492 KB correct
18 Correct 1 ms 1364 KB correct
19 Correct 3 ms 1472 KB correct
20 Correct 2 ms 1492 KB correct
21 Correct 2 ms 1364 KB correct
22 Correct 2 ms 1364 KB correct
23 Correct 1 ms 1364 KB correct
24 Correct 1 ms 1364 KB correct
25 Correct 1 ms 1364 KB correct
26 Correct 2 ms 1364 KB correct
27 Correct 2 ms 1400 KB correct
28 Correct 1 ms 1364 KB correct
29 Correct 1 ms 1364 KB correct
30 Correct 2 ms 1468 KB correct
31 Correct 1 ms 1364 KB correct
32 Correct 1 ms 1364 KB correct
33 Correct 1 ms 1472 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1364 KB correct
2 Correct 1 ms 1364 KB correct
3 Correct 1 ms 1344 KB correct
4 Correct 1 ms 1340 KB correct
5 Correct 1 ms 1356 KB correct
6 Correct 1 ms 1364 KB correct
7 Correct 1 ms 1236 KB correct
8 Correct 1 ms 1236 KB correct
9 Correct 1 ms 1236 KB correct
10 Correct 1 ms 1236 KB correct
11 Correct 1 ms 1236 KB correct
12 Correct 1 ms 1236 KB correct
13 Correct 1 ms 1344 KB correct
14 Correct 2 ms 1472 KB correct
15 Correct 2 ms 1492 KB correct
16 Correct 2 ms 1492 KB correct
17 Correct 2 ms 1492 KB correct
18 Correct 1 ms 1364 KB correct
19 Correct 3 ms 1472 KB correct
20 Correct 2 ms 1492 KB correct
21 Correct 2 ms 1364 KB correct
22 Correct 2 ms 1364 KB correct
23 Correct 1 ms 1364 KB correct
24 Correct 1 ms 1364 KB correct
25 Correct 1 ms 1364 KB correct
26 Correct 2 ms 1364 KB correct
27 Correct 2 ms 1400 KB correct
28 Correct 1 ms 1364 KB correct
29 Correct 1 ms 1364 KB correct
30 Correct 2 ms 1468 KB correct
31 Correct 1 ms 1364 KB correct
32 Correct 1 ms 1364 KB correct
33 Correct 1 ms 1472 KB correct
34 Correct 82 ms 3080 KB correct
35 Correct 80 ms 3056 KB correct
36 Correct 71 ms 2744 KB correct
37 Correct 6 ms 1876 KB correct
38 Correct 82 ms 3072 KB correct
39 Correct 75 ms 2936 KB correct
40 Correct 65 ms 2748 KB correct
41 Correct 87 ms 3092 KB correct
42 Correct 84 ms 3080 KB correct
43 Correct 43 ms 2508 KB correct
44 Correct 38 ms 2260 KB correct
45 Correct 40 ms 2452 KB correct
46 Correct 30 ms 2248 KB correct
47 Correct 14 ms 2004 KB correct
48 Correct 4 ms 1732 KB correct
49 Correct 6 ms 1876 KB correct
50 Correct 16 ms 2004 KB correct
51 Correct 45 ms 2436 KB correct
52 Correct 42 ms 2376 KB correct
53 Correct 31 ms 2260 KB correct
54 Correct 42 ms 2576 KB correct
55 Correct 40 ms 2456 KB correct
56 Correct 44 ms 2388 KB correct
57 Correct 44 ms 2452 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1236 KB correct
2 Correct 1 ms 1364 KB correct
3 Incorrect 14 ms 5496 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1364 KB correct
2 Correct 1 ms 1364 KB correct
3 Correct 1 ms 1344 KB correct
4 Correct 1 ms 1340 KB correct
5 Correct 1 ms 1356 KB correct
6 Correct 1 ms 1364 KB correct
7 Correct 1 ms 1236 KB correct
8 Correct 1 ms 1236 KB correct
9 Correct 1 ms 1236 KB correct
10 Correct 1 ms 1236 KB correct
11 Correct 1 ms 1236 KB correct
12 Correct 1 ms 1236 KB correct
13 Correct 1 ms 1344 KB correct
14 Correct 2 ms 1472 KB correct
15 Correct 2 ms 1492 KB correct
16 Correct 2 ms 1492 KB correct
17 Correct 2 ms 1492 KB correct
18 Correct 1 ms 1364 KB correct
19 Correct 3 ms 1472 KB correct
20 Correct 2 ms 1492 KB correct
21 Correct 2 ms 1364 KB correct
22 Correct 2 ms 1364 KB correct
23 Correct 1 ms 1364 KB correct
24 Correct 1 ms 1364 KB correct
25 Correct 1 ms 1364 KB correct
26 Correct 2 ms 1364 KB correct
27 Correct 2 ms 1400 KB correct
28 Correct 1 ms 1364 KB correct
29 Correct 1 ms 1364 KB correct
30 Correct 2 ms 1468 KB correct
31 Correct 1 ms 1364 KB correct
32 Correct 1 ms 1364 KB correct
33 Correct 1 ms 1472 KB correct
34 Correct 82 ms 3080 KB correct
35 Correct 80 ms 3056 KB correct
36 Correct 71 ms 2744 KB correct
37 Correct 6 ms 1876 KB correct
38 Correct 82 ms 3072 KB correct
39 Correct 75 ms 2936 KB correct
40 Correct 65 ms 2748 KB correct
41 Correct 87 ms 3092 KB correct
42 Correct 84 ms 3080 KB correct
43 Correct 43 ms 2508 KB correct
44 Correct 38 ms 2260 KB correct
45 Correct 40 ms 2452 KB correct
46 Correct 30 ms 2248 KB correct
47 Correct 14 ms 2004 KB correct
48 Correct 4 ms 1732 KB correct
49 Correct 6 ms 1876 KB correct
50 Correct 16 ms 2004 KB correct
51 Correct 45 ms 2436 KB correct
52 Correct 42 ms 2376 KB correct
53 Correct 31 ms 2260 KB correct
54 Correct 42 ms 2576 KB correct
55 Correct 40 ms 2456 KB correct
56 Correct 44 ms 2388 KB correct
57 Correct 44 ms 2452 KB correct
58 Correct 1 ms 1236 KB correct
59 Correct 1 ms 1364 KB correct
60 Incorrect 14 ms 5496 KB WA in grader: NO
61 Halted 0 ms 0 KB -