Submission #432068

# Submission time Handle Problem Language Result Execution time Memory
432068 2021-06-17T20:15:44 Z rainboy Simurgh (IOI17_simurgh) C++
51 / 100
122 ms 3888 KB
#include "simurgh.h"
#include <stdlib.h>
#include <string.h>

using namespace std;

#define N	240
#define M	(N * (N - 1) / 2)

typedef vector<int> vi;

int ds[N], n;

int find(int i) {
	return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}

int join(int i, int j) {
	i = find(i);
	j = find(j);
	if (i == j)
		return 0;
	if (ds[i] > ds[j])
		ds[i] = j;
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i;
	}
	return 1;
}

int ii[M], jj[M];
int *eh[N], eo[N];

void append(int i, int h) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
	eh[i][o] = h;
}

char cc[M], marked[M];

int dfs(int p, int i, int t) {
	int o;

	if (i == t)
		return 1;
	for (o = eo[i]; o--; ) {
		int h = eh[i][o], j = i ^ ii[h] ^ jj[h];

		if (j != p && dfs(i, j, t)) {
			marked[h] = 1;
			return 1;
		}
	}
	return 0;
}

vi hh, hh_;
int k_;

int count(int i, int m) {
	int g, m_, k;

	memset(ds, -1, n * sizeof *ds);
	m_ = 0;
	for (g = 0; g < m; g++) {
		int h = eh[i][g];

		join(ii[h], jj[h]);
		hh_[m_++] = h;
	}
	k = 0;
	for (g = 0; g < n - 1; g++)
		if (join(ii[hh[g]], jj[hh[g]]))
			hh_[m_++] = hh[g];
		else if (cc[hh[g]])
			k++;
	return count_common_roads(hh_) - k_ + k;
}

void solve(int i, int l, int r, int a, int b) {
	int m, c;

	if (a == b)
		return;
	if (r - l == 1) {
		cc[eh[i][l]] = 1;
		return;
	}
	m = (l + r) / 2;
	c = count(i, m);
	solve(i, l, m, a, c), solve(i, m, r, c, b);
}

vi find_roads(int n_, vi ii_, vi jj_) {
	int m = ii_.size(), m_, g, h, i;

	n = n_;
	for (h = 0; h < m; h++)
		ii[h] = ii_[h], jj[h] = jj_[h];
	hh.resize(n - 1), hh_.resize(n - 1);
	memset(ds, -1, n * sizeof *ds);
	for (i = 0; i < n; i++)
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
	m_ = 0;
	for (h = 0; h < m; h++)
		if (join(ii[h], jj[h])) {
			hh[m_++] = h, cc[h] = -1;
			append(ii[h], h), append(jj[h], h);
		} else
			cc[h] = -2;
	k_ = count_common_roads(hh);
	for (h = 0; h < m; h++) {
		int upd, c, tmp, k;

		if (cc[h] != -2)
			continue;
		for (g = 0; g < n - 1; g++)
			marked[hh[g]] = 0;
		dfs(-1, ii[h], jj[h]);
		upd = 0;
		for (g = 0; g < n - 1; g++)
			if (marked[hh[g]] && cc[hh[g]] == -1) {
				upd = 1;
				break;
			}
		if (!upd)
			continue;
		c = -1;
		for (g = 0; g < n - 1; g++)
			if (marked[hh[g]] && cc[hh[g]] != -1) {
				tmp = hh[g], hh[g] = h, k = count_common_roads(hh), hh[g] = tmp;
				c = cc[hh[g]] ^ (k == k_ ? 0 : 1);
				break;
			}
		for (g = 0; g < n - 1; g++)
			if (marked[hh[g]] && cc[hh[g]] == -1) {
				tmp = hh[g], hh[g] = h, k = count_common_roads(hh), hh[g] = tmp;
				if (k == k_ + 1)
					c = 1;
				else if (k == k_ - 1)
					c = 0;
				if (c != -1)
					cc[hh[g]] = c ^ (k == k_ ? 0 : 1);
			}
		if (c == -1)
			c = 0;
		for (g = 0; g < n - 1; g++)
			if (marked[hh[g]] && cc[hh[g]] == -1)
				cc[hh[g]] = c;
	}
	for (h = 0; h < m; h++)
		if (cc[h] == -1)
			cc[h] = 1;
		else if (cc[h] == -2)
			append(ii[h], h), append(jj[h], h);
	for (i = 0; i < n; i++) {
		int o, o_;

		for (o = 0, o_ = 0; o < eo[i]; o++) {
			h = eh[i][o];
			if (cc[h] == -2)
				cc[h] = 0, eh[i][o_++] = h;
		}
		eo[i] = o_;
		solve(i, 0, eo[i], 0, count(i, eo[i]));
	}
	m_ = 0;
	for (h = 0; h < m; h++)
		if (cc[h] == 1)
			hh[m_++] = h;
	return hh;
}

Compilation message

simurgh.cpp: In function 'void append(int, int)':
simurgh.cpp:39:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   39 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB correct
2 Correct 0 ms 204 KB correct
3 Correct 1 ms 280 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 0 ms 204 KB correct
7 Correct 1 ms 204 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 1 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 236 KB correct
13 Correct 0 ms 204 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB correct
2 Correct 0 ms 204 KB correct
3 Correct 1 ms 280 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 0 ms 204 KB correct
7 Correct 1 ms 204 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 1 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 236 KB correct
13 Correct 0 ms 204 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 4 ms 332 KB correct
17 Correct 3 ms 332 KB correct
18 Correct 1 ms 204 KB correct
19 Correct 4 ms 332 KB correct
20 Correct 2 ms 284 KB correct
21 Correct 2 ms 348 KB correct
22 Correct 1 ms 332 KB correct
23 Correct 1 ms 332 KB correct
24 Correct 2 ms 204 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 204 KB correct
29 Correct 1 ms 204 KB correct
30 Correct 1 ms 332 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 280 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB correct
2 Correct 0 ms 204 KB correct
3 Correct 1 ms 280 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 0 ms 204 KB correct
7 Correct 1 ms 204 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 1 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 236 KB correct
13 Correct 0 ms 204 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 4 ms 332 KB correct
17 Correct 3 ms 332 KB correct
18 Correct 1 ms 204 KB correct
19 Correct 4 ms 332 KB correct
20 Correct 2 ms 284 KB correct
21 Correct 2 ms 348 KB correct
22 Correct 1 ms 332 KB correct
23 Correct 1 ms 332 KB correct
24 Correct 2 ms 204 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 204 KB correct
29 Correct 1 ms 204 KB correct
30 Correct 1 ms 332 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 280 KB correct
34 Correct 118 ms 1472 KB correct
35 Correct 109 ms 1428 KB correct
36 Correct 84 ms 1148 KB correct
37 Correct 12 ms 332 KB correct
38 Correct 122 ms 1472 KB correct
39 Correct 96 ms 1336 KB correct
40 Correct 77 ms 1144 KB correct
41 Correct 119 ms 1468 KB correct
42 Correct 122 ms 1428 KB correct
43 Correct 53 ms 1012 KB correct
44 Correct 41 ms 824 KB correct
45 Correct 50 ms 884 KB correct
46 Correct 37 ms 764 KB correct
47 Correct 18 ms 460 KB correct
48 Correct 5 ms 324 KB correct
49 Correct 7 ms 388 KB correct
50 Correct 18 ms 536 KB correct
51 Correct 49 ms 868 KB correct
52 Correct 44 ms 828 KB correct
53 Correct 42 ms 772 KB correct
54 Correct 47 ms 964 KB correct
55 Correct 48 ms 892 KB correct
56 Correct 49 ms 872 KB correct
57 Correct 51 ms 892 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB correct
2 Correct 1 ms 204 KB correct
3 Runtime error 19 ms 3888 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB correct
2 Correct 0 ms 204 KB correct
3 Correct 1 ms 280 KB correct
4 Correct 0 ms 204 KB correct
5 Correct 1 ms 204 KB correct
6 Correct 0 ms 204 KB correct
7 Correct 1 ms 204 KB correct
8 Correct 1 ms 204 KB correct
9 Correct 1 ms 204 KB correct
10 Correct 1 ms 204 KB correct
11 Correct 1 ms 204 KB correct
12 Correct 1 ms 236 KB correct
13 Correct 0 ms 204 KB correct
14 Correct 2 ms 332 KB correct
15 Correct 2 ms 332 KB correct
16 Correct 4 ms 332 KB correct
17 Correct 3 ms 332 KB correct
18 Correct 1 ms 204 KB correct
19 Correct 4 ms 332 KB correct
20 Correct 2 ms 284 KB correct
21 Correct 2 ms 348 KB correct
22 Correct 1 ms 332 KB correct
23 Correct 1 ms 332 KB correct
24 Correct 2 ms 204 KB correct
25 Correct 1 ms 204 KB correct
26 Correct 1 ms 332 KB correct
27 Correct 1 ms 332 KB correct
28 Correct 1 ms 204 KB correct
29 Correct 1 ms 204 KB correct
30 Correct 1 ms 332 KB correct
31 Correct 1 ms 332 KB correct
32 Correct 1 ms 332 KB correct
33 Correct 1 ms 280 KB correct
34 Correct 118 ms 1472 KB correct
35 Correct 109 ms 1428 KB correct
36 Correct 84 ms 1148 KB correct
37 Correct 12 ms 332 KB correct
38 Correct 122 ms 1472 KB correct
39 Correct 96 ms 1336 KB correct
40 Correct 77 ms 1144 KB correct
41 Correct 119 ms 1468 KB correct
42 Correct 122 ms 1428 KB correct
43 Correct 53 ms 1012 KB correct
44 Correct 41 ms 824 KB correct
45 Correct 50 ms 884 KB correct
46 Correct 37 ms 764 KB correct
47 Correct 18 ms 460 KB correct
48 Correct 5 ms 324 KB correct
49 Correct 7 ms 388 KB correct
50 Correct 18 ms 536 KB correct
51 Correct 49 ms 868 KB correct
52 Correct 44 ms 828 KB correct
53 Correct 42 ms 772 KB correct
54 Correct 47 ms 964 KB correct
55 Correct 48 ms 892 KB correct
56 Correct 49 ms 872 KB correct
57 Correct 51 ms 892 KB correct
58 Correct 1 ms 204 KB correct
59 Correct 1 ms 204 KB correct
60 Runtime error 19 ms 3888 KB Execution killed with signal 11
61 Halted 0 ms 0 KB -