:heavy_check_mark: test/graph/strongly_connected_components/yosupo_scc.test.cpp

Depends on

Code

// verification-helper: PROBLEM https://judge.yosupo.jp/problem/scc

#include "src/graph/strongly_connected_components.hpp"

#include <iostream>
#include <vector>

using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	StronglyConnectedComponents scc(n);
	for (int i = 0; i < m; ++i) {
		int a, b;
		cin >> a >> b;
		scc.add_edge(a, b);
	}

	int k = scc.build();
	vector<vector<int>> groups(k);
	for (int i = 0; i < n; ++i) groups[scc.component_id(i)].push_back(i);

	cout << k << endl;
	for (auto const &group : groups) {
		cout << group.size();
		for (auto const &v : group) cout << " " << v;
		cout << endl;
	}

	return 0;
}
#line 1 "test/graph/strongly_connected_components/yosupo_scc.test.cpp"
// verification-helper: PROBLEM https://judge.yosupo.jp/problem/scc

#line 1 "src/graph/strongly_connected_components.hpp"



#include <algorithm>
#include <vector>

class StronglyConnectedComponents {
private:
	int n;
	std::vector<std::vector<int>> graph, rev_graph;
	std::vector<int> components;

	void dfs(int v, std::vector<bool> &used, std::vector<int> &order) {
		if (used[v]) return;
		used[v] = true;
		for (auto const &nv : graph[v]) dfs(nv, used, order);
		order.push_back(v);
	}

	void rev_dfs(int v, int id, std::vector<int> &components) {
		if (components[v] != -1) return;
		components[v] = id;
		for (auto const &nv : rev_graph[v]) rev_dfs(nv, id, components);
	}

public:
	StronglyConnectedComponents(int n) : n(n), graph(n), rev_graph(n), components(n) {}

	void add_edge(int from, int to) {
		graph[from].push_back(to);
		rev_graph[to].push_back(from);
	}

	int build() {
		std::vector<bool> used(n);
		std::vector<int> order(n);
		for (int i = 0; i < n; ++i) {
			if (used[i]) continue;
			dfs(i, used, order);
		}
		std::reverse(order.begin(), order.end());

		int cnt = 0;
		components.assign(n, -1);
		for (auto const &v : order) {
			if (components[v] != -1) continue;
			rev_dfs(v, cnt, components);
			++cnt;
		}

		return cnt;
	}

	int component_id(int v) { return components[v]; }
};


#line 4 "test/graph/strongly_connected_components/yosupo_scc.test.cpp"

#include <iostream>
#line 7 "test/graph/strongly_connected_components/yosupo_scc.test.cpp"

using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	StronglyConnectedComponents scc(n);
	for (int i = 0; i < m; ++i) {
		int a, b;
		cin >> a >> b;
		scc.add_edge(a, b);
	}

	int k = scc.build();
	vector<vector<int>> groups(k);
	for (int i = 0; i < n; ++i) groups[scc.component_id(i)].push_back(i);

	cout << k << endl;
	for (auto const &group : groups) {
		cout << group.size();
		for (auto const &v : group) cout << " " << v;
		cout << endl;
	}

	return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 13 ms 7 MB
g++ large_cycle_00 :heavy_check_mark: AC 969 ms 397 MB
g++ max_random_00 :heavy_check_mark: AC 1622 ms 121 MB
g++ max_random_01 :heavy_check_mark: AC 1674 ms 121 MB
g++ max_random_02 :heavy_check_mark: AC 1638 ms 121 MB
g++ max_random_03 :heavy_check_mark: AC 1737 ms 121 MB
g++ max_random_04 :heavy_check_mark: AC 1636 ms 121 MB
g++ random_00 :heavy_check_mark: AC 1265 ms 98 MB
g++ random_01 :heavy_check_mark: AC 1334 ms 108 MB
g++ random_02 :heavy_check_mark: AC 379 ms 70 MB
g++ random_03 :heavy_check_mark: AC 730 ms 70 MB
g++ random_04 :heavy_check_mark: AC 572 ms 55 MB
clang++ example_00 :heavy_check_mark: AC 12 ms 9 MB
clang++ large_cycle_00 :heavy_check_mark: AC 699 ms 261 MB
clang++ max_random_00 :heavy_check_mark: AC 1431 ms 123 MB
clang++ max_random_01 :heavy_check_mark: AC 1469 ms 123 MB
clang++ max_random_02 :heavy_check_mark: AC 1406 ms 125 MB
clang++ max_random_03 :heavy_check_mark: AC 1456 ms 125 MB
clang++ max_random_04 :heavy_check_mark: AC 1462 ms 123 MB
clang++ random_00 :heavy_check_mark: AC 1153 ms 104 MB
clang++ random_01 :heavy_check_mark: AC 1259 ms 114 MB
clang++ random_02 :heavy_check_mark: AC 357 ms 59 MB
clang++ random_03 :heavy_check_mark: AC 639 ms 72 MB
clang++ random_04 :heavy_check_mark: AC 545 ms 55 MB
Back to top page