Submission #305132

# Submission time Handle Problem Language Result Execution time Memory
305132 2020-09-22T15:42:40 Z miss_robot One-Way Streets (CEOI17_oneway) C++14
100 / 100
326 ms 40056 KB
#include <bits/stdc++.h>

#pragma GCC optimize("O3")

using namespace std;

const int N = 1e5+1, L = 17;
int n, m, q, c;
int num[N], low[N], bridge[N], p[L][N], up[N], dir[N], in[N], out[N], height[N];
pair<int, int> e[N];
vector<int> g[N], ind[N];
vector<int> h[N], dni[N];
char sol[N];

void dfs(int u, int l){
	num[u] = low[u] = ++c;
	for(int i = 0, v, x; i < (int)g[u].size(); i++){
		v = g[u][i], x = ind[u][i];
		if(!num[v]){
			dfs(v, x);
			if(low[v] > num[u]) bridge[x] = 1;
			low[u] = min(low[u], low[v]);
		}
		if(x != l) low[u] = min(low[u], num[v]);
	}
}

void fnd(int u){
	if(num[u]) return;
	num[u] = c;
	for(int i = 0, v, x; i < (int)g[u].size(); i++){
		v = g[u][i], x = ind[u][i];
		if(bridge[x]) continue;
		fnd(v);
	}
}

void tree(int u, int l, int d){
	p[0][u] = l;
	height[u] = d++;
	for(int i = 0, v, x; i < (int)h[u].size(); i++){
		v = h[u][i], x = dni[u][i];
		if(v == l){
			up[u] = x;
			if(num[e[x].first] != u) dir[u] = 1;
		}
		else{
			tree(v, u, d);
		}
	}
}

int kth(int u, int k){
	for(int i = L-1; i >= 0; i--)
		if(k >= (1<<i))
			u = p[i][u], k -= (1<<i);
	return u;
}

int lca(int u, int v){
	if(height[u] < height[v]) swap(u, v);
	u = kth(u, height[u]-height[v]);
	if(u == v) return u;
	for(int i = L-1; i >= 0; i--)
		if(p[i][u] != p[i][v])
			u = p[i][u], v = p[i][v];
	return p[0][u];
}

void solve(int u){
	for(int v : h[u]){
		if(v != p[0][u]){
			solve(v);
			in[u] += in[v];
			out[u] += out[v];
		}
	}
	if(out[u]){
		if(dir[u]) sol[up[u]] = 'L';
		else sol[up[u]] = 'R';
	}
	if(in[u]){
		if(dir[u]) sol[up[u]] = 'R';
		else sol[up[u]] = 'L';
	}
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cin >> n >> m;
	for(int i = 0, u, v; i < m; i++){
		cin >> u >> v;
		u--, v--;
		e[i] = {u, v};
		g[u].push_back(v), g[v].push_back(u);
		ind[u].push_back(i), ind[v].push_back(i);
		sol[i] = 'B';
	}
	for(int i = 0; i < n; i++) if(!num[i]) dfs(i, -1);
	c = 0;
	for(int i = 0; i < n; i++) num[i] = 0;
	for(int i = 0; i < n; i++){
		if(num[i]) continue;
		c++;
		fnd(i);
	}
	for(int i = 0, u, v; i < m; i++){
		if(!bridge[i]) continue;
		tie(u, v) = e[i];
		h[num[u]].push_back(num[v]), h[num[v]].push_back(num[u]);
		dni[num[u]].push_back(i), dni[num[v]].push_back(i);
	}
	for(int i = 1; i <= c; i++) if(!p[0][i]) tree(i, i, 0);
	for(int j = 1; j < L; j++)
		for(int i = 1; i <= c; i++)
			p[j][i] = p[j-1][p[j-1][i]];
	cin >> q;
	for(int i = 0, u, v, a; i < q; i++){
		cin >> u >> v;
		u--, v--;
		u = num[u], v = num[v];
		a = lca(u, v);
		out[u]++, out[a]--;
		in[v]++, in[a]--;
	}
	for(int i = 1; i <= c; i++) if(!height[i]) solve(i);
	cout << sol << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 7 ms 9984 KB Output is correct
4 Correct 7 ms 10112 KB Output is correct
5 Correct 7 ms 10112 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 8 ms 10112 KB Output is correct
8 Correct 7 ms 10112 KB Output is correct
9 Correct 7 ms 9984 KB Output is correct
10 Correct 6 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 7 ms 9984 KB Output is correct
4 Correct 7 ms 10112 KB Output is correct
5 Correct 7 ms 10112 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 8 ms 10112 KB Output is correct
8 Correct 7 ms 10112 KB Output is correct
9 Correct 7 ms 9984 KB Output is correct
10 Correct 6 ms 9984 KB Output is correct
11 Correct 68 ms 16248 KB Output is correct
12 Correct 91 ms 17888 KB Output is correct
13 Correct 148 ms 19704 KB Output is correct
14 Correct 163 ms 24108 KB Output is correct
15 Correct 175 ms 26104 KB Output is correct
16 Correct 257 ms 34660 KB Output is correct
17 Correct 186 ms 35960 KB Output is correct
18 Correct 254 ms 34592 KB Output is correct
19 Correct 176 ms 37104 KB Output is correct
20 Correct 81 ms 17400 KB Output is correct
21 Correct 69 ms 17656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 7 ms 9984 KB Output is correct
4 Correct 7 ms 10112 KB Output is correct
5 Correct 7 ms 10112 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 8 ms 10112 KB Output is correct
8 Correct 7 ms 10112 KB Output is correct
9 Correct 7 ms 9984 KB Output is correct
10 Correct 6 ms 9984 KB Output is correct
11 Correct 68 ms 16248 KB Output is correct
12 Correct 91 ms 17888 KB Output is correct
13 Correct 148 ms 19704 KB Output is correct
14 Correct 163 ms 24108 KB Output is correct
15 Correct 175 ms 26104 KB Output is correct
16 Correct 257 ms 34660 KB Output is correct
17 Correct 186 ms 35960 KB Output is correct
18 Correct 254 ms 34592 KB Output is correct
19 Correct 176 ms 37104 KB Output is correct
20 Correct 81 ms 17400 KB Output is correct
21 Correct 69 ms 17656 KB Output is correct
22 Correct 326 ms 37120 KB Output is correct
23 Correct 281 ms 35576 KB Output is correct
24 Correct 307 ms 35832 KB Output is correct
25 Correct 269 ms 40056 KB Output is correct
26 Correct 275 ms 36836 KB Output is correct
27 Correct 248 ms 35576 KB Output is correct
28 Correct 42 ms 14076 KB Output is correct
29 Correct 94 ms 18168 KB Output is correct
30 Correct 91 ms 18552 KB Output is correct
31 Correct 97 ms 18680 KB Output is correct
32 Correct 136 ms 25660 KB Output is correct