Submission #417843

# Submission time Handle Problem Language Result Execution time Memory
417843 2021-06-04T11:50:32 Z Hegdahl Toy Train (IOI17_train) C++17
0 / 100
22 ms 1996 KB
#include <bits/stdc++.h>
#include "train.h"

using namespace std;

const int mxN = 5000;
vector<int> g[mxN], gp[mxN], condensed[mxN];
int a[mxN], r[mxN];

bool all_a = true, all_b = true;

int vis[mxN], scc[mxN];
vector<int> order;
void dfs1(int cur) {
  vis[cur] = 1;
  for (int nxt : g[cur])
    if (!vis[nxt])
      dfs1(nxt);
  order.push_back(cur);
}

void dfs2(int root, int cur) {
  vis[cur] = 1;
  scc[cur] = root;
  for (int nxt : gp[cur])
    if (!vis[nxt])
      dfs2(root, nxt);
}

void dfs3(vector<int> &ans, int cur) {
  ans[cur] = 0;
  vis[cur] = 1;
  for (int nxt : gp[cur])
    if (!vis[nxt]) dfs3(ans, nxt);
}

vector<int> who_wins(vector<int> _a, vector<int> _r, vector<int> u, vector<int> v) {
  const int n = (int)_a.size();
  const int m = (int)u.size();

  for (int i = 0; i < n; ++i)
    a[i] = _a[i];
  for (int i = 0; i < n; ++i)
    r[i] = _r[i];

  for (int i = 0; i < n; ++i) {
    all_a &= a[i];
    all_b &= !a[i];
  }
  assert(all_b);

  for (int i = 0; i < n; ++i)
    g[i].clear();
  for (int i = 0; i < n; ++i)
    gp[i].clear();
  for (int mm = 0; mm < m; ++mm)
    if (r[v[mm]] == 0)
      g[u[mm]].push_back(v[mm]);
  for (int mm = 0; mm < m; ++mm)
    if (r[v[mm]] == 0)
      gp[v[mm]].push_back(u[mm]);

  fill(vis, vis+n, 0);
  for (int i = 0; i < n; ++i)
    if (!vis[i])
      dfs1(i);
  reverse(order.begin(), order.end());
  fill(vis, vis+n, 0);
  for (int i : order)
    if (!vis[i])
      dfs2(i, i);

  /*
  cerr << "scc: ";
  for (int i = 0; i < n; ++i)
    cerr << scc[i] << " \n"[i==n-1]; // */

  vector<int> e_cnt(n), ans(n, 1);
  for (int mm = 0; mm < m; ++mm)
    e_cnt[scc[u[mm]]] += scc[u[mm]] == scc[v[mm]];

  /*
  cerr << "e_cnt: ";
  for (int i = 0; i < n; ++i)
    cerr << e_cnt[i] << " \n"[i==n-1]; // */

  for (int i = 0; i < n; ++i)
    ans[scc[i]] &= e_cnt[scc[i]] && !r[i];

  for (int i = 0; i < n; ++i)
    ans[i] = ans[scc[i]];

  //*
  cerr << "scc_ans: ";
  for (int i = 0; i < n; ++i)
    cerr << ans[i] << " \n"[i==n-1]; // */

  for (int i = 0; i < n; ++i)
    gp[i].clear();
  for (int mm = 0; mm < m; ++mm)
    gp[v[mm]].push_back(u[mm]);

  fill(vis, vis+n, 0);
  for (int i = 0; i < n; ++i)
    if (!vis[i] && ans[i]==0)
      dfs3(ans, i);

  return ans;
}
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 1504 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 1100 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 7 ms 1996 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 1388 KB 3rd lines differ - on the 1st token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 1996 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 1504 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -