# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
956569 | ezzzay | Cop and Robber (BOI14_coprobber) | C++14 | 0 ms | 0 KiB |
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 <iostream>
#include<bits/stdc++.h>
using namespace std;
#define MAX_N 500
#define pb push_back
bool vis[MAX_N];
int x=1;
vector<int>v[MAX_N];
int c=0;
int start(int N, bool A[MAX_N][MAX_N]) {
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(A[i][j]){
v[i].pb(j);
}
}
}
for(int i=0;i<N;i++){
if(v[i].size()==1){
x=i;
break;
}
}
return x;
}
int h=0;
void dfs(int a, int p){
vis[a]=1;
if(a==c){
h=p;
return;
}
for(auto b:v[a]){
if(vis[b]==0)dfs(b,p);
}
}
int nextMove(int R) {
c=R;
memset(vis,0,sizeof vis);
for(auto y:v[x]){
dfs(y,y);
}
return h;
}
int main() {
int N;
cin >> N;
bool A[MAX_N][MAX_N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> A[i][j];
}
}
int P = start(N,A);
cout << P << endl;
int R;
cin >> R;
while (true) {
if (P == R) break;
P = nextMove(R);
cout << P << endl;
if (P == R) break;
cin >> R;
}
}