Submission #237346

# Submission time Handle Problem Language Result Execution time Memory
237346 2020-06-06T04:07:29 Z nikatamliani One-Way Streets (CEOI17_oneway) C++14
100 / 100
496 ms 39136 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, LG = 20;
int d[N], a[N], b[N], in[N], out[N], dp[N], p[N][2], L[N][LG], n, m, q;
vector < pair < int, int > > edges[N];
bool ok[N], vis[N], marked[N][2];
map < pair < int, int >, int > ind;
void dfs(int x, int p, int index){
	static int timer = 0;
	if(vis[x])return;
	vis[x] = true; 
	d[x] = d[p] + 1;
	L[x][0] = p;
	for(int i = 1; i < LG; ++i) L[x][i] = L[L[x][i - 1]][i - 1];
	in[x] = ++timer;
	for(int i = 0; i < (int)edges[x].size(); ++i){
		int to = edges[x][i].first;
		if(edges[x][i].second != index){
			if(vis[to]){
				dp[x] += in[x] < in[to] ? -1 : 1;
			}else{
				dfs(to, x, edges[x][i].second);
				dp[x] += dp[to];
			}
		}
	}
	out[x] = ++timer;
	ok[ind[{x, p}]] = !dp[x];
}
bool isAnc(int potAnc, int potChild){
	return !potAnc || in[potAnc] <= in[potChild] && out[potAnc] >= out[potChild];
}
int lca(int u, int v){
	if(isAnc(u, v))return u;
	if(isAnc(v, u))return v;
	for(int i = LG - 1; ~i; --i){
		if(!isAnc(L[u][i], v))u = L[u][i];
	}
	return L[u][0];
}
int find(int x, int dist){
	for(int bit = 0; bit < LG; ++bit){
		if(dist >> bit & 1)x = L[x][bit];
	}
	return x;
}
void make_set(int x){
	p[x][0] = p[x][1] = x;
	marked[x][0] = marked[x][1] = 0;
}
int find_set(int x, bool TYPE){
	return p[x][TYPE] == x ? x : p[x][TYPE] = find_set(p[x][TYPE], TYPE);
}
void union_sets(int a, int b, bool TYPE){
	a = find_set(a, TYPE);
	b = find_set(b, TYPE);
	marked[a][TYPE] = marked[b][TYPE] = 1;
	if(a == b)return;
	if(d[a] > d[b])swap(a, b);
	p[b][TYPE] = a;
}
void unite(int fr, int to, bool TYPE){
	while(d[fr] >= d[to]){
		int nxt = L[find_set(fr, TYPE)][0];
		union_sets(fr, to, TYPE);
		fr = nxt;
	}
}
void directEdges(int fr, int to){
	int l = lca(fr, to);
	int dist1 = d[fr] - d[l];
	int dist2 = d[to] - d[l];
	if(dist1)unite(fr, find(fr, dist1 - 1), 0);
	if(dist2)unite(to, find(to, dist2 - 1), 1);
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= m; ++i){
		cin >> a[i] >> b[i];
		edges[a[i]].push_back({b[i], i});
		edges[b[i]].push_back({a[i], i});
		ind[{a[i], b[i]}] = ind[{b[i], a[i]}] = i;
	}
	for(int i = 1; i <= n; ++i)make_set(i);
	for(int i = 1; i <= n; ++i){
		if(!vis[i]){
			dfs(i, 0, 0);
		}
	}
	cin >> q;
	for(int u, v, i = 1; i <= q; ++i){
		cin >> u >> v;
		directEdges(u, v);
	}
	for(int i = 1; i <= m; ++i){
		char direction = 'B';
		if(ok[i]){
			int child = a[i], parent = b[i];
			bool swapped = false;
			if(d[child] < d[parent])swap(parent, child), swapped = true;
			int x = find_set(child, 0);
			int y = find_set(child, 1);
			if(marked[x][0]) direction = 'R';
			if(marked[y][1]) direction = 'L';
			if(swapped && direction != 'B')direction = 'L' + 'R' - direction;
		}
		cout << direction;
	}
}

Compilation message

oneway.cpp: In function 'bool isAnc(int, int)':
oneway.cpp:31:47: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  return !potAnc || in[potAnc] <= in[potChild] && out[potAnc] >= out[potChild];
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 7 ms 3072 KB Output is correct
4 Correct 7 ms 3072 KB Output is correct
5 Correct 7 ms 3072 KB Output is correct
6 Correct 6 ms 2944 KB Output is correct
7 Correct 7 ms 3072 KB Output is correct
8 Correct 7 ms 3072 KB Output is correct
9 Correct 7 ms 2944 KB Output is correct
10 Correct 7 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 7 ms 3072 KB Output is correct
4 Correct 7 ms 3072 KB Output is correct
5 Correct 7 ms 3072 KB Output is correct
6 Correct 6 ms 2944 KB Output is correct
7 Correct 7 ms 3072 KB Output is correct
8 Correct 7 ms 3072 KB Output is correct
9 Correct 7 ms 2944 KB Output is correct
10 Correct 7 ms 2944 KB Output is correct
11 Correct 237 ms 23800 KB Output is correct
12 Correct 262 ms 25976 KB Output is correct
13 Correct 323 ms 29180 KB Output is correct
14 Correct 357 ms 32888 KB Output is correct
15 Correct 383 ms 34168 KB Output is correct
16 Correct 344 ms 31608 KB Output is correct
17 Correct 315 ms 33912 KB Output is correct
18 Correct 382 ms 31608 KB Output is correct
19 Correct 309 ms 35320 KB Output is correct
20 Correct 285 ms 27252 KB Output is correct
21 Correct 220 ms 26232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 7 ms 3072 KB Output is correct
4 Correct 7 ms 3072 KB Output is correct
5 Correct 7 ms 3072 KB Output is correct
6 Correct 6 ms 2944 KB Output is correct
7 Correct 7 ms 3072 KB Output is correct
8 Correct 7 ms 3072 KB Output is correct
9 Correct 7 ms 2944 KB Output is correct
10 Correct 7 ms 2944 KB Output is correct
11 Correct 237 ms 23800 KB Output is correct
12 Correct 262 ms 25976 KB Output is correct
13 Correct 323 ms 29180 KB Output is correct
14 Correct 357 ms 32888 KB Output is correct
15 Correct 383 ms 34168 KB Output is correct
16 Correct 344 ms 31608 KB Output is correct
17 Correct 315 ms 33912 KB Output is correct
18 Correct 382 ms 31608 KB Output is correct
19 Correct 309 ms 35320 KB Output is correct
20 Correct 285 ms 27252 KB Output is correct
21 Correct 220 ms 26232 KB Output is correct
22 Correct 496 ms 35096 KB Output is correct
23 Correct 453 ms 32760 KB Output is correct
24 Correct 462 ms 32808 KB Output is correct
25 Correct 432 ms 39136 KB Output is correct
26 Correct 457 ms 34340 KB Output is correct
27 Correct 460 ms 32872 KB Output is correct
28 Correct 81 ms 7544 KB Output is correct
29 Correct 351 ms 27892 KB Output is correct
30 Correct 328 ms 27820 KB Output is correct
31 Correct 346 ms 28440 KB Output is correct
32 Correct 317 ms 29280 KB Output is correct