답안 #419856

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
419856 2021-06-07T14:15:35 Z flappybird Simurgh (IOI17_simurgh) C++14
51 / 100
2966 ms 5688 KB
#include "simurgh.h"

#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef int ll;

#define MAX 1010
#define MAXS 10

ll N, M;
ll sp[MAX][MAXS];
ll depth[MAX];
vector<ll> adj[MAX], tree[MAX], back[MAX], ans[MAX];
vector<ll> query;
vector<ll> U, V;
ll vis[MAX];
bool ck[MAX];
ll isroyal[MAX];
ll treeres;
ll leaf[MAX];
ll adj2[MAX][MAX];
vector<ll> leafv;

void dfs(ll x = 0, ll p = -1, ll d = 0) {
	sp[x][0] = p;
	depth[x] = d;
	ll i;
	vis[x] = 1;
	for (i = 1; i < MAXS; i++) sp[x][i] = sp[sp[x][i - 1]][i - 1];
	for (auto v : adj[x]) if (!vis[v]) dfs(v, x, d + 1);
}
//{a, b}={c, d}인지 체크
bool chk(ll a, ll b, ll c, ll d) {
	if (a > b) swap(a, b);
	if (c > d) swap(c, d);
	return a == c && b == d;
}

ll getline(ll u, ll v) {
	ll i;
	for (i = 0; i < M; i++) if (chk(u, v, U[i], V[i])) return i;
	assert(0);
}

ll getline(ll i) {
	return getline(U[i], V[i]);
}

ll lca(ll u, ll v) {
	if (depth[u] != depth[v]) {
		if (depth[u] < depth[v]) swap(u, v);
		ll i;
		for (i = MAXS - 1; i >= 0; i--) if (depth[sp[u][i]] >= depth[v]) u = sp[u][i];
	}
	if (u == v) return u;
	ll i;
	for (i = MAXS - 1; i >= 0; i--) if (sp[u][i] != sp[v][i]) u = sp[u][i], v = sp[v][i];
	return sp[v][0];
}

ll dis(ll u, ll v) {
	return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}

void erase(vector<ll>& v, ll a) {
	ll i;
	for (i = 0; i < v.size(); i++) if (v[i] == a) break;
	v.erase((v.begin() + i));
}

ll p(ll r, ll x) {
	if (lca(r, x) == x) {
		for (auto v : adj[x]) {
			if (lca(v, r) == v && lca(x, v) == x) return v;
		}
	}
	else return sp[x][0];
}

ll a(ll i) {
	if (sp[U[i]][0] == V[i]) return U[i];
	else return V[i];
}

//v is parent
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
	ll cnt = 0;
	N = n, M = u.size();
	if (M == N - 1) {
		ll i;
		vector<ll> v;
		for (i = 0; i < M; i++) v.push_back(i);
		return v;
	}
	ll i;
	for (i = 0; i < M; i++) adj[u[i]].push_back(v[i]), adj[v[i]].push_back(u[i]);
	dfs();
	for (i = 0; i < M; i++) {
		if (lca(u[i], v[i]) == u[i]) swap(u[i], v[i]);
		if (sp[u[i]][0] == v[i]) tree[u[i]].push_back(v[i]), tree[v[i]].push_back(u[i]);
		else back[u[i]].push_back(v[i]), back[v[i]].push_back(u[i]);
	}
	U = u, V = v;
	for (i = 0; i < N; i++) {
		for (auto v : tree[i]) {
			if (i < v) query.push_back(getline(i, v));
		}
	}
	cnt++;
	assert(cnt <= 29998);
	treeres = count_common_roads(query);
	ll k, m;
	vector<ll> ans;
	for (k = 0; k < N; k++) isroyal[k] = -1;
	for (k = 0; k < N; k++) {
		for (m = 0; m < back[k].size(); m++) {
			ll v1 = k, v2 = back[k][m];
			ll l = lca(v1, v2);
			ll x = -1;
			ll c = 1;
			for (i = v1; i != l; i = sp[i][0]) {
				if (isroyal[i] != -1) x = i;
				else c = 0;
			}
			for (i = v2; i != l; i = sp[i][0]) {
				if (isroyal[i] != -1) x = i;
				else c = 0;
			}
			if (c) continue;
			if (x != -1) {
				query.push_back(getline(v1, v2));
				erase(query, getline(x, sp[x][0]));
				cnt++;
				assert(cnt <= 29998);
				ll q = count_common_roads(query);
				ll chk = 0;
				if (q + isroyal[x] - treeres) ans.push_back(getline(v1, v2)), chk = 1;
				adj2[v1][v2] = adj2[v2][v1] = 1;
				query.push_back(getline(x, sp[x][0]));
				for (i = v1; i != l; i = sp[i][0]) {
					if (isroyal[i] != -1) continue;
					erase(query, getline(i, sp[i][0]));
					cnt++;
					assert(cnt <= 29998);
					ll res = count_common_roads(query);
					if (treeres - (res - chk)) isroyal[i] = 1;
					else isroyal[i] = 0;
					query.push_back(getline(i, sp[i][0]));
				}
				for (i = v2; i != l; i = sp[i][0]) {
					if (isroyal[i] != -1) continue;
					erase(query, getline(i, sp[i][0]));
					cnt++;
					assert(cnt <= 29998);
					ll res = count_common_roads(query);
					if (treeres - (res - chk)) isroyal[i] = 1;
					else isroyal[i] = 0;
					query.push_back(getline(i, sp[i][0]));
				}
				erase(query, getline(v1, v2));
			}
			else {
				query.push_back(getline(v1, v2));
				map<ll, ll> mp;
				for (ll j = v1; (j != l); j = sp[j][0]) {
					ll e = getline(j, sp[j][0]);
					erase(query, e);
					cnt++;
					assert(cnt <= 29998);
					ll res = count_common_roads(query) - treeres;
					mp[j] = res;
					query.push_back(e);
				}
				for (ll j = v2; (j != l); j = sp[j][0]) {
					ll e = getline(j, sp[j][0]);
					erase(query, e);
					cnt++;
					assert(cnt <= 29998);
					ll res = count_common_roads(query) - treeres;
					mp[j] = res;
					query.push_back(e);
				}
				erase(query, getline(v1, v2));
				set<ll> st;
				for (ll j = v1; (j != l); j = sp[j][0]) st.insert(mp[j]);
				for (ll j = v2; (j != l); j = sp[j][0]) st.insert(mp[j]);
				if (st.size() == 1) {
					if (*st.begin() >= 0) {
						for (ll j = v1; (j != l); j = sp[j][0]) isroyal[j] = 0;
						for (ll j = v2; (j != l); j = sp[j][0]) isroyal[j] = 0;
					}
					else {
						for (ll j = v1; (j != l); j = sp[j][0]) isroyal[j] = 1;
						for (ll j = v2; (j != l); j = sp[j][0]) isroyal[j] = 1;
					}
				}
				else {
					if (*st.begin() == -1) {
						for (ll j = v1; (j != l); j = sp[j][0]) isroyal[j] = -mp[j];
						for (ll j = v2; (j != l); j = sp[j][0]) isroyal[j] = -mp[j];
					}
					else {
						for (ll j = v1; (j != l); j = sp[j][0]) isroyal[j] = 1 - mp[j];
						for (ll j = v2; (j != l); j = sp[j][0]) isroyal[j] = 1 - mp[j];
					}
				}
			}
		}
	};
	for (i = 1; i < N; i++) if (isroyal[i]) isroyal[i] = 1;
	//assert(cnt <= 3 * N);
	for (i = 1; i < N; i++) if (isroyal[i] == 1) ans.push_back(getline(i, sp[i][0]));
	for (i = 0; i < N; i++) {
		for (auto x : adj[i]) {
			if (p(i, x) == i) continue;
			if (adj2[x][i]) continue;
			erase(query, getline(x, p(i, x)));
			ll e = getline(i, x);
			query.push_back(e);
			cnt++;
			assert(cnt <= 29998);
			ll res = count_common_roads(query) + isroyal[a(getline(x, p(i, x)))] - treeres;
			query.push_back(getline(x, p(i, x)));
			erase(query, e);
			adj2[i][x] = adj2[x][i] = 1;
			if (res == 1) ans.push_back(e);
		}
	}
	sort(ans.begin(), ans.end());
	ans.erase(unique(ans.begin(), ans.end()), ans.end());
	assert(ans.size() == N - 1);
	return ans;
}

Compilation message

simurgh.cpp: In function 'void erase(std::vector<int>&, ll)':
simurgh.cpp:72:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |  for (i = 0; i < v.size(); i++) if (v[i] == a) break;
      |              ~~^~~~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:121:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |   for (m = 0; m < back[k].size(); m++) {
      |               ~~^~~~~~~~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from simurgh.cpp:4:
simurgh.cpp:236:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'int'} [-Wsign-compare]
  236 |  assert(ans.size() == N - 1);
      |         ~~~~~~~~~~~^~~~~~~~
simurgh.cpp: In function 'll p(ll, ll)':
simurgh.cpp:83:1: warning: control reaches end of non-void function [-Wreturn-type]
   83 | }
      | ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Correct 1 ms 332 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Correct 1 ms 332 KB correct
14 Correct 7 ms 668 KB correct
15 Correct 7 ms 588 KB correct
16 Correct 7 ms 672 KB correct
17 Correct 5 ms 588 KB correct
18 Correct 2 ms 588 KB correct
19 Correct 5 ms 588 KB correct
20 Correct 5 ms 548 KB correct
21 Correct 5 ms 588 KB correct
22 Correct 5 ms 588 KB correct
23 Correct 3 ms 588 KB correct
24 Correct 3 ms 588 KB correct
25 Correct 1 ms 588 KB correct
26 Correct 3 ms 588 KB correct
27 Correct 3 ms 588 KB correct
28 Correct 2 ms 588 KB correct
29 Correct 1 ms 588 KB correct
30 Correct 3 ms 588 KB correct
31 Correct 3 ms 588 KB correct
32 Correct 3 ms 588 KB correct
33 Correct 3 ms 644 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Correct 1 ms 332 KB correct
14 Correct 7 ms 668 KB correct
15 Correct 7 ms 588 KB correct
16 Correct 7 ms 672 KB correct
17 Correct 5 ms 588 KB correct
18 Correct 2 ms 588 KB correct
19 Correct 5 ms 588 KB correct
20 Correct 5 ms 548 KB correct
21 Correct 5 ms 588 KB correct
22 Correct 5 ms 588 KB correct
23 Correct 3 ms 588 KB correct
24 Correct 3 ms 588 KB correct
25 Correct 1 ms 588 KB correct
26 Correct 3 ms 588 KB correct
27 Correct 3 ms 588 KB correct
28 Correct 2 ms 588 KB correct
29 Correct 1 ms 588 KB correct
30 Correct 3 ms 588 KB correct
31 Correct 3 ms 588 KB correct
32 Correct 3 ms 588 KB correct
33 Correct 3 ms 644 KB correct
34 Correct 2535 ms 2800 KB correct
35 Correct 2333 ms 2772 KB correct
36 Correct 1146 ms 2504 KB correct
37 Correct 14 ms 1356 KB correct
38 Correct 2503 ms 2904 KB correct
39 Correct 1758 ms 2672 KB correct
40 Correct 1103 ms 2500 KB correct
41 Correct 2471 ms 2884 KB correct
42 Correct 2364 ms 2796 KB correct
43 Correct 740 ms 2292 KB correct
44 Correct 466 ms 2116 KB correct
45 Correct 639 ms 2076 KB correct
46 Correct 388 ms 1988 KB correct
47 Correct 85 ms 1656 KB correct
48 Correct 5 ms 1356 KB correct
49 Correct 15 ms 1468 KB correct
50 Correct 84 ms 1772 KB correct
51 Correct 643 ms 2076 KB correct
52 Correct 455 ms 1956 KB correct
53 Correct 369 ms 2068 KB correct
54 Correct 726 ms 2404 KB correct
55 Correct 611 ms 2120 KB correct
56 Correct 614 ms 2116 KB correct
57 Correct 585 ms 2124 KB correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Incorrect 2966 ms 5688 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB correct
2 Correct 1 ms 332 KB correct
3 Correct 1 ms 332 KB correct
4 Correct 1 ms 332 KB correct
5 Correct 1 ms 332 KB correct
6 Correct 1 ms 332 KB correct
7 Correct 1 ms 332 KB correct
8 Correct 1 ms 332 KB correct
9 Correct 1 ms 332 KB correct
10 Correct 1 ms 332 KB correct
11 Correct 1 ms 332 KB correct
12 Correct 1 ms 332 KB correct
13 Correct 1 ms 332 KB correct
14 Correct 7 ms 668 KB correct
15 Correct 7 ms 588 KB correct
16 Correct 7 ms 672 KB correct
17 Correct 5 ms 588 KB correct
18 Correct 2 ms 588 KB correct
19 Correct 5 ms 588 KB correct
20 Correct 5 ms 548 KB correct
21 Correct 5 ms 588 KB correct
22 Correct 5 ms 588 KB correct
23 Correct 3 ms 588 KB correct
24 Correct 3 ms 588 KB correct
25 Correct 1 ms 588 KB correct
26 Correct 3 ms 588 KB correct
27 Correct 3 ms 588 KB correct
28 Correct 2 ms 588 KB correct
29 Correct 1 ms 588 KB correct
30 Correct 3 ms 588 KB correct
31 Correct 3 ms 588 KB correct
32 Correct 3 ms 588 KB correct
33 Correct 3 ms 644 KB correct
34 Correct 2535 ms 2800 KB correct
35 Correct 2333 ms 2772 KB correct
36 Correct 1146 ms 2504 KB correct
37 Correct 14 ms 1356 KB correct
38 Correct 2503 ms 2904 KB correct
39 Correct 1758 ms 2672 KB correct
40 Correct 1103 ms 2500 KB correct
41 Correct 2471 ms 2884 KB correct
42 Correct 2364 ms 2796 KB correct
43 Correct 740 ms 2292 KB correct
44 Correct 466 ms 2116 KB correct
45 Correct 639 ms 2076 KB correct
46 Correct 388 ms 1988 KB correct
47 Correct 85 ms 1656 KB correct
48 Correct 5 ms 1356 KB correct
49 Correct 15 ms 1468 KB correct
50 Correct 84 ms 1772 KB correct
51 Correct 643 ms 2076 KB correct
52 Correct 455 ms 1956 KB correct
53 Correct 369 ms 2068 KB correct
54 Correct 726 ms 2404 KB correct
55 Correct 611 ms 2120 KB correct
56 Correct 614 ms 2116 KB correct
57 Correct 585 ms 2124 KB correct
58 Correct 1 ms 332 KB correct
59 Correct 1 ms 332 KB correct
60 Incorrect 2966 ms 5688 KB WA in grader: NO
61 Halted 0 ms 0 KB -