#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const int maxk = 5010, maxn = 30010;
vector < int > g[maxn], ng[maxn];
int lat[maxn], erl[maxn], K;
int tf = 0;
void init(int n, int k)
{
K = k;
for (int i = 0; i < k - 1; i ++)
{
g[i].push_back(i + 1);
ng[i + 1].push_back(i);
}
for (int i = 0; i < k; i ++)
erl[i] = i, lat[i] = i - 1;
for (int i = k; i < n; i ++)
erl[i] = k, lat[i] = -1;
}
void update_erl(int v)
{
if (lat[v] >= erl[v])
tf = 1;
for (int u : ng[v])
{
if (erl[v] < erl[u])
{
erl[u] = erl[v];
update_erl(u);
}
}
}
void update_lat(int u)
{
if (lat[u] >= erl[u])
tf = 1;
int mx = lat[u];
/*if (u < K)
mx = max(mx, u);*/
for (int v : g[u])
{
if (lat[v] < mx)
{
lat[v] = mx;
update_lat(v);
}
}
}
int add_teleporter(int u, int v)
{
g[u].push_back(v);
ng[v].push_back(u);
if (erl[v] < erl[u])
{
erl[u] = erl[v];
update_erl(u);
}
int mx = lat[u];
if (u < K)
mx = max(u, mx);
if (mx > lat[v])
{
lat[v] = mx;
update_lat(v);
}
return tf;
}
namespace {
int read_int() {
int x;
if (scanf("%d", &x) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
return x;
}
} // namespace
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;
}
/**
6 6 3
3 4
5 0
4 5
5 3
1 4
1 4
*/
Compilation message
game.cpp: In function 'void init(int, int)':
game.cpp:20:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
20 | for (int i = 0; i < k; i ++)
| ^~~
game.cpp:22:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
22 | for (int i = k; i < n; i ++)
| ^~~
/usr/bin/ld: /tmp/ccLdnBuX.o: in function `main':
stub.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cckDGFQU.o:game.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status