计蒜客第七章:邻接表的使用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/shidonghang/article/details/102692061

计蒜客习题:邻接表的使用

题目

在这里插入图片描述

样例

在这里插入图片描述

思路

利用vector构建邻接表,简单使用,不再赘述。

代码

#include<iostream>
#include<vector>
using namespace std;
int n,m;
vector<int> mp[105];
int main()
{
    cin>>n>>m;
    while(m--)
    {
        int a,x,y;
        cin>>a>>x>>y;
        mp[x].push_back(y);
        if(a) mp[y].push_back(x);
    }
    for(int i=0;i<n;i++)
    {
        cout<<i<<":";
        for(int j=mp[i].size()-1;j>=0;j--)
            cout<<" "<<mp[i][j];
        cout<<endl;
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/shidonghang/article/details/102692061