답안 #656836

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
656836 2022-11-08T09:29:30 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;
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 (char ch : ans) cout << ch;
	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 Incorrect 5 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -