zawatins-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub zawa-tin/zawatins-library

:heavy_check_mark: test/ABC282-D.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"

#include "../src/graph/Read-Graph.hpp"
#include "../src/graph/simple/connectedComponents.hpp"
#include "../src/graph/simple/bipartiteJudge.hpp"

#include <iostream>
#include <vector>

inline long long nC2(int value) {
	return (long long)value * (value - 1) / 2;
}

int main() {
	// int N, M; std::cin >> N >> M;
 //   	auto G = zawa::read_graph(N, M); 
	// auto colors = zawa::bipartiteJudge(G);
	// if (colors.ok()) {
	// 	auto groups = zawa::connectedComponents(G).comps();
	// 	long long ans = nC2(N) - M;
	// 	for (auto group : groups) {
	// 		int c1 = 0;
	// 		for (auto x : group) {
	// 			c1 += colors[x];
	// 		}
	// 		ans -= nC2(c1) + nC2(group.size() - c1);
	// 	}
	// 	std::cout << ans << std::endl;
	// }
	// else {
	// 	std::cout << 0 << std::endl;
	// }

	std::cout << "Hello World" << std::endl;
}

/*
 * AtCoder Beginner Contest 282 D- Make Bipartite 2
 * https://atcoder.jp/contests/abc282/submissions/39400340
 */
#line 1 "test/ABC282-D.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"

#line 2 "src/graph/Read-Graph.hpp"

#include <vector>
#include <iostream>

namespace zawa {

std::vector<std::vector<int>> read_graph(int n, int m, bool undirect = true, bool minus = true) {
    std::vector<std::vector<int>> res(n, std::vector(0, 0));
    for (int _ = 0 ; _ < m ; _++) {
        int u, v;
        std::cin >> u >> v;
        res[u - minus].emplace_back(v - minus);
        if (undirect) {
            res[v - minus].emplace_back(u - minus);
        }
    }
    return res;
}

std::vector<std::vector<int>> read_tree(int n, bool undirect = true, bool minus = true) {
    return read_graph(n, n - 1, undirect, minus);
}

}
#line 2 "src/graph/simple/connectedComponents.hpp"

#line 4 "src/graph/simple/connectedComponents.hpp"
#include <stack>

namespace zawa {

class connectedComponents {
private:
	std::vector<int> ids;
	std::vector<std::vector<int>> groups;    

	void build(const std::vector<std::vector<int>>& G) {
		int id = 0;
		for (int i = 0 ; i < (int)G.size() ; i++) {
			if (ids[i] == -1) {
				ids[i] = id;
				std::stack<int> stk({ i });		
				while (stk.size()) {
					int v = stk.top();
					stk.pop();
					for (auto x : G[v]) {
						if (ids[x] == -1) {
							ids[x] = id;
							stk.push(x);
						}
					}
				}
				id++;
			}
		}
		groups = std::vector(id, std::vector(0, 0));
		for (int i = 0 ; i < (int)ids.size() ; i++) {
			groups[ids[i]].push_back(i);
		}
	}

public:

	connectedComponents(const std::vector<std::vector<int>>& G) : ids(G.size(), -1) {
		build(G);
	}

	template <class cost_type>
	connectedComponents(const std::vector<std::vector<std::pair<int, cost_type>>>& G) : ids(G.size(), -1) {
		std::vector tmpG(G.size(), std::vector(0, 0));
		for (int i = 0 ; i < (int)G.size() ; i++) {
			for (auto [x, _] : G[i]) {
				tmpG[i].push_back(x);
			}
		}
		build(tmpG);
	}

	inline int operator [](int i) const {
		return ids[i];
	}

	inline std::size_t size() const {
		return groups.size();
	}

	inline std::size_t size(int x) const {
		return groups[ids[x]].size();
	}

	inline std::vector<std::vector<int>> comps() const {
		return groups;
	}

	inline std::vector<int> comp(int id) const {
		return groups[id];
	}

	bool same(int i, int j) const {
		return ids[i] == ids[j];
	}
};

} // namespace zawa
#line 2 "src/graph/simple/bipartiteJudge.hpp"

#line 5 "src/graph/simple/bipartiteJudge.hpp"
#include <utility>

namespace zawa {

class bipartiteJudge {
private:
	std::vector<bool> colors;
	bool isBipartiteGraph;

	void build(const std::vector<std::vector<int>>& G) {
		if (G.empty()) {
			return;
		}
		std::stack<std::pair<int, bool>> S;
		std::vector<bool> used(G.size(), false);
		for (int i = 0 ; i < (int)G.size() ; i++) {
			if (!used[i]) {
				S.emplace(i, true);
				used[i] = true;
				colors[i] = true;
				while (S.size()) {
					auto [v, col] = S.top();
					S.pop();
					for (const auto& x : G[v]) {
						if (used[x]) {
							isBipartiteGraph &= colors[x] != col;
						}
						else {
							used[x] = true;
							colors[x] = !col;
							S.emplace(x, !col);
						}
					}
				}
			}
		}
	}

public:
	
	bipartiteJudge(const std::vector<std::vector<int>>& G) : colors(G.size()), isBipartiteGraph(true) {
		build(G);
	}

	template <class cost_type>
	bipartiteJudge(const std::vector<std::vector<std::pair<int, cost_type>>>& G) : colors(G.size()), isBipartiteGraph(true) {
		std::vector tmpG(G.size(), std::vector(0, 0));
		for (std::size_t i = 0 ; i < G.size() ; i++) {
			for (const auto& [x, _] : G[i]) {
				tmpG[i].push_back(x);
			}
		}
		build(tmpG);
	}

	inline const bool ok() const {
		return isBipartiteGraph;
	}

	inline bool operator[](int i) const {
		return colors[i];
	}
};

} // namespace zawa
#line 6 "test/ABC282-D.test.cpp"

#line 9 "test/ABC282-D.test.cpp"

inline long long nC2(int value) {
	return (long long)value * (value - 1) / 2;
}

int main() {
	// int N, M; std::cin >> N >> M;
 //   	auto G = zawa::read_graph(N, M); 
	// auto colors = zawa::bipartiteJudge(G);
	// if (colors.ok()) {
	// 	auto groups = zawa::connectedComponents(G).comps();
	// 	long long ans = nC2(N) - M;
	// 	for (auto group : groups) {
	// 		int c1 = 0;
	// 		for (auto x : group) {
	// 			c1 += colors[x];
	// 		}
	// 		ans -= nC2(c1) + nC2(group.size() - c1);
	// 	}
	// 	std::cout << ans << std::endl;
	// }
	// else {
	// 	std::cout << 0 << std::endl;
	// }

	std::cout << "Hello World" << std::endl;
}

/*
 * AtCoder Beginner Contest 282 D- Make Bipartite 2
 * https://atcoder.jp/contests/abc282/submissions/39400340
 */
Back to top page