#ifndef Nhoksocqt1
#include "game.h"
#endif // Nhoksocqt1
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
template<class X, class Y>
inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int MAXN = 300005;
const int MAXM = 800005;
vector<ii> adj[MAXN], adjr[MAXN];
int min_spe_to[MAXN], max_spe_from[MAXN];
int low[MAXN], num[MAXN], numNode, numEdge, numSpecial;
bool dx[MAXN], dxe[MAXM];
void init(int _N, int _K) {
numNode = _N, numSpecial = _K, numEdge = 0;
for (int i = 0; i + 1 < numSpecial; ++i)
adj[i].push_back(ii(i + 1, numEdge++));
for (int i = 0; i < numNode; ++i) {
min_spe_to[i] = numSpecial;
max_spe_from[i] = (i < numSpecial) ? i : -1;
}
}
stack<int> st;
bool tarjan(int u) {
low[u] = num[u] = ++num[numNode];
st.push(u);
for (int it = 0; it < sz(adj[u]); ++it) {
int v(adj[u][it].fi), id(adj[u][it].se);
if(!dxe[id]) {
dxe[id] = 1;
if(!num[v]) {
if(tarjan(v))
return true;
low[u] = min(low[u], low[v]);
} else {
low[u] = min(low[u], num[v]);
}
}
}
if(low[u] == num[u]) {
int v, cnt(0);
bool has_special(0);
do {
v = st.top(); st.pop();
low[v] = num[v] = 1e9+7;
has_special |= (v < numSpecial), ++cnt;
} while(v != u);
return (cnt > 1 && has_special);
}
return false;
}
bool sub3(int u, int v) {
adj[u].push_back(ii(v, numEdge++));
for (int i = 0; i <= numNode; ++i)
low[i] = num[i] = 0;
while(sz(st))
st.pop();
for (int i = 0; i < numEdge; ++i)
dxe[i] = 0;
for (int i = 0; i < numSpecial; ++i) {
if(!num[i] && tarjan(i))
return true;
}
return false;
}
bool brute(int u, int v) {
adj[u].push_back(ii(v, 0));
adjr[v].push_back(ii(u, 0));
queue<int> qu;
if(min_spe_to[u] > min(min_spe_to[v], v)) {
min_spe_to[u] = min(min_spe_to[v], v);
qu.push(u);
}
while(sz(qu)) {
int u(qu.front()); qu.pop();
if(max_spe_from[u] >= min_spe_to[u])
return true;
for (int it = 0; it < sz(adjr[u]); ++it) {
int v(adjr[u][it].fi);
if(min_spe_to[v] > min_spe_to[u]) {
min_spe_to[v] = min_spe_to[u];
qu.push(v);
}
}
}
int max_spe_from_now = max(max_spe_from[u], (u < numSpecial ? u : -1));
if(max_spe_from[v] < max_spe_from_now) {
max_spe_from[v] = max_spe_from_now;
qu.push(v);
}
while(sz(qu)) {
int u(qu.front()); qu.pop();
if(max_spe_from[u] >= min_spe_to[u])
return true;
for (int it = 0; it < sz(adj[u]); ++it) {
int v(adj[u][it].fi);
if(max_spe_from[v] < max_spe_from[u]) {
max_spe_from[v] = max_spe_from[u];
qu.push(v);
}
}
}
return false;
}
int add_teleporter(int u, int v) {
if(max(u, v) < numSpecial)
return (u >= v);
if(u == v)
return false;
if(numNode <= 1000) {
return sub3(u, v);
} else
if(numNode <= 30000 && numSpecial <= 1000) {
return brute(u, v);
}
return false;
}
#ifdef Nhoksocqt1
int main(void) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define TASK "game"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int n, k, m;
cin >> n >> k >> m;
init(n, k);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
int ans = add_teleporter(u, v);
cout << "ANSWER FOR EDGE " << u << " TO " << v << ": " << ans << '\n';
}
return 0;
}
#endif // Nhoksocqt1
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20108 KB |
Output is correct |
2 |
Correct |
5 ms |
20056 KB |
Output is correct |
3 |
Correct |
4 ms |
20056 KB |
Output is correct |
4 |
Correct |
4 ms |
20056 KB |
Output is correct |
5 |
Correct |
4 ms |
20056 KB |
Output is correct |
6 |
Correct |
4 ms |
20056 KB |
Output is correct |
7 |
Correct |
6 ms |
20056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20108 KB |
Output is correct |
2 |
Correct |
5 ms |
20056 KB |
Output is correct |
3 |
Correct |
4 ms |
20056 KB |
Output is correct |
4 |
Correct |
4 ms |
20056 KB |
Output is correct |
5 |
Correct |
4 ms |
20056 KB |
Output is correct |
6 |
Correct |
4 ms |
20056 KB |
Output is correct |
7 |
Correct |
6 ms |
20056 KB |
Output is correct |
8 |
Correct |
4 ms |
20056 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
4 ms |
20056 KB |
Output is correct |
11 |
Correct |
4 ms |
20056 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
4 ms |
20056 KB |
Output is correct |
14 |
Correct |
4 ms |
20056 KB |
Output is correct |
15 |
Correct |
5 ms |
20056 KB |
Output is correct |
16 |
Correct |
4 ms |
20056 KB |
Output is correct |
17 |
Correct |
5 ms |
20056 KB |
Output is correct |
18 |
Correct |
6 ms |
20056 KB |
Output is correct |
19 |
Correct |
4 ms |
20056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20108 KB |
Output is correct |
2 |
Correct |
5 ms |
20056 KB |
Output is correct |
3 |
Correct |
4 ms |
20056 KB |
Output is correct |
4 |
Correct |
4 ms |
20056 KB |
Output is correct |
5 |
Correct |
4 ms |
20056 KB |
Output is correct |
6 |
Correct |
4 ms |
20056 KB |
Output is correct |
7 |
Correct |
6 ms |
20056 KB |
Output is correct |
8 |
Correct |
4 ms |
20056 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
4 ms |
20056 KB |
Output is correct |
11 |
Correct |
4 ms |
20056 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
4 ms |
20056 KB |
Output is correct |
14 |
Correct |
4 ms |
20056 KB |
Output is correct |
15 |
Correct |
5 ms |
20056 KB |
Output is correct |
16 |
Correct |
4 ms |
20056 KB |
Output is correct |
17 |
Correct |
5 ms |
20056 KB |
Output is correct |
18 |
Correct |
6 ms |
20056 KB |
Output is correct |
19 |
Correct |
4 ms |
20056 KB |
Output is correct |
20 |
Correct |
6 ms |
20140 KB |
Output is correct |
21 |
Correct |
4 ms |
20056 KB |
Output is correct |
22 |
Correct |
6 ms |
20056 KB |
Output is correct |
23 |
Correct |
5 ms |
20056 KB |
Output is correct |
24 |
Correct |
17 ms |
20224 KB |
Output is correct |
25 |
Correct |
52 ms |
20400 KB |
Output is correct |
26 |
Correct |
64 ms |
20332 KB |
Output is correct |
27 |
Correct |
78 ms |
20312 KB |
Output is correct |
28 |
Correct |
31 ms |
20560 KB |
Output is correct |
29 |
Correct |
45 ms |
20216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20108 KB |
Output is correct |
2 |
Correct |
5 ms |
20056 KB |
Output is correct |
3 |
Correct |
4 ms |
20056 KB |
Output is correct |
4 |
Correct |
4 ms |
20056 KB |
Output is correct |
5 |
Correct |
4 ms |
20056 KB |
Output is correct |
6 |
Correct |
4 ms |
20056 KB |
Output is correct |
7 |
Correct |
6 ms |
20056 KB |
Output is correct |
8 |
Correct |
4 ms |
20056 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
4 ms |
20056 KB |
Output is correct |
11 |
Correct |
4 ms |
20056 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
4 ms |
20056 KB |
Output is correct |
14 |
Correct |
4 ms |
20056 KB |
Output is correct |
15 |
Correct |
5 ms |
20056 KB |
Output is correct |
16 |
Correct |
4 ms |
20056 KB |
Output is correct |
17 |
Correct |
5 ms |
20056 KB |
Output is correct |
18 |
Correct |
6 ms |
20056 KB |
Output is correct |
19 |
Correct |
4 ms |
20056 KB |
Output is correct |
20 |
Correct |
6 ms |
20140 KB |
Output is correct |
21 |
Correct |
4 ms |
20056 KB |
Output is correct |
22 |
Correct |
6 ms |
20056 KB |
Output is correct |
23 |
Correct |
5 ms |
20056 KB |
Output is correct |
24 |
Correct |
17 ms |
20224 KB |
Output is correct |
25 |
Correct |
52 ms |
20400 KB |
Output is correct |
26 |
Correct |
64 ms |
20332 KB |
Output is correct |
27 |
Correct |
78 ms |
20312 KB |
Output is correct |
28 |
Correct |
31 ms |
20560 KB |
Output is correct |
29 |
Correct |
45 ms |
20216 KB |
Output is correct |
30 |
Correct |
18 ms |
21624 KB |
Output is correct |
31 |
Correct |
10 ms |
20824 KB |
Output is correct |
32 |
Correct |
24 ms |
22332 KB |
Output is correct |
33 |
Correct |
23 ms |
22428 KB |
Output is correct |
34 |
Correct |
936 ms |
22692 KB |
Output is correct |
35 |
Correct |
323 ms |
22500 KB |
Output is correct |
36 |
Correct |
39 ms |
22388 KB |
Output is correct |
37 |
Correct |
26 ms |
22176 KB |
Output is correct |
38 |
Correct |
29 ms |
21880 KB |
Output is correct |
39 |
Correct |
39 ms |
22616 KB |
Output is correct |
40 |
Correct |
740 ms |
22648 KB |
Output is correct |
41 |
Correct |
154 ms |
22380 KB |
Output is correct |
42 |
Correct |
107 ms |
22008 KB |
Output is correct |
43 |
Correct |
39 ms |
22808 KB |
Output is correct |
44 |
Correct |
389 ms |
22396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
20108 KB |
Output is correct |
2 |
Correct |
5 ms |
20056 KB |
Output is correct |
3 |
Correct |
4 ms |
20056 KB |
Output is correct |
4 |
Correct |
4 ms |
20056 KB |
Output is correct |
5 |
Correct |
4 ms |
20056 KB |
Output is correct |
6 |
Correct |
4 ms |
20056 KB |
Output is correct |
7 |
Correct |
6 ms |
20056 KB |
Output is correct |
8 |
Correct |
4 ms |
20056 KB |
Output is correct |
9 |
Correct |
4 ms |
20056 KB |
Output is correct |
10 |
Correct |
4 ms |
20056 KB |
Output is correct |
11 |
Correct |
4 ms |
20056 KB |
Output is correct |
12 |
Correct |
4 ms |
20056 KB |
Output is correct |
13 |
Correct |
4 ms |
20056 KB |
Output is correct |
14 |
Correct |
4 ms |
20056 KB |
Output is correct |
15 |
Correct |
5 ms |
20056 KB |
Output is correct |
16 |
Correct |
4 ms |
20056 KB |
Output is correct |
17 |
Correct |
5 ms |
20056 KB |
Output is correct |
18 |
Correct |
6 ms |
20056 KB |
Output is correct |
19 |
Correct |
4 ms |
20056 KB |
Output is correct |
20 |
Correct |
6 ms |
20140 KB |
Output is correct |
21 |
Correct |
4 ms |
20056 KB |
Output is correct |
22 |
Correct |
6 ms |
20056 KB |
Output is correct |
23 |
Correct |
5 ms |
20056 KB |
Output is correct |
24 |
Correct |
17 ms |
20224 KB |
Output is correct |
25 |
Correct |
52 ms |
20400 KB |
Output is correct |
26 |
Correct |
64 ms |
20332 KB |
Output is correct |
27 |
Correct |
78 ms |
20312 KB |
Output is correct |
28 |
Correct |
31 ms |
20560 KB |
Output is correct |
29 |
Correct |
45 ms |
20216 KB |
Output is correct |
30 |
Correct |
18 ms |
21624 KB |
Output is correct |
31 |
Correct |
10 ms |
20824 KB |
Output is correct |
32 |
Correct |
24 ms |
22332 KB |
Output is correct |
33 |
Correct |
23 ms |
22428 KB |
Output is correct |
34 |
Correct |
936 ms |
22692 KB |
Output is correct |
35 |
Correct |
323 ms |
22500 KB |
Output is correct |
36 |
Correct |
39 ms |
22388 KB |
Output is correct |
37 |
Correct |
26 ms |
22176 KB |
Output is correct |
38 |
Correct |
29 ms |
21880 KB |
Output is correct |
39 |
Correct |
39 ms |
22616 KB |
Output is correct |
40 |
Correct |
740 ms |
22648 KB |
Output is correct |
41 |
Correct |
154 ms |
22380 KB |
Output is correct |
42 |
Correct |
107 ms |
22008 KB |
Output is correct |
43 |
Correct |
39 ms |
22808 KB |
Output is correct |
44 |
Correct |
389 ms |
22396 KB |
Output is correct |
45 |
Incorrect |
60 ms |
20312 KB |
Wrong Answer[1] |
46 |
Halted |
0 ms |
0 KB |
- |