QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#123372 | #5665. AA Country and King Dreamoon | GenshinImpactsFault# | RE | 1ms | 22396kb | C++14 | 2.5kb | 2023-07-12 14:09:14 | 2023-07-12 14:09:17 |
Judging History
answer
#include<bits/stdc++.h>
#define inf 1e9
#define eps 1e-6
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define REP(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define fr first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
inline ll read()
{
char ch = getchar();
ll s = 0,w = 1;
while(ch < '0' || ch > '9'){if(ch == '-') w = -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){s = s * 10 + ch -'0'; ch = getchar();}
return s * w;
}
#define N 600010
int d[N], dep[N], fa[N];
int c[N], vst[N];
vector<int>ch[N];
int n;
void s1(int &x, int now)
{
if(!c[now])return ;
if(c[now] == fa[x]) {x = fa[x]; s1(x, now + 1);}
else
{
fa[c[now]] = x; ch[x].pb(c[now]); dep[c[now]] = dep[x] + 1;
x = c[now]; s1(x, now + 1); vst[c[now]] = 1;
}
}
void s2(int &x, int now)
{
if(!c[now])return ;
// cerr << "s2" << now << ' ' << x << " " << c[now] << endl;
if(c[now] == fa[x]) {x = fa[x]; s2(x, now - 1);}
else
{
fa[c[now]] = x; ch[x].pb(c[now]); dep[c[now]] = dep[x] + 1;
x = c[now]; s2(x, now - 1); vst[c[now]] = 1;
}
}
void sol()
{
n = read();
FOR(i, 1, 2 * n - 1)c[i] = read();
if(n == 1){cout << 1 << '\n'; return ;}
if(!c[1]) c[1] = 1;
if(!c[2 * n - 1]) c[2 * n - 1] = 1;
vst[1] = 1;
int x = 1, y = 1;
s1(x, 2); s2(y, 2 * n - 2);
set<int>S;
FOR(i, 1, n) if(!vst[i]) S.insert(i);
int tx = x, ty = y;
while(tx != ty)
{
if(dep[tx] > dep[ty]) d[tx] = fa[tx], tx = fa[tx];
else d[fa[ty]] = ty, ty = fa[ty];
}
// cerr << "?" << x << ' ' << y << endl;
d[y] = inf;
vector<int>p;
int now = x;
while(!(S.empty() && now == y))
{
int tox;
if(S.empty())tox = inf;
else tox = (*S.begin());
if(tox < d[now])
{
p.pb(tox); fa[tox] = d[tox] = now; ch[now].pb(tox);
now = tox; S.erase(tox);
}
else
{
p.pb(d[now]); now = d[now];
}
}
p.pop_back();
vector<int>t1,t2;
FOR(i, 1, 2 * n - 1){if(!c[i])break;t1.pb(c[i]);}
REP(i, 2 * n - 1, 1){if(!c[i])break;t2.pb(c[i]);}
reverse(t2.begin(),t2.end());
for(int x:t1)cout << x << ' '; //cerr << '\n';
for(int x:p)cout << x << ' '; //cerr << '\n';
for(int x:t2)cout << x << ' '; //cerr << '\n';
cout << '\n';
FOR(i,1,2 * n - 1)fa[i] = dep[i] = d[i] = c[i] = vst[i] = 0, ch[i].clear();
}
signed main()
{
int T = read();
while(T --)sol();
return 0;
}
/*
9
5
1 2 3 2 0 2 1 5 1
5
1 2 3 0 0 2 1 5 1
5
1 2 0 0 0 2 1 5 1
5
1 2 0 0 0 0 1 5 1
5
1 0 0 0 0 0 1 5 1
5
1 0 0 0 0 0 0 5 1
5
1 0 0 0 0 0 0 0 1
5
1 0 0 0 0 0 0 0 0
5
0 0 0 0 0 0 0 0 0
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 22396kb
input:
9 5 1 2 3 2 0 2 1 5 1 5 1 2 3 0 0 2 1 5 1 5 1 2 0 0 0 2 1 5 1 5 1 2 0 0 0 0 1 5 1 5 1 0 0 0 0 0 1 5 1 5 1 0 0 0 0 0 0 5 1 5 1 0 0 0 0 0 0 0 1 5 1 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0
output:
1 2 3 2 4 2 1 5 1 1 2 3 2 4 2 1 5 1 1 2 3 2 4 2 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1 1 2 1 3 1 4 1 5 1
result:
ok 9 lines
Test #2:
score: -100
Runtime Error
input:
28668 2 0 2 1 2 0 0 1 2 0 0 0 2 1 0 1 2 1 0 0 2 1 2 0 3 0 2 1 3 1 3 0 0 1 3 1 3 0 0 0 3 1 3 0 0 0 0 1 3 0 0 0 0 0 3 1 0 1 3 1 3 1 0 0 3 1 3 1 0 0 0 1 3 1 0 0 0 0 3 1 2 0 3 1 3 1 2 0 0 1 3 1 2 0 0 0 3 1 2 1 0 1 3 1 2 1 0 0 3 1 2 1 3 0 3 0 2 3 2 1 3 0 0 3 2 1 3 0 0 0 2 1 3 1 0 3 2 1 3 1 0 0 2 1 3 1 2 ...