I created an array of vector called ed
outside of main, then I modified it in main using push_back, finally I called a function that used the array of vectors. But in the function it says the vector is empty (size()==0)
#include <cstdio>
#include <string.h>
#include <vector>
using namespace std;
int previ[100001], typ[100001], lvl[100001];
vector<int> ed[100001];
void add(int toadd, int pre, int lv){
previ[toadd] = pre;
lvl[toadd] = lv;
printf("%d\n", ed[0].size());//prints out 0
for(int &a: ed[toadd]){
//printf("%d\n", a);
if(previ[a] = -1){
add(a, toadd, lv + 1);
}
}
}
int main(){
FILE *fi = fopen("milkvisits.in", "r"), *fo = fopen("milkvisits.out", "w");
int n, m;
fscanf(fi, "%d %d", &n, &m);
vector<int> ed[n];
memset(previ, -1, sizeof previ);
for(int a = 0; a < m; a ++){
fscanf(fi, "%d", &typ[a]);
}
int ta, tb;
for(int a = 1; a < n; a ++){
fscanf(fi, "%d %d", &ta, &tb);
ta--; tb --;
ed[ta].push_back(tb);
ed[tb].push_back(ta);
}
printf("%d\n", ed[0].size());//prints out 2
add(0, -2, 1);//inside the function it prints the size as 0
printf("%d\n", ed[0].size());//prints out 2
fclose(fi);
fclose(fo);
return 0;
}
Here is the input file milkvisits.in I used:
5 5
1 1 2 1 2
1 2
2 3
2 4
1 5
1 4 1
1 4 2
1 3 2
1 3 1
5 5 1