Submission #1324082

#TimeUsernameProblemLanguageResultExecution timeMemory
1324082WeIlIaNOne-Way Streets (CEOI17_oneway)C++20
0 / 100
12 ms9332 KiB
#include <bits/stdc++.h>
using namespace std;

#define MOD1 1000000007
#define MOD2 998244353
#define fir first
#define sec second

#define pushf push_front
#define pushb push_back
#define popf pop_front
#define popb pop_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define lbound(v, x) lower_bound(all(v), x) - v.begin()
#define ubound(v, x) upper_bound(all(v), x) - v.begin()
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

#define FOR1(a) for (int _ = 0; _ < (a); ++_)
#define FOR2(i, a) for (int i = 0; i < (a); ++i)
#define FOR3(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR1(a) for (int _ = (a)-1; _ >= 0; --_)
#define RFOR2(i, a) for (int i = (a)-1; i >= 0; --i)
#define RFOR3(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define overload3(a, b, c, d, ...) d
// Always choose the fourth argument to call. Hence, which function to call is determined by the number of given arguments.
#define REP(...) overload3(__VA_ARGS__, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define RREP(...) overload3(__VA_ARGS__, RFOR3, RFOR2, RFOR1)(__VA_ARGS__)

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
typedef queue<int> qi;
typedef queue<ll> qll;
typedef queue<pii> qpii;
typedef queue<pll> qpll;
typedef deque<int> dqi;
typedef deque<ll> dqll;
typedef deque<pii> dqpii;
typedef deque<pll> dqpll;
typedef priority_queue<int> pqi;
typedef priority_queue<ll> pqll;
typedef priority_queue<pii> pqpii;
typedef priority_queue<pll> pqpll;
typedef priority_queue<int, vi, greater<int> > r_pqi;
typedef priority_queue<ll, vll, greater<ll> > r_pqll;
typedef priority_queue<pii, vpii, greater<pii> > r_pqpii;
typedef priority_queue<pll, vpll, greater<pll> > r_pqpll;

const int N = 1e6;

int n, m, num = 0, tp = 0, cnt = 0;
vpii edge[N];
int low[N], dfn[N], stk[N], ecc[N];
// the nodes that are on the current path
bitset<N> in_stack;

void tarjan(int u, int p) {
	low[u] = dfn[u] = num++, stk[tp++] = u, in_stack[u] = 1;
	REP(i, edge[u].size()) {
		int v = edge[u][i].fir;
		if(v == p) {
			p = -1;
			continue;
		}
		if(dfn[v] == -1) {
			tarjan(v, u);
			low[u] = min(low[u], low[v]);
		}
		else if(in_stack[v]) {
			low[u] = min(low[u], dfn[v]);
		}
	}
	if (dfn[u] == low[u]) {
		//cerr<<cnt<<' '<<u<<' '<<stk[tp-1]<<endl;
		do {
			tp--;
			ecc[stk[tp]] = cnt;
			in_stack[stk[tp]] = 0;
		} while (stk[tp] != u);
		cnt++;
	}
}

vpii adj[N];
char ans[N];
int val[N];
bool vis[N];

int dfs(int u, int p) {
	int sum = val[u];
	vis[u] = 1;
	for(auto [v, id] : adj[u]) {
		// cerr<<u<<' '<<v<<endl;
		if(v == p) continue;
		int cur = dfs(v, u);
		sum += cur;
		if(id < 0) {
			id = -id;
			cur = -cur;
		}
		if(cur < 0) {
			ans[id] = 'L';
		}
		else if(cur > 0) {
			ans[id] = 'R';
		}
		else {
			ans[id] = 'B';
		}
	}
	return sum;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	memset(dfn, -1, sizeof(dfn));
	memset(low, -1, sizeof(low));
	REP(i, m) {
		int a, b;
		cin>>a>>b;
		a--, b--;
		edge[a].pushb(mp(b, i));
		edge[b].pushb(mp(a, -i));
	}
	REP(i, n) {
		if(dfn[i] == -1) {
			tarjan(i, -1);
		}
	}
	REP(u, n) {
		for(auto [v, id] : edge[u]) {
			if(ecc[u] != ecc[v]) {
				adj[ecc[u]].pushb(mp(ecc[v], id));
				// cerr<<u<<' '<<v<<' '<<ecc[u]<<' '<<ecc[v]<<endl;
			}
			else {
				ans[abs(id)] = 'B';
			}
		}
	}
	int q;
	cin>>q;
	while(q--) {
		int a, b;
		cin>>a>>b;
		a = ecc[a-1], b = ecc[b-1];
		val[a]--, val[b]++;
	}
	// REP(i, n) {
	// 	cerr<<ecc[i]<<' ';
	// }
	memset(vis, 0, sizeof(vis));
	REP(i, n) {
		if(vis[i] == 0) {
			dfs(i, -1);
		}
	}
	REP(i, m) {
		cout<<ans[i];
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...