Submission #960979

# Submission time Handle Problem Language Result Execution time Memory
960979 2024-04-11T10:29:45 Z Trisanu_Das Simurgh (IOI17_simurgh) C++17
70 / 100
178 ms 7116 KB
#include "bits/stdc++.h"
#include "simurgh.h"
using namespace std;
using ll = long long;
#define pb push_back
#define sz(v) ((int)v.size())
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
int p[500];
int getp(int x) { return p[x] = (x == p[x] ? x : getp(p[x])); }
int uni(int a, int b) {
	a = getp(a);
	b = getp(b);
	if (a == b) return 0;
	p[a] = b;
	return 1;
}
 
const int N = 500;
vector<array<int, 2>> adj[N];
 
vector<int>
dosp(int n, vector<int> u, vector<int> v) {
	int m = n*(n-1)/2;
	int x, y, z;
	vector<int> edg(n), d(n), r(n-1), ans, t(m, 0);
	vector<array<int, 2>> ak;
	for (int i = 0; i < m; ++i) {
		adj[u[i]].pb({v[i],i});
		adj[v[i]].pb({u[i],i});
	}
 
	int sp = 0;
	for (int c = 0; c < n; ++c) {
		r.clear();
		for (auto x : adj[c])
			r.pb(x[1]);
		d[c] = count_common_roads(r);
		if (d[c] == 1) sp = c;
	}
 
	r.clear();
	x = 1;
	if (sp) x = 0;
 
	for (auto u : adj[x])
		if (u[0] != sp) r.pb(u[1]);
 
	r.pb(0);
	x = 0; z = 0;
	for (auto u : adj[sp]) {
		r[n-2] = u[1];
		edg[u[0]] = u[1];
		y = count_common_roads(r);
		if (y > x) {
			x = y;
			z = u[1];
		}
	}
 
	t[z] = 1;
	ans.pb(z);
	r.clear();
 
	for (int i = 0; i < n; ++i) {
		if (i == sp) continue;
		if (sz(ans) == n-1) break;
		y = d[i];
		r.clear();
		ak.clear();
		z = 0;
		for (auto u : adj[i]) {
			if (t[u[1]]) {
				--y;
				++z;
				r.pb(u[1]);
			} else if (u[0] == sp) {
				r.pb(u[1]);
			} else ak.pb(u);
		}
 
		if (y == 0) continue;
		int lp = 0; int rp = sz(ak)-1;
		int uz = 0;
		
		while (lp < rp) {
			int m = (lp+rp)/2;
			uz = 0;
			for (int j = 0; j < sz(ak); ++j) {
				if(j < lp || j > m) {
					if (t[edg[ak[j][0]]])
						++uz;
					r.pb(edg[ak[j][0]]);
				}
				else
					r.pb(ak[j][1]);
			}
 
			int vd = count_common_roads(r);
			if (vd > uz+z)
				rp = m;
			else
				lp = m+1;
 
			for (int j = 0; j < sz(ak); ++j)
				r.pop_back();
		}
 
		t[ak[lp][1]] = 1;
		ans.pb(ak[lp][1]);
		if (y > 1) --i;
	}
 
	return ans;
}
 
vector<int>
find_roads(int n, vector<int> u, vector<int> v) {
	int m = sz(u);
	if (m == n*(n-1)/2)
		return dosp(n, u, v);
	vector<int> ans, r, us(m, 0), z, order(m);
	iota(order.begin(), order.end(), 0);
	shuffle(order.begin(), order.end(), rng);
	for (int i = 0; i < n-1; ++i) {
		for (int i = 0; i < n; ++i) p[i] = i;
		int c = 0;
		shuffle(order.begin(), order.end(), rng);
		if (sz(ans) == n-1) break;
		r = ans;
		for (auto x : ans) {
			c += uni(u[x], v[x]);
		}
 
		int p = 0;
		while (c < n-2) {
			if (uni(u[order[p]], v[order[p]])) {
				++c;
				r.pb(order[p]);
			}
			++p;
		}
 
		while (us[order[p]] || getp(u[order[p]]) == getp(v[order[p]]))
			++p;
		r.pb(order[p]);
		z.clear();
		z.pb(order[p]);
		assert(sz(r) == n-1);
		int x = count_common_roads(r);
 
		for (int j = p+1; j < m; ++j) {
			if(us[order[j]]) continue;
			if (getp(u[order[j]]) == getp(v[order[j]])) continue;
			r[n-2] = order[j];
			us[order[j]] = 1;
			int l = count_common_roads(r);
			if (l == x) {
				z.pb(order[j]);
			} else if (l < x) {
				break;
			} else if (l > x) {
				z.clear(); z.pb(order[j]);
				break;
			}
		}
 
		for (auto j : z)
			ans.pb(j);
	}
 
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 500 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 1 ms 344 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 1 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 1 ms 456 KB correct
11 Correct 1 ms 344 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 500 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 1 ms 344 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 1 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 1 ms 456 KB correct
11 Correct 1 ms 344 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 2 ms 348 KB correct
18 Correct 1 ms 344 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 3 ms 348 KB correct
21 Correct 2 ms 344 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 344 KB correct
24 Correct 2 ms 348 KB correct
25 Correct 1 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 456 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 2 ms 348 KB correct
31 Correct 1 ms 456 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 460 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 500 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 1 ms 344 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 1 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 1 ms 456 KB correct
11 Correct 1 ms 344 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 2 ms 348 KB correct
18 Correct 1 ms 344 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 3 ms 348 KB correct
21 Correct 2 ms 344 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 344 KB correct
24 Correct 2 ms 348 KB correct
25 Correct 1 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 456 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 2 ms 348 KB correct
31 Correct 1 ms 456 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 460 KB correct
34 Correct 13 ms 2036 KB correct
35 Correct 175 ms 1280 KB correct
36 Correct 114 ms 1016 KB correct
37 Correct 12 ms 348 KB correct
38 Correct 177 ms 1312 KB correct
39 Correct 158 ms 1112 KB correct
40 Correct 130 ms 1036 KB correct
41 Correct 12 ms 1880 KB correct
42 Correct 178 ms 1304 KB correct
43 Correct 88 ms 896 KB correct
44 Correct 75 ms 808 KB correct
45 Correct 89 ms 1112 KB correct
46 Correct 66 ms 600 KB correct
47 Correct 30 ms 600 KB correct
48 Correct 5 ms 348 KB correct
49 Correct 10 ms 348 KB correct
50 Correct 33 ms 604 KB correct
51 Correct 87 ms 856 KB correct
52 Correct 70 ms 808 KB correct
53 Correct 62 ms 756 KB correct
54 Correct 90 ms 896 KB correct
55 Correct 84 ms 884 KB correct
56 Correct 85 ms 856 KB correct
57 Correct 84 ms 856 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 42 ms 5132 KB correct
4 Correct 71 ms 7004 KB correct
5 Correct 56 ms 6876 KB correct
6 Correct 53 ms 7000 KB correct
7 Correct 53 ms 7020 KB correct
8 Correct 57 ms 7116 KB correct
9 Correct 57 ms 7000 KB correct
10 Correct 57 ms 7100 KB correct
11 Correct 56 ms 7000 KB correct
12 Correct 58 ms 7108 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 66 ms 7004 KB correct
15 Correct 55 ms 7004 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 500 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 1 ms 344 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 1 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 1 ms 456 KB correct
11 Correct 1 ms 344 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 2 ms 348 KB correct
18 Correct 1 ms 344 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 3 ms 348 KB correct
21 Correct 2 ms 344 KB correct
22 Correct 1 ms 348 KB correct
23 Correct 1 ms 344 KB correct
24 Correct 2 ms 348 KB correct
25 Correct 1 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 1 ms 456 KB correct
29 Correct 1 ms 348 KB correct
30 Correct 2 ms 348 KB correct
31 Correct 1 ms 456 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 460 KB correct
34 Correct 13 ms 2036 KB correct
35 Correct 175 ms 1280 KB correct
36 Correct 114 ms 1016 KB correct
37 Correct 12 ms 348 KB correct
38 Correct 177 ms 1312 KB correct
39 Correct 158 ms 1112 KB correct
40 Correct 130 ms 1036 KB correct
41 Correct 12 ms 1880 KB correct
42 Correct 178 ms 1304 KB correct
43 Correct 88 ms 896 KB correct
44 Correct 75 ms 808 KB correct
45 Correct 89 ms 1112 KB correct
46 Correct 66 ms 600 KB correct
47 Correct 30 ms 600 KB correct
48 Correct 5 ms 348 KB correct
49 Correct 10 ms 348 KB correct
50 Correct 33 ms 604 KB correct
51 Correct 87 ms 856 KB correct
52 Correct 70 ms 808 KB correct
53 Correct 62 ms 756 KB correct
54 Correct 90 ms 896 KB correct
55 Correct 84 ms 884 KB correct
56 Correct 85 ms 856 KB correct
57 Correct 84 ms 856 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 0 ms 348 KB correct
60 Correct 42 ms 5132 KB correct
61 Correct 71 ms 7004 KB correct
62 Correct 56 ms 6876 KB correct
63 Correct 53 ms 7000 KB correct
64 Correct 53 ms 7020 KB correct
65 Correct 57 ms 7116 KB correct
66 Correct 57 ms 7000 KB correct
67 Correct 57 ms 7100 KB correct
68 Correct 56 ms 7000 KB correct
69 Correct 58 ms 7108 KB correct
70 Correct 0 ms 348 KB correct
71 Correct 66 ms 7004 KB correct
72 Correct 55 ms 7004 KB correct
73 Correct 1 ms 344 KB correct
74 Correct 56 ms 7004 KB correct
75 Incorrect 173 ms 4196 KB WA in grader: NO
76 Halted 0 ms 0 KB -