#include "Joi.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
namespace shared_joi {
const int M = 60;
const int N = 16384;
vector<int> A[N];
int par[N], dis[N];
bool dia[N];
int bit[N];
int s, t;
int n;
namespace dsu {
int par[N];
int rt(int v) {return par[v]==-1?v:(par[v]=rt(par[v]));}
bool unite(int v, int u) {
v = rt(v);
u = rt(u);
if (v == u)
return 0;
par[u] = v;
return 1;
}
}
pii dfs(int v, int p, int h) {
par[v] = p;
dis[v] = h;
pii ans = {h, v};
for (int u : A[v]) {
if (u == p)
continue;
ans = max(ans, dfs(u, v, h+1));
}
return ans;
}
vector<int> ord;
void dfs2(int v, int p, int &rem) {
ord.push_back(v);
int d = -1;
for (int u : A[v]) {
if (u == p)
continue;
if (dia[u]) {
d = u;
continue;
}
if (!rem)
continue;
--rem;
dfs2(u, v, rem);
ord.push_back(v);
}
if (d != -1)
dfs2(d, v, rem);
}
void init(int _n, int m, int V[], int U[]) {
n = _n;
memset(dsu::par, -1, sizeof(dsu::par));
Loop (i,0,m) {
if (!dsu::unite(V[i], U[i]))
continue;
A[V[i]].push_back(U[i]);
A[U[i]].push_back(V[i]);
}
s = dfs(0, -1, 0).second;
t = dfs(s, -1, 0).second;
for (int v = t; v != -1; v = par[v])
dia[v] = 1;
if (dis[t] >= (M-1)) {
Loop (i,0,n)
bit[i] = dis[i]%M;
return;
}
memset(bit, -1, sizeof(bit));
dfs2(s, -1, *new int((M-1)-dis[t]));
int nxt = 0;
for (int v : ord) {
if (bit[v] == -1)
bit[v] = nxt++;
}
assert(nxt == M);
assert(ord[0] == s);
}
}
using namespace shared_joi;
void Joi(int N, int M, int A[], int B[], long long X, int T)
{
init(N, M, A, B);
Loop (i,0,N) {
int x = bit[i] == -1? 0: ((X >> bit[i]) & 1);
MessageBoard(i, x);
}
}
#include "Ioi.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
namespace shared_ioi {
const int M = 60;
const int N = 16384;
vector<int> A[N];
int par[N], dis[N];
bool dia[N];
int bit[N];
int s, t;
int n;
namespace dsu {
int par[N];
int rt(int v) {return par[v]==-1?v:(par[v]=rt(par[v]));}
bool unite(int v, int u) {
v = rt(v);
u = rt(u);
if (v == u)
return 0;
par[u] = v;
return 1;
}
}
pii dfs(int v, int p, int h) {
par[v] = p;
dis[v] = h;
pii ans = {h, v};
for (int u : A[v]) {
if (u == p)
continue;
ans = max(ans, dfs(u, v, h+1));
}
return ans;
}
vector<int> ord;
void dfs2(int v, int p, int &rem) {
ord.push_back(v);
int d = -1;
for (int u : A[v]) {
if (u == p)
continue;
if (dia[u]) {
d = u;
continue;
}
if (!rem)
continue;
--rem;
dfs2(u, v, rem);
ord.push_back(v);
}
if (d != -1)
dfs2(d, v, rem);
}
void init(int _n, int m, int V[], int U[]) {
n = _n;
memset(dsu::par, -1, sizeof(dsu::par));
Loop (i,0,m) {
if (!dsu::unite(V[i], U[i]))
continue;
A[V[i]].push_back(U[i]);
A[U[i]].push_back(V[i]);
}
s = dfs(0, -1, 0).second;
t = dfs(s, -1, 0).second;
for (int v = t; v != -1; v = par[v])
dia[v] = 1;
if (dis[t] >= (M-1)) {
Loop (i,0,n)
bit[i] = dis[i]%M;
return;
}
memset(bit, -1, sizeof(bit));
dfs2(s, -1, *new int((M-1)-dis[t]));
int nxt = 0;
for (int v : ord) {
if (bit[v] == -1)
bit[v] = nxt++;
}
assert(nxt == M);
}
}
using namespace shared_ioi;
int cur;
void mov(int v)
{
cur = Move(v);
}
ll ans = 0;
void up(int bit, bool val)
{
ans |= (ll)val << bit;
}
void solve1(int v)
{
if (dis[v] >= (M-1)) {
up(bit[v], cur);
Loop (_,0,(M-1)) {
mov(par[v]);
v = par[v];
up(bit[v], cur);
}
} else {
while (v != s) {
mov(par[v]);
v = par[v];
}
up(bit[v], cur);
Loop (_,0,(M-1)) {
for (int u : A[v]) {
if (u == par[v] || !dia[u])
continue;
mov(u);
v = u;
up(bit[v], cur);
break;
}
}
}
}
void solve2(int v)
{
while (v != s) {
mov(par[v]);
v = par[v];
}
up(bit[v], cur);
Loop (i,1,ord.size()) {
int u = ord[i];
mov(u);
v = u;
up(bit[v], cur);
}
}
long long Ioi(int _n, int _m, int A[], int B[], int P, int V, int T)
{
init(_n, _m, A, B);
cur = V;
if (dis[t] >= (M-1))
solve1(P);
else
solve2(P);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1536 KB |
Output is correct |
2 |
Correct |
1 ms |
1536 KB |
Output is correct |
3 |
Correct |
2 ms |
1796 KB |
Output is correct |
4 |
Correct |
2 ms |
1544 KB |
Output is correct |
5 |
Correct |
1 ms |
1512 KB |
Output is correct |
6 |
Correct |
2 ms |
1668 KB |
Output is correct |
7 |
Correct |
2 ms |
1548 KB |
Output is correct |
8 |
Correct |
2 ms |
1808 KB |
Output is correct |
9 |
Correct |
2 ms |
1800 KB |
Output is correct |
10 |
Correct |
1 ms |
1552 KB |
Output is correct |
11 |
Correct |
4 ms |
2004 KB |
Output is correct |
12 |
Correct |
2 ms |
1676 KB |
Output is correct |
13 |
Correct |
2 ms |
1812 KB |
Output is correct |
14 |
Correct |
2 ms |
1800 KB |
Output is correct |
15 |
Correct |
2 ms |
1776 KB |
Output is correct |
16 |
Correct |
2 ms |
1780 KB |
Output is correct |
17 |
Correct |
2 ms |
1808 KB |
Output is correct |
18 |
Correct |
2 ms |
1796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3756 KB |
Output is correct |
2 |
Correct |
20 ms |
4188 KB |
Output is correct |
3 |
Correct |
20 ms |
4072 KB |
Output is correct |
4 |
Correct |
14 ms |
3464 KB |
Output is correct |
5 |
Correct |
13 ms |
4548 KB |
Output is correct |
6 |
Correct |
13 ms |
3876 KB |
Output is correct |
7 |
Correct |
13 ms |
3948 KB |
Output is correct |
8 |
Correct |
13 ms |
3864 KB |
Output is correct |
9 |
Correct |
16 ms |
3848 KB |
Output is correct |
10 |
Correct |
11 ms |
3492 KB |
Output is correct |
11 |
Correct |
16 ms |
3488 KB |
Output is correct |
12 |
Correct |
12 ms |
3352 KB |
Output is correct |
13 |
Correct |
11 ms |
3340 KB |
Output is correct |
14 |
Correct |
12 ms |
3344 KB |
Output is correct |
15 |
Correct |
13 ms |
3364 KB |
Output is correct |
16 |
Correct |
14 ms |
3480 KB |
Output is correct |
17 |
Correct |
13 ms |
3356 KB |
Output is correct |
18 |
Correct |
13 ms |
3488 KB |
Output is correct |
19 |
Correct |
13 ms |
3460 KB |
Output is correct |
20 |
Correct |
12 ms |
3992 KB |
Output is correct |
21 |
Correct |
11 ms |
3984 KB |
Output is correct |
22 |
Correct |
15 ms |
4004 KB |
Output is correct |
23 |
Correct |
14 ms |
4108 KB |
Output is correct |
24 |
Correct |
13 ms |
4008 KB |
Output is correct |
25 |
Correct |
13 ms |
3884 KB |
Output is correct |
26 |
Correct |
13 ms |
3964 KB |
Output is correct |
27 |
Correct |
13 ms |
3988 KB |
Output is correct |
28 |
Correct |
13 ms |
4008 KB |
Output is correct |
29 |
Correct |
12 ms |
3836 KB |
Output is correct |
30 |
Correct |
14 ms |
3880 KB |
Output is correct |
31 |
Correct |
1 ms |
1648 KB |
Output is correct |
32 |
Correct |
2 ms |
1676 KB |
Output is correct |
33 |
Correct |
2 ms |
1808 KB |
Output is correct |
34 |
Correct |
1 ms |
1676 KB |
Output is correct |
35 |
Correct |
2 ms |
1500 KB |
Output is correct |
36 |
Correct |
2 ms |
1548 KB |
Output is correct |
37 |
Correct |
2 ms |
1536 KB |
Output is correct |
38 |
Correct |
2 ms |
1676 KB |
Output is correct |
39 |
Correct |
2 ms |
1668 KB |
Output is correct |
40 |
Correct |
2 ms |
1548 KB |
Output is correct |
41 |
Correct |
2 ms |
1548 KB |
Output is correct |
42 |
Correct |
2 ms |
1676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1540 KB |
Output is correct |
2 |
Correct |
1 ms |
1536 KB |
Output is correct |
3 |
Correct |
2 ms |
1536 KB |
Output is correct |
4 |
Correct |
3 ms |
1956 KB |
Output is correct |
5 |
Correct |
3 ms |
1948 KB |
Output is correct |
6 |
Correct |
2 ms |
1948 KB |
Output is correct |
7 |
Correct |
2 ms |
1964 KB |
Output is correct |
8 |
Correct |
2 ms |
2084 KB |
Output is correct |
9 |
Correct |
13 ms |
4420 KB |
Output is correct |
10 |
Correct |
11 ms |
4428 KB |
Output is correct |
11 |
Correct |
11 ms |
4432 KB |
Output is correct |
12 |
Correct |
1 ms |
1544 KB |
Output is correct |
13 |
Correct |
1 ms |
1544 KB |
Output is correct |
14 |
Correct |
1 ms |
1540 KB |
Output is correct |
15 |
Correct |
1 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
3820 KB |
Output is correct |
2 |
Correct |
20 ms |
4188 KB |
Output is correct |
3 |
Correct |
20 ms |
4200 KB |
Output is correct |
4 |
Correct |
15 ms |
3600 KB |
Output is correct |
5 |
Correct |
13 ms |
4500 KB |
Output is correct |
6 |
Correct |
15 ms |
3888 KB |
Output is correct |
7 |
Correct |
13 ms |
3880 KB |
Output is correct |
8 |
Correct |
13 ms |
3880 KB |
Output is correct |
9 |
Correct |
13 ms |
3888 KB |
Output is correct |
10 |
Correct |
11 ms |
3468 KB |
Output is correct |
11 |
Correct |
14 ms |
3568 KB |
Output is correct |
12 |
Correct |
12 ms |
3332 KB |
Output is correct |
13 |
Correct |
13 ms |
3324 KB |
Output is correct |
14 |
Correct |
13 ms |
3328 KB |
Output is correct |
15 |
Correct |
13 ms |
3460 KB |
Output is correct |
16 |
Correct |
13 ms |
3364 KB |
Output is correct |
17 |
Correct |
13 ms |
3496 KB |
Output is correct |
18 |
Correct |
13 ms |
3364 KB |
Output is correct |
19 |
Correct |
13 ms |
3364 KB |
Output is correct |
20 |
Correct |
11 ms |
3988 KB |
Output is correct |
21 |
Correct |
11 ms |
3884 KB |
Output is correct |
22 |
Correct |
13 ms |
4012 KB |
Output is correct |
23 |
Correct |
14 ms |
4060 KB |
Output is correct |
24 |
Correct |
15 ms |
3908 KB |
Output is correct |
25 |
Correct |
13 ms |
4004 KB |
Output is correct |
26 |
Correct |
13 ms |
3996 KB |
Output is correct |
27 |
Correct |
13 ms |
3908 KB |
Output is correct |
28 |
Correct |
13 ms |
3972 KB |
Output is correct |
29 |
Correct |
12 ms |
3832 KB |
Output is correct |
30 |
Correct |
13 ms |
3864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3780 KB |
Output is correct |
2 |
Correct |
19 ms |
4092 KB |
Output is correct |
3 |
Correct |
20 ms |
4088 KB |
Output is correct |
4 |
Correct |
13 ms |
3456 KB |
Output is correct |
5 |
Correct |
13 ms |
4428 KB |
Output is correct |
6 |
Correct |
13 ms |
3952 KB |
Output is correct |
7 |
Correct |
14 ms |
3884 KB |
Output is correct |
8 |
Correct |
14 ms |
3820 KB |
Output is correct |
9 |
Correct |
14 ms |
3844 KB |
Output is correct |
10 |
Correct |
12 ms |
3508 KB |
Output is correct |
11 |
Correct |
12 ms |
3476 KB |
Output is correct |
12 |
Correct |
12 ms |
3340 KB |
Output is correct |
13 |
Correct |
12 ms |
3340 KB |
Output is correct |
14 |
Correct |
13 ms |
3344 KB |
Output is correct |
15 |
Correct |
13 ms |
3372 KB |
Output is correct |
16 |
Correct |
13 ms |
3488 KB |
Output is correct |
17 |
Correct |
13 ms |
3364 KB |
Output is correct |
18 |
Correct |
13 ms |
3488 KB |
Output is correct |
19 |
Correct |
13 ms |
3372 KB |
Output is correct |
20 |
Correct |
11 ms |
3976 KB |
Output is correct |
21 |
Correct |
11 ms |
3972 KB |
Output is correct |
22 |
Correct |
13 ms |
3964 KB |
Output is correct |
23 |
Correct |
13 ms |
4008 KB |
Output is correct |
24 |
Correct |
13 ms |
4012 KB |
Output is correct |
25 |
Correct |
13 ms |
3992 KB |
Output is correct |
26 |
Correct |
13 ms |
4016 KB |
Output is correct |
27 |
Correct |
14 ms |
3984 KB |
Output is correct |
28 |
Correct |
13 ms |
3972 KB |
Output is correct |
29 |
Correct |
11 ms |
3780 KB |
Output is correct |
30 |
Correct |
13 ms |
3840 KB |
Output is correct |
31 |
Correct |
2 ms |
1676 KB |
Output is correct |
32 |
Correct |
2 ms |
1796 KB |
Output is correct |
33 |
Correct |
2 ms |
1800 KB |
Output is correct |
34 |
Correct |
2 ms |
1784 KB |
Output is correct |
35 |
Correct |
2 ms |
1512 KB |
Output is correct |
36 |
Correct |
2 ms |
1540 KB |
Output is correct |
37 |
Correct |
2 ms |
1552 KB |
Output is correct |
38 |
Correct |
2 ms |
1680 KB |
Output is correct |
39 |
Correct |
2 ms |
1676 KB |
Output is correct |
40 |
Correct |
1 ms |
1548 KB |
Output is correct |
41 |
Correct |
2 ms |
1524 KB |
Output is correct |
42 |
Correct |
1 ms |
1636 KB |
Output is correct |