답안 #657001

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
657001 2022-11-08T17:01:47 Z AmirAli_H1 One-Way Streets (CEOI17_oneway) C++17
100 / 100
184 ms 31020 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;
const int maxn = 1e5 + 5;
const int maxlg = 20;
vector<pii> adj[maxn];
vector<pii> ques[maxn];
pii edgs[maxn];
vector<int> oedgs, nedgs;
bool mark[maxn];
int h[maxn], mn[maxn];
pii mq[maxn];
int st[maxn], ft[maxn];
int timer = 0;
int up[maxn][maxlg];
int col[maxn]; int c = 0;
char ans[maxn];

void pre_dfs(int v, int i = -1) {
	mark[v] = 1; mn[v] = h[v];
	for (auto f : adj[v]) {
		int u = f.F, j = f.S;
		if (!mark[u]) {
			h[u] = h[v] + 1;
			pre_dfs(u, j);
			mn[v] = min(mn[v], mn[u]);
		}
		else if (j != i) {
			mn[v] = min(mn[v], h[u]);
			oedgs.pb(j);
		}
	}
	if (mn[v] >= h[v] && i != -1) nedgs.pb(i);
	else if (i != -1) oedgs.pb(i);
}

void dfs_col(int v) {
	mark[v] = 1; col[v] = c;
	for (auto f : adj[v]) {
		int u = f.F, j = f.S;
		if (!mark[u]) dfs_col(u);
	}
}

void pre_dfsx(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, j = f.S;
		if (!mark[u]) pre_dfsx(u, v);
	}
	ft[v] = timer++;
}

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

int get_lca(int v, int u) {
	if (is_gr(v, u)) return v;
	if (is_gr(u, v)) return u;
	
	for (int i = maxlg - 1; i >= 0; i--) {
		if (!is_gr(up[u][i], v)) u = up[u][i];
	}
	return up[u][0];
}

void solve_dfs(int v, int i = -1) {
	mark[v] = 1; mq[v] = Mp(h[v], 0);
	for (auto f : adj[v]) {
		int u = f.F, j = f.S;
		if (!mark[u]) {
			h[u] = h[v] + 1;
			solve_dfs(u, j);
			if (mq[u].S != 0 && (mq[v].S == 0 || mq[u].F < mq[v].F)) {
				mq[v] = mq[u];
			}
		}
	}
	for (auto f : ques[v]) {
		int u = f.F, e = f.S;
		if (mq[v].S == 0 || h[u] < mq[v].F) {
			mq[v] = Mp(h[u], e);
		}
	}
	if (mq[v].F < h[v] && mq[v].S != 0 && i != -1) {
		if (mq[v].S == 1) {
			if (v == col[edgs[i].F]) ans[i] = 'R';
			else ans[i] = 'L';
		}
		else {
			if (v == col[edgs[i].F]) ans[i] = 'L';
			else ans[i] = '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--;
		if (u != v) {
			adj[u].pb(Mp(v, i)); adj[v].pb(Mp(u, i));
			edgs[i] = Mp(u, v);
		}
	}
	
	fill(mark, mark + n, 0);
	for (int i = 0; i < n; i++) {
		if (!mark[i]) pre_dfs(i);
	}
	
	for (int i = 0; i < n; i++) adj[i].clear();
	for (int i : oedgs) {
		int u = edgs[i].F, v = edgs[i].S;
		adj[u].pb(Mp(v, i)); adj[v].pb(Mp(u, i));
	}
	
	fill(mark, mark + n, 0);
	for (int i = 0; i < n; i++) {
		if (!mark[i]) {
			dfs_col(i); c++;
		}
	}
	
	for (int i = 0; i < n; i++) adj[i].clear();
	for (int i : nedgs) {
		int u = col[edgs[i].F], v = col[edgs[i].S];
		adj[u].pb(Mp(v, i)); adj[v].pb(Mp(u, i));
	}
	
	fill(mark, mark + n, 0);
	for (int i = 0; i < c; i++) {
		if (!mark[i]) pre_dfsx(i);
	}
	
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int u, v;
		cin >> u >> v; u--; v--;
		u = col[u]; v = col[v];
		if (u == v) continue;
		int r = get_lca(u, v);
		if (r != u && r != v) {
			ques[u].pb(Mp(r, 1)); ques[v].pb(Mp(r, 2));
		}
		else if (r == v) {
			ques[u].pb(Mp(r, 1));
		}
		else if (r == u) {
			ques[v].pb(Mp(r, 2));
		}
	}
	
	fill(ans, ans + m, 'B');
	fill(mark, mark + n, 0); fill(h, h + n, 0);
	for (int i = 0; i < c; i++) {
		if (!mark[i]) solve_dfs(i);
	}
	
	for (int i = 0; i < m; i++) cout << ans[i];
	cout << endl;
	
	return 0;
}

Compilation message

oneway.cpp: In function 'void dfs_col(int)':
oneway.cpp:61:16: warning: unused variable 'j' [-Wunused-variable]
   61 |   int u = f.F, j = f.S;
      |                ^
oneway.cpp: In function 'void pre_dfsx(int, int)':
oneway.cpp:76:16: warning: unused variable 'j' [-Wunused-variable]
   76 |   int u = f.F, j = f.S;
      |                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 55 ms 13148 KB Output is correct
12 Correct 62 ms 13848 KB Output is correct
13 Correct 58 ms 14908 KB Output is correct
14 Correct 82 ms 17420 KB Output is correct
15 Correct 106 ms 18320 KB Output is correct
16 Correct 89 ms 20920 KB Output is correct
17 Correct 90 ms 22768 KB Output is correct
18 Correct 89 ms 20840 KB Output is correct
19 Correct 86 ms 24048 KB Output is correct
20 Correct 49 ms 12880 KB Output is correct
21 Correct 55 ms 12608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5204 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 55 ms 13148 KB Output is correct
12 Correct 62 ms 13848 KB Output is correct
13 Correct 58 ms 14908 KB Output is correct
14 Correct 82 ms 17420 KB Output is correct
15 Correct 106 ms 18320 KB Output is correct
16 Correct 89 ms 20920 KB Output is correct
17 Correct 90 ms 22768 KB Output is correct
18 Correct 89 ms 20840 KB Output is correct
19 Correct 86 ms 24048 KB Output is correct
20 Correct 49 ms 12880 KB Output is correct
21 Correct 55 ms 12608 KB Output is correct
22 Correct 174 ms 26952 KB Output is correct
23 Correct 165 ms 26564 KB Output is correct
24 Correct 184 ms 26520 KB Output is correct
25 Correct 159 ms 31020 KB Output is correct
26 Correct 181 ms 27736 KB Output is correct
27 Correct 166 ms 26540 KB Output is correct
28 Correct 33 ms 11996 KB Output is correct
29 Correct 67 ms 16200 KB Output is correct
30 Correct 69 ms 16232 KB Output is correct
31 Correct 71 ms 15996 KB Output is correct
32 Correct 92 ms 21208 KB Output is correct