QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#770900#9551. The Emperorucup-team3646TL 4ms17400kbC++202.9kb2024-11-22 01:40:282024-11-22 01:40:28

Judging History

你现在查看的是最新测评结果

  • [2024-11-22 01:40:28]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:17400kb
  • [2024-11-22 01:40:28]
  • 提交

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)
    return;
  if (TO[frm] == -1)
  {
    TO[frm] = to;
    NOW[frm] = to;
  }
  dist[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;
    todo1.push_back({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)
        {
          todo1.push_back({frm, to, 2});
        }
      }
    }
  }
  while (todo1.size() > 0 || todo2.size() > 0)
  {
    if (todo1.size() > 0)
    {
      auto [frm, to, w] = todo1.back();
      todo1.pop_back();
      add(frm, to, w);
      for (auto frm2 : G[frm])
      {
        if (PUSH[frm2].first == POP[to].first)
        {
          todo1.push_back({frm2, POP[to].second, (w + 2) % mod});
        }
      }
    }
    else
    {
      int v = todo2.back();
      todo2.pop_back();
      search(v);
    }
  }

  cout << -1 << endl;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 17400kb

input:

1
HALT; PUSH 1 GOTO 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 4ms
memory: 17324kb

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:

5

result:

ok 1 number(s): "5"

Test #3:

score: -100
Time Limit Exceeded

input:

1
POP 1 GOTO 1; PUSH 1 GOTO 1

output:


result: