This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll inf = 1e9;
const int md1 = 1e9+7;
const int md2 = 998244353;
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz(v) ((int)v.size())
#define forn(i,n) for(int i = 0; i < n; ++i)
#define forbe(i,b,e) for(int i = b; i < e; ++i)
#define pb push_back
#define pry puts("YES")
#define prn puts("NO")
#define endl '\n'
#define fst first
#define scn second
const int N = 2e5;
const int M = 1e5;
vector<int> etrig[M+2];
vector<int> ansX;
vector<int> ansY;
int cnt = 1;
const ll P = 2e7;
const ll S = 4e5;
ll p = 0;
int
dfs(int x, int s, int k)
{
assert(s<sz(etrig[x]));
if ((sz(etrig[x])-s-1)/(1<<k) == 0) return etrig[x][s];
int size = 1 + (sz(etrig[x])-s-1)/(1<<k);
/* if (size&1) */
/* assert(0); */
int _y = dfs(x,s+(1<<k),k+1);
int _x = dfs(x,s,k+1);
if (_x == _y)
return _x;
p += size;
ansX.pb(0);
ansY.pb(0);
ansY[cnt-1] = _y;
ansX[cnt-1] = _x;
++cnt;
return -(cnt-1);
}
int
donode(int x)
{
int scount = cnt++;
ansX.pb(0);
ansY.pb(0);
int last = 0;
for (int j = 0; j < 20; ++j) {
if (sz(etrig[x])&(1<<j))
last = j;
}
if (sz(etrig[x]) != (1<<last))
++last;
if (sz(etrig[x]) < (1<<last)) {
ansX.pb(0);
ansY.pb(0);
ansX[cnt-1] = -cnt;
ansY[cnt-1] = -scount;
int temp = etrig[x][sz(etrig[x])-1];
etrig[x][sz(etrig[x])-1] = -cnt;
while (sz(etrig[x]) < (1<<last))
etrig[x].pb(-cnt);
etrig[x][sz(etrig[x])-1] = temp;
++cnt;
}
p += (1<<last);
ansX[scount-1] = dfs(x,0,1);
ansY[scount-1] = dfs(x,1,1);
return - scount;
}
void answer(vector<int> c, vector<int> X, vector<int> Y);
void
create_circuit(int m, vector<int> a)
{
int n = sz(a);
vector<int> trigexit(m+1);
for (int i = 0; i < n-1; ++i) {
etrig[a[i]].pb(a[i+1]);
/* assert(a[i] != 0); */
}
/* assert(a[n-1] != 0); */
etrig[a[n-1]].pb(0);
trigexit[0] = a[0];
for (int i = 1; i <= m; ++i) {
if (sz(etrig[i]) == 0) {
trigexit[i] = 0;
} else if (sz(etrig[i]) == 1) {
trigexit[i] = etrig[i][0];
} else {
trigexit[i] = donode(i);
}
}
/* if (cnt > S) { */
/* cout << "S: " << cnt << endl; */
/* } */
/* assert(cnt <= S); */
/* if (p > P) { */
/* cout << "P: " << p << endl; */
/* } */
/* assert(p <= P); */
answer(trigexit,ansX,ansY);
return;
}
#ifdef ONPC
void
answer(vector<int> c, vector<int> X, vector<int> Y)
{
vector<bool> state(sz(X));
vector<int> ans;
/* for (int i = 0; i < sz(c); ++i) { */
/* cout << i << ": " << c[i] << endl; */
/* } */
/* for (int i = 0; i < sz(X); ++i) { */
/* cout << i+1 << ": " << X[i] << ' ' << Y[i] << endl; */
/* } */
int trg = c[0];
do {
if (trg >= 0) {
ans.pb(trg);
trg = c[trg];
} else {
int indx = abs(trg)-1;
if (!state[indx]) trg = X[indx];
else trg = Y[indx];
state[indx] = !state[indx];
}
} while (trg != 0);
for (int i = 0; i < sz(X); ++i) {
if (state[i]) {
cout << i << endl;
/* cout << "0\n"; */
return;
}
}
for (int i = 0; i < sz(ans); ++i) {
cout << ans[i] << ' ';
}
cout << endl;
}
void
solve()
{
int m,n;
cin >> m >> n;
vector<int> c(n);
for (int i = 0; i < n; ++i) {
cin >> c[i];
}
cout << m << ' ' << n << endl;
/* vector<int> c = {1,2,1}; */
create_circuit(m,c);
}
int32_t
main()
{
/* freopen("in", "r", stdin); */
int t = 1;
/* cin >> t; */
while(t--)
solve();
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |