Submission #207876

# Submission time Handle Problem Language Result Execution time Memory
207876 2020-03-09T10:49:14 Z Siberian One-Way Streets (CEOI17_oneway) C++14
100 / 100
502 ms 58468 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define all(x) x.begin(), x.end()

template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 & y) {if (x > y) x = y;}
template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 & y) {if (x < y) x = y;}
#define int ll

struct edge{
	int to, ind;
	edge() {}
	edge(int _to, int _ind) {
		to = _to, ind = _ind;
	}
};

const int MAXN = 1e5 + 10, MAXLOG = 17;
int n, m, p;
vector<edge> g[MAXN];
vector<pair<int, int>> ed;
map<pair<int, int>, int> cnt;

void read() {
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		ed.push_back({u, v});
		cnt[{min(u, v), max(u, v)}]++;
		g[u].push_back({v, i});
		g[v].push_back({u, i});
	}
}

set<int> bridges;
bool used[MAXN];
int tin[MAXN], up[MAXN], timer = 0;

void dfs_bridges(int v, int p) {
	used[v] = true;
	tin[v] = up[v] = timer++;
	for (auto [to, ind] : g[v]) {
		if (to == p) continue;
		if (used[to]) {
			chkmin(up[v], tin[to]);
		}
		else {
			dfs_bridges(to, v);
			chkmin(up[v], up[to]);
			if (up[to] > tin[v] && cnt[{min(to, v), max(to, v)}] < 2) {
				bridges.insert(ind);
			}
		}
	}
}

int color[MAXN];

void dfs_color(int v, int c) {
	color[v] = c;
	for (auto [to, ind] : g[v]) {
		if (color[to] != -1) continue;
		if (bridges.count(ind)) continue;
		dfs_color(to, c);
	}
}

vector<edge> gr[MAXN];
int tout[MAXN], dp[MAXN][MAXLOG];
int Ind[MAXN], h[MAXN];

void dfs_lca(int v, int p, int len) {
	used[v] = true;
	tin[v] = timer++;
	dp[v][0] = p;
	h[v] = len++;
	for (int i = 1; i < MAXLOG; i++)
		dp[v][i] = dp[dp[v][i - 1]][i - 1];
	for (auto [i, ind] : gr[v]) {
		if (i != p) {
			Ind[i] = ind;
			dfs_lca(i, v, len);
		}
	}
	tout[v] = timer++;
}

bool is_upper(int v, int u) {
	return tin[v] <= tin[u] && tout[v] >= tout[u];
}

int lca(int v, int u) {
	if (is_upper(v, u)) return v;
	if (is_upper(u, v)) return u;
	for (int i = MAXLOG - 1; i >= 0; i--)
		if (!is_upper(dp[v][i], u))
			v = dp[v][i];
	return dp[v][0];
}

string ans;

int par[MAXN], sz[MAXN], fans[MAXN];

void make() {
	for (int i = 0; i < MAXN; i++)
		par[i] = i, sz[i] = 1, fans[i] = i;
}

int get_par(int v) {
	if (v == par[v]) return v;
	return par[v] = get_par(par[v]);
}

void uni(int a, int b) {
	a = get_par(a);
	b = get_par(b);
	if (a == b) return;
	if (sz[a] > sz[b]) swap(a, b);
	par[b] = a;
	if (h[fans[a]] > h[fans[b]])
		fans[a] = fans[b];
}

void build() {
	make();
	ans = "";
	for (int i = 0; i < m; i++) {
		ans += "B";
	}
	for (int i = 0; i < n; i++) {
		if (!used[i]) {
			dfs_bridges(i, -1);
		}
	}
	for (int i = 0; i < n; i++) color[i] = -1;
	
	int c = 0;
	for (int i = 0; i < n; i++) {
		if (color[i] == -1) {
			dfs_color(i, c++);
		}
	}
	
	for (auto i : bridges) {
		int u = color[ed[i].first];
		int v = color[ed[i].second];
		assert(u != v);
		gr[u].push_back({v, i});
		gr[v].push_back({u, i});
	}
	
	for (int i = 0; i < c; i++) {
		used[i] = false;
	}
	for (int i = 0; i < c; i++) {
		if (!used[i]) {
			dfs_lca(i, i, 0);
		}
	}
}

void no() {
	cout << "-1" << endl;
	exit(0);
}

void make(int u, int v, int ind) {
	if (color[ed[ind].first] == u && color[ed[ind].second] == v) {
		if (ans[ind] == 'L') no();
		ans[ind] = 'R';
	}
	else {
		if (ans[ind] == 'R') no();
		ans[ind] = 'L';
	}
}

void upd(int s, int f) {
	int v = color[s];
	int u = color[f];
	if (v == u) return;
	//cout << s << " " << f << endl;
	s = v;
	f = u;
	while (!is_upper(v, f)) {
		make(v, dp[v][0], Ind[v]);
		uni(v, dp[v][0]);
		v = fans[get_par(v)];
	}
	while (!is_upper(u, s)) {
		make(dp[u][0], u, Ind[u]);
		uni(u, dp[u][0]);
		u = fans[get_par(u)];
	}
	//cout << ans << endl;
}

void run() {
	build();
	cin >> p;
	while (p--) {
		int s, f;
		cin >> s >> f;
		s--;
		f--;
		upd(s, f);
	}
}

void write() {
	cout << ans << endl;
}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	read();
	run();
	write();
	return 0;
}

Compilation message

oneway.cpp: In function 'void dfs_bridges(ll, ll)':
oneway.cpp:48:12: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
  for (auto [to, ind] : g[v]) {
            ^
oneway.cpp: In function 'void dfs_color(ll, ll)':
oneway.cpp:67:12: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
  for (auto [to, ind] : g[v]) {
            ^
oneway.cpp: In function 'void dfs_lca(ll, ll, ll)':
oneway.cpp:85:12: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
  for (auto [i, ind] : gr[v]) {
            ^
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 9 ms 7672 KB Output is correct
4 Correct 10 ms 7800 KB Output is correct
5 Correct 10 ms 7928 KB Output is correct
6 Correct 9 ms 7544 KB Output is correct
7 Correct 10 ms 7928 KB Output is correct
8 Correct 10 ms 7800 KB Output is correct
9 Correct 9 ms 7544 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 9 ms 7672 KB Output is correct
4 Correct 10 ms 7800 KB Output is correct
5 Correct 10 ms 7928 KB Output is correct
6 Correct 9 ms 7544 KB Output is correct
7 Correct 10 ms 7928 KB Output is correct
8 Correct 10 ms 7800 KB Output is correct
9 Correct 9 ms 7544 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
11 Correct 140 ms 23012 KB Output is correct
12 Correct 151 ms 24420 KB Output is correct
13 Correct 191 ms 26468 KB Output is correct
14 Correct 238 ms 32480 KB Output is correct
15 Correct 255 ms 35172 KB Output is correct
16 Correct 422 ms 48476 KB Output is correct
17 Correct 311 ms 51044 KB Output is correct
18 Correct 402 ms 48612 KB Output is correct
19 Correct 305 ms 52836 KB Output is correct
20 Correct 139 ms 23652 KB Output is correct
21 Correct 119 ms 23016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 8 ms 7416 KB Output is correct
3 Correct 9 ms 7672 KB Output is correct
4 Correct 10 ms 7800 KB Output is correct
5 Correct 10 ms 7928 KB Output is correct
6 Correct 9 ms 7544 KB Output is correct
7 Correct 10 ms 7928 KB Output is correct
8 Correct 10 ms 7800 KB Output is correct
9 Correct 9 ms 7544 KB Output is correct
10 Correct 9 ms 7544 KB Output is correct
11 Correct 140 ms 23012 KB Output is correct
12 Correct 151 ms 24420 KB Output is correct
13 Correct 191 ms 26468 KB Output is correct
14 Correct 238 ms 32480 KB Output is correct
15 Correct 255 ms 35172 KB Output is correct
16 Correct 422 ms 48476 KB Output is correct
17 Correct 311 ms 51044 KB Output is correct
18 Correct 402 ms 48612 KB Output is correct
19 Correct 305 ms 52836 KB Output is correct
20 Correct 139 ms 23652 KB Output is correct
21 Correct 119 ms 23016 KB Output is correct
22 Correct 404 ms 51912 KB Output is correct
23 Correct 432 ms 50792 KB Output is correct
24 Correct 502 ms 50920 KB Output is correct
25 Correct 400 ms 58468 KB Output is correct
26 Correct 397 ms 52708 KB Output is correct
27 Correct 408 ms 51176 KB Output is correct
28 Correct 65 ms 14820 KB Output is correct
29 Correct 166 ms 25256 KB Output is correct
30 Correct 153 ms 25444 KB Output is correct
31 Correct 170 ms 25956 KB Output is correct
32 Correct 205 ms 34660 KB Output is correct