Submission #108912

# Submission time Handle Problem Language Result Execution time Memory
108912 2019-05-02T15:06:42 Z Noam527 Simurgh (IOI17_simurgh) C++17
100 / 100
250 ms 7928 KB
#include <bits/stdc++.h>
#define CHECK cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7;
const ll inf = 1e18;
const int OO = 0;
const int OOO = 1;
using namespace std;

int count_common_roads(const vector<int>& r);

int ask(const vector<int> &r) {
	return count_common_roads(r);
}

struct edge {
	int to, i;
	edge() {}
	edge(int tt, int ii) {
		to = tt;
		i = ii;
	}
};
struct dsu {
	int n;
	vector<int> p, r;
	dsu() {}
	dsu(int sz) {
		n = sz;
		p.resize(n);
		r.resize(n);
		clear();
	}
	void clear() {
		for (int i = 0; i < n; i++) {
			p[i] = i;
			r[i] = 0;
		}
	}
	int find(int x) {
		return x == p[x] ? x : p[x] = find(p[x]);
	}
	bool unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y) return false;
		if (r[x] < r[y]) p[x] = y;
		else {
			p[y] = x;
			if (r[x] == r[y]) r[x]++;
		}
		return true;
	}
};

const int mxn = 505, mxm = 125000;

int n, m;
vector<edge> g[mxn], gg[mxn]; // in gg, only "directed" edges
int vis[mxn], par[mxn], pare[mxn];
int dep[mxn];
vector<int> tree;
int org;
int value[mxm];

void dfs(int v = 0, int d = 0) {
	vis[v] = 1;
	dep[v] = d;
	for (const auto &i : g[v])
		if (!vis[i.to]) {
			par[i.to] = v;
			pare[i.to] = i.i;
			tree[i.to - 1] = i.i;
			dfs(i.to, 1 + d);
		}
}

void process(pair<int, int> aa, int i) {
	int u = aa.first, v = aa.second, vv, tmp;
	if (dep[u] > dep[v]) swap(u, v);
	vv = v;
	if (tree[v - 1] == i) return;
	bool found = false;
	int good = -1;
	while (vv != u) {
		if (value[pare[vv]] == -1) {
			found = true;
			break;
		}
		vv = par[vv];
	}
	if (OO) cout << "processing edge " << u << " " << v << ", " << found << '\n';
	if (!found) return;
	vv = v;
	while (vv != u) {
		if (value[pare[vv]] != -1) {
			good = vv;
			break;
		}
		vv = par[vv];
	}
	int curedge;
	if (good == -1) {
		vv = v;
		found = false;
		while (vv != u) {
			swap(tree[vv - 1], i);
			int x = ask(tree);
			swap(tree[vv - 1], i);
			if (x != org) {
				found = true;
				if (x < org) curedge = 0;
				else curedge = 1;
				break;
			}
			vv = par[vv];
		}
		if (!found) curedge = 0;
	}
	else {
		swap(tree[good - 1], i);
		int x = ask(tree);
		swap(tree[good - 1], i);
		if (x < org) curedge = 0;
		else if (x > org) curedge = 1;
		else curedge = value[pare[vv]];
	}
	if (OO) cout << "curedge " << curedge << '\n';
	vv = v;
	while (vv != u) {
		if (value[pare[vv]] == -1) {
			swap(tree[vv - 1], i);
			int x = ask(tree);
			swap(tree[vv - 1], i);
			if (x < org) value[pare[vv]] = 1;
			else if (x > org) value[pare[vv]] = 0;
			else value[pare[vv]] = curedge;
			if (OO) {
				cout << "so, value of " << pare[vv] << " is " << value[pare[vv]] << '\n';
			}
		}
		vv = par[vv];
	}
}

dsu ds;
vector<int> rr;

int query(int s, int pre) {
	ds.clear();
	int nxt = 0;
	for (int i = 0; i < pre; i++) {
		ds.unite(s, gg[s][i].to);
		rr[nxt++] = gg[s][i].i;
	}
	int ans = 0;
	for (int i = 1; i < n; i++)
		if (ds.unite(i, par[i])) {
			ans -= value[tree[i - 1]];
			rr[nxt++] = tree[i - 1];
		}
	//if (OO) cout << "query " << s << " " << pre << ", ans " << ans << '\n';
	return ans + ask(rr);
}

vector<int> calc(int s) {
	vector<int> rtn;
	int tot = query(s, gg[s].size());
	if (OO) cout << "calc " << s << ", tot " << tot << '\n';
	if (tot == 0) return{};
	int lo = 1, hi = (int)gg[s].size(), mid;
	for (int i = 1; i <= tot; i++) {
		hi = (int)gg[s].size();
		while (lo < hi) {
			mid = (lo + hi) / 2;
			if (query(s, mid) < i) lo = mid + 1;
			else hi = mid;
		}
		if (OO) cout << "edge " << i << " is " << s << ", " << gg[s][lo - 1].to << ", index " << gg[s][lo - 1].i << '\n';
		rtn.push_back(gg[s][lo - 1].i);
	}
	return rtn;
}

vector<int> find_roads(int N, vector<int> u, vector<int> v) {
	n = N;
	m = u.size();
	tree.resize(n - 1);
	ds = dsu(n);
	rr.resize(n - 1);
	for (int i = 0; i < m; i++) {
		g[u[i]].push_back(edge(v[i], i));
		g[v[i]].push_back(edge(u[i], i));
		gg[min(u[i], v[i])].push_back(edge(max(u[i], v[i]), i));
		value[i] = -1;
	}
	dfs();
	if (OO) {
		cout << "my spanning tree\n";
		for (const auto &i : tree) cout << i << " "; cout << '\n';
		cout << "par: ";
		for (const auto &i : tree) cout << par[i] << " "; cout << '\n';
		cout << "par: ";
		for (const auto &i : tree) cout << pare[i] << " "; cout << '\n';
	}
	org = ask(tree);
	for (int i = 0; i < m; i++) {
		process(make_pair(u[i], v[i]), i);
	}
	for (const auto &i : tree)
		if (value[i] == -1) value[i] = 1; // bridge
	if (OO) {
		cout << "my spanning tree\n";
		for (const auto &i : tree) cout << i << " "; cout << '\n';
		cout << "with values\n";
		for (const auto &i : tree) cout << value[i] << " "; cout << '\n';
	}

	vector<int> tmp, ans(m, 0);
	for (int i = 0; i < n; i++) {
		tmp = calc(i);
		for (const auto &j : tmp) ans[j] = 1;
	}
	tmp.clear();
	for (int i = 0; i < m; i++)
		if (ans[i])
			tmp.push_back(i);
	return tmp;
}

Compilation message

simurgh.cpp: In function 'void process(std::pair<int, int>, int)':
simurgh.cpp:80:39: warning: unused variable 'tmp' [-Wunused-variable]
  int u = aa.first, v = aa.second, vv, tmp;
                                       ^~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:201:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for (const auto &i : tree) cout << i << " "; cout << '\n';
   ^~~
simurgh.cpp:201:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for (const auto &i : tree) cout << i << " "; cout << '\n';
                                                ^~~~
simurgh.cpp:203:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for (const auto &i : tree) cout << par[i] << " "; cout << '\n';
   ^~~
simurgh.cpp:203:53: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for (const auto &i : tree) cout << par[i] << " "; cout << '\n';
                                                     ^~~~
simurgh.cpp:205:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for (const auto &i : tree) cout << pare[i] << " "; cout << '\n';
   ^~~
simurgh.cpp:205:54: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for (const auto &i : tree) cout << pare[i] << " "; cout << '\n';
                                                      ^~~~
simurgh.cpp:215:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for (const auto &i : tree) cout << i << " "; cout << '\n';
   ^~~
simurgh.cpp:215:48: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for (const auto &i : tree) cout << i << " "; cout << '\n';
                                                ^~~~
simurgh.cpp:217:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for (const auto &i : tree) cout << value[i] << " "; cout << '\n';
   ^~~
simurgh.cpp:217:55: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for (const auto &i : tree) cout << value[i] << " "; cout << '\n';
                                                       ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB correct
2 Correct 2 ms 384 KB correct
3 Correct 3 ms 384 KB correct
4 Correct 2 ms 384 KB correct
5 Correct 2 ms 384 KB correct
6 Correct 3 ms 384 KB correct
7 Correct 2 ms 384 KB correct
8 Correct 2 ms 384 KB correct
9 Correct 2 ms 384 KB correct
10 Correct 2 ms 384 KB correct
11 Correct 2 ms 384 KB correct
12 Correct 2 ms 384 KB correct
13 Correct 2 ms 384 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB correct
2 Correct 2 ms 384 KB correct
3 Correct 3 ms 384 KB correct
4 Correct 2 ms 384 KB correct
5 Correct 2 ms 384 KB correct
6 Correct 3 ms 384 KB correct
7 Correct 2 ms 384 KB correct
8 Correct 2 ms 384 KB correct
9 Correct 2 ms 384 KB correct
10 Correct 2 ms 384 KB correct
11 Correct 2 ms 384 KB correct
12 Correct 2 ms 384 KB correct
13 Correct 2 ms 384 KB correct
14 Correct 3 ms 384 KB correct
15 Correct 5 ms 384 KB correct
16 Correct 4 ms 512 KB correct
17 Correct 4 ms 384 KB correct
18 Correct 3 ms 384 KB correct
19 Correct 3 ms 384 KB correct
20 Correct 3 ms 384 KB correct
21 Correct 3 ms 384 KB correct
22 Correct 3 ms 384 KB correct
23 Correct 3 ms 384 KB correct
24 Correct 4 ms 384 KB correct
25 Correct 3 ms 384 KB correct
26 Correct 3 ms 384 KB correct
27 Correct 3 ms 384 KB correct
28 Correct 3 ms 384 KB correct
29 Correct 4 ms 384 KB correct
30 Correct 3 ms 384 KB correct
31 Correct 4 ms 384 KB correct
32 Correct 4 ms 384 KB correct
33 Correct 3 ms 512 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB correct
2 Correct 2 ms 384 KB correct
3 Correct 3 ms 384 KB correct
4 Correct 2 ms 384 KB correct
5 Correct 2 ms 384 KB correct
6 Correct 3 ms 384 KB correct
7 Correct 2 ms 384 KB correct
8 Correct 2 ms 384 KB correct
9 Correct 2 ms 384 KB correct
10 Correct 2 ms 384 KB correct
11 Correct 2 ms 384 KB correct
12 Correct 2 ms 384 KB correct
13 Correct 2 ms 384 KB correct
14 Correct 3 ms 384 KB correct
15 Correct 5 ms 384 KB correct
16 Correct 4 ms 512 KB correct
17 Correct 4 ms 384 KB correct
18 Correct 3 ms 384 KB correct
19 Correct 3 ms 384 KB correct
20 Correct 3 ms 384 KB correct
21 Correct 3 ms 384 KB correct
22 Correct 3 ms 384 KB correct
23 Correct 3 ms 384 KB correct
24 Correct 4 ms 384 KB correct
25 Correct 3 ms 384 KB correct
26 Correct 3 ms 384 KB correct
27 Correct 3 ms 384 KB correct
28 Correct 3 ms 384 KB correct
29 Correct 4 ms 384 KB correct
30 Correct 3 ms 384 KB correct
31 Correct 4 ms 384 KB correct
32 Correct 4 ms 384 KB correct
33 Correct 3 ms 512 KB correct
34 Correct 39 ms 1892 KB correct
35 Correct 34 ms 1912 KB correct
36 Correct 29 ms 1664 KB correct
37 Correct 9 ms 512 KB correct
38 Correct 36 ms 1920 KB correct
39 Correct 32 ms 1792 KB correct
40 Correct 36 ms 1664 KB correct
41 Correct 35 ms 1912 KB correct
42 Correct 44 ms 1892 KB correct
43 Correct 31 ms 1408 KB correct
44 Correct 23 ms 1024 KB correct
45 Correct 25 ms 1180 KB correct
46 Correct 21 ms 1024 KB correct
47 Correct 13 ms 768 KB correct
48 Correct 7 ms 384 KB correct
49 Correct 9 ms 512 KB correct
50 Correct 13 ms 768 KB correct
51 Correct 21 ms 1152 KB correct
52 Correct 20 ms 1024 KB correct
53 Correct 19 ms 1072 KB correct
54 Correct 32 ms 1408 KB correct
55 Correct 24 ms 1152 KB correct
56 Correct 25 ms 1152 KB correct
57 Correct 27 ms 1152 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB correct
2 Correct 2 ms 384 KB correct
3 Correct 121 ms 4956 KB correct
4 Correct 203 ms 7032 KB correct
5 Correct 225 ms 6848 KB correct
6 Correct 215 ms 6992 KB correct
7 Correct 224 ms 6904 KB correct
8 Correct 202 ms 6876 KB correct
9 Correct 250 ms 7032 KB correct
10 Correct 226 ms 6908 KB correct
11 Correct 242 ms 6904 KB correct
12 Correct 196 ms 7032 KB correct
13 Correct 2 ms 384 KB correct
14 Correct 212 ms 6980 KB correct
15 Correct 211 ms 7132 KB correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB correct
2 Correct 2 ms 384 KB correct
3 Correct 3 ms 384 KB correct
4 Correct 2 ms 384 KB correct
5 Correct 2 ms 384 KB correct
6 Correct 3 ms 384 KB correct
7 Correct 2 ms 384 KB correct
8 Correct 2 ms 384 KB correct
9 Correct 2 ms 384 KB correct
10 Correct 2 ms 384 KB correct
11 Correct 2 ms 384 KB correct
12 Correct 2 ms 384 KB correct
13 Correct 2 ms 384 KB correct
14 Correct 3 ms 384 KB correct
15 Correct 5 ms 384 KB correct
16 Correct 4 ms 512 KB correct
17 Correct 4 ms 384 KB correct
18 Correct 3 ms 384 KB correct
19 Correct 3 ms 384 KB correct
20 Correct 3 ms 384 KB correct
21 Correct 3 ms 384 KB correct
22 Correct 3 ms 384 KB correct
23 Correct 3 ms 384 KB correct
24 Correct 4 ms 384 KB correct
25 Correct 3 ms 384 KB correct
26 Correct 3 ms 384 KB correct
27 Correct 3 ms 384 KB correct
28 Correct 3 ms 384 KB correct
29 Correct 4 ms 384 KB correct
30 Correct 3 ms 384 KB correct
31 Correct 4 ms 384 KB correct
32 Correct 4 ms 384 KB correct
33 Correct 3 ms 512 KB correct
34 Correct 39 ms 1892 KB correct
35 Correct 34 ms 1912 KB correct
36 Correct 29 ms 1664 KB correct
37 Correct 9 ms 512 KB correct
38 Correct 36 ms 1920 KB correct
39 Correct 32 ms 1792 KB correct
40 Correct 36 ms 1664 KB correct
41 Correct 35 ms 1912 KB correct
42 Correct 44 ms 1892 KB correct
43 Correct 31 ms 1408 KB correct
44 Correct 23 ms 1024 KB correct
45 Correct 25 ms 1180 KB correct
46 Correct 21 ms 1024 KB correct
47 Correct 13 ms 768 KB correct
48 Correct 7 ms 384 KB correct
49 Correct 9 ms 512 KB correct
50 Correct 13 ms 768 KB correct
51 Correct 21 ms 1152 KB correct
52 Correct 20 ms 1024 KB correct
53 Correct 19 ms 1072 KB correct
54 Correct 32 ms 1408 KB correct
55 Correct 24 ms 1152 KB correct
56 Correct 25 ms 1152 KB correct
57 Correct 27 ms 1152 KB correct
58 Correct 2 ms 384 KB correct
59 Correct 2 ms 384 KB correct
60 Correct 121 ms 4956 KB correct
61 Correct 203 ms 7032 KB correct
62 Correct 225 ms 6848 KB correct
63 Correct 215 ms 6992 KB correct
64 Correct 224 ms 6904 KB correct
65 Correct 202 ms 6876 KB correct
66 Correct 250 ms 7032 KB correct
67 Correct 226 ms 6908 KB correct
68 Correct 242 ms 6904 KB correct
69 Correct 196 ms 7032 KB correct
70 Correct 2 ms 384 KB correct
71 Correct 212 ms 6980 KB correct
72 Correct 211 ms 7132 KB correct
73 Correct 2 ms 384 KB correct
74 Correct 241 ms 7012 KB correct
75 Correct 202 ms 7836 KB correct
76 Correct 95 ms 3320 KB correct
77 Correct 215 ms 7928 KB correct
78 Correct 230 ms 7800 KB correct
79 Correct 247 ms 7800 KB correct
80 Correct 243 ms 7876 KB correct
81 Correct 180 ms 7032 KB correct
82 Correct 220 ms 7672 KB correct
83 Correct 158 ms 4472 KB correct
84 Correct 139 ms 5372 KB correct
85 Correct 119 ms 4992 KB correct
86 Correct 94 ms 3320 KB correct
87 Correct 78 ms 2816 KB correct
88 Correct 80 ms 2108 KB correct
89 Correct 73 ms 1912 KB correct
90 Correct 70 ms 1828 KB correct
91 Correct 29 ms 640 KB correct
92 Correct 25 ms 384 KB correct
93 Correct 138 ms 5112 KB correct
94 Correct 97 ms 3320 KB correct
95 Correct 100 ms 3428 KB correct
96 Correct 69 ms 1920 KB correct
97 Correct 72 ms 2100 KB correct
98 Correct 81 ms 2816 KB correct
99 Correct 82 ms 2048 KB correct
100 Correct 50 ms 768 KB correct
101 Correct 21 ms 512 KB correct
102 Correct 142 ms 4188 KB correct
103 Correct 148 ms 4088 KB correct
104 Correct 124 ms 4252 KB correct
105 Correct 121 ms 4160 KB correct