#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
#define int long long
using namespace std;
const int sz = 2e5 + 10;
const int sm = 25;
vector<pair<int, int>> aj[sz], ajgr[sz];
int br[sz], gr[sz], dp[sz];
int in[sz], lo[sz];
int pn[sz], pe[sz];
int le[sz], ri[sz];
int up[sm][sz];
int n, m, p;
bool dr[sz];
char la[sz];
int tr;
void taj(int v, int p = 0) {
in[v] = lo[v] = ++tr;
bool ps = false;
for (auto& u : aj[v]) {
int to = u.first, nm = u.second;
if (to == p && !ps) {
ps = true;
continue;
} else if (in[to]) {
lo[v] = min(lo[v], in[to]);
} else {
taj(to, v);
lo[v] = min(lo[v], lo[to]);
if (lo[to] > in[v]) {
br[nm] = true;
}
}
}
}
void cmp(int v, int gn) {
gr[v] = gn;
for (auto& u : aj[v]) {
int to = u.first, nm = u.second;
if (!gr[to] && !br[nm]) {
cmp(to, gn);
}
}
}
void dpt(int v, int p = 0, int d = 1) {
up[0][v] = pn[v];
dp[v] = d;
for (auto& u : ajgr[v]) {
int to = u.first, nm = u.second;
if (to != p) {
pn[to] = v;
pe[to] = nm;
dpt(to, v, d + 1);
}
}
}
int lca(int a, int b) {
if (dp[a] > dp[b]) {
swap(a, b);
}
int d = dp[b] - dp[a];
for (int i = 0; i < sm; i++) {
if (d & (1ll << i)) {
b = up[i][b];
}
}
if (a == b) {
return a;
}
for (int i = sm - 1; i >= 0; i--) {
if (up[i][a] != up[i][b]) {
a = up[i][a];
b = up[i][b];
}
}
return up[0][a];
}
void gdr(int a, int b, int d) {
if (a == b) {
return;
}
if (!dr[a]) {
dr[a] = true;
int to = pn[a];
int nm = pe[a];
int gna = gr[le[nm]];
if (0 == d) {
if (gna == a) {
la[nm] = 'R';
} else {
la[nm] = 'L';
}
} else {
if (gna == a) {
la[nm] = 'L';
} else {
la[nm] = 'R';
}
}
gdr(to, b, d);
}
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
aj[a].emplace_back(b, i);
aj[b].emplace_back(a, i);
le[i] = a;
ri[i] = b;
la[i] = 'B';
}
for (int i = 1; i <= n; i++) {
if (!in[i]) {
taj(i);
}
}
int gn = 0;
for (int i = 1; i <= n; i++) {
if (!gr[i]) {
cmp(i, ++gn);
}
}
for (int i = 1; i <= m; i++) {
if (br[i]) {
int gna = gr[le[i]];
int gnb = gr[ri[i]];
ajgr[gna].emplace_back(gnb, i);
ajgr[gnb].emplace_back(gna, i);
}
}
for (int i = 1; i <= gn; i++) {
if (!dp[i]) {
dpt(i);
}
}
for (int i = 1; i < sm; i++) {
for (int j = 1; j <= gn; j++) {
up[i][j] = up[i - 1][up[i - 1][j]];
}
}
vector<vector<int>> ord;
int q;
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
a = gr[a];
b = gr[b];
int l = lca(a, b);
ord.push_back({dp[l], a, b, l});
}
sort(begin(ord), end(ord));
for (auto& i : ord) {
int a = i[1];
int b = i[2];
int l = i[3];
gdr(a, l, 0);
gdr(b, l, 1);
}
for (int i = 1; i <= m; i++) {
cout << la[i];
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10052 KB |
Output is correct |
2 |
Correct |
4 ms |
10076 KB |
Output is correct |
3 |
Correct |
4 ms |
10076 KB |
Output is correct |
4 |
Correct |
5 ms |
10332 KB |
Output is correct |
5 |
Correct |
5 ms |
10332 KB |
Output is correct |
6 |
Correct |
5 ms |
10000 KB |
Output is correct |
7 |
Correct |
5 ms |
10376 KB |
Output is correct |
8 |
Correct |
5 ms |
10488 KB |
Output is correct |
9 |
Correct |
5 ms |
10076 KB |
Output is correct |
10 |
Correct |
5 ms |
10076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10052 KB |
Output is correct |
2 |
Correct |
4 ms |
10076 KB |
Output is correct |
3 |
Correct |
4 ms |
10076 KB |
Output is correct |
4 |
Correct |
5 ms |
10332 KB |
Output is correct |
5 |
Correct |
5 ms |
10332 KB |
Output is correct |
6 |
Correct |
5 ms |
10000 KB |
Output is correct |
7 |
Correct |
5 ms |
10376 KB |
Output is correct |
8 |
Correct |
5 ms |
10488 KB |
Output is correct |
9 |
Correct |
5 ms |
10076 KB |
Output is correct |
10 |
Correct |
5 ms |
10076 KB |
Output is correct |
11 |
Correct |
32 ms |
19540 KB |
Output is correct |
12 |
Correct |
33 ms |
21332 KB |
Output is correct |
13 |
Correct |
38 ms |
23644 KB |
Output is correct |
14 |
Correct |
48 ms |
29900 KB |
Output is correct |
15 |
Correct |
54 ms |
33024 KB |
Output is correct |
16 |
Correct |
65 ms |
48208 KB |
Output is correct |
17 |
Correct |
64 ms |
49720 KB |
Output is correct |
18 |
Correct |
64 ms |
48200 KB |
Output is correct |
19 |
Correct |
62 ms |
51028 KB |
Output is correct |
20 |
Correct |
33 ms |
20052 KB |
Output is correct |
21 |
Correct |
30 ms |
20828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10052 KB |
Output is correct |
2 |
Correct |
4 ms |
10076 KB |
Output is correct |
3 |
Correct |
4 ms |
10076 KB |
Output is correct |
4 |
Correct |
5 ms |
10332 KB |
Output is correct |
5 |
Correct |
5 ms |
10332 KB |
Output is correct |
6 |
Correct |
5 ms |
10000 KB |
Output is correct |
7 |
Correct |
5 ms |
10376 KB |
Output is correct |
8 |
Correct |
5 ms |
10488 KB |
Output is correct |
9 |
Correct |
5 ms |
10076 KB |
Output is correct |
10 |
Correct |
5 ms |
10076 KB |
Output is correct |
11 |
Correct |
32 ms |
19540 KB |
Output is correct |
12 |
Correct |
33 ms |
21332 KB |
Output is correct |
13 |
Correct |
38 ms |
23644 KB |
Output is correct |
14 |
Correct |
48 ms |
29900 KB |
Output is correct |
15 |
Correct |
54 ms |
33024 KB |
Output is correct |
16 |
Correct |
65 ms |
48208 KB |
Output is correct |
17 |
Correct |
64 ms |
49720 KB |
Output is correct |
18 |
Correct |
64 ms |
48200 KB |
Output is correct |
19 |
Correct |
62 ms |
51028 KB |
Output is correct |
20 |
Correct |
33 ms |
20052 KB |
Output is correct |
21 |
Correct |
30 ms |
20828 KB |
Output is correct |
22 |
Correct |
162 ms |
57852 KB |
Output is correct |
23 |
Correct |
141 ms |
56572 KB |
Output is correct |
24 |
Correct |
130 ms |
56532 KB |
Output is correct |
25 |
Correct |
137 ms |
61188 KB |
Output is correct |
26 |
Correct |
142 ms |
57660 KB |
Output is correct |
27 |
Correct |
140 ms |
56572 KB |
Output is correct |
28 |
Correct |
33 ms |
23304 KB |
Output is correct |
29 |
Correct |
57 ms |
27908 KB |
Output is correct |
30 |
Correct |
59 ms |
28164 KB |
Output is correct |
31 |
Correct |
55 ms |
28160 KB |
Output is correct |
32 |
Correct |
82 ms |
38792 KB |
Output is correct |