用vector容器实现邻接表

来源:互联网 发布:淘宝入门知识 编辑:程序博客网 时间:2024/06/05 15:21
在做图论题的时侯经常会用到节省时间和空间的邻接表代替邻接矩阵。
下面就是用vector容器实现一个简单的邻接表的代码。
#include <iostream>#include <vector>using namespace std;typedef struct{ int b ;}edge ;vector<edge>g[10] ;int main(){    int m ;    int x , y ;    cin >> m ;    edge temp ;    for(int i = 0; i < m; i ++)    {      cin >> x >> y ;      temp.b = y ;      g[x].push_back(temp) ;    }    for(int i = 0; i < 10; i ++)    {     for(int j = 0; j < (int)g[i].size(); j ++)    {       cout << g[i][j].b <<" " ;    }    cout << endl ;    }    return 0;}

原创粉丝点击