Language: C++

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <cassert>
using namespace std;
typedef long long ll;

#define REP(i,n) for (int i=0; i<(int)(n); ++i)
#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define FOREQ(i,k,n) for (int i=(k); i<=(int)(n); ++i)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define SZ(v) (int)((v).size())
#define MEMSET(v,h) memset((v),(h),sizeof(v))

int n;
char str[10000];
char temp[10000];
int prev[100010];
int next[100010];

int main() {
    while (scanf("%d ", &n), n) {
        MEMSET(prev, 0);
        MEMSET(next, 0);

        map<pair<string, int>, int> position;
        FOREQ(i, 1, n) {
            gets(str);
            int len = strlen(str);
            if (str[0] == '-') { continue; }
            if (len == 1 && str[0] == 'v') {
                prev[i] = i + 1;
                next[i + 1] = i;
                continue;
            }
            int index = 0;
            REP(j, len) {
                if (!isdigit(str[j])) {
                    temp[j] = str[j];
                    temp[j + 1] = 0;
                    continue;
                }
                index = j;
                break;
            }
            int num;
            char c = 0;
            sscanf(str + index, "%d%c", &num, &c);
            position[make_pair(temp, num)] = i;
            if (num == 1) {
                set<pair<string, int> > visit;
                visit.insert(make_pair(temp, 1));
                pair<string, int> here(temp, 2);
                while (position.count(here)) {
                    visit.insert(here);
                    int x = position[here];
                    int y = position[make_pair(temp, here.second - 1)];
                    prev[x] = y;
                    next[y] = x;
                    here.second++;
                }
                FORIT(it, visit) {
                    position.erase(*it);
                }
            }
            if (c == 'v') {
                prev[i] = i + 1;
                next[i + 1] = i;
                continue;
            }
        }

        //FOREQ(i, 1, n) {
        //    cout << prev[i] << " " << next[i] << endl;
        //}
        FOREQ(i, 1, n) {
            if (prev[i] == 0) {
                int index = i;
                while (true) {
                    printf("%d\n", index);
                    if (next[index] == 0) { break; }
                    index = next[index];
                }
            }
        }
    }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:32:16: error: reference to 'prev' is ambiguous
   32 |         MEMSET(prev, 0);
      |                ^~~~
a.cc:22:29: note: in definition of macro 'MEMSET'
   22 | #define MEMSET(v,h) memset((v),(h),sizeof(v))
      |                             ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/bits/specfun.h:43,
                 from /usr/include/c++/14/cmath:3906,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:27:5: note:                 'int prev [100010]'
   27 | int prev[100010];
      |     ^~~~
a.cc:32:16: error: reference to 'prev' is ambiguous
   32 |         MEMSET(prev, 0);
      |                ^~~~
a.cc:22:43: note: in definition of macro 'MEMSET'
   22 | #define MEMSET(v,h) memset((v),(h),sizeof(v))
      |                                           ^
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:27:5: note:                 'int prev [100010]'
   27 | int prev[100010];
      |     ^~~~
a.cc:33:16: error: reference to 'next' is ambiguous
   33 |         MEMSET(next, 0);
      |                ^~~~
a.cc:22:29: note: in definition of macro 'MEMSET'
   22 | #define MEMSET(v,h) memset((v),(h),sizeof(v))
      |                             ^
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:28:5: note:                 'int next [100010]'
   28 | int next[100010];
      |     ^~~~
a.cc:33:16: error: reference to 'next' is ambiguous
   33 |         MEMSET(next, 0);
      |                ^~~~
a.cc:22:43: note: in definition of macro 'MEMSET'
   22 | #define MEMSET(v,h) memset((v),(h),sizeof(v))
      |                                           ^
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:28:5: note:                 'int next [100010]'
   28 | int next[100010];
      |     ^~~~
a.cc:37:13: error: 'gets' was not declared in this scope; did you mean 'getw'?
   37 |             gets(str);
      |             ^~~~
      |             getw
a.cc:41:17: error: reference to 'prev' is ambiguous
   41 |                 prev[i] = i + 1;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:27:5: note:                 'int prev [100010]'
   27 | int prev[100010];
      |     ^~~~
a.cc:42:17: error: reference to 'next' is ambiguous
   42 |                 next[i + 1] = i;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:28:5: note:                 'int next [100010]'
   28 | int next[100010];
      |     ^~~~
a.cc:67:21: error: reference to 'prev' is ambiguous
   67 |                     prev[x] = y;
      |                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:27:5: note:                 'int prev [100010]'
   27 | int prev[100010];
      |     ^~~~
a.cc:68:21: error: reference to 'next' is ambiguous
   68 |                     next[y] = x;
      |                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:28:5: note:                 'int next [100010]'
   28 | int next[100010];
      |     ^~~~
a.cc:76:17: error: reference to 'prev' is ambiguous
   76 |                 prev[i] = i + 1;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:27:5: note:                 'int prev [100010]'
   27 | int prev[100010];
      |     ^~~~
a.cc:77:17: error: reference to 'next' is ambiguous
   77 |                 next[i + 1] = i;
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:28:5: note:                 'int next [100010]'
   28 | int next[100010];
      |     ^~~~
a.cc:86:17: error: reference to 'prev' is ambiguous
   86 |             if (prev[i] == 0) {
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:27:5: note:                 'int prev [100010]'
   27 | int prev[100010];
      |     ^~~~
a.cc:90:25: error: reference to 'next' is ambiguous
   90 |                     if (next[index] == 0) { break; }
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:28:5: note:                 'int next [100010]'
   28 | int next[100010];
      |     ^~~~
a.cc:91:29: error: reference to 'next' is ambiguous
   91 |                     index = next[index];
      |                             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:28:5: note:                 'int next [100010]'
   28 | int next[100010];
      |     ^~~~