이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#include <bits/stdc++.h>
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<typename T>
bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef KIVI
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif
const int max_n = 1e5 + 11, inf = 1000111222;
struct ed {
int to, id;
};
vector <ed> edge[max_n], g[max_n];
int used[max_n], bridge[max_n], tin[max_n], timer = 1, low[max_n];
inline void dfs (int v, int p = -1) {
used[v] = 1;
tin[v] = low[v] = timer++;
for (auto &e : edge[v]) {
int to = e.to, id = e.id;
if (id == p) {
continue;
}
if (used[to]) {
umin(low[v], tin[to]);
}
else {
dfs(to, id);
umin(low[v], low[to]);
if (low[to] > tin[v]) {
bridge[id] = 1;
}
}
}
}
int cmp[max_n];
inline void color (int v, int c) {
used[v] = 2;
cmp[v] = c;
for (auto &e : edge[v]) {
int to = e.to, id = e.id;
if (used[to] == 1 && !bridge[id]) {
color(to, c);
}
}
}
int up[max_n], down[max_n];
int res[max_n], d[max_n];
inline void go (int v, int p = -1) {
used[v] = 4;
for (auto &i : g[v]) {
int to = i.to, id = i.id;
if (to == p) {
continue;
}
d[to] = d[v] + 1;
go(to, v);
if (up[to]) {
res[id] = 1;
}
if (down[to]) {
res[id] = 2;
}
up[v] += up[to];
down[v] += down[to];
}
}
int L = 0, T = 0;
vector <int> upp[max_n];
int tin1[max_n], tout[max_n];
inline void dfs_lca (int v, int p = -1) {
tin1[v] = T++;
used[v] = 3;
upp[v].resize(L + 1);
upp[v][0] = p == -1 ? v : p;
for (int i = 1; i <= L; i++) {
upp[v][i] = upp[upp[v][i - 1]][i - 1];
}
for (auto &i : g[v]) {
int to = i.to;
if (to == p) continue;
dfs_lca(to, v);
}
tout[v] = T;
}
inline bool upper (int a, int b) {
return tin1[a] <= tin1[b] && tout[b] <= tout[a];
}
inline int lca (int a, int b) {
if (upper(a, b)) return a;
if (upper(b, a)) return b;
for (int i = L; i >= 0; i--) {
if (!upper(upp[a][i], b)) {
a = upp[a][i];
}
}
return upp[a][0];
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, p;
cin >> n >> m;
while ((1 << L) <= n) ++L;
vector <pii> e(m);
for (int i = 0, a, b; i < m; i++) {
cin >> a >> b;
--a, --b;
e[i] = {a, b};
edge[a].pb({b, i});
edge[b].pb({a, i});
}
cin >> p;
for (int i = 0; i < n; i++) {
if (!used[i]) {
dfs(i);
}
}
int num = 0;
for (int i = 0; i < n; i++) {
if (used[i] == 1) {
color(i, num);
++num;
}
}
for (int i = 0, j = 0; i < m; i++) {
int a = cmp[e[i].first];
int b = cmp[e[i].second];
if (a != b) {
g[a].pb({b, j});
g[b].pb({a, j});
++j;
}
}
for (int i = 0; i < num; i++) {
if (used[i] == 2) {
dfs_lca(i);
}
}
for (int i = 0, a, b; i < p; i++) {
cin >> a >> b;
--a, --b;
a = cmp[a];
b = cmp[b];
if (a == b) {
continue;
}
int v = lca(a, b);
down[a] += 1;
down[v] -= 1;
up[b] += 1;
up[v] -= 1;
}
for (int i = 0; i < num; i++) {
if (used[i] == 3) {
go(i);
}
}
for (int i = 0, j = 0; i < m; i++) {
int a = cmp[e[i].first];
int b = cmp[e[i].second];
if (a != b) {
if (res[j] == 0) {
cout << 'B';
}
else if (res[j] == 1) {
if (d[a] < d[b]) {
cout << 'R';
}
else {
cout << 'L';
}
}
else {
if (d[a] < d[b]) {
cout << 'L';
}
else {
cout << 'R';
}
}
++j;
}
else {
cout << 'B';
}
}
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |