Submission #572589

# Submission time Handle Problem Language Result Execution time Memory
572589 2022-06-04T17:59:26 Z cjoa Toy Train (IOI17_train) C++17
28 / 100
209 ms 1364 KB
#include "train.h"
#include <queue>
#include <algorithm>

#include <iostream>
#include <cassert>

using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;

enum Player {
   Arezou = 1, Borzou = 0
};

enum State {
   UNKNOWN = -1, LOSING = 0, POSSIBLY_WINNING = 1
};


int N, M;
VVI G;
VI in_deg;
VI owner;

void bfs(int player, const VI& sources, vector<int>& states) {
   queue<int> q;
   for (int i : sources)
      q.push(i);

   VI num_neighbors_required(G.size());
   for (int i = 0; i < N; i++) {
      if (states[i] == UNKNOWN) {
         if (owner[i] == player)
            num_neighbors_required[i] = 1;
         else
            num_neighbors_required[i] = in_deg[i];
      }
   }

   while (!q.empty()) {
      int cur = q.front(); q.pop();
      for (int nxt : G[cur]) {
         if (states[nxt] != UNKNOWN)
            continue;
         num_neighbors_required[nxt]--;
         if (num_neighbors_required[nxt] == 0) {
            q.push(nxt);
            states[nxt] = player == Arezou ? POSSIBLY_WINNING : LOSING;
         }
      }
   }
}

std::vector<int> who_wins(std::vector<int> a, std::vector<int> r,
                          std::vector<int> u, std::vector<int> v)
{
   owner = a;

   N = a.size();
   M = u.size();
   assert( (int) v.size() == M );
   
   // Build transpose graph G (that is, reverse each edge)
   G = VVI(N);
   in_deg = VI(N);
   for (int j = 0; j < M; ++j) {
      G[ v[j] ].push_back( u[j] );
      in_deg[ u[j] ]++;
   }

   vector<int> states(N, UNKNOWN);
   for (int ronda = 1; ronda <= 10000; ronda++) {
      VI sources;
      for (int i = 0; i < N; i++)
         if (r[i] == 1 and states[i] != LOSING) {
            sources.push_back(i);
            states[i] = POSSIBLY_WINNING;
         }

      bfs(Arezou, sources, states);

      /*
      cerr << "Round #" << ronda << endl;
      for (int i = 0; i < N; i++)
         cerr << states[i] << ' ';
      cerr << endl;
      */

      if (count(states.begin(), states.end(), UNKNOWN) == 0)
         break;

      sources.clear();
      for (int i = 0; i < N; i++) {
         switch (states[i]) {
            case UNKNOWN:
               states[i] = LOSING;
               sources.push_back(i);
               break;
            case POSSIBLY_WINNING:
               states[i] = UNKNOWN;
               break;
         }
      }

      bfs(Borzou, sources, states);

      /*
      for (int i = 0; i < N; i++)
         cerr << states[i] << ' ';
      cerr << endl;
      */

      if (count(states.begin(), states.end(), UNKNOWN) == 0)
         break;

   }

   VI res(N);
   for (int i = 0; i < N; i++) {
      assert( states[i] != UNKNOWN );
      if (states[i] == LOSING)
         res[i] = 0;
      else
         res[i] = 1;
   }

   return res;
}

# Verdict Execution time Memory Grader output
1 Correct 5 ms 940 KB Output is correct
2 Correct 5 ms 952 KB Output is correct
3 Correct 4 ms 852 KB Output is correct
4 Correct 7 ms 952 KB Output is correct
5 Correct 5 ms 852 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 4 ms 852 KB Output is correct
8 Correct 6 ms 852 KB Output is correct
9 Correct 5 ms 856 KB Output is correct
10 Correct 4 ms 852 KB Output is correct
11 Correct 5 ms 824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 304 KB Output is correct
5 Correct 0 ms 296 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Incorrect 0 ms 212 KB 3rd lines differ - on the 2nd token, expected: '0', found: '1'
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 90 ms 1312 KB Output is correct
2 Correct 173 ms 1332 KB Output is correct
3 Correct 209 ms 1344 KB Output is correct
4 Correct 7 ms 1340 KB Output is correct
5 Incorrect 7 ms 1236 KB 3rd lines differ - on the 11th token, expected: '0', found: '1'
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1084 KB Output is correct
2 Correct 6 ms 1108 KB Output is correct
3 Correct 7 ms 1344 KB Output is correct
4 Correct 10 ms 1348 KB Output is correct
5 Correct 6 ms 1236 KB Output is correct
6 Correct 7 ms 1248 KB Output is correct
7 Correct 6 ms 1308 KB Output is correct
8 Correct 6 ms 1236 KB Output is correct
9 Correct 6 ms 1208 KB Output is correct
10 Correct 7 ms 1236 KB Output is correct
11 Correct 6 ms 1364 KB Output is correct
12 Correct 6 ms 1236 KB Output is correct
13 Correct 6 ms 1236 KB Output is correct
14 Correct 6 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1236 KB Output is correct
2 Correct 7 ms 1212 KB Output is correct
3 Correct 8 ms 1236 KB Output is correct
4 Correct 7 ms 1108 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 3 ms 852 KB Output is correct
7 Correct 4 ms 852 KB Output is correct
8 Correct 7 ms 928 KB Output is correct
9 Correct 4 ms 852 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 4 ms 824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 940 KB Output is correct
2 Correct 5 ms 952 KB Output is correct
3 Correct 4 ms 852 KB Output is correct
4 Correct 7 ms 952 KB Output is correct
5 Correct 5 ms 852 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 4 ms 852 KB Output is correct
8 Correct 6 ms 852 KB Output is correct
9 Correct 5 ms 856 KB Output is correct
10 Correct 4 ms 852 KB Output is correct
11 Correct 5 ms 824 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 304 KB Output is correct
16 Correct 0 ms 296 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Incorrect 0 ms 212 KB 3rd lines differ - on the 2nd token, expected: '0', found: '1'
20 Halted 0 ms 0 KB -