Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <queue>
- #include <math.h>
- class MatrixGraph {
- private:
- std::vector<std::vector<bool>> data;
- public:
- MatrixGraph(size_t N);
- ~MatrixGraph() {};
- void AddEdge(size_t from, size_t to);
- std::vector<size_t> GetVertices(size_t vertice);
- };
- MatrixGraph::MatrixGraph(size_t N) {
- std::vector<bool> tmp;
- for (size_t i = 0; i < N; i++) {
- std::vector<bool> tmp;
- for (size_t j = 0; j < N; j++)
- tmp.push_back(false);
- data.push_back(tmp);
- }
- }
- void MatrixGraph::AddEdge(size_t from, size_t to) {
- data.at(from).at(to) = true;
- data.at(to).at(from) = true;
- }
- std::vector<size_t> MatrixGraph::GetVertices(size_t from) {
- std::vector<size_t> res;
- for (size_t to = 0; to < data.size(); to++) {
- if (data.at(from).at(to) == true)
- res.push_back(to);
- }
- return res;
- }
- bool in(size_t digit, std::vector<size_t> vec) {
- for (size_t i = 0; i < vec.size(); i++) {
- if (digit == vec[i]) return true;
- }
- return false;
- }
- int main()
- {
- size_t N, M;
- std::cin >> N >> M;
- int res = N + 1;
- MatrixGraph graph(N);
- for (size_t i = 0; i < M; i++) {
- size_t from, to;
- std::cin >> from >> to;
- graph.AddEdge(from, to);
- }
- std::vector<size_t> A, B;
- std::vector<bool> used(N);
- for (size_t j = 0; j < N; j++) {
- if (used[j]) continue;
- std::queue<int> q;
- q.push(j);
- used[j] = true;
- A.push_back(j);
- bool sign;
- while (!q.empty()) {
- int v = q.front();
- q.pop();
- if (in(v, A)) sign = true;
- else sign = false;
- for (size_t i = 0; i < graph.GetVertices(v).size(); i++) {
- int to = graph.GetVertices(v)[i];
- if (!used[to]) {
- used[to] = true;
- q.push(to);
- if (sign == true) {
- B.push_back(to);
- }
- else {
- A.push_back(to);
- }
- }
- else if (in(to, A) && in(v, A) || in(to, B) && in(v, B)) {
- std::cout << "NO";
- return 0;
- }
- }
- }
- }
- std::cout << "YES";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement