Graph Representation (Adjacency List)

PHOTO EMBED

Fri Jun 07 2024 13:13:50 GMT+0000 (Coordinated Universal Time)

Saved by @ayushg103 #c++

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3+5;
vector<int> graph[N];
int main()
{
    int v,e,j,k;
    cin>>v>>e;
    for(int i=0;i<v;++i)
    {
        cin>>j>>k;
        graph[k].push_back(j);
        graph[j].push_back(k);
        
    }
}
content_copyCOPY

Think of vector<int> v as a single dynamic list that can grow or shrink as needed. This is like having one flexible, expandable storage box where you can add or remove items (integers) freely. , vector<int> v[N] is an array of dynamic lists. This means you have a fixed number (N) of these flexible, expandable storage boxes.