답안 #644224

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
644224 2022-09-24T05:53:09 Z ymm One-Way Streets (CEOI17_oneway) C++17
100 / 100
280 ms 51264 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 100'010;

enum {IN, OUT};
enum {UP = 1, DOWN = 2};

class s2l
{
private:
	unordered_multiset<ll> s[2];
	ll llp(int x, int y)
	{
		return x ^ ((ll)y << 32);
	}
public:
	size_t size()
	{
		return s[0].size() + s[1].size();
	}
	void insert(int v, int u, bool dir)
	{
		auto it = s[!dir].find(llp(u, v));
		if (it != s[!dir].end())
			s[!dir].erase(it);
		else
			s[dir].insert(llp(v, u));
	}
	void merge(s2l *x)
	{
		if (size() < x->size()) {
			s[IN].swap(x->s[IN]);
			s[OUT].swap(x->s[OUT]);
		}
		for (auto p : x->s[IN])
			insert(p, p>>32, IN);
		for (auto p : x->s[OUT])
			insert(p, p>>32, OUT);
		delete(x);
	}
	int get()
	{
		return (DOWN & -s[OUT].empty()) | (UP & -s[IN].empty());
	}
};

vector<pii> Ao[N];
vector<pii> A2[N];
int col2[N];
bool ncut[N];
int h[N];

bool find_ncut_vis[N];
int find_ncut(int v, int p, int h)
{
	::h[v] = h;
	int mn = h;
	find_ncut_vis[v] = 1;
	for (auto [u, ei] : Ao[v]) {
		if (ei == p)
			continue;
		if (find_ncut_vis[u]) {
			mn = min(mn, ::h[u]);
			ncut[ei] = 1;
		} else {
			int x = find_ncut(u, ei, h+1);
			if (x <= h)
				ncut[ei] = 1;
			mn = min(mn, x);
		}
	}
	return mn;
}

void make_col2(int v, int col)
{
	col2[v] = col;
	for (auto [u, ei] : Ao[v]) {
		if (ncut[ei] && col2[u] == -1)
			make_col2(u, col);
	}
}

vector<pii> Eo;
vector<pii> req2[N];

void make_A2()
{
	Loop (i,0,Eo.size()) {
		auto [v, u] = Eo[i];
		v = col2[v];
		u = col2[u];
		if (v != u) {
			A2[v].push_back({u, i});
			A2[u].push_back({v, i});
		}
	}
}

void read_and_make_req2()
{
	int p;
	cin >> p;
	Loop (i,0,p) {
		int v, u;
		cin >> v >> u;
		--v; --u;
		v = col2[v]; u = col2[u];
		if (v != u) {
			req2[v].push_back({u, OUT});
			req2[u].push_back({v, IN});
		}
	}
}

pii ans2[N];

bool main_dfs_vis[N];
s2l *main_dfs(int v, int p)
{
	main_dfs_vis[v] = 1;
	s2l *obj = new s2l;
	for (auto [u, dir] : req2[v])
		obj->insert(v, u, dir);
	for (auto [u, ei] : A2[v]) {
		if (ei == p)
			continue;
		obj->merge(main_dfs(u, ei));
	}
	if (p != -1)
		ans2[p] = {obj->get(), v};
	return obj;
}

char get_char(int ei)
{
	auto [v, u] = Eo[ei];
	switch (ans2[ei].first) {
	case UP|DOWN: return 'B';
	case UP     : return ans2[ei].second == col2[u]? 'L': 'R';
	case    DOWN: return ans2[ei].second == col2[u]? 'R': 'L';
	default     : return 'N';
	}
}

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	Loop (i,0,m) {
		int v, u;
		cin >> v >> u;
		--v; --u;
		Ao[v].push_back({u,i});
		Ao[u].push_back({v,i});
		Eo.push_back({v, u});
	}
	Loop (i,0,n)
		if (!find_ncut_vis[i])
			find_ncut(i, -1, 0);
	//Loop (i,0,m) cout << ncut[i] << ' '; cout << '\n';
	int n2 = 0;
	memset(col2, -1, sizeof(col2));
	Loop (i,0,n) {
		if (col2[i] == -1)
			make_col2(i, n2++);
	}
	//Loop (i,0,n) cout << col2[i] << ' '; cout << '\n';
	make_A2();
	read_and_make_req2();
	Loop (i,0,n2)
		if (!main_dfs_vis[i])
			delete(main_dfs(i, -1));
	Loop (i,0,m) {
		auto [v, u] = Eo[i];
		cout << (col2[v] == col2[u]? 'B': get_char(i));
	}
	cout << '\n';
}

Compilation message

oneway.cpp: In function 'void make_A2()':
oneway.cpp:2:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    2 | #define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
      |                                        ^
oneway.cpp:95:2: note: in expansion of macro 'Loop'
   95 |  Loop (i,0,Eo.size()) {
      |  ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7764 KB Output is correct
2 Correct 5 ms 7764 KB Output is correct
3 Correct 5 ms 7764 KB Output is correct
4 Correct 4 ms 7764 KB Output is correct
5 Correct 4 ms 7892 KB Output is correct
6 Correct 5 ms 7840 KB Output is correct
7 Correct 5 ms 7892 KB Output is correct
8 Correct 4 ms 7892 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 5 ms 7764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7764 KB Output is correct
2 Correct 5 ms 7764 KB Output is correct
3 Correct 5 ms 7764 KB Output is correct
4 Correct 4 ms 7764 KB Output is correct
5 Correct 4 ms 7892 KB Output is correct
6 Correct 5 ms 7840 KB Output is correct
7 Correct 5 ms 7892 KB Output is correct
8 Correct 4 ms 7892 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 5 ms 7764 KB Output is correct
11 Correct 36 ms 12480 KB Output is correct
12 Correct 46 ms 13740 KB Output is correct
13 Correct 56 ms 14796 KB Output is correct
14 Correct 66 ms 15808 KB Output is correct
15 Correct 60 ms 16216 KB Output is correct
16 Correct 95 ms 17392 KB Output is correct
17 Correct 104 ms 22260 KB Output is correct
18 Correct 82 ms 17492 KB Output is correct
19 Correct 76 ms 25616 KB Output is correct
20 Correct 45 ms 12740 KB Output is correct
21 Correct 39 ms 13476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7764 KB Output is correct
2 Correct 5 ms 7764 KB Output is correct
3 Correct 5 ms 7764 KB Output is correct
4 Correct 4 ms 7764 KB Output is correct
5 Correct 4 ms 7892 KB Output is correct
6 Correct 5 ms 7840 KB Output is correct
7 Correct 5 ms 7892 KB Output is correct
8 Correct 4 ms 7892 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 5 ms 7764 KB Output is correct
11 Correct 36 ms 12480 KB Output is correct
12 Correct 46 ms 13740 KB Output is correct
13 Correct 56 ms 14796 KB Output is correct
14 Correct 66 ms 15808 KB Output is correct
15 Correct 60 ms 16216 KB Output is correct
16 Correct 95 ms 17392 KB Output is correct
17 Correct 104 ms 22260 KB Output is correct
18 Correct 82 ms 17492 KB Output is correct
19 Correct 76 ms 25616 KB Output is correct
20 Correct 45 ms 12740 KB Output is correct
21 Correct 39 ms 13476 KB Output is correct
22 Correct 248 ms 36084 KB Output is correct
23 Correct 278 ms 30712 KB Output is correct
24 Correct 280 ms 30076 KB Output is correct
25 Correct 221 ms 51264 KB Output is correct
26 Correct 225 ms 35828 KB Output is correct
27 Correct 236 ms 30824 KB Output is correct
28 Correct 31 ms 11912 KB Output is correct
29 Correct 88 ms 23760 KB Output is correct
30 Correct 93 ms 26216 KB Output is correct
31 Correct 65 ms 18720 KB Output is correct
32 Correct 152 ms 36840 KB Output is correct