답안 #303116

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
303116 2020-09-19T22:28:52 Z Kenzo_1114 One-Way Streets (CEOI17_oneway) C++17
0 / 100
7 ms 9856 KB
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
const int MAXK = 21;

int n, m, p, depth[MAXN], low[MAXN], id[MAXN], par[MAXN], dp[MAXK][MAXN];
vector<int> graph[MAXN], edge[MAXN];
bool bridge[MAXN], marc[MAXN];
char ans[MAXN];

void dfs(int v, int p)
{
	par[v] = p;
	dp[0][v] = p;
	depth[v] = (v == p) ? 0 : depth[p] + 1;
	low[v] = depth[v];

	bool first = false;
	for(int i = 0; i < (int) graph[v].size(); i++)
	{
		int u = graph[v][i];
		int ed = edge[v][i];

		id[u] = ed;
		
		if(low[u] != -1)
		{	
			if(first || (u != p))	low[v] = min(low[v], depth[u]);
			if(u == p)	first = true;
			continue;
		}

		dfs(u, v);
		low[v] = min(low[v], low[u]);

		if(depth[v] < low[u])	bridge[ed / 2] = true;
	}
}

void calc()
{
	for(int k = 1; k < MAXK; k++)
		for(int i = 1; i <= n; i++)
			dp[k][i] = dp[k - 1][dp[k - 1][i]];
}

int lca(int a, int b)
{
	if(a == b)	return a;
	if(depth[a] >= depth[b])	swap(a, b);

	for(int k = MAXK - 1; k >= 0; k--)
		if(depth[dp[k][b]] >= depth[a])
			b = dp[k][b];

	if(a == b)	return a;

	for(int k = MAXK - 1; k >= 0; k--)
		if(dp[k][a] != dp[k][b])
			a = dp[k][a], b = dp[k][b];

	return dp[0][a];
}

int main ()
{
	scanf("%d %d", &n, &m);

	for(int i = 0; i <= n; i++)	low[i] = -1;

	for(int i = 0, u, v; i < m; i++)
	{
		scanf("%d %d", &u, &v);
		graph[u].push_back(v);
		graph[v].push_back(u);
		edge[u].push_back(2 * i);
		edge[v].push_back(2 * i + 1);
		ans[i] = 'B';
	}

	dfs(1, 1);
	calc();

//	for(int i = 0; i < m; i++)	
//		if(bridge[i])	printf("edge %d is bridge\n", i);

	scanf("%d", &p);

	for(int i = 0, x, y; i < p; i++)
	{
		scanf("%d %d", &x, &y);

		int LCA = lca(x, y);
//		printf("LCA = %d\n", LCA);

		while(x != LCA)
		{
			if(bridge[id[x] / 2])	ans[id[x] / 2] = (id[x] % 2) ? 'R' : 'L';
			x = par[x];
		}

		while(y != LCA)
		{
			if(bridge[id[y] / 2])	ans[id[y] / 2] = (id[y] % 2) ? 'L' : 'R';
			y = par[y];
		}
	}

	for(int i = 0; i < m; i++)	printf("%c", ans[i]);
	printf("\n");
}

/*

5 6
1 2
1 2
4 3
2 3
1 3
5 1
2
4 5
1 3

*/

Compilation message

oneway.cpp: In function 'int main()':
oneway.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   67 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
oneway.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   73 |   scanf("%d %d", &u, &v);
      |   ~~~~~^~~~~~~~~~~~~~~~~
oneway.cpp:87:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   87 |  scanf("%d", &p);
      |  ~~~~~^~~~~~~~~~
oneway.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   91 |   scanf("%d %d", &x, &y);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 9856 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 9856 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 9856 KB Output isn't correct
2 Halted 0 ms 0 KB -