Submission #1028879

# Submission time Handle Problem Language Result Execution time Memory
1028879 2024-07-20T09:48:41 Z DorostWef Simurgh (IOI17_simurgh) C++17
100 / 100
269 ms 8184 KB
#include "simurgh.h"
#include <bits/stdc++.h>

using namespace std;
typedef pair <int, int> pii;
#define F first
#define S second
const int N = 506, M = N * N;
set <int> st;
vector <pii> g[N];
bool vis[N];
int h[N], par[N];
int a[M], b[M];
int w[M];
vector <int> tree;
int p[N], sz[N];
int d[N];
int nn;
bool ff[M];

void asser (bool f) {
	if (!f) {
		while (true)
			cerr << "wef" << endl;
	}
}

int get_par (int v) {
	if (p[v] == v)
		return v;
	return p[v] = get_par (p[v]);
}

bool merge(int u, int v) {
	u = get_par (u);
	v = get_par (v);
	if (u == v)
		return false;
	if (sz[u] > sz[v])
		swap (u, v);
	p[u] = v;
	return true;
}

void clear() {
	for (int i = 0; i < N; i++) {
		p[i] = i;
		sz[i] = 1;
	}
}

int cc = 0;

int askt () {
	cc++;
	vector <int> wef;
	for (int x : st)
		wef.push_back(x);
	asser (wef.size() == nn - 1);
	return count_common_roads(wef);
}

void dfs (int v) {
	vis[v] = true;
	for (pii p : g[v]) {
		int u = p.F;
		if (!vis[u]) {
			h[u] = h[v] + 1;
			par[u] = p.S;
			dfs (u);
			tree.push_back (p.S);
			st.insert (p.S);
		}
	}
}

int askj (vector <int> wef) {
	clear();
	for (int i : wef) {
		asser (merge (a[i], b[i]));
	}
	int k = 0;
	for (int i : tree) {
		if (merge (a[i], b[i])) {
			wef.push_back (i);
			k += w[i];
		}
	}
	asser (wef.size() == nn - 1);
	cc++;
	if (cc >= 7900)
		asser (false);
	int wefwef = count_common_roads (wef) - k;
	return wefwef;
}

std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
	nn = n;
	int m = (int)u.size();
	for (int i = 0; i < m; i++) {
		w[i] = -1;
		a[i] = u[i];
		b[i] = v[i];
		g[a[i]].push_back(make_pair (b[i], i));
		g[b[i]].push_back(make_pair (a[i], i));
	}
	dfs (0);
	int k = askt();
	for (int i = 0; i < m; i++) {
		if (abs (h[a[i]] - h[b[i]]) >= 2) {
			if (h[a[i]] < h[b[i]])
				swap (a[i], b[i]);
			vector <int> alo;
			int vv = a[i];
			bool f = false;
			int h = -1;
			while (vv != b[i]) {
				if (w[par[vv]] == -1)
					f = true;
				if (w[par[vv]] != -1) 
					h = par[vv];
				alo.push_back (par[vv]);
				vv = a[par[vv]] ^ b[par[vv]] ^ vv;
			}
			if (!f)
				continue;
			if (h != -1) {
				st.insert(i);
				assert (st.count (h));
				st.erase (h);
				int o = askt();
				o = o - (k - w[h]);
				st.insert (h);
				for (int few : alo) {
					if (w[few] == -1) {
						assert (st.count (few));
						st.erase (few);
						w[few] = k - (askt() - o);
						st.insert (few);
					}
				}
				st.erase(i);
			} else {
				st.insert (i);
				int mx = k;
				vector <int> what;
				for (int few : alo) {
					assert (st.count (few));
					st.erase (few);
					int wef = askt();
					mx = max (mx, wef);
					what.push_back(wef);
					st.insert (few);
				}
				st.erase (i);
				for (int j = 0; j < (int)alo.size(); j++) {
					w[alo[j]] = mx - what[j];
				}
			}
		}
	}
	for (int i : tree) {
		if (w[i] == -1)
			w[i] = 1;
	}
	for (int i = 0; i < n; i++) {
		vector <int> adj;
		for (pair <int, int> p : g[i])
			adj.push_back (p.S);
		d[i] = askj (adj);
	}
	vector <int> ans;
	for (int i = 0; i < n - 1; i++) {
		int x = -1;
		for (int j = 0; j < n; j++) {
			if (d[j] == 1) {
				x = j;
			}
		}
		asser (x != -1);
		vector <int> adj;
		for (pair <int, int> p : g[x]) {
			if (!ff[p.S]) 
				adj.push_back (p.S);
		}
		asser (adj.size() >= 1);
		int l = -1, r = (int)adj.size() - 1;
		while (r - l > 1) {
			int mid = (l + r) >> 1;
			vector <int> wef;
			for (int j = 0; j <= mid; j++) {
				wef.push_back(adj[j]);
			}
			if (askj(wef) == 1) {
				r = mid;
			} else {
				l = mid;
			}
		}
		ff[adj[r]] = true;
		ans.push_back (adj[r]);
		d[a[adj[r]]]--;
		d[b[adj[r]]]--;
	}
	return ans;
}

Compilation message

simurgh.cpp: In function 'int askt()':
simurgh.cpp:59:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   59 |  asser (wef.size() == nn - 1);
      |         ~~~~~~~~~~~^~~~~~~~~
simurgh.cpp: In function 'int askj(std::vector<int>)':
simurgh.cpp:89:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   89 |  asser (wef.size() == nn - 1);
      |         ~~~~~~~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 1 ms 348 KB correct
8 Correct 0 ms 464 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 1 ms 348 KB correct
8 Correct 0 ms 464 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 560 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 396 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 604 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 468 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 2 ms 348 KB correct
33 Correct 1 ms 524 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 1 ms 348 KB correct
8 Correct 0 ms 464 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 560 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 396 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 604 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 468 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 2 ms 348 KB correct
33 Correct 1 ms 524 KB correct
34 Correct 42 ms 1880 KB correct
35 Correct 33 ms 1884 KB correct
36 Correct 27 ms 1628 KB correct
37 Correct 7 ms 348 KB correct
38 Correct 32 ms 1884 KB correct
39 Correct 31 ms 1916 KB correct
40 Correct 37 ms 1628 KB correct
41 Correct 33 ms 1884 KB correct
42 Correct 32 ms 2056 KB correct
43 Correct 18 ms 1372 KB correct
44 Correct 14 ms 1116 KB correct
45 Correct 15 ms 1276 KB correct
46 Correct 12 ms 1116 KB correct
47 Correct 11 ms 800 KB correct
48 Correct 3 ms 600 KB correct
49 Correct 6 ms 592 KB correct
50 Correct 9 ms 604 KB correct
51 Correct 15 ms 1288 KB correct
52 Correct 14 ms 1368 KB correct
53 Correct 19 ms 1124 KB correct
54 Correct 16 ms 1496 KB correct
55 Correct 19 ms 1116 KB correct
56 Correct 19 ms 1288 KB correct
57 Correct 27 ms 1116 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 1 ms 348 KB correct
3 Correct 141 ms 5076 KB correct
4 Correct 217 ms 7144 KB correct
5 Correct 269 ms 7252 KB correct
6 Correct 210 ms 8184 KB correct
7 Correct 233 ms 7252 KB correct
8 Correct 241 ms 7252 KB correct
9 Correct 214 ms 7152 KB correct
10 Correct 234 ms 7252 KB correct
11 Correct 227 ms 7252 KB correct
12 Correct 210 ms 7148 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 204 ms 7152 KB correct
15 Correct 209 ms 7144 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 1 ms 348 KB correct
8 Correct 0 ms 464 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 560 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 1 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 396 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 604 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 468 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 2 ms 348 KB correct
33 Correct 1 ms 524 KB correct
34 Correct 42 ms 1880 KB correct
35 Correct 33 ms 1884 KB correct
36 Correct 27 ms 1628 KB correct
37 Correct 7 ms 348 KB correct
38 Correct 32 ms 1884 KB correct
39 Correct 31 ms 1916 KB correct
40 Correct 37 ms 1628 KB correct
41 Correct 33 ms 1884 KB correct
42 Correct 32 ms 2056 KB correct
43 Correct 18 ms 1372 KB correct
44 Correct 14 ms 1116 KB correct
45 Correct 15 ms 1276 KB correct
46 Correct 12 ms 1116 KB correct
47 Correct 11 ms 800 KB correct
48 Correct 3 ms 600 KB correct
49 Correct 6 ms 592 KB correct
50 Correct 9 ms 604 KB correct
51 Correct 15 ms 1288 KB correct
52 Correct 14 ms 1368 KB correct
53 Correct 19 ms 1124 KB correct
54 Correct 16 ms 1496 KB correct
55 Correct 19 ms 1116 KB correct
56 Correct 19 ms 1288 KB correct
57 Correct 27 ms 1116 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 1 ms 348 KB correct
60 Correct 141 ms 5076 KB correct
61 Correct 217 ms 7144 KB correct
62 Correct 269 ms 7252 KB correct
63 Correct 210 ms 8184 KB correct
64 Correct 233 ms 7252 KB correct
65 Correct 241 ms 7252 KB correct
66 Correct 214 ms 7152 KB correct
67 Correct 234 ms 7252 KB correct
68 Correct 227 ms 7252 KB correct
69 Correct 210 ms 7148 KB correct
70 Correct 0 ms 348 KB correct
71 Correct 204 ms 7152 KB correct
72 Correct 209 ms 7144 KB correct
73 Correct 0 ms 348 KB correct
74 Correct 213 ms 7140 KB correct
75 Correct 250 ms 7004 KB correct
76 Correct 79 ms 2908 KB correct
77 Correct 228 ms 7156 KB correct
78 Correct 211 ms 7252 KB correct
79 Correct 211 ms 7184 KB correct
80 Correct 230 ms 6996 KB correct
81 Correct 197 ms 6236 KB correct
82 Correct 205 ms 7004 KB correct
83 Correct 140 ms 4064 KB correct
84 Correct 141 ms 4872 KB correct
85 Correct 100 ms 4444 KB correct
86 Correct 67 ms 3156 KB correct
87 Correct 63 ms 2624 KB correct
88 Correct 44 ms 1952 KB correct
89 Correct 58 ms 1900 KB correct
90 Correct 54 ms 1628 KB correct
91 Correct 21 ms 604 KB correct
92 Correct 11 ms 572 KB correct
93 Correct 95 ms 4620 KB correct
94 Correct 64 ms 2908 KB correct
95 Correct 59 ms 3164 KB correct
96 Correct 39 ms 1628 KB correct
97 Correct 47 ms 1884 KB correct
98 Correct 60 ms 2632 KB correct
99 Correct 67 ms 1884 KB correct
100 Correct 29 ms 860 KB correct
101 Correct 14 ms 2640 KB correct
102 Correct 131 ms 3676 KB correct
103 Correct 133 ms 3872 KB correct
104 Correct 118 ms 3676 KB correct
105 Correct 114 ms 3676 KB correct