QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#770920 | #9551. The Emperor | ucup-team3646 | WA | 0ms | 17344kb | C++20 | 2.9kb | 2024-11-22 01:56:28 | 2024-11-22 01:56:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, l, r) for (ll i = l; i < r; i++)
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
#define all(A) A.begin(), A.end()
#define elif else if
using pii = pair<ll, ll>;
bool chmin(auto &a, const auto &b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, const auto &b) { return a < b ? a = b, 1 : 0; }
struct IOSetup
{
IOSetup()
{
cin.tie(0);
ios::sync_with_stdio(0);
}
} iosetup;
template <class T>
void print(vector<T> a)
{
for (auto x : a)
cerr << x << ' ';
cout << endl;
}
void print(auto x) { cout << x << endl; }
template <class Head, class... Tail>
void print(Head &&head, Tail &&...tail)
{
cout << head << ' ';
print(forward<Tail>(tail)...);
}
const ll mod = 998244353;
int N;
vector<pair<int, int>> POP, PUSH;
vector<array<ll, 3>> todo1;
vi todo2;
vvi seen(1100, vi(1100, 0));
vi TO(1100, -1), NOW(1100, -1);
vector<vll> dist(1100, vll(1100, -1));
void add(int frm, int to, ll w)
{
if (frm == 0 && POP[to].first == -1)
{
cout << (w + 1) % mod << endl;
exit(0);
}
if (dist[frm][to] != -1)
todo2.push_back(frm);
return;
if (TO[frm] == -1)
{
TO[frm] = to;
NOW[frm] = to;
}
dist[frm][to] = w;
todo1.push_back({frm,to,w});
todo2.push_back(frm);
}
void search(int v)
{
int nxt = TO[NOW[v]];
if (nxt != -1)
{
ll w = dist[v][NOW[v]] + dist[NOW[v]][nxt];
w %= mod;
add(v,nxt,w);
NOW[v] = nxt;
}
}
int main()
{
cin >> N;
POP.resize(N);
PUSH.resize(N);
rep(i, N)
{
string s;
cin >> s;
if (s[0] == 'P')
{
cin >> POP[i].first;
cin >> s;
cin >> POP[i].second;
cin >> s;
POP[i].second--;
}
else
{
POP[i] = {-1, -1};
}
cin >> s;
cin >> PUSH[i].first;
cin >> s;
cin >> PUSH[i].second;
PUSH[i].second--;
}
if (POP[0].first == -1)
{
cout << 1 << endl;
exit(0);
}
vvi G(N);
rep(v, N)
{
G[PUSH[v].second].push_back(v);
}
rep(v, N)
{
if (POP[v].first != -1)
{
int to = POP[v].second;
for (auto frm : G[v])
{
if (PUSH[frm].first == POP[v].first)
{
add(frm, to, 2);
}
}
}
}
while (todo1.size() > 0 || todo2.size() > 0)
{
if (todo1.size() > 0)
{
auto [frm, to, w] = todo1.back();
todo1.pop_back();
for (auto frm2 : G[frm])
{
if (PUSH[frm2].first == POP[to].first)
{
add(frm2, POP[to].second, (w + 2) % mod);
}
}
}
else
{
int v = todo2.back();
todo2.pop_back();
search(v);
}
}
cout << -1 << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17344kb
input:
1 HALT; PUSH 1 GOTO 1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 17176kb
input:
5 POP 1 GOTO 2; PUSH 1 GOTO 2 HALT; PUSH 1 GOTO 3 POP 1 GOTO 4; PUSH 2 GOTO 4 POP 1 GOTO 2; PUSH 2 GOTO 4 HALT; PUSH 99 GOTO 4
output:
-1
result:
wrong answer 1st numbers differ - expected: '5', found: '-1'