답안 #559822

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
559822 2022-05-10T17:12:37 Z GioChkhaidze One-Way Streets (CEOI17_oneway) C++14
0 / 100
3 ms 5076 KB
#include <bits/stdc++.h>

#define pb push_back
#define f first
#define s second

using namespace std;

const int N = 1e5 + 5;

char ans[N];
bool f[N], brd[N];
int n, m, q, timer, depth, ts;
int a[N], b[N], disc[N], low[N], p[N], sz[N], dep[N], in[N], out[N], W[N][4], Pr[N][20];
vector < int > bridges;
vector < pair < int , int > > v[N], g[N];

void dfs(int x, int p) {
	f[x] = true;
	disc[x] = low[x] = ++timer;
	for (int i = 0; i < v[x].size(); ++i) {
		int to = v[x][i].f, ed = v[x][i].s;
		if (to == p) continue;
		if (!f[to]) {
			dfs(to, x);
			low[x] = min(low[x], low[to]);
			if (low[to] > disc[x]) {
				brd[ed] = true;
				bridges.pb(ed);
			}
		}
			else {
			low[x] = min(low[x], disc[to]);		
		}
	}
}

bool is_anc(int a, int b) {
	return in[a] <= in[b] && out[b] <= out[a];
}

int lca(int a, int b) {
	if (dep[a] > dep[b]) swap(a, b);
	if (is_anc(a, b)) return a;
	for (int i = 17; i >= 0; --i) 
		if (!is_anc(Pr[a][i], b)) a = Pr[a][i];
	return Pr[a][0];
}

void wfs(int x, int p) {
	f[x] = true;
	in[x] = ++ts;
	Pr[x][0] = p;
	for (int j = 1; j < 18; ++j) {
		Pr[x][j] = Pr[Pr[x][j - 1]][j - 1];
	}
	for (int i = 0; i < g[x].size(); ++i) {
		int to = g[x][i].f, ed = g[x][i].s;
		if (to == p) continue;
		dep[to] = dep[x] + 1;
		wfs(to, x);
	}
	out[x] = ts;
}

void ufs(int x, int edg) {
	for (int i = 0; i < g[x].size(); ++i) {
		int to = g[x][i].f, ed = g[x][i].s;
		if (ed == edg) continue;
		ufs(to, ed);
		W[x][0] += W[to][0];
		W[x][1] += W[to][1];
	}
	
	if (edg != -1){
		if (W[x][0] > 0) {
			if (b[edg] == x) ans[edg] = 'R';
				else ans[edg] = 'L';	
		}
			else
		if (W[x][1] > 0) {
			if (b[edg] == x) ans[edg] = 'L';
				else ans[edg] = 'R';
		}
	}
}

int P(int x) {
	if (p[x] == x) return x;
	return p[x] = P(p[x]);
}

main () {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);
	cin >> n >> m;
	for (int i = 1; i <= m; ++i) {
		ans[i] = 'B';
		cin >> a[i] >> b[i];
		v[a[i]].pb({b[i], i});
		v[b[i]].pb({a[i], i});
	}
	
	for (int i = 1; i <= n; ++i) {
		if (!f[i]) {
			dfs(i, -1);
		}
	}
	
	for (int i = 1; i <= n; ++i) {
		p[i] = i, sz[i] = 1, f[i] = 0;
	}
	
	for (int i = 1; i <= m; ++i) {
		if (brd[i]) continue;
		int A = P(a[i]), B = P(b[i]);
		if (A == B) continue;
		if (sz[A] < sz[B]) swap(A, B);
		sz[A] += sz[B];
		p[B] = A;
	}
	
	vector < int > nd;
	for (int i = 0; i < bridges.size(); ++i) {
		int id = bridges[i];
		int A = a[id];
		int B = b[id];
		A = P(A), B = P(B);
		g[A].pb({B, id});
		g[B].pb({A, id});
		if (!f[A]) {
			nd.pb(A);
			f[A] = true;
		}
		if (!f[B]) {
			nd.pb(B);
			f[B] = true;
		}
	}
	
	for (int i = 0; i < nd.size(); ++i) {
		f[nd[i]] = false;
	}
	
	for (int i = 0; i < nd.size(); ++i) {
		if (!f[nd[i]]) {
			wfs(nd[i], nd[i]);	
		}
	}
	
	cin >> q;
	for (int i = 1; i <= q; ++i) {
		int x, y;
		cin >> x >> y;
		x = P(x), y = P(y);
		if (x == y) continue;
		int z = lca(x, y);
		if (z != x) --W[z][0], ++W[x][0];
		if (z != y) --W[z][1], ++W[y][1];
	}
	
	for (int i = 0; i < nd.size(); ++i) {
		f[nd[i]] = false;
	}
	
	for (int i = 0; i < nd.size(); ++i) {
		if (!f[nd[i]]) {
			ufs(nd[i], -1);	
		}
	}
	
	for (int i = 1; i <= m; ++i) {
		cout << ans[i];
	}
}

Compilation message

oneway.cpp: In function 'void dfs(int, int)':
oneway.cpp:21:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |  for (int i = 0; i < v[x].size(); ++i) {
      |                  ~~^~~~~~~~~~~~~
oneway.cpp: In function 'void wfs(int, int)':
oneway.cpp:57:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |  for (int i = 0; i < g[x].size(); ++i) {
      |                  ~~^~~~~~~~~~~~~
oneway.cpp:58:23: warning: unused variable 'ed' [-Wunused-variable]
   58 |   int to = g[x][i].f, ed = g[x][i].s;
      |                       ^~
oneway.cpp: In function 'void ufs(int, int)':
oneway.cpp:67:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |  for (int i = 0; i < g[x].size(); ++i) {
      |                  ~~^~~~~~~~~~~~~
oneway.cpp: At global scope:
oneway.cpp:93:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   93 | main () {
      | ^~~~
oneway.cpp: In function 'int main()':
oneway.cpp:124:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |  for (int i = 0; i < bridges.size(); ++i) {
      |                  ~~^~~~~~~~~~~~~~~~
oneway.cpp:141:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |  for (int i = 0; i < nd.size(); ++i) {
      |                  ~~^~~~~~~~~~~
oneway.cpp:145:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |  for (int i = 0; i < nd.size(); ++i) {
      |                  ~~^~~~~~~~~~~
oneway.cpp:162:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |  for (int i = 0; i < nd.size(); ++i) {
      |                  ~~^~~~~~~~~~~
oneway.cpp:166:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |  for (int i = 0; i < nd.size(); ++i) {
      |                  ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 5076 KB Output isn't correct
2 Halted 0 ms 0 KB -