Submission #1082979

# Submission time Handle Problem Language Result Execution time Memory
1082979 2024-09-02T09:27:52 Z happypotato Simurgh (IOI17_simurgh) C++17
100 / 100
292 ms 6388 KB
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
vector<vector<pii>> adj;
vector<pii> par;
vector<int> dep;
vector<bool> vis;
vector<bool> ontree;
vector<int> ans;
int n, m;
void dfstree(int u = 1, int pp = 0, int d = 1) {
	dep[u] = d;
	vis[u] = true;
	for (pii v : adj[u]) {
		if (vis[v.ff]) continue;
		par[v.ff] = {u, v.ss};
		ontree[v.ss] = true;
		dfstree(v.ff, u, d + 1);
	}
}
set<int> qry;
void del(int x) { qry.erase(qry.find(x)); }
void add(int x) { qry.insert(x); }
int ask() {
	vector<int> r;
	for (int x : qry) r.pb(x);
	return count_common_roads(r);
}
vector<int> find_roads(int N, vector<int> U, vector<int> V) {
	n = N; m = (int)(U.size());
	adj.resize(n + 1);
	par.clear(); par.resize(n + 1, make_pair(0, 0));
	dep.clear(); dep.resize(n + 1, 0);
	vis.clear(); vis.resize(n + 1, false);
	ontree.clear(); ontree.resize(m, false);
	ans.clear(); ans.resize(m, -1);
	for (int i = 0; i < m; i++) {
		U[i]++; V[i]++;
		adj[U[i]].pb({V[i], i});
		adj[V[i]].pb({U[i], i});
	}

	dfstree();
	for (int i = 0; i < m; i++) {
		if (ontree[i]) {
			// cerr << "DFS TREE: " << U[i] << ' ' << V[i] << endl;
			qry.insert(i);
		}
	}
	int INIT = ask();
	// cerr << INIT << endl;

	for (int i = 0; i < m; i++) {
		if (dep[U[i]] > dep[V[i]]) swap(U[i], V[i]);
		if (ontree[i]) continue;
		vector<int> chain;
		int cur = V[i];
		while (cur != U[i]) {
			chain.pb(par[cur].ss);
			cur = par[cur].ff;
		}
		// cerr << "edge " << i << ": ";
		// for (int x : chain) cerr << x << ' ';
		// cerr << endl;
		int len = chain.size();
		int cnt = 0;
		for (int x : chain) cnt += (ans[x] != -1);
		if (cnt == 0) {
			// everything unknown: query everything
			vector<int> res;
			add(i);
			for (int x : chain) {
				del(x);
				res.pb(ask());
				add(x);
			}
			del(i);
			// cannot be all 1s, so if all equal assign 0s
			int maxi = INIT;
			for (int x : res) maxi = max(maxi, x);
			// cerr << "checking ";
			// for (int x : chain) cerr << x << ' ';
			// cerr << ", maxi = " << maxi << endl;
			ans[i] = maxi - INIT;
			for (int i = 0; i < len; i++) {
				ans[chain[i]] = maxi - res[i];
			}
		} else if (cnt < len) {
			// more than one known: query everything else
			pii known = {-1, -1};
			for (int x : chain) {
				if (ans[x] != -1) {
					known = {x, ans[x]};
					break;
				}
			}
			// query everything else except for this, calculate expected sum
			del(known.ff); add(i);
			int TOT = ask() + known.ss;
			add(known.ff);
			// calcuate i from TOT and INIT
			ans[i] = TOT - INIT;
			// erase every unknown edge and calculate
			for (int x : chain) {
				if (ans[x] != -1) continue;
				del(x);
				ans[x] = TOT - ask();
				add(x);
			}
			del(i);
		}
	}
	for (int i = 0; i < m; i++) {
		// check for bridges
		if (ontree[i] && ans[i] == -1) ans[i] = 1;
	}

	vector<int> edges;
	auto test = [&](int lb, int rb) -> bool {
		int expect = INIT;
		for (int i = lb; i <= rb; i++) {
			int u = U[edges[i]], v = V[edges[i]];
			// u is ancestor of v -> remove {v, par[v]}
			del(par[v].ss); expect -= ans[par[v].ss];
			add(edges[i]);
			// cerr << u << ' ' << v << ' ' << edges[i] << ": ";
			// cerr << par[v].ff << ' ' << par[v].ss << ' ' << ans[par[v].ss] << endl;
		}
		bool ret = (ask() > expect);
		for (int i = lb; i <= rb; i++) {
			int u = U[edges[i]], v = V[edges[i]];
			add(par[v].ss);
			del(edges[i]);
		}
		return ret;
	};

	// query remaining edges
	for (int u = 1; u <= n; u++) {
		edges.clear();
		for (pii v : adj[u]) {
			if (dep[v.ff] > dep[u] && ans[v.ss] == -1) edges.pb(v.ss);
		}
		// cerr << "edges: ";
		// for (int x : edges) cerr << x << ' ';
		// cerr << endl;
		if (edges.empty()) continue;
		while (test(0, (int)(edges.size()) - 1)) {
			int lb = 0, rb = (int)(edges.size()) - 1;
			while (lb < rb) {
				int mid = (lb + rb) >> 1;
				if (test(lb, mid)) rb = mid;
				else lb = mid + 1;
			}
			// edges[lb] is answer
			ans[edges[lb]] = 1;
			edges.erase(edges.begin() + lb);
		}
	}

	vector<int> fin;
	for (int i = 0; i < m; i++) {
		if (ans[i] == 1) fin.pb(i);
	}
	// cerr << "ANSWER: ";
	// for (int x : fin) cerr << x << ' ';
	// cerr << endl;
	return fin;
}

Compilation message

simurgh.cpp: In lambda function:
simurgh.cpp:126:8: warning: unused variable 'u' [-Wunused-variable]
  126 |    int u = U[edges[i]], v = V[edges[i]];
      |        ^
simurgh.cpp:135:8: warning: unused variable 'u' [-Wunused-variable]
  135 |    int u = U[edges[i]], v = V[edges[i]];
      |        ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 1 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 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 1 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 1 ms 348 KB correct
13 Correct 0 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 1 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 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 1 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 1 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 344 KB correct
15 Correct 2 ms 348 KB correct
16 Correct 2 ms 504 KB correct
17 Correct 2 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 436 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 2 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 392 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 348 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 1 ms 344 KB correct
33 Correct 1 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 1 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 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 1 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 1 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 344 KB correct
15 Correct 2 ms 348 KB correct
16 Correct 2 ms 504 KB correct
17 Correct 2 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 436 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 2 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 392 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 348 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 1 ms 344 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 51 ms 1828 KB correct
35 Correct 48 ms 1624 KB correct
36 Correct 38 ms 1624 KB correct
37 Correct 5 ms 344 KB correct
38 Correct 50 ms 1628 KB correct
39 Correct 43 ms 1628 KB correct
40 Correct 34 ms 1368 KB correct
41 Correct 45 ms 1640 KB correct
42 Correct 43 ms 1624 KB correct
43 Correct 18 ms 1112 KB correct
44 Correct 15 ms 856 KB correct
45 Correct 20 ms 1116 KB correct
46 Correct 14 ms 856 KB correct
47 Correct 8 ms 604 KB correct
48 Correct 2 ms 348 KB correct
49 Correct 3 ms 348 KB correct
50 Correct 6 ms 724 KB correct
51 Correct 18 ms 1112 KB correct
52 Correct 24 ms 860 KB correct
53 Correct 14 ms 860 KB correct
54 Correct 19 ms 1116 KB correct
55 Correct 18 ms 1112 KB correct
56 Correct 19 ms 1116 KB correct
57 Correct 17 ms 1112 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 159 ms 4248 KB correct
4 Correct 274 ms 6224 KB correct
5 Correct 292 ms 5980 KB correct
6 Correct 250 ms 6112 KB correct
7 Correct 242 ms 6232 KB correct
8 Correct 241 ms 6136 KB correct
9 Correct 279 ms 6228 KB correct
10 Correct 272 ms 5976 KB correct
11 Correct 268 ms 6128 KB correct
12 Correct 267 ms 5980 KB correct
13 Correct 0 ms 344 KB correct
14 Correct 269 ms 6388 KB correct
15 Correct 277 ms 6144 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 1 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 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 1 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 1 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 344 KB correct
15 Correct 2 ms 348 KB correct
16 Correct 2 ms 504 KB correct
17 Correct 2 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 2 ms 436 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 2 ms 348 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 348 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 392 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 348 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 1 ms 344 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 51 ms 1828 KB correct
35 Correct 48 ms 1624 KB correct
36 Correct 38 ms 1624 KB correct
37 Correct 5 ms 344 KB correct
38 Correct 50 ms 1628 KB correct
39 Correct 43 ms 1628 KB correct
40 Correct 34 ms 1368 KB correct
41 Correct 45 ms 1640 KB correct
42 Correct 43 ms 1624 KB correct
43 Correct 18 ms 1112 KB correct
44 Correct 15 ms 856 KB correct
45 Correct 20 ms 1116 KB correct
46 Correct 14 ms 856 KB correct
47 Correct 8 ms 604 KB correct
48 Correct 2 ms 348 KB correct
49 Correct 3 ms 348 KB correct
50 Correct 6 ms 724 KB correct
51 Correct 18 ms 1112 KB correct
52 Correct 24 ms 860 KB correct
53 Correct 14 ms 860 KB correct
54 Correct 19 ms 1116 KB correct
55 Correct 18 ms 1112 KB correct
56 Correct 19 ms 1116 KB correct
57 Correct 17 ms 1112 KB correct
58 Correct 1 ms 344 KB correct
59 Correct 0 ms 348 KB correct
60 Correct 159 ms 4248 KB correct
61 Correct 274 ms 6224 KB correct
62 Correct 292 ms 5980 KB correct
63 Correct 250 ms 6112 KB correct
64 Correct 242 ms 6232 KB correct
65 Correct 241 ms 6136 KB correct
66 Correct 279 ms 6228 KB correct
67 Correct 272 ms 5976 KB correct
68 Correct 268 ms 6128 KB correct
69 Correct 267 ms 5980 KB correct
70 Correct 0 ms 344 KB correct
71 Correct 269 ms 6388 KB correct
72 Correct 277 ms 6144 KB correct
73 Correct 1 ms 344 KB correct
74 Correct 288 ms 5900 KB correct
75 Correct 260 ms 5980 KB correct
76 Correct 97 ms 2392 KB correct
77 Correct 276 ms 6232 KB correct
78 Correct 265 ms 5976 KB correct
79 Correct 283 ms 5980 KB correct
80 Correct 274 ms 5976 KB correct
81 Correct 205 ms 5468 KB correct
82 Correct 267 ms 5980 KB correct
83 Correct 151 ms 3420 KB correct
84 Correct 106 ms 4272 KB correct
85 Correct 96 ms 3932 KB correct
86 Correct 64 ms 2752 KB correct
87 Correct 48 ms 2140 KB correct
88 Correct 37 ms 1628 KB correct
89 Correct 37 ms 1624 KB correct
90 Correct 32 ms 1368 KB correct
91 Correct 11 ms 800 KB correct
92 Correct 5 ms 344 KB correct
93 Correct 92 ms 3980 KB correct
94 Correct 63 ms 2740 KB correct
95 Correct 63 ms 2904 KB correct
96 Correct 34 ms 1368 KB correct
97 Correct 47 ms 1628 KB correct
98 Correct 46 ms 2136 KB correct
99 Correct 38 ms 1624 KB correct
100 Correct 15 ms 784 KB correct
101 Correct 7 ms 348 KB correct
102 Correct 98 ms 3108 KB correct
103 Correct 104 ms 3164 KB correct
104 Correct 98 ms 3164 KB correct
105 Correct 99 ms 3308 KB correct