This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "coprobber.h"
#include <bits/stdc++.h>
using namespace std;
int N, C;
vector<int> adj[MAX_N];
int dist[MAX_N][MAX_N];
int calls, maxSCC;
int num[MAX_N], low[MAX_N];
bool instk[MAX_N];
vector<int> stk;
void tarjan(int u, int p) {
num[u] = low[u] = calls++;
instk[u] = true;
stk.push_back(u);
for (int v : adj[u]) {
if (num[v] == -1) {
tarjan(v, u);
}
if (instk[v] && v != p) {
low[u] = min(low[u], low[v]);
}
}
if (low[u] == num[u]) {
int sz = 0;
// cout << "new SCC: ";
while (true) {
int v = stk.back();
stk.pop_back();
instk[v] = false;
++sz;
// cout << v << " ";
if (v == u) {
break;
}
}
// cout << endl;
maxSCC = max(maxSCC, sz);
}
}
void bfs(int src, int dist[MAX_N]) {
fill(dist, dist+N, -1);
dist[src] = 0;
queue<int> q;
q.push(src);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : adj[u]) {
if (dist[v] == -1) {
dist[v] = dist[u] + 1;
q.push(v);
}
}
}
}
int start(int _N, bool A[MAX_N][MAX_N]) {
N = _N;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (A[i][j]) {
adj[i].push_back(j);
}
}
}
maxSCC = 0;
calls = 0;
memset(num, -1, sizeof(num));
memset(instk, false, sizeof(instk));
for (int i = 0; i < N; ++i) {
if (num[i] == -1) {
tarjan(i, -1);
}
}
// cout << "max SCC size: " << maxSCC << endl;
if (maxSCC > 3) {
// there is cycle with size bigger than 3
// robber can always win
return -1;
}
for (int i = 0; i < N; ++i) {
bfs(i, dist[i]);
}
C = 0;
return C;
}
int nextMove(int R) {
int best_next = adj[C][0];
for (int v : adj[C]) {
if (dist[v][R] < dist[best_next][R]) {
best_next = v;
}
}
C = best_next;
return C;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |