#include <bits/stdc++.h>
#define all(s) s.begin(), s.end()
#define lb(s, a) lower_bound(all(s), (a)) - s.begin()
#define ii pair <int, int>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int ar = 2e5+2;
const ll mod = 1e9+7;
const int oo = 1e9;
int n, m;
int timeDfs = 0, scc = 0;
int low[ar], num[ar];
int id[ar];
bool deleted[ar];
vector <int> tplt[ar];
struct set3 {
int v, i, t;
bool operator < (set3 a) {
return v < a.v;
}
bool operator == (set3 a) {
return v == a.v;
}
};
vector <set3> g[ar];
stack <int> st;
void dfs(int u, int p) {
num[u] = low[u] = ++timeDfs;
st.push(u);
for (auto [v, i, t] : g[u]) if(v != p) {
if (deleted[v]) continue;
if (!num[v]){
dfs(v, u);
low[u] = min(low[u], low[v]);
}
else low[u] = min(low[u], num[v]);
}
if (low[u] == num[u]) {
scc++;
int v;
do {
v = st.top();
st.pop();
tplt[scc].push_back(v);
id[v] = scc;
deleted[v] = true;
}
while (v != u);
}
}
int h[ar], up[ar][18], val[ar];
set3 par[ar];
void dfs2(int u, int p) {
for(auto x : tplt[u]) {
for(auto [v, i, t] : g[x]) {
v = id[v];
if(v == u || v == p) continue;
h[v] = h[u] + 1;
up[v][0] = u;
par[v] = {u, i, t};
for(int j = 1; j < 17; ++j)
up[v][j] = up[up[v][j - 1]][j - 1];
dfs2(v, u);
}
}
}
int lca(int u, int v) {
if(h[u] != h[v]) {
if(h[u] < h[v]) swap(u, v);
int k = h[u] - h[v];
for(int j = 0; (1 << j) <= k; ++j)
if(k >> j & 1)
u = up[u][j];
}
if(u == v) return u;
int k = 31 - __builtin_clz(h[u]);
for(int j = k; j >= 0; --j)
if(up[u][j] != up[v][j]) {
u = up[u][j];
v = up[v][j];
}
return up[u][0];
}
void direct(int u, int p, int t) {
int goc = u;
if(u == p) return;
while(u != p) {
// cout << u << '\n';
if(h[par[u].v] < h[p] || par[u].v == 0)
break;
val[par[u].i] = par[u].t ^ t;
u = par[u].v;
}
u = goc;
set3 tmp;
while(u != p) {
// cout << u << '\n';
if(h[par[u].v] < h[p] || par[u].v == 0) break;
tmp = par[u];
par[u] = par[p];
u = tmp.v;
}
}
vector <int> same[ar];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "CEOI17_ONEWAY"
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> m;
int u, v;
memset(val, -1, sizeof val);
for(int i = 1; i <= m; ++i) {
cin >> u >> v;
g[u].push_back({v, i, 0});
g[v].push_back({u, i, 1});
}
for(int i = 1; i <= n; ++i) {
sort(all(g[i]));
for(int j = 0; j < g[i].size();) {
int k = j + 1;
while(k < g[i].size() && g[i][j].v == g[i][k].v)
same[g[i][j].i].push_back(g[i][k].i), ++k;
j = k;
}
g[i].resize(unique(all(g[i])) - g[i].begin());
}
for(int i = 1; i <= n; ++i) if(id[i] == 0) {
dfs(i, 0);
dfs2(id[i], 0);
}
// for(int i = 1; i <= scc; ++i)
// cout << par[i].v << ' ' << h[i] << '\n';
int q;
cin >> q;
while(q--) {
cin >> u >> v;
u = id[u]; v = id[v];
int p = lca(u, v);
// cout << u << ' ' << v << ' ' << p << '\n';
direct(u, p, 0);
direct(v, p, 1);
// cout << "end\n";
}
for(int i = 1; i <= m; ++i)
for(auto j : same[i])
val[j] = val[i];
// cout << m << '\n';
for(int i = 1; i <= m; ++i)
if(val[i] == -1) cout << 'B';
else if(val[i] == 0) cout << 'L';
else cout << 'R';
}
Compilation message
oneway.cpp: In function 'int main()':
oneway.cpp:137:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<set3>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for(int j = 0; j < g[i].size();) {
| ~~^~~~~~~~~~~~~
oneway.cpp:139:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<set3>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | while(k < g[i].size() && g[i][j].v == g[i][k].v)
| ~~^~~~~~~~~~~~~
oneway.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
oneway.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
21080 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21084 KB |
Output is correct |
5 |
Correct |
3 ms |
21148 KB |
Output is correct |
6 |
Correct |
3 ms |
21084 KB |
Output is correct |
7 |
Correct |
3 ms |
21172 KB |
Output is correct |
8 |
Correct |
4 ms |
21160 KB |
Output is correct |
9 |
Incorrect |
4 ms |
21084 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
21080 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21084 KB |
Output is correct |
5 |
Correct |
3 ms |
21148 KB |
Output is correct |
6 |
Correct |
3 ms |
21084 KB |
Output is correct |
7 |
Correct |
3 ms |
21172 KB |
Output is correct |
8 |
Correct |
4 ms |
21160 KB |
Output is correct |
9 |
Incorrect |
4 ms |
21084 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
21080 KB |
Output is correct |
2 |
Correct |
3 ms |
21080 KB |
Output is correct |
3 |
Correct |
3 ms |
21084 KB |
Output is correct |
4 |
Correct |
3 ms |
21084 KB |
Output is correct |
5 |
Correct |
3 ms |
21148 KB |
Output is correct |
6 |
Correct |
3 ms |
21084 KB |
Output is correct |
7 |
Correct |
3 ms |
21172 KB |
Output is correct |
8 |
Correct |
4 ms |
21160 KB |
Output is correct |
9 |
Incorrect |
4 ms |
21084 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |