1242B - 0-1 MST - CodeForces Solution


dfs and similar dsu graphs sortings *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define speed                     \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define setp(x) fixed << setprecision(x)
#define endl "\n"
#define mod 1000000007
#define mod1 998244353
#define ff first
#define ss second
#define MAX 500005
#define N 100005
#define INF 1000000009
#define all(v) v.begin(), v.end()
#define sbit(a) __builtin_popcount(a)
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef pair<ll, ll> pll;
typedef pair<pll, ll> ppl;
typedef map<ll, ll> mpll;
typedef map<vector<ll>, ll> mpvl;
ll power(ll x, ll y)
{
    ll res = 1;

    x = x;

    if (x == 0)
        return 0;
    while (y > 0)
    {

        if (y & 1)
            res = (res * x);

        y = y >> 1;
        x = (x * x);
    }

    return res;
}
set<ll> adj[N];
set<ll> se;
void dfs(ll v)
{
    for (auto it = se.begin(); it != se.end();)
    {
        if (adj[v].find(*it) == adj[v].end())
        {
            ll nxt = *it;
            se.erase(nxt); //atmost n times it will come here
            dfs(nxt);
            it = se.lower_bound(nxt); //take lower_bound//do lower_bound not it++ because meory locations may change
        }
        else
        {
            it++; //at most m times it will come here
        }
    }
}
void solve()
{
    ll n, m;
    cin >> n >> m;
    for (ll i = 1; i <= m; i++)
    {
        ll x, y;
        cin >> x >> y;
        adj[x].insert(y);
        adj[y].insert(x);
    }
    for (ll i = 1; i <= n; i++)
        se.insert(i);
    ll zerocomponents = 0;
    while (se.size())
    {
        zerocomponents++;
        ll v = *se.begin();
		se.erase(se.begin());
        dfs(v);
    }
    cout << zerocomponents - 1 << endl;
    return;
}
int main()
{

    speed;
    ll kk;
    kk = 1;
    // cin >> kk;
    while (kk--)
    {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters