977E - Cyclic Components - CodeForces Solution


dfs and similar dsu graphs *1500

Please click on ads to support us..

Python Code:

import threading, sys


def main():
    n, m = map(int, input().split())
    graph = [[] for _ in range(n)]

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

    visited = [False for _ in range(n)]


    def check(node):
        nonlocal cycle
        visited[node - 1] = True
        if len(graph[node - 1]) != 2:
            cycle = False
        for neighbor in graph[node - 1]:
            if not visited[neighbor-1]:
                check(neighbor)
        ans = 0
    for n in range(n):
        if not visited[n]:
            node = n + 1
            cycle = True
            check(node)
            if cycle:
                ans += 1
    print(ans)

if __name__ == '__main__':
    sys.setrecursionlimit(1 << 30)
    threading.stack_size(1 << 27)

    main_thread = threading.Thread(target=main)
    main_thread.start()
    main_thread.join()

C++ Code:

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define pii pair<int,int>
#define vint vector<int>
#define vpii vector<pii>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define unmap unordered_map
#define graph vector<vint>
#define clr(x) memset(x,0,sizeof(x));
using namespace std;
const int N = (int)2e5 + 69;
const int mod = (int)1e4 + 7;
int INF = INT_MAX;
int power(int a, int b) { int ans = 1; while (b) { if (b % 2) ans *= a; a *= a; b /= 2; } return ans; }
int gcd(int a, int b) { if (b == 0)return a;return gcd(b, a % b); }

graph g;
int vis[N];
int road[N];
bool tmp = 1;
void dfs(int node) {
    vis[node] = 1;
    if (g[node].size() != 2)
        tmp = 0;
    for (int c : g[node])
        if (!vis[c])
            dfs(c);
    return;
}
int messi = 10, cr7 = 7;
int32_t main() {

    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n, m;
    cin >> n >> m;
    g.resize(n + 1);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            dfs(i);
            ans += tmp;
            tmp = 1;
        }
    }
    cout << ans << endl;
}


Comments

Submit
0 Comments
More Questions

1031A - Golden Plate
1559C - Mocha and Hiking
427B - Prison Transfer
330A - Cakeminator
426A - Sereja and Mugs
363A - Soroban
1585C - Minimize Distance
1506E - Restoring the Permutation
1539A - Contest Start
363D - Renting Bikes
1198D - Rectangle Painting 1
1023B - Pair of Toys
1725A - Accumulation of Dominoes
1675E - Replace With the Previous Minimize
839A - Arya and Bran
16B - Burglar and Matches
1625B - Elementary Particles
1725G - Garage
1725B - Basketball Together
735A - Ostap and Grasshopper
1183B - Equalize Prices
1481A - Space Navigation
1437B - Reverse Binary Strings
1362B - Johnny and His Hobbies
1299A - Anu Has a Function
1111A - Superhero Transformation
954A - Diagonal Walking
39F - Pacifist frogs
1451C - String Equality
386A - Second-Price Auction