Submission #939937

# Submission time Handle Problem Language Result Execution time Memory
939937 2024-03-07T01:50:10 Z vjudge1 One-Way Streets (CEOI17_oneway) C++17
100 / 100
569 ms 72364 KB
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()

using namespace std;

int pow(int a,int b,int m){int ans=1;while(b){if(b&1){ans=(ans*a)%m;}b>>=1;a=(a*a)%m;}return ans;}
int binpow(int a,int b){int ans=1;while(b){if(b&1){ans=(ans*a);}b>>=1;a=(a*a);}return ans;}
  
const int N = 1e5 + 10;


int low[N], h[N], depth[N];
vector <int> g[N];
vector <int> gc[N];
int lead[N];
int vis[N], par[N];
int tin[N], tout[N], up[N][25], used[N];
int timer;

map <pair <int,int>, int > brid, was;

void dfsc(int v){
	vis[v] = 1;
	for(auto to : g[v]){
		if(vis[to] == 0 && !brid.count({v, to})){
			lead[to] = lead[v];
			dfsc(to);
		}
	}
}

void dfs_lca(int v, int p = 1) {
	tin[v] = ++timer;
	par[v] = p;
	depth[v] = depth[p] + 1;
	up[v][0] = p;
	for (int i=1; i<=20; ++i)
		up[v][i] = up[up[v][i-1]][i-1];
	for (auto to : gc[v]) {
		if (to == p)continue;
		dfs_lca(to, v);
	}
	tout[v] = ++timer;
}

bool upper (int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

int lca (int a, int b) {
	if (upper (a, b))  return a;
	if (upper (b, a))  return b;
	for (int i=20; i>=0; --i)
		if (! upper (up[a][i], b))
			a = up[a][i];
	return up[a][0];
}


void dfs_low(int v, int p){
    used[v] = true;
    tin[v] = low[v] = ++timer;
	
    for (auto u : g[v]){
        if (u == p) continue;
        if (used[u]){
            low[v] = min(low[v], tin[u]);
        }else{
            dfs_low(u, v);
            low[v] = min(low[v], low[u]);
            if (was[{v, u}] <=  1 && low[u] > tin[v]){
				brid[{v, u}] = 1;
				brid[{u, v}] = 1;
			}
        }
    }
}

main(){
	iostream::sync_with_stdio(false);  
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, m;
    cin >> n >> m;
    
    vector <pair <int,int> > road;
    for(int i = 0; i < m; i++){
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
		was[{a, b}]++;
		was[{b, a}]++;
		road.pb({a, b});
	}
    
    for(int i = 1;  i<= n; i++){
		if(!used[i])dfs_low(i, i);
	}
	
	for(int i = 1; i <= n; i++){
		if(lead[i] == 0){
			lead[i] = i;
			dfsc(i);
		}	
	}
	set <pair <int,int> > st;
	for(auto x : road){
		if(brid.count(x)){
			if(st.find(x) != st.end())return EXIT_FAILURE;
			st.insert(x);
			gc[lead[x.ff]].pb(lead[x.ss]);
			gc[lead[x.ss]].pb(lead[x.ff]);
		}
	}
	
	memset(used, 0, sizeof(used));
	for(int i = 1; i <= n; i++){
		if(par[i] == 0){
			dfs_lca(i, i);
		}
	}
	
	int p;
	cin >> p;
	map <pair <int,int>, int> mp;
	vector <pair <int, pair <int,int> > > l;
	while(p--){
		int a, b;
		cin >> a >> b;
		a = lead[a];	
		b = lead[b];
		if(a == b)continue;
		int lc = lca(a, b);
		l.pb({depth[lc], {a, b}});
	}
	sort(all(l));
	vector <int> d(n + 1, 0);
	int cnt = 0;
	for(auto x : l){
		int a = x.ss.ff, b = x.ss.ss;
		int lc = lca(a, b);
		while(a != lc && d[a] == 0){
			cnt++;
			d[a] = 1;
			mp[{lead[a], par[lead[a]]}] = 1;
			a = par[lead[a]];
		}
		while(b != lc && d[b] == 0){
			cnt++;
			d[b] = 1;
			mp[{par[lead[b]], lead[b]}] = 1;
			b = par[lead[b]];
		}
		if(cnt > n * 2)return EXIT_FAILURE;
	}
	for(auto x : road){
		int f = x.ff, s = x.ss;
		f = lead[f];
		s = lead[s];
		if(mp.count({f, s})){
			cout << 'R';
		}else if(mp.count({s, f})){
			cout << 'L';
		}else
			cout << 'B';
	}
}

Compilation message

oneway.cpp:86:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   86 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11096 KB Output is correct
2 Correct 2 ms 11096 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 4 ms 11480 KB Output is correct
5 Correct 5 ms 11608 KB Output is correct
6 Correct 3 ms 11100 KB Output is correct
7 Correct 4 ms 11608 KB Output is correct
8 Correct 4 ms 11352 KB Output is correct
9 Correct 4 ms 11100 KB Output is correct
10 Correct 3 ms 11312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11096 KB Output is correct
2 Correct 2 ms 11096 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 4 ms 11480 KB Output is correct
5 Correct 5 ms 11608 KB Output is correct
6 Correct 3 ms 11100 KB Output is correct
7 Correct 4 ms 11608 KB Output is correct
8 Correct 4 ms 11352 KB Output is correct
9 Correct 4 ms 11100 KB Output is correct
10 Correct 3 ms 11312 KB Output is correct
11 Correct 117 ms 29384 KB Output is correct
12 Correct 162 ms 32608 KB Output is correct
13 Correct 170 ms 36872 KB Output is correct
14 Correct 255 ms 43108 KB Output is correct
15 Correct 278 ms 45060 KB Output is correct
16 Correct 461 ms 56720 KB Output is correct
17 Correct 401 ms 61608 KB Output is correct
18 Correct 416 ms 56772 KB Output is correct
19 Correct 402 ms 63392 KB Output is correct
20 Correct 136 ms 34248 KB Output is correct
21 Correct 136 ms 33460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11096 KB Output is correct
2 Correct 2 ms 11096 KB Output is correct
3 Correct 3 ms 11356 KB Output is correct
4 Correct 4 ms 11480 KB Output is correct
5 Correct 5 ms 11608 KB Output is correct
6 Correct 3 ms 11100 KB Output is correct
7 Correct 4 ms 11608 KB Output is correct
8 Correct 4 ms 11352 KB Output is correct
9 Correct 4 ms 11100 KB Output is correct
10 Correct 3 ms 11312 KB Output is correct
11 Correct 117 ms 29384 KB Output is correct
12 Correct 162 ms 32608 KB Output is correct
13 Correct 170 ms 36872 KB Output is correct
14 Correct 255 ms 43108 KB Output is correct
15 Correct 278 ms 45060 KB Output is correct
16 Correct 461 ms 56720 KB Output is correct
17 Correct 401 ms 61608 KB Output is correct
18 Correct 416 ms 56772 KB Output is correct
19 Correct 402 ms 63392 KB Output is correct
20 Correct 136 ms 34248 KB Output is correct
21 Correct 136 ms 33460 KB Output is correct
22 Correct 499 ms 67516 KB Output is correct
23 Correct 518 ms 64724 KB Output is correct
24 Correct 569 ms 64756 KB Output is correct
25 Correct 447 ms 72364 KB Output is correct
26 Correct 483 ms 66632 KB Output is correct
27 Correct 510 ms 64696 KB Output is correct
28 Correct 46 ms 14792 KB Output is correct
29 Correct 161 ms 36740 KB Output is correct
30 Correct 170 ms 36536 KB Output is correct
31 Correct 151 ms 36552 KB Output is correct
32 Correct 241 ms 46564 KB Output is correct