#include "doll.h"
#include <set>
#include <tuple>
#include <cassert>
#include <iostream>
using namespace std;
const int INF = 1e9;
bool drain(int i, int root, vector<int> &X, vector<int> &Y) {
bool isDrain = false;
if (X[-i] < 0) {if (drain(X[-i], root, X, Y)) isDrain = true;}
else if (X[-i] == 0 || X[-i] == INF) X[-i] = root, isDrain = true;
if (Y[-i] < 0) {if (drain(Y[-i], root, X, Y)) isDrain = true;}
else if (Y[-i] == 0 || Y[-i] == INF) Y[-i] = root, isDrain = true;
return isDrain;
}
void create_circuit(int M, vector<int> A) {
int N = A.size();
vector<vector<int>> nxt(1+M);
nxt[0].push_back(A[0]);
for (int i = 0; i < N-1; ++i) {
nxt[A[i]].push_back(A[i+1]);
}
nxt[A[N-1]].push_back(0);
int S = 0;
vector<int> X(1), Y(1);
vector<int> C(1+M); // root switch for each trigger
for (int i = 0; i <= M; ++i) {
if (nxt[i].empty()) { // unused trigger
C[i] = i;
continue;
}
int p2 = 1, expo = 0;
while (p2 < (int)nxt[i].size()) ++expo, p2 <<= 1;
// Order targets (stored in nxt) for tree of switches
vector<int> orderedNxt(1, 0);
for (int j = 1, previousPow2 = 1; j <= expo; ++j, previousPow2 <<= 1) {
vector<int> nextLevel(2*orderedNxt.size());
for (int k = 0; k < (int)nextLevel.size(); ++k) {
nextLevel[k] = orderedNxt[k>>1] + (k&1)*previousPow2;
}
orderedNxt = nextLevel;
}
for (int j = 0; j < p2; ++j) {
if (orderedNxt[j] >= (int)nxt[i].size()) orderedNxt[j] = INF;
else orderedNxt[j] = nxt[i][orderedNxt[j]];
}
// Now build switches for pairs of targets
vector<int> curSwitches = orderedNxt;
while (curSwitches.size() > 1) {
vector<int> nextSwitches((int)curSwitches.size()>>1);
for (int j = 0; j < (int)nextSwitches.size(); ++j) {
int x = curSwitches[2*j], y = curSwitches[2*j+1];
if (x==INF && y==INF) {// this should never happen
nextSwitches[j] = INF;
continue;
}
// create switch
// Create new switch
++S;
X.push_back(x);
Y.push_back(y);
nextSwitches[j] = -S;
}
curSwitches = nextSwitches;
}
// now curSwitches[0] is the root switch for this trigger
C[i] = curSwitches[0];
}
// Now we need to "drain" the switches at the end so they all have state X at the end
// find the roots of the trees that need draining
vector<int> needDrain;
for (int i = 0; i <= M; ++i) {
if (C[i] >= 0) {
if (C[i] == 0 || C[i] == INF) C[i] = i;
} else if (drain(C[i], C[i], X, Y) && i != A[N-1]) {
needDrain.push_back(i);
}
}
auto getBottom = [&C, &Y](int i) {
int root = C[i]; i = C[i];
while (true) {
if (i > 0) {
if (C[i] == root) return i;
i = C[i];
} else {
if (Y[-i] == root) return i;
i = Y[-i];
}
}
};
// link up the drain chain drain chain
int prevBottom = getBottom(A[N-1]);
for (int i : needDrain) {
// connect bottom (end) of previous to root of current
if (prevBottom > 0) C[prevBottom] = C[i];
else Y[-prevBottom] = C[i];
// find bottom of current
prevBottom = getBottom(i);
}
// connect last bottom back to the origin
if (prevBottom > 0) C[prevBottom] = 0;
else Y[-prevBottom] = 0;
X.erase(X.begin());
Y.erase(Y.begin());
answer(C, X, Y);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
26 ms |
6292 KB |
Output is correct |
3 |
Correct |
25 ms |
5204 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
10 ms |
3796 KB |
Output is correct |
6 |
Correct |
44 ms |
7744 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
26 ms |
6292 KB |
Output is correct |
3 |
Correct |
25 ms |
5204 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
10 ms |
3796 KB |
Output is correct |
6 |
Correct |
44 ms |
7744 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
48 ms |
7228 KB |
Output is correct |
9 |
Correct |
56 ms |
8620 KB |
Output is correct |
10 |
Correct |
82 ms |
10996 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
26 ms |
6292 KB |
Output is correct |
3 |
Correct |
25 ms |
5204 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
10 ms |
3796 KB |
Output is correct |
6 |
Correct |
44 ms |
7744 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
48 ms |
7228 KB |
Output is correct |
9 |
Correct |
56 ms |
8620 KB |
Output is correct |
10 |
Correct |
82 ms |
10996 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
95 ms |
11436 KB |
Output is correct |
15 |
Correct |
48 ms |
5756 KB |
Output is correct |
16 |
Correct |
75 ms |
8612 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
88 ms |
12408 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
212 KB |
Output is partially correct |
2 |
Correct |
37 ms |
5104 KB |
Output is correct |
3 |
Partially correct |
61 ms |
11056 KB |
Output is partially correct |
4 |
Partially correct |
91 ms |
10960 KB |
Output is partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
212 KB |
Output is partially correct |
2 |
Correct |
37 ms |
5104 KB |
Output is correct |
3 |
Partially correct |
61 ms |
11056 KB |
Output is partially correct |
4 |
Partially correct |
91 ms |
10960 KB |
Output is partially correct |
5 |
Partially correct |
98 ms |
12316 KB |
Output is partially correct |
6 |
Partially correct |
113 ms |
12704 KB |
Output is partially correct |
7 |
Partially correct |
105 ms |
12456 KB |
Output is partially correct |
8 |
Partially correct |
105 ms |
12848 KB |
Output is partially correct |
9 |
Partially correct |
69 ms |
8088 KB |
Output is partially correct |
10 |
Partially correct |
99 ms |
13512 KB |
Output is partially correct |
11 |
Partially correct |
104 ms |
13096 KB |
Output is partially correct |
12 |
Partially correct |
59 ms |
8816 KB |
Output is partially correct |
13 |
Partially correct |
82 ms |
8408 KB |
Output is partially correct |
14 |
Partially correct |
64 ms |
8324 KB |
Output is partially correct |
15 |
Partially correct |
69 ms |
8224 KB |
Output is partially correct |
16 |
Partially correct |
2 ms |
468 KB |
Output is partially correct |
17 |
Partially correct |
56 ms |
7196 KB |
Output is partially correct |
18 |
Partially correct |
50 ms |
7132 KB |
Output is partially correct |
19 |
Partially correct |
54 ms |
8512 KB |
Output is partially correct |
20 |
Partially correct |
75 ms |
11168 KB |
Output is partially correct |
21 |
Partially correct |
85 ms |
13076 KB |
Output is partially correct |
22 |
Partially correct |
75 ms |
10684 KB |
Output is partially correct |