Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
const int inf = 1e9 + 7;
const ll INF = 1e18;
int mod = 1000000007;
#define abs(x) (x >= 0 ? x : -(x))
#define lb(v, x) (int)(lower_bound(all(v), x) - v.begin())
#define ub(v, x) (int)(upper_bound(all(v), x) - v.begin())
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> T pow(T a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
ll modpow(ll a, ll b, ll _mod) { return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1; }
template<class T> ostream& operator << (ostream& os, const vector<T>& vec) { for (auto &vi: vec) os << vi << " "; return os; }
template<class T, class U> ostream& operator << (ostream& os, const pair<T, U>& p) { os << p.F << " " << p.S; return os; }
template<typename T >inline istream& operator >> (istream& is, vector<T>& v) { rep(j, sz(v)) is >> v[j]; return is; }
template<class T> inline void add(T &a, int b) { a += b; if (a >= mod) a -= mod; }


void solve();

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed << setprecision(10);

    int T;
    // cin >> T;
    T = 1;

    while (T--) {
        solve();
    }
}

struct UnionFind {
    vector<int> data;
    int sz;
    UnionFind(int _sz) : data(_sz, -1) { sz = _sz; }
    bool connect(int x, int y) {
        if ((x = root(x)) == (y = root(y))) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y]; data[y] = x; sz--;
        return true;
    }
    bool issame(int x, int y) { return root(x) == root(y); }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    int size() { return sz; }
};

const int N = 200005;
vector<int> seen(N);
vector<int> use(N); // 辺
vector<vector<pii> > G(N);
vector<int> x;

int dfs(int v) {
    // cout << "v " << v << endl;
    int c = x[v];
    for (pii p : G[v]) {
        int nv = p.F;
        if (seen[nv]) continue;
        seen[nv] = 1;
        // cout << nv << endl;
        int d = dfs(nv);
        c ^= d;
        // その下の頂点を 0 に
        use[p.S] = d;
    }
    // cout << "c " << v << " " << c << endl;
    return c;
}

void solve() {
    int n, m;
    cin >> n >> m;

    vector<int> y(n);
    {
      vector<pii> v(n);
      rep(i, n) cin >> v[i].F >> v[i].S;
      sort(all(v));

      rep(i, n) y[i] = v[i].F;
      vector<int> v2;
      v2.eb(0);
      rep(i, n) v2.eb(v[i].S);
      v2.eb(0);
      // cout << v2 << endl;

      // 差をとる
      // 差なら, 2 箇所を変えるだけ
      rep(i, sz(v2) - 1) x.eb(v2[i] != v2[i + 1]);
      // cout << x << endl;
    }

    vector<pii> w(m);
    rep(i, m) cin >> w[i].F >> w[i].S;

    UnionFind uni(n + 1); // 全域木にする

    // 辺を作る
    rep(i, m) {
        int it = lb(y, w[i].F);
        if (it == sz(y)) continue;
        int it2 = lb(y, w[i].S + 1);
        it2--;
        if (it2 < 0 || it >= it2 || !uni.connect(it, it2 + 1)) continue;

        G[it].eb(it2 + 1, i);
        G[it2 + 1].eb(it, i);
    }

    // 特殊ケース
    if (n == 1) {
        if (v2[0] == 0) { cout << 0 << endl; return; }
        else {
            rep(i, m) {
                if (w[i].F <= v2[0] && v2[0] <= w[i].S) {
                    cout << 1 << endl << i + 1 << endl;
                    return;
                }
            }
            cout << -1 << endl;
            return;
        }
    }

    rep(i, n + 1) {
        if (sz(G[i]) == 0 && x[i] == 1) {
            // cout << i << endl;
            cout << -1 << endl;
            return;
        }
        // 葉から dfs
        if (sz(G[i]) != 1 || seen[i]) continue; // n = 1 が怖い
        // cout << "i " << i << endl;
        seen[i] = 1; // debuged
        if (dfs(i)) {
            cout << -1 << endl;
            return;
        }
    }
    cout << accumulate(all(use), 0) << endl;
    rep(i, n + 1) if (use[i]) cout << i + 1 << " "; cout << endl;
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:128:13: error: 'v2' was not declared in this scope
  128 |         if (v2[0] == 0) { cout << 0 << endl; return; }
      |             ^~