dfs and similar dsu graphs *1500

Please click on ads to support us..

Python Code:

n, m = map(int, input().split())
graph = [[] for _ in range(n + 1)]
visited = [False for _ in range(n + 1)]

for i in range(m):
    u, v = map(int, input().split())
    graph[u].append(v)
    graph[v].append(u)

def DFS(u):
    stack = [u]
    vertices = 0
    edges = 0

    while len(stack) > 0:
        cur = stack.pop()
        vertices += 1
        edges += len(graph[cur])
        visited[cur] = True
        for nei in graph[cur]:
            if not visited[nei]:
                visited[nei] = True
                stack.append(nei)
    edges /= 2
    return edges == ((vertices - 1) * vertices) / 2

flag = True
for i in range(1, n + 1):
    if not visited[i]:
        flag = DFS(i)
        if not flag:
            break

print("YES" if flag else "NO")

C++ Code:

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;
template<typename T> 
using ordered_multiset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
 
template<class key, class value, class cmp = std::less<key>>
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
// less, less_equal, greater

void init_code(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif // ONLINE_JUDGE
}
 
                             // MACROS

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
#define ff first
#define ss second
#define pb push_back
#define FOR(i, a, b)  for(int i=a; i<=b; i++)
#define FORR(i, a, b)  for(int i=a; i>=b; i--)  
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define vlb(v, x) lower_bound(all(v), x)
#define vub(v, x) upper_bound(all(v), x)
#define slb(v, x) v.lower_bound(x)
#define sub(v, x) v.upper_bound(x)   
#define MOD 1000000007
#define MP make_pair

// Debugging
#ifndef ONLINE_JUDGE
#include "debug.cpp"
#else
#define dbg(x)
#endif
    
                        // FUNCTIONS
ll lcm(ll a, ll b){ ll res = (a*b)/__gcd(a,b); return res; }
ll binpow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res;}

                                // PRIMARY FUNCTION

vector<bool> vis(2e5);
vector<vector<int>> adj(2e5);
bool bad = false;

pair<int, ll> dfs(int node){
    if(vis[node])   return MP(0, 0);
    vis[node] = true;

    pair<int, ll> res;
    res.ff = 1;
    res.ss = adj[node].size();
    for(int child:adj[node]){
        pair<int, ll> x = dfs(child);
        res.ff += x.ff;
        res.ss += x.ss;
    }
    return res;
}

void solve()
{
    int n, m;
    cin >> n >> m;
    FOR(i, 1, m){
        int x, y;
        cin >> x >> y;
        --x, --y;
        adj[x].pb(y);
        adj[y].pb(x);
    }

    FOR(i, 0, n-1){
        if(vis[i] == false){
            pair<int, ll> res = dfs(i);
            if(res.ss != res.ff*1ll*(res.ff-1)){
                cout << "NO\n";
                return;
            }
        }
    }

    cout << "YES\n";
}



 // MAIN FUNCTION
    
int main()
{
    ios_base::sync_with_stdio(0);
    init_code();

    solve();
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

1625C - Road Optimization
1715D - 2+ doors
267A - Subtractions
1582A - Luntik and Concerts
560A - Currency System in Geraldion
946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra
1627D - Not Adding
893B - Beautiful Divisors
864B - Polycarp and Letters