Submission #656712

# Submission time Handle Problem Language Result Execution time Memory
656712 2022-11-08T05:35:03 Z AmirAli_H1 One-Way Streets (CEOI17_oneway) C++17
0 / 100
5 ms 5076 KB
// In the name of Allah

#include <bits/stdc++.h>
using namespace std;

typedef long long int	ll;
typedef long double	ld;
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pll;

#define all(x)		(x).begin(),(x).end()
#define len(x)		((ll) (x).size())
#define F		first
#define S		second
#define pb		push_back
#define sep             ' '
#define endl            '\n'
#define Mp		make_pair
#define debug(x)	cerr << #x << ": " <<  x << endl;
#define kill(x)		cout << x << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define file_io(x,y)	freopen(x, "r", stdin); freopen(y, "w", stdout);

int n, m, q;
const int maxn = 1e5 + 4;
const int maxlg = 20;
vector<pii> adj[maxn], adjq[maxn];
pii edgs[maxn];
char ans[maxn];
bool mark[maxn];
int h[maxn], mn[maxn];
pii mnq[maxn];
int st[maxn], ft[maxn];
int timer = 0;
int up[maxn][maxlg];

void pre_dfs(int v, int p = -1) {
	mark[v] = 1;
	st[v] = timer++;
	up[v][0] = (p != -1) ? p : v;
	for (int i = 1; i < maxlg; i++) {
		up[v][i] = up[up[v][i - 1]][i - 1];
	}
	for (auto f : adj[v]) {
		int u = f.F, i = f.S;
		if (!mark[u]) {
			h[u] = h[v] + 1;
			pre_dfs(u, v);
		}
	}
	ft[v] = timer++;
}

bool is_gr(int u, int v) {
	return (st[u] <= st[v] && ft[u] >= ft[v]);
}

int get_kgr(int v, int k) {
	if (k == 0) return v;
	int i = __builtin_ctz(k);
	return get_kgr(up[v][i], k ^ (1 << i));
}

bool check(int v, int u, int k) {
	int r = get_kgr(v, k);
	return is_gr(r, u);
}

int get_lca(int v, int u) {
	if (is_gr(v, u)) return v;
	if (is_gr(u, v)) return u;
	
	int l = 0, r = h[v] + 1;
	while (r - l > 1) {
		int mid = (l + r) / 2;
		if (!check(v, u, mid)) l = mid;
		else r = mid;
	}
	return get_kgr(v, r);
}

void dfs(int v, int j = -1) {
	mark[v] = 1;
	mn[v] = h[v]; mnq[v] = Mp(h[v], 0);
	for (auto f : adj[v]) {
		int u = f.F, i = f.S;
		if (!mark[u]) {
			dfs(u, i);
			mn[v] = min(mn[v], mn[u]);
			if (mnq[u].S != 0 && mnq[u].F < mnq[v].F) mnq[v] = mnq[u];
		}
		else if (i != j) {
			mn[v] = min(mn[v], h[u]);
		}
	}
	for (auto f : adjq[v]) {
		int u = get_lca(v, f.F), e = f.S;
		if (h[u] < mnq[v].F) mnq[v] = Mp(h[u], e);
	}
	if (mn[v] >= h[v] && j != -1) {
		for (int i = 0; i < n; i++) {
			if (!is_gr(v, i)) continue;
			for (auto f : adjq[i]) {
				int u = f.F, e = f.S;
				if (!is_gr(v, u)) {
					if (e == 1) {
						if (edgs[j].F == v) ans[j] = 'R';
						else ans[j] = 'L';
					}
					else {
						if (edgs[j].F == v) ans[j] = 'L';
						else ans[j] = 'R';
					}
				}
			}
		}
	}
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int u, v;
		cin >> u >> v; u--; v--;
		adj[u].pb(Mp(v, i)); adj[v].pb(Mp(u, i));
		edgs[i] = Mp(u, v);
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		int u, v;
		cin >> u >> v; u--; v--;
		adjq[u].pb(Mp(v, 1)); adjq[v].pb(Mp(u, 2));
	}
	
	fill(ans, ans + m, 'B');
	
	fill(mark, mark + n, 0);
	for (int i = 0; i < n; i++) {
		if (!mark[i]) pre_dfs(i);
	}
	fill(mark, mark + n, 0);
	for (int i = 0; i < n; i++) {
		if (!mark[i]) dfs(i);
	}
	
	for (char ch : ans) cout << ch;
	cout << endl;
	
	return 0;
}

Compilation message

oneway.cpp: In function 'void pre_dfs(int, int)':
oneway.cpp:45:16: warning: unused variable 'i' [-Wunused-variable]
   45 |   int u = f.F, i = f.S;
      |                ^
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -