-2
#include <iostream>
#include <vector>
using namespace std;
#define endl "\n"
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
vector<pair<int,vector<int>>>v;
v.resize(t+1);
for(int i=0;i<t;i++)
{
v[i].first=i+1;
}
int x=t;
while(t--)
{
int m,n;
cin>>m>>n;
v[m-1].second.push_back(n);
v[n-1].second.push_back(m);
}
for(int i=0;i<x;i++)
{
cout<<v[i].first<<endl;
auto rt=v[i].second.begin();
while(rt!=v[i].second.end())
{
cout<<*rt;
}
cout<<endl;
}
return 0;
}
- 中的矢量I特意定義對向量中的向量來存儲一個鄰接表只是爲了瞭解對內部載體和反之亦然的概念。
- 我知道我可以去更簡單的vectorarr [n];但正如我之前提到的,我想了解其他的東西。
- 在while循環中,我試圖在鄰接列表上添加頂點m,n。
- 最後,我試圖打印最終的鄰接表,但是我的概念有些問題,因爲它沒有像應該那樣工作。
未來,請發佈所有必要的代碼來編譯(包括,使用等)。這有很大幫助。 – bejado