Disappearing Vertices

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.5 kB
2
Indexable
Never
#include <bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define MOD (int) 1e9 + 7
#define all(x) begin(x), end(x)
typedef long long ll;
using namespace std;
template <typename T> // cin >> vector<T>
istream &operator>>(istream &istream, vector<T> &v) {
    for (auto &it : v)
        cin >> it;
    return istream;
}
template <typename T> // cout << vector<T>
ostream &operator<<(ostream &ostream, const vector<T> &c) {
    for (auto &it : c)
        cout << it << " ";
    return ostream;
}



void solve(void) 
{
    int n, m; cin >> n >> m;
    vector<int> a(m), b(m), deg(n); cin >> a >> b;
    vector<vector<int>> adj;
    adj.resize(n);

    for(int i = 0; i < m; i++) {
        adj[a[i]].PB(b[i]);
        adj[b[i]].PB(a[i]);
        deg[a[i]]++;
        deg[b[i]]++;
    }

    queue<int> q;
    for(int i = 0; i < n; i++) {
        if(deg[i] <= 1) {
            q.push(i);
        }
    }

    int ans = 0;
    while(!q.empty()) {
        ans++;
        int sz = q.size();
        for(int i = 0; i < sz; i++) {
            int curr = q.front(); q.pop();
            for(auto x : adj[curr]) {
                deg[x]--;
                if(deg[x] == 1) {
                    q.push(x);
                }
            }
        }                
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
}