# GRAPH

```ll vis[maxn],in[maxn],out[maxn],dis[maxn];
queue<ll> q ;

void bfs(ll u)
{
q.push(u);
vis[u]=1;
dis[u]=0;
while(!q.empty())
{
ll qfront = q.front();
q.pop();
cout<<qfront<<" "<<dis[qfront]<<"\n";
{
if(!vis[v])
{
vis[v]=1;
dis[v]=dis[qfront]+1;
q.push(v);
}
}
}
}
```
```class Solution {
int ans ;
public:

int find(int u,vector<int>& parent)
{
if(u == parent[u])
return u;

else
return parent[u] = find(parent[u],parent);
}

void combine (int u, int v,vector<int>& size,vector<int>& parent)
{
u = find(u,parent);
v = find(v,parent);

if(u == v)
return;

else
{
ans--;
if(size[u] > size[v])
{
parent[v] = u;
size[u] += size[v];
}

else
{
parent[u] = v;
size[v] += size[u];
}

}
}

int findCircleNum(vector<vector<int>>& isConnected) {
int n = isConnected.size();
vector<int> parent(n+1,0);
vector<int> size(n+1,1);
size[0]=0;
for(int i=1;i<=n;i++)
parent[i]=i;
ans =n;
for(int i=0;i<n;i++)
for(int j=0;j<isConnected[0].size();j++)
{
if(i!=j&&isConnected[i][j]==1)
{
combine(i+1,j+1,size,parent);
}
}

return ans;
}
};```
```#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define mod 1000000007
#define maxn 1000001
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define mem(a, val) memset(a, val, sizeof(a))
//===========================================================================================================================================================

ll vis[maxn],in[maxn],out[maxn],dis[maxn];
queue<ll> q ;

int main()
{
fio;
ll t=1;
cin >> t;
while (t--)
{
ll n,k;
cin>>n>>k;

for (ll i = 0; i <= n + 2; i++)
{
vis[i] = dis[i] = in[i] = out[i] = 0;
}

while(k--)
{
ll x,y;
cin>>x>>y;