vendredi 24 février 2017

Adding elements to a vector of pairs of vector

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;
    }
    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<t;i++)
    {
        cout<<v[i].first<<endl;
        auto rt=v[i].second.begin();
        for(rt!=v[i].second.end())
        {
            cout<<*rt;
        }
        cout<<endl;
    }
    return 0;
}

  1. I have defined purposely a vector of pairs of vector to store an adjacency list just to understand the concept of pairs inside vectors and vice-versa.
  2. I know i could have gone for the simpler vectorarr[n]; way but as i previously mentioned i want to understand something else.
  3. In the while loop, I have tried to add vertices m,n on the adjacency list.
  4. And at the end, I have tried to print the final adjacency list but something is wrong with my concept as it is not working as it should.

Aucun commentaire:

Enregistrer un commentaire