# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
978206 |
2024-05-09T03:18:21 Z |
model_code |
Spy 3 (JOI24_spy3) |
C++17 |
|
81 ms |
7144 KB |
#include "Aoi.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
namespace {
const ll linf = 1LL << 60;
string encode(ll num, int len) {
string s;
for (int i = len - 1; i >= 0; i--) s.push_back('0' + (num >> i & 1));
return s;
}
// {dist, pre}
pair<vector<ll>, vector<int>> dijkstra(int n, const vector<int> &a, const vector<int> &b, const vector<ll> &c) {
vector<vector<tuple<int, ll, int>>> G(n);
for (int i = 0; i < a.size(); i++) {
if (c[i] == -1) continue;
G[a[i]].emplace_back(b[i], c[i], i);
G[b[i]].emplace_back(a[i], c[i], i);
}
vector<ll> dist(n, linf);
vector<int> pre(n, -1);
priority_queue<pair<ll, int>, vector<pair<ll, int >>, greater<>> pq;
dist[0] = 0;
pq.emplace(0, 0);
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d > dist[u]) continue;
for (auto [v, len, id]: G[u]) {
if (dist[v] > d + len) {
dist[v] = d + len;
pre[v] = id;
pq.emplace(dist[v], v);
}
}
}
return {dist, pre};
}
}
std::string aoi(int n, int m, int q, int k, std::vector<int> a,
std::vector<int> b, std::vector<long long> c,
std::vector<int> t, std::vector<int> x) {
vector<int> x_id(m, -1);
for (int i = 0; i < k; i++) x_id[x[i]] = i;
auto [dist, pre] = dijkstra(n, a, b, c);
vector<vector<int>> G(n + q); // 最çŸè·¯æœ¨
for (int i = 1; i < n; i++) {
G[a[pre[i]] ^ b[pre[i]] ^ i].push_back(i);
}
vector<int> bit(n); // bit[i] : é ‚ç‚¹ i ã® subtree ã«å«ã¾ã‚Œã‚‹ t[j] ãŸã¡ã®é›†åˆã‚’ 0 ~ (1<<q)-1 ã§
for (int i = 0; i < q; i++) bit[t[i]] = 1 << i;
auto dfs = [&](auto &dfs, int u) -> void {
for (int v: G[u]) {
dfs(dfs, v);
bit[u] |= bit[v];
}
};
dfs(dfs, 0);
vector<int> s(k);
for (int i = 0; i < k; i++) {
int e = x[i];
if (dist[a[e]] > dist[b[e]]) swap(a[e], b[e]);
if (pre[b[e]] == e) {
s[i] = bit[b[e]];
}
}
sort(bit.begin(), bit.end());
bit.erase(unique(bit.begin(), bit.end()), bit.end());
// è¦ç´ æ•°ã®æ˜‡é †ã«ã‚½ãƒ¼ãƒˆ
sort(bit.begin(), bit.end(), [](int a, int b) { return __builtin_popcount(a) < __builtin_popcount(b); });
vector<pair<int, int>> div(1 << q); // div[i] : é›†åˆ i ã¯ä½•ã¨ä½•ã«åˆ†å‰²ã•ã‚Œã‚‹ã‹
{
set<int> st;
for (int i = 0; i < q; i++) {
st.insert(1 << i);
}
for (int i: bit) {
if (!i) continue;
vector<int> mg;
for (int j: st) if ((j & i) == j) mg.push_back(j);
for (int j = 1; j < mg.size(); j++) {
int now = mg[j - 1] | mg[j];
div[now] = {mg[j - 1], mg[j]};
st.erase(mg[j - 1]);
st.erase(mg[j]);
st.insert(now);
mg[j] = now;
}
}
}
vector binom(q, vector<ll>(q));
for (int i = 0; i < q; i++) {
binom[i][0] = 1;
for (int j = 1; j <= i; j++) {
binom[i][j] = binom[i - 1][j - 1] + binom[i - 1][j];
}
}
vector<ll> dp(q + 1); // dp[i] : {1, 2, ..., i} ã‚’ã„ãã¤ã‹ã®é›†åˆã«åˆ†å‰²ã™ã‚‹æ–¹æ³•ã®æ•°
dp[1] = 1;
for (int i = 2; i <= q; i++) {
for (int j = 1; j < i; j++) {
dp[i] += binom[i - 1][j - 1] * dp[j] * dp[i - j];
}
}
vector<int> ls = {0};
auto f = [&](auto &f, int now) -> ll {
ls.push_back(now);
int cnt = __builtin_popcount(now);
if (cnt == 1) return 0;
vector<int> fac;
for (int i = 0; i < q; i++) if (now >> i & 1) fac.push_back(i);
auto [l, r] = div[now];
if (~l >> fac[0] & 1) swap(l, r);
int lc = __builtin_popcount(l);
int rc = __builtin_popcount(r);
ll res = 0;
for (int i = 1; i < lc; i++) {
res += binom[cnt - 1][i - 1] * dp[i] * dp[cnt - i];
}
vector<int> nv;
for (int i = 0; i < cnt; i++) {
if (l >> fac[i] & 1) nv.push_back(0);
else nv.push_back(1);
}
vector<int> v;
for (int i = 0; i < lc; i++) v.push_back(0);
for (int i = 0; i < rc; i++) v.push_back(1);
do {
if (v == nv) break;
res += dp[lc] * dp[rc];
} while (next_permutation(v.begin(), v.end()));
ll lid = f(f, l);
ll rid = f(f, r);
res += lid * dp[rc] + rid;
return res;
};
string res = encode(f(f, (1 << q) - 1), 53);
for (int i = 0; i < k; i++) {
res += encode(find(ls.begin(), ls.end(), s[i]) - ls.begin(), 5);
}
return res;
}
#include "Bitaro.h"
#include <bits/stdc++.h>
using namespace std;
namespace {
using ll = long long;
const ll linf = 1LL << 60;
ll decode(string::iterator l, string::iterator r) {
ll res = 0;
while (l < r) {
res *= 2;
res += *l - '0';
++l;
}
return res;
}
// {dist, pre}
pair<vector<ll>, vector<int>> dijkstra(int n, const vector<int> &a, const vector<int> &b, const vector<ll> &c) {
vector<vector<tuple<int, ll, int>>> G(n);
for (int i = 0; i < a.size(); i++) {
if (c[i] == -1) continue;
G[a[i]].emplace_back(b[i], c[i], i);
G[b[i]].emplace_back(a[i], c[i], i);
}
vector<ll> dist(n, linf);
vector<int> pre(n, -1);
priority_queue<pair<ll, int>, vector<pair<ll, int >>, greater<>> pq;
dist[0] = 0;
pq.emplace(0, 0);
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (d > dist[u]) continue;
for (auto [v, len, id]: G[u]) {
if (dist[v] > d + len) {
dist[v] = d + len;
pre[v] = id;
pq.emplace(dist[v], v);
}
}
}
return {dist, pre};
}
}
void bitaro(int n, int m, int q, int k, std::vector<int> a, std::vector<int> b,
std::vector<long long> c, std::vector<int> t, std::vector<int> x,
std::string s) {
vector binom(q, vector<ll>(q));
for (int i = 0; i < q; i++) {
binom[i][0] = 1;
for (int j = 1; j <= i; j++) {
binom[i][j] = binom[i - 1][j - 1] + binom[i - 1][j];
}
}
vector<ll> dp(q + 1); // dp[i] : {1, 2, ..., i} ã‚’ã„ãã¤ã‹ã®é›†åˆã«åˆ†å‰²ã™ã‚‹æ–¹æ³•ã®æ•°
dp[1] = 1;
for (int i = 2; i <= q; i++) {
for (int j = 1; j < i; j++) {
dp[i] += binom[i - 1][j - 1] * dp[j] * dp[i - j];
}
}
vector<int> ls = {0};
auto f = [&](auto &f, int now, ll val) -> void {
ls.push_back(now);
int cnt = __builtin_popcount(now);
if (cnt == 1) return;
vector<int> fac;
for (int i = 0; i < q; i++) if (now >> i & 1) fac.push_back(i);
int lc = 1;
for (;; lc++) {
ll cur = binom[cnt - 1][lc - 1] * dp[lc] * dp[cnt - lc];
if (val < cur) break;
val -= cur;
}
int rc = cnt - lc;
vector<int> v;
for (int i = 0; i < lc; i++) v.push_back(0);
for (int i = 0; i < rc; i++) v.push_back(1);
do {
if (val < dp[lc] * dp[rc]) break;
val -= dp[lc] * dp[rc];
} while (next_permutation(v.begin(), v.end()));
int l = 0, r = 0;
for (int i = 0; i < cnt; i++) {
if (!v[i]) l |= 1 << fac[i];
else r |= 1 << fac[i];
}
f(f, l, val / dp[rc]);
f(f, r, val % dp[rc]);
};
f(f, (1 << q) - 1, decode(s.begin(), s.begin() + 53));
vector use(q, vector<bool>(k));
int iter = 53;
for (int i = 0; i < k; i++) {
int bit = ls[decode(s.begin() + iter, s.begin() + iter + 5)];
iter += 5;
for (int j = 0; j < q; j++) if (bit >> j & 1) use[j][i] = true;
}
for (int i = 0; i < q; i++) {
for (int j = 0; j < k; j++) {
if (use[i][j]) c[x[j]] = 0;
}
auto [dist, pre] = dijkstra(n, a, b, c);
int now = t[i];
vector<int> es;
while (now) {
es.push_back(pre[now]);
now = a[pre[now]] ^ b[pre[now]] ^ now;
}
reverse(es.begin(), es.end());
answer(es);
for (int j = 0; j < k; j++) c[x[j]] = -1;
}
}
Compilation message
Aoi.cpp: In function 'std::pair<std::vector<long long int>, std::vector<int> > {anonymous}::dijkstra(int, const std::vector<int>&, const std::vector<int>&, const std::vector<long long int>&)':
Aoi.cpp:20:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
Aoi.cpp: In function 'std::string aoi(int, int, int, int, std::vector<int>, std::vector<int>, std::vector<long long int>, std::vector<int>, std::vector<int>)':
Aoi.cpp:87:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int j = 1; j < mg.size(); j++) {
| ~~^~~~~~~~~~~
Bitaro.cpp: In function 'std::pair<std::vector<long long int>, std::vector<int> > {anonymous}::dijkstra(int, const std::vector<int>&, const std::vector<int>&, const std::vector<long long int>&)':
Bitaro.cpp:24:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
4924 KB |
Output is correct |
2 |
Correct |
0 ms |
776 KB |
Output is correct |
3 |
Correct |
58 ms |
5968 KB |
Output is correct |
4 |
Correct |
52 ms |
6048 KB |
Output is correct |
5 |
Correct |
65 ms |
5788 KB |
Output is correct |
6 |
Correct |
61 ms |
6152 KB |
Output is correct |
7 |
Correct |
63 ms |
5760 KB |
Output is correct |
8 |
Correct |
58 ms |
5904 KB |
Output is correct |
9 |
Correct |
52 ms |
6036 KB |
Output is correct |
10 |
Correct |
23 ms |
5668 KB |
Output is correct |
11 |
Correct |
61 ms |
6176 KB |
Output is correct |
12 |
Correct |
77 ms |
6304 KB |
Output is correct |
13 |
Correct |
65 ms |
5884 KB |
Output is correct |
14 |
Correct |
61 ms |
5736 KB |
Output is correct |
15 |
Correct |
64 ms |
5880 KB |
Output is correct |
16 |
Correct |
19 ms |
5300 KB |
Output is correct |
17 |
Correct |
64 ms |
6340 KB |
Output is correct |
18 |
Correct |
64 ms |
6028 KB |
Output is correct |
19 |
Correct |
66 ms |
6344 KB |
Output is correct |
20 |
Correct |
48 ms |
6140 KB |
Output is correct |
21 |
Correct |
61 ms |
6188 KB |
Output is correct |
22 |
Correct |
72 ms |
6160 KB |
Output is correct |
23 |
Correct |
51 ms |
6176 KB |
Output is correct |
24 |
Correct |
72 ms |
6244 KB |
Output is correct |
25 |
Correct |
76 ms |
6240 KB |
Output is correct |
26 |
Correct |
69 ms |
5964 KB |
Output is correct |
27 |
Correct |
1 ms |
1300 KB |
Output is correct |
28 |
Correct |
57 ms |
6316 KB |
Output is correct |
29 |
Correct |
34 ms |
4832 KB |
Output is correct |
30 |
Correct |
62 ms |
6308 KB |
Output is correct |
31 |
Correct |
35 ms |
6064 KB |
Output is correct |
32 |
Correct |
77 ms |
6256 KB |
Output is correct |
33 |
Correct |
67 ms |
6424 KB |
Output is correct |
34 |
Correct |
74 ms |
7144 KB |
Output is correct |
35 |
Correct |
59 ms |
6884 KB |
Output is correct |
36 |
Correct |
60 ms |
6580 KB |
Output is correct |
37 |
Correct |
16 ms |
3424 KB |
Output is correct |
38 |
Correct |
37 ms |
4772 KB |
Output is correct |
39 |
Correct |
34 ms |
5028 KB |
Output is correct |
40 |
Correct |
10 ms |
4112 KB |
Output is correct |
41 |
Correct |
77 ms |
6644 KB |
Output is correct |
42 |
Correct |
45 ms |
6488 KB |
Output is correct |
43 |
Correct |
81 ms |
6952 KB |
Output is correct |
44 |
Correct |
24 ms |
6324 KB |
Output is correct |
45 |
Correct |
19 ms |
3456 KB |
Output is correct |
46 |
Correct |
32 ms |
4408 KB |
Output is correct |
47 |
Correct |
32 ms |
4668 KB |
Output is correct |
48 |
Correct |
0 ms |
772 KB |
Output is correct |
49 |
Correct |
1 ms |
776 KB |
Output is correct |
50 |
Correct |
17 ms |
4960 KB |
Output is correct |
51 |
Correct |
2 ms |
1816 KB |
Output is correct |
52 |
Correct |
1 ms |
1304 KB |
Output is correct |
53 |
Correct |
28 ms |
4884 KB |
Output is correct |
54 |
Correct |
17 ms |
3496 KB |
Output is correct |
55 |
Correct |
46 ms |
4880 KB |
Output is correct |
56 |
Correct |
44 ms |
6188 KB |
Output is correct |
57 |
Correct |
59 ms |
6048 KB |
Output is correct |
58 |
Correct |
58 ms |
5172 KB |
Output is correct |
59 |
Correct |
72 ms |
6812 KB |
Output is correct |
60 |
Correct |
65 ms |
6452 KB |
Output is correct |
61 |
Correct |
74 ms |
6536 KB |
Output is correct |
62 |
Correct |
67 ms |
6316 KB |
Output is correct |
63 |
Correct |
77 ms |
6716 KB |
Output is correct |
64 |
Correct |
19 ms |
5400 KB |
Output is correct |
65 |
Correct |
37 ms |
4436 KB |
Output is correct |
66 |
Correct |
32 ms |
6332 KB |
Output is correct |
67 |
Correct |
37 ms |
4224 KB |
Output is correct |
68 |
Correct |
32 ms |
6436 KB |
Output is correct |
69 |
Correct |
0 ms |
784 KB |
Output is correct |
70 |
Correct |
0 ms |
784 KB |
Output is correct |
71 |
Correct |
0 ms |
784 KB |
Output is correct |
72 |
Correct |
17 ms |
3332 KB |
Output is correct |
73 |
Correct |
43 ms |
4408 KB |
Output is correct |
74 |
Correct |
39 ms |
4456 KB |
Output is correct |
75 |
Correct |
11 ms |
3856 KB |
Output is correct |
76 |
Correct |
0 ms |
776 KB |
Output is correct |
77 |
Correct |
57 ms |
6564 KB |
Output is correct |
78 |
Correct |
52 ms |
6580 KB |
Output is correct |
79 |
Correct |
59 ms |
6624 KB |
Output is correct |
80 |
Correct |
1 ms |
792 KB |
Output is correct |
81 |
Correct |
63 ms |
5860 KB |
Output is correct |
82 |
Correct |
59 ms |
5876 KB |
Output is correct |
83 |
Correct |
67 ms |
6008 KB |
Output is correct |