Spoj BUGLIFE - A Bug’s Life Solution
Problem Link: https://www.spoj.com/problems/BUGLIFE/en/
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1e5 + 2;
- std::vector<int> g[N];
- bool vis[N], col[N], ok;
- void dfs(int u) {
- vis[u] = true;
- for (auto v : g[u]) {
- if (!vis[v]) {
- col[v] = col[u] ^ 1;
- dfs(v);
- } else {
- if (col[u] == col[v]) {
- ok = false;
- }
- }
- }
- }
- int main() {
- int t, k;
- cin >> t;
- for (k = 1; k <= t; k++) {
- int n, m;
- cin >> n >> m;
- while (m--) {
- int u, v;
- cin >> u >> v;
- g[u].push_back(v);
- g[v].push_back(u);
- }
- ok = true;
- for (int i = 1; i <= n; i++) {
- if (!vis[i]) {
- dfs(i);
- }
- }
- if (ok) {
- cout << "No suspicious bugs found!\n";
- } else {
- cout << "Suspicious bugs found!\n";
- }
- for (int i = 1; i <= n; i++) g[i].clear();
- }
- }
No comments