Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 4 5
- 1 2
- 1 3
- 2 3
- 3 4
- 4 2
- Edge is 1 -> 2
- Weight is 10
- Edge is 1 -> 3
- Weight is 10
- Edge is 2 -> 3
- Weight is 10
- Edge is 3 -> 4
- Weight is 10
- Edge is 4 -> 2
- Weight is 10
- */
- // From above, First line is number of vertex and number of edges (4 Vertices, 5 Edges)
- // The rest of the 5 lines refer to the edges,
- // 1 2 mean 1 connect to 2, 1 connect to 3 etc
- #include <bits/stdc++.h>
- using namespace std;
- typedef vector<int> vi; // vector of int
- typedef pair<int,int> ii; // pair
- typedef vector<ii> vii; // vector of pairs
- int main()
- {
- int n,m ; // n is the number of Vertices, m is the number of Edges
- cin>>n>>m;
- vector<vii> adjList(n+1); //For vertex 1...n. vector of vector pairs
- //Reading edges for the input for graph
- for(int i=0;i<m;i++)
- {
- int u,v;
- cin>>u>>v;
- /*Here u->v is the edge and pair second term can be used to store weight in
- case of weighted graph.
- */
- adjList[u].push_back(make_pair(v,10));
- }
- //To print the edges of every nodes stored in the adjacency list
- for(int i=1;i<=n;i++)
- {
- for(int j=0;j<(int)adjList[i].size();j++)
- {
- cout<<"Edge is "<<i<<" -> "<<adjList[i][j].first<<endl;
- cout<<"Weight is "<<adjList[i][j].second<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement