답안 #1027569

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1027569 2024-07-19T07:40:13 Z vjudge1 One-Way Streets (CEOI17_oneway) C++17
100 / 100
51 ms 20172 KB
#include<bits/stdc++.h>

using namespace std;
int n, m, p, u, v, lead[100005], low[100005], num[100005], d = 0, cnt = 0, ans[100005], timer = 0, q, val[100005];
vector<pair<int,int>> adj[100005];
vector<pair<int,int>> adj2[100005], edge;
stack<int> s;
bool mark[100005];
void dfs(int u, int id) {
	low[u] = num[u] = ++d;
	s.push(u);
	for (pair<int, int> W: adj[u]) {
		int node = W.first;
		if(W.second == id) continue;
		if(num[node] != 0) low[u] = min(low[u], num[node]);
		else {
			dfs(node, W.second);
			low[u] = min(low[u], low[node]);
		}
	}
	if(low[u] == num[u]) {
		cnt++;
		while(s.top() != u) lead[s.top()] = cnt, s.pop();
		lead[s.top()] = cnt, s.pop();
	}
}
void cal(int u, int par) {
	for (pair<int, int> node: adj2[u]) {
		if(mark[node.first]) continue;
		mark[node.first] = true;
		cal(node.first, u);
		val[u] += val[node.first];
	}
}
void get_ans(int u, int par) {
	for (pair<int, int> node: adj2[u]) {
		if(mark[node.first]) continue;
		mark[node.first] = true;
		int x, y;
		x = u, y = node.first;
		int id = node.second;
		//cout << "QWFQWF " << x << ' ' << y  << ' ' << id << '\n';
		if(val[node.first] < 0) {
			//cout << "EDGE " << x << ' ' << y << ' ' << edge[id].first << ' ' << edge[id].second << '\n';
			if(lead[edge[id].first] == x && lead[edge[id].second] == y) ans[id+1] = 1;
			else ans[id+1] = 2;
		}
		else if(val[node.first] > 0) {
			//cout << "EDGE " << x << ' ' << y << ' ' << edge[id].first << ' ' << edge[id].second << '\n';
			if(lead[edge[id].first] == x && lead[edge[id].second] == y) ans[id+1] = 2;
			else ans[id+1] = 1;
		}
		else ans[id+1] = 0;
		get_ans(node.first, u);
	}
}
void solve() {
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		cin >> u >> v;
		edge.push_back({u,v});
		adj[u].push_back({v,i});
		adj[v].push_back({u,i});
	}
	for (int i = 1; i <= n; i++) {
		if(num[i] == 0) dfs(i, -1);
	}
	int idx = 0;
	for (pair<int, int> i: edge) {
		if(lead[i.first] != lead[i.second]) adj2[lead[i.first]].push_back({lead[i.second], idx}), adj2[lead[i.second]].push_back({lead[i.first],idx});
		idx++;
	}
	cin >> q;
	while(q--) {
		cin >> u >> v;
		u = lead[u];
		v = lead[v];
		val[u]++;
		val[v]--;
	}
	for (int i = 1; i <= cnt; i++) if(!mark[i]) cal(i,-1);
	memset(mark, 0, sizeof(mark));
	for (int i = 1; i <= cnt; i++) if(!mark[i]) get_ans(i, -1);
	for (int i = 1; i <= m; i++) if(ans[i] == 0) cout << 'B'; else if(ans[i] == 1) cout << 'R'; else cout << 'L';
}

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen("input.inp","r")){
		freopen("input.inp", "r", stdin);
		freopen("output.out", "w", stdout);
	}
	int t = 1;
	// cin >> t;
	while(t--) {
		solve();
	}
}

Compilation message

oneway.cpp: In function 'int main()':
oneway.cpp:90:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |   freopen("input.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
oneway.cpp:91:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |   freopen("output.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 1 ms 6748 KB Output is correct
6 Correct 2 ms 6748 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 2 ms 6760 KB Output is correct
10 Correct 1 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 1 ms 6748 KB Output is correct
6 Correct 2 ms 6748 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 2 ms 6760 KB Output is correct
10 Correct 1 ms 6748 KB Output is correct
11 Correct 20 ms 11720 KB Output is correct
12 Correct 21 ms 12496 KB Output is correct
13 Correct 27 ms 13520 KB Output is correct
14 Correct 32 ms 14188 KB Output is correct
15 Correct 51 ms 14652 KB Output is correct
16 Correct 42 ms 15556 KB Output is correct
17 Correct 38 ms 16832 KB Output is correct
18 Correct 36 ms 15344 KB Output is correct
19 Correct 31 ms 18120 KB Output is correct
20 Correct 21 ms 11984 KB Output is correct
21 Correct 19 ms 11728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6748 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 1 ms 6748 KB Output is correct
5 Correct 1 ms 6748 KB Output is correct
6 Correct 2 ms 6748 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 2 ms 6760 KB Output is correct
10 Correct 1 ms 6748 KB Output is correct
11 Correct 20 ms 11720 KB Output is correct
12 Correct 21 ms 12496 KB Output is correct
13 Correct 27 ms 13520 KB Output is correct
14 Correct 32 ms 14188 KB Output is correct
15 Correct 51 ms 14652 KB Output is correct
16 Correct 42 ms 15556 KB Output is correct
17 Correct 38 ms 16832 KB Output is correct
18 Correct 36 ms 15344 KB Output is correct
19 Correct 31 ms 18120 KB Output is correct
20 Correct 21 ms 11984 KB Output is correct
21 Correct 19 ms 11728 KB Output is correct
22 Correct 45 ms 16736 KB Output is correct
23 Correct 45 ms 15304 KB Output is correct
24 Correct 47 ms 15560 KB Output is correct
25 Correct 45 ms 20172 KB Output is correct
26 Correct 48 ms 16580 KB Output is correct
27 Correct 42 ms 15404 KB Output is correct
28 Correct 20 ms 9680 KB Output is correct
29 Correct 33 ms 11460 KB Output is correct
30 Correct 32 ms 11728 KB Output is correct
31 Correct 33 ms 11948 KB Output is correct
32 Correct 38 ms 14488 KB Output is correct