답안 #938297

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
938297 2024-03-05T04:24:06 Z vjudge1 One-Way Streets (CEOI17_oneway) C++17
0 / 100
3000 ms 14684 KB
#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()
#define int long long

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 = 2e5 + 10,  inf = 1e18;

int incyc[N], deg[N];
vector <int> g[N];
int lead[N];
int vis[N], par[N];
int tin[N], tout[N], up[N][25];
int timer;

void dfs(int v){
	vis[v] = 1;
	for(auto to : g[v]){
		if(vis[to] == 0 && deg[to] > 1){
			lead[to] = lead[v];
			dfs(to);
		}
	}
}

void dfs2(int v, int p = 1) {
	tin[v] = ++timer;
	par[v] = p;
	up[v][0] = p;
	for (int i=1; i<=20; ++i)
		up[v][i] = up[up[v][i-1]][i-1];
	for (size_t i=0; i<g[v].size(); ++i) {
		int to = g[v][i];
		if (to == p)continue;
		if(lead[to] == 0 || lead[to] == to)
			dfs2 (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];
}

int lift[N], desc[N];

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;
		deg[a]++;
		deg[b]++;
		g[a].pb(b);
		g[b].pb(a);
		road.pb({a, b});
	}
    queue <int> q;
	for(int i = 1; i <= n; i++){
		if(deg[i] == 1)q.push(i);
	}
	while(!q.empty()){
		int v = q.front();
		q.pop();
		for(auto to : g[v]){
			if(--deg[to] == 1)q.push(to);
		}
	}
	
	for(int i = 1; i <= n; i++){
		if(deg[i] > 1 && lead[i] == 0){
			lead[i] = i;
			dfs(i);
		}
	}
	for(int i = 1; i <= n; i++){
		if(deg[i] > 1){
			//cout << i <<"--" << lead[i]<< endl;
		}
	}
	for(int i = 1; i <= n; i++){
		if(lead[i] == 0 || lead[i] == i)continue;
		for(auto to : g[i]){
			if(lead[to] == 0){
				g[lead[i]].pb(to);
				g[to].pb(lead[i]);
			}
		}
	}
	dfs2(1, 1);
	int p;
	cin >> p;
	while(p--){
		int a, b;
		cin >> a >> b;
		//cout << lead[a] <<"="<< lead[b] << endl;
		if(lead[a] != 0 && lead[a] != a)a = lead[a];	
		if(lead[b] != 0 && lead[b] != b)b = lead[b];
		int lc = lca(a, b);
		//cout << lc <<"-" << a <<" "<< b << endl;
		while(a != lc){
		//	cout << a <<"->"<< par[a]<< endl;
			lift[a] = 1;
			a = par[a];
		}
		while(b != lc){
		//	cout << b <<"->"<< par[b] << endl;
			b = par[b];
			desc[b] = 1;
		}
	}
	for(auto x : road){
		if(lead[x.ff] != 0 && lead[x.ff] == lead[x.ss]){
			cout << 'B';
			continue;
		}
		int f = x.ff, s = x.ss;
		if(tin[x.ff] < tin[x.ss]){
			if(desc[x.ff]){
				cout << 'R';
			}else if(lift[x.ss]){
				cout << 'L';
			}else
				cout << 'B';
		}else{
			if(lift[x.ff]){
				cout << 'R';
			}else if(desc[x.ss]){
				cout << 'L';
			}else
				cout << 'B';
		}
	}
}

Compilation message

oneway.cpp:64:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   64 | main(){
      | ^~~~
oneway.cpp: In function 'int main()':
oneway.cpp:139:7: warning: unused variable 'f' [-Wunused-variable]
  139 |   int f = x.ff, s = x.ss;
      |       ^
oneway.cpp:139:17: warning: unused variable 's' [-Wunused-variable]
  139 |   int f = x.ff, s = x.ss;
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3062 ms 14684 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3062 ms 14684 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3062 ms 14684 KB Time limit exceeded
2 Halted 0 ms 0 KB -