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<bits/stdc++.h>
using namespace std;
mt19937 rnd(51);
#define ll long long
#define pb push_back
#define ld long double
const int N = 3e5 + 10, INF = 1e9, K = 500;
int n, k, in_block[N], out_block[N], in[N], out[N], block[N], cnt[N];
bool used[N];
vector<int> g[N], gr[N];
bool cycle = 0;
set<int> need;
void dfs_block(int v) {
if (v >= k && out_block[v] < in_block[v]) {
cycle = 1;
}
if (v >= k && !used[v] && out_block[v] == in_block[v]) {
need.insert(in_block[v]);
block[v] = in_block[v];
used[v] = 1;
}
for (auto to : g[v]) {
if (in_block[v] > in_block[to]) {
in_block[to] = in_block[v];
dfs_block(to);
}
}
}
void zhfs_block(int v) {
if (v >= k && out_block[v] < in_block[v]) {
cycle = 1;
}
if (v >= k && !used[v] && out_block[v] == in_block[v]) {
need.insert(in_block[v]);
block[v] = in_block[v];
used[v] = 1;
}
for (auto to : gr[v]) {
if (out_block[v] < out_block[to]) {
out_block[to] = out_block[v];
zhfs_block(to);
}
}
}
void dfs(int v) {
cnt[v]++;
assert(cnt[v] <= N / K);
if (v >= k && out[v] <= in[v]) {
cycle = 1;
}
for (auto to : g[v]) {
if (block[v] == block[to] && in[v] > in[to]) {
in[to] = in[v];
dfs(to);
}
}
}
void zhfs(int v) {
cnt[v]++;
assert(cnt[v] <= N / K);
if (v >= k && out[v] <= in[v]) {
cycle = 1;
}
for (auto to : gr[v]) {
if (block[v] == block[to] && out[v] < out[to]) {
out[to] = out[v];
zhfs(to);
}
}
}
int add_teleporter(int u, int v) {
if (max(u, v) < k) {
if (u >= v) {
cycle = 1;
}
} else {
g[u].pb(v);
gr[v].pb(u);
if (in_block[v] < in_block[u]) {
in_block[v] = in_block[u];
dfs_block(v);
}
if (out_block[u] > out_block[v]) {
out_block[u] = out_block[v];
zhfs_block(u);
}
for (auto v : need) {
for (int vert = v * K; vert < min(k, (v + 1) * K); vert++) {
zhfs(vert);
dfs(vert);
}
}
need.clear();
if (block[v] == block[u] && in[v] < in[u]) {
in[v] = in[u];
dfs(v);
}
if (block[v] == block[u] && out[u] > out[v]) {
out[u] = out[v];
zhfs(u);
}
}
return cycle;
}
void init(int n_, int k_) {
n = n_, k = k_;
for (int i = 0; i < n; i++) {
in_block[i] = -INF, out_block[i] = INF;
in[i] = -INF, out[i] = INF;
block[i] = -(i + 1);
}
for (int i = 0; i < k; i++) {
in_block[i] = out_block[i] = block[i] = i / K;
in[i] = out[i] = i;
if (i + 1 < k) {
add_teleporter(i, i + 1);
}
}
}
#ifdef LOCAL
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<pair<int,int>> e(m);
for (int i = 0; i < m; i++) {
cin >> e[i].first >> e[i].second;
}
init(n, k);
int i;
for (i = 0; i < m; i++) {
int res = add_teleporter(e[i].first, e[i].second);
assert(res == 0 || res == 1);
if (res == 1) {
break;
}
}
cout << i << endl;
return 0;
}
#endif // LOCAL
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |