From a3dd2581ed4847670f81157091016c14ca18803d Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Tue, 14 Jan 2020 18:29:11 +0700 Subject: [usth/MATH2.3] Mathemate Discretely --- usth/MATH2.3/2/bipartite.cc | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 usth/MATH2.3/2/bipartite.cc (limited to 'usth/MATH2.3/2/bipartite.cc') diff --git a/usth/MATH2.3/2/bipartite.cc b/usth/MATH2.3/2/bipartite.cc new file mode 100644 index 0000000..49b8dfd --- /dev/null +++ b/usth/MATH2.3/2/bipartite.cc @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +using namespace std; + +int +main () +{ + unordered_map> adj; + size_t n, u, v; + cin >> n; + while (n--) + { + cin >> u >> v; + adj[u][v]++; + adj[v][u]++; + } + + queue q; + set one, another; + q.push (u); + one.insert (u); + + while (!q.empty ()) + { + u = q.front (); + q.pop (); + if (adj[u][u]) + { + cout << "no" << endl; + return 0; + } + for (auto const& [v, b] : adj[u]) + { + if (!b) + continue; + if (one.count (v) && one.count (u) + || another.count (v) && another.count (u)) + { + cout << "no" << endl; + return 0; + } + if (one.count (v) || another.count (v)) + continue; + q.push (v); + if (one.count (u)) + another.insert (v); + else + one.insert (v); + } + } + cout << "yes" << endl; +} -- cgit 1.4.1