0
#include <stdio.h>
#include <vector>
using namespace std;

int max(int a, int b)
{
    if (a > b)
        return a;
    else
        return b;
}

typedef struct
{
    int ini, fin;
}TNodo;

TNodo nodo[1000000];

int n, q, a, b, rc, rp, res, cont, tmp;
vector< vector<int> > A(1000000);

int rangos(int pos)
{
    if (nodo[pos].ini)
        return 0;
    nodo[pos].ini = ++cont;
    int tam = A[pos].size();
    if (tam > 1 || !pos)
    {
        while (!A[pos].empty())
        {
            tmp = rangos(A[pos][0]);
            A[pos].erase(A[pos].begin + 1);
            if (tmp > nodo[pos].fin)
                nodo[pos].fin = tmp;
        }
    }
    else
        nodo[pos].fin = nodo[pos].ini;
    return nodo[pos].fin;
}

int main()
{

    scanf("%d %d\n", &n, &q);

    for (int i = 0; i < n - 1; i++)
    {
        scanf("%d %d\n", &a, &b);
        a--;
        b--;
        A[a].push_back(b);
        A[b].push_back(a);
    }

    rangos(0);

    while (q--)
    {
        scanf("%d %d", &a, &b);
        if (q)
            scanf("\n");
        b--;
        a--;
        if ((nodo[a].fin < nodo[b].ini || nodo[a].ini > nodo[b].ini) && a != b)
            res = 1;
        else
            res = 0;
        printf("%d\n", res);
    }

    return 0;
}

I'm trying to solve a problem in C++, and because of the memory limit, I have to erase elements from my vector of vectors, but I don't know how, and my compiler says (which is Visual Studio 2017) that I'm send invalids arguments, in "A[pos].erase()", with tow errors C3867 and C2664, so What's the right way?

jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    Don't use a `vector` of `vector`s: https://stackoverflow.com/questions/38244435/what-are-the-issues-with-a-vector-of-vectors – Jonathan Mee Dec 22 '17 at 14:24
  • `A[pos].begin` -> `A[pos].begin()`. `begin()` is a function, not a variable – UnholySheep Dec 22 '17 at 14:49
  • I really find myself at a loss for what you're trying to do, but it looks like a `set` or perhaps `multiset` may be preferable to `vector` though. – Jonathan Mee Dec 22 '17 at 14:51
  • The vector of vectors is just a adjacency list, and the code just do make the range for what nodes contain other node, so i can say quickly if a node is sun of an another – Rubén Pérez Palacios Dec 22 '17 at 15:03
  • And what are you thinking `rangos` will do? Other than access an uninitialized `nodo[pos].ini`? – Jonathan Mee Dec 22 '17 at 15:16
  • @JonathanMee, well i don´t have to initialized "nodo[pos].ini", because in rangos i put it a value . In function rangos, index all the nodes , with nodo[pos].ini, wich is the index of nodo[pos], then i see al his node his connected and nodo[pos].fin is the higher index of all his suns, so all the node that them index is higher or equal to .ini and lower and equal to .fin, are sun of he. All nodes makes a tree and it root is the node[0] – Rubén Pérez Palacios Dec 24 '17 at 01:30
  • @RubénPérezPalacios No, you're wrong, you do need to initialize because the *first* thing you do in `rangos` is evaluate `nodo[pos].ini`. Actually without the uninitialized value you would have created an infinite loop. Cause you traverse to a node which is connected to the node that you just came from, meaning that you will bounce between the 2 nodes infinitely. – Jonathan Mee Dec 24 '17 at 16:08
  • @RubénPérezPalacios, have you made any progress? You have several issues with your code. In addition to what has already been pointed out, erasing elements from a vector doesn't deallocate/free the memory from the vector. I suggest that you read [this article](https://swdevmastery.com/c-stdvector-for-post-beginners-including-real-issues-ive-seen-in-practice-and-surprising-modern-usage-recommendations/). It will help you better understand how a vector works, and how to deallocate the vector's memory. – Luis Guzman Dec 27 '17 at 15:01

0 Answers0