Codeforces Round #435 (Div. 2) 862B. Mahmoud and Ehab and the bipartiteness Solution
Problem Link: https://codeforces.com/contest/862/problem/B
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;
- vector<int> g[N];
- bool vis[N];
- int col[N];
- long long l = 0, r = 0;
- void dfs(int u) {
- vis[u] = true;
- for (auto v : g[u]) {
- if (!vis[v]) {
- col[v] = col[u] ^ 1;
- if (col[v] == 1)
- l++;
- else
- r++;
- dfs(v);
- }
- }
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- long long n, m;
- cin >> n;
- m = n - 1;
- while (m--) {
- int u, v;
- cin >> u >> v;
- g[u].push_back(v);
- g[v].push_back(u);
- }
- dfs(1);
- cout << ((r + 1) * l) - (n - 1) << endl;
- }
No comments