# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
718555 | lam | Game (APIO22_game) | C++17 | 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 <cstdio>
#include <cstdlib>
#include <vector>
#include "game.h"
namespace {
int read_int() {
int x;
if (scanf("%d", &x) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
return x;
}
} // namespace
#include "game.h"
#include <bits/stdc++.h>
using namespace std;
int n,k;
typedef pair<int,int> ii;
#define ff first
#define ss second
const int maxn = 3e4 + 10;
vector <int> adj[maxn];
bool vis[1010][maxn];
stack<int> st;
bool dau[maxn];
bool ccheck = 0;
void dfs(int id, int x)
{
vis[id][x] = 1;
if (x <= id) ccheck = 1;
for (int i:adj[x])
if (!vis[id][i]) dfs(id,i);
}
void init(int N, int K) {
n=N; k=K; ccheck=0;
for (int i=1; i<=n; i++) adj[i].clear();
for (int i=2; i<=k; i++) adj[i-1].push_back(i);
for (int i=1; i<k; i++)
dfs(i,i+1);
}
int add_teleporter(int u, int v) {
u++; v++;
adj[u].push_back(v);
for (int i=1; i<=k; i++)
if ((u==i||vis[i][u])&&!vis[i][v]) dfs(i,v);
return ccheck;
}
int main() {
int N = read_int();
int M = read_int();
int K = read_int();
std::vector<int> u(M), v(M);
for (int i = 0; i < M; ++i) {
u[i] = read_int();
v[i] = read_int();
}
init(N, K);
int i;
for (i = 0; i < M; ++i) {
int answer = add_teleporter(u[i], v[i]);
if (answer != 0 && answer != 1) {
i = -1;
break;
} else if (answer == 1) {
break;
}
}
printf("%d\n", i);
return 0;
}