Problem:
B: チェーンレター
問題
\(0\) 日目に \(N\) 人の人の元へ呪いの手紙が届けられました。
    人 \(i\) に届いた手紙には呪いのメッセージの \(i\) 文字目のみが書かれています。
人々は手紙が怖いため、手紙を受け取る度に当日にもらったメッセージを特定の人々へ送って、その手紙を廃棄します。
人 \(i\) はもらったメッセージを手紙にして、人 \(v_{i1}, v_{i2}, \dots, v_{i{K_i}}\) の \(K_i\) 人に送ります。それぞれの手紙は届くのに \(d_{i1}, d_{i2}, ..., d_{iK_i}\) 日かかります。
ある人のもとへ同じ日に複数の呪いの手紙が届く場合、これらの手紙は合体して一通の手紙として届きます。
    より正確には、\(d\) 日目に人 \(i\) に文字の集合 \(S_1, S_2, \dots, S_m\) が書かれた \(m\) 通の手紙が届く場合、\(\cup_{j = 1}^m S_j\) が書かれた一通の手紙となり届きます。
    例えば、文字 \(1, 2, 4\) が書かれた手紙と文字 \(1, 3, 4\) が書かれた手紙が合体すると、文字 \(1, 2, 3, 4\) が書かれた手紙になります。
文字 \(1\) から文字 \(N\) が全て揃った手紙を「完成した呪いの手紙」と呼ぶとき、ちょうど \(D\) 日目に完成した呪いの手紙を受け取る人を出力してください。
制約
\(2 \leq N \leq 100\)
\(1 \leq D \leq 10^9\)
\(0\leq K_i\leq N\)
\(1 \leq v_{ij} \leq N\)
\(j_1 \neq j_2\) ならば \(v_{ij_1} \neq v_{ij_2}\)
\(d_{ij} \geq 1\)
\(∑_{1\leq i\leq N}\max(\{0\} \cup \{d_{ij}\ |\ 1\leq j\leq K_i \}) \leq 1000\)
各差出人が出す手紙が届くまでにかかる日数の最大値の総和が \(1000\) を超えない
入力はすべて整数
入力
N
D
K_1
v_{11}
d_{11}
︙
v_{1K_1}
d_{1K_1}
︙
K_N
v_{N1}
d_{N1}
︙
v_{NK_N}
d_{NK_N}
出力
条件を満たす人が存在するならば以下の形式で出力してください。
M
f_1
f_2
...
f_M
ここで、\(M\) は条件を満たす人の総数を表し、\(f_i\ (1\leq i\leq M)\) は人 \(f_i\) を表す。
条件を満たす人が存在しないならば \(0\) を出力してください。
入力例1
3 4
1
2 1
2
1 1
3 1
1
1 1
出力例1
2
1 2
0日目
人1は文字1、人2は文字2、人3は文字3を含む手紙を受け取ります。
その後、人1は人2へ、人2は人1，3へ、人3は人1へ手紙を送ります（以降の日にちも同様）。
1日目
人1は文字2, 3、人2は文字1、人3は文字2を含む手紙を受け取ります。
2日目
人1は文字1, 2、人2は文字2, 3、人3は文字1を含む手紙を受け取ります。
3日目
人1は文字1, 2, 3、人2は文字1, 2、人3は文字2, 3を含む手紙を受け取ります。
4日目
人1は文字1, 2, 3、人2は文字1, 2, 3、人3は文字1, 2を含む手紙を受け取ります。
よって、4日後に完成した呪いの手紙を受け取るのは人1, 2の2人です。
入力例2
2 100
1
2 2
1
1 2
出力例2
0
呪いの手紙が完成しないこともあります。
入力例3
2 1
1
2 1
2
1 1
2 1
出力例3
1
2

Submitted solutions:

code_snippet: // AOJ #3286 Chain Letter
// 2025.4.1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e15;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) {
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
}

int main(){
    int n = Cin(), D = Cin();

    vector<vector<pair<int,int>>> g(n);
    for(int i=0; i<n; i++){
        int k = Cin();
        while(k--){
            int v = Cin()-1, d = Cin();
            g[i].push_back({v, d});
        }
    }

    vector<vector<ll>> d(n, vector<ll>(n, INF));
    for (int i=0; i<n; i++){
        for(auto &e : g[i]){
            int v = e.first;
            int w = e.second;
            d[i][v] = min(d[i][v], (ll)w);
        }
    }
    for (int k=0; k<n; k++){
        for (int i=0; i<n; i++){
            if(d[i][k]==INF) continue;
            for (int j=0; j<n; j++){
                if(d[k][j]==INF) continue;
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }

    vector<int> ord(n, -1), low(n), comp(n, -1);
    vector<bool> inStack(n, false);
    stack<int> st;
    int idx = 0, cid = 0;
    function<void(int)> dfs = [&](int u){
        ord[u] = low[u] = idx++;
        st.push(u); inStack[u] = true;
        for(auto &e : g[u]){
            int v = e.first;
            if(ord[v]==-1) dfs(v), low[u] = min(low[u], low[v]);
            else if(inStack[v]) low[u] = min(low[u], ord[v]);
        }
        if(low[u] == ord[u]){
            while(true){
                int v = st.top(); st.pop();
                inStack[v] = false;
                comp[v] = cid;
                if(v==u) break;
            }
            cid++;
        }
    };
    for (int i=0; i<n; i++) if(ord[i]==-1) dfs(i);
    vector<vector<int>> comps(cid);
    for (int i=0; i<n; i++) comps[comp[i]].push_back(i);

    vector<bool> hasCycle(cid, false);
    for (int c=0; c<cid; c++){
        if(comps[c].size() >= 2){
            hasCycle[c] = true;
            continue;
        }
        int u = comps[c][0];
        for(auto &e : g[u]) if(e.first == u){ hasCycle[c] = true; break; }
    }

    const int L_bound = 100000;
    vector<vector<bool>> dp(cid, vector<bool>(L_bound+1, false));
    vector<int> mval(cid, INT_MAX), gval(cid, 0), T0(cid, L_bound+1);
    for (int c=0; c<cid; c++){
        if(!hasCycle[c]) continue;
        vector<int> coins;
        for(auto u : comps[c]){
            if(d[u][u] < INF){
                coins.push_back((int)d[u][u]);
                mval[c] = min(mval[c], (int)d[u][u]);
            }
            for(auto v : comps[c]){
                if(u==v) continue;
                if(d[u][v] < INF && d[v][u] < INF){
                    int cyc = (int)(d[u][v] + d[v][u]);
                    coins.push_back(cyc);
                    mval[c] = min(mval[c], cyc);
                }
            }
        }
        if(coins.empty()){
            hasCycle[c] = false;
            continue;
        }
        int gtmp = 0;
        for(auto coin : coins) gtmp = (gtmp==0 ? coin : gcd(gtmp, coin));
        gval[c] = gtmp;
        dp[c][0] = true;
        for (int x=0; x<=L_bound; x++){
            if(!dp[c][x]) continue;
            for(auto coin : coins) if(x+coin <= L_bound) dp[c][x+coin] = true;
        }
        for (int t = mval[c]; t<=L_bound; t++){
            if(t % gval[c] != mval[c] % gval[c]) continue;
            bool ok = true;
            for (int tt = t; tt<=L_bound; tt += gval[c]){
                if(tt >= mval[c] && !dp[c][tt]){ ok = false; break; }
            }
            if(ok){ T0[c] = t; break; }
        }
    }

    auto can = [&](int i, int j) -> bool {
        if(d[i][j] == D) return true;
        if(d[i][j] > D) {  }
        for (int u = 0; u < n; u++){
            if(d[i][u] == INF || d[u][j] == INF) continue;
            ll base = d[i][u] + d[u][j];
            if(base > D) continue;
            if(base == D) return true;
            ll extra = D - base;
            int c = comp[u];
            if(!hasCycle[c]) continue;
            if(extra < mval[c]) continue;
            if(extra <= L_bound){ if(dp[c][extra]) return true; }
            else if(extra >= T0[c] && ((extra - mval[c]) % gval[c] == 0)) return true;
        }
        return false;
    };

    vector<int> ans;
    for (int j=0; j<n; j++){
        bool ok = true;
        for (int i=0; i<n; i++) if(!can(i, j)){ ok = false; break; }
        if(ok) ans.push_back(j);
    }
    if(ans.empty()) Cout(0), pc('\n');
    else {
        sort(ans.begin(), ans.end());
        Cout(ans.size()), pc('\n');
        for (int i=0; i<ans.size(); i++){
            Cout(ans[i]+1);
            pc(i+1==ans.size()? '\n': ' ');
        }
    }
    return 0;
}
accuracy: 0.375
time_ms: 40
memory_kb: 3516
final_rank: 15

code_snippet: // AOJ #3286 Chain Letter
// 2025.4.1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e15;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) {
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
}

int main(){
    int n = Cin(), D = Cin();

    vector<vector<pair<int,int>>> g(n);
    for(int i=0;i<n;i++){
        int k = Cin();
        for(int j=0;j<k;j++){
            int v = Cin()-1, d = Cin();
            g[i].push_back({v,d});
        }
    }

    vector<vector<ll>> d(n, vector<ll>(n, INF));
    for (int i=0;i<n;i++){
        d[i][i] = 0;
        for(auto &e : g[i]){
            int v = e.first; int w = e.second;
            d[i][v] = min(d[i][v], (ll)w);
        }
    }
    for(int k=0;k<n;k++)
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(d[i][k] < INF && d[k][j] < INF) d[i][j] = min(d[i][j], d[i][k]+d[k][j]);

    vector<int> order(n,-1), low(n), sccId(n,-1);
    vector<bool> onStack(n, false);
    stack<int> st;
    int idx = 0, sccCount = 0;
    function<void(int)> dfs = [&](int u){
        order[u] = low[u] = idx++;
        st.push(u);
        onStack[u] = true;
        for(auto &e : g[u]){
            int v = e.first;
            if(order[v] == -1){
                dfs(v);
                low[u] = min(low[u], low[v]);
            } else if(onStack[v]) low[u] = min(low[u], order[v]);
        }
        if(low[u] == order[u]){
            while(true){
                int v = st.top(); st.pop();
                onStack[v] = false;
                sccId[v] = sccCount;
                if(v==u) break;
            }
            sccCount++;
        }
    };
    for(int i=0;i<n;i++) if(order[i] == -1) dfs(i);
    vector<vector<int>> scc(sccCount);
    for(int i=0;i<n;i++) scc[sccId[i]].push_back(i);

    vector<bool> cyc(n, false);
    for(int id=0; id<sccCount; id++){
        if(scc[id].size() >= 2){
            for(auto u : scc[id]) cyc[u] = true;
        } else {
            int u = scc[id][0];
            for(auto &e : g[u]){
                if(e.first == u){
                    cyc[u] = true;
                    break;
                }
            }
        }
    }

    vector<vector<int>> coins(n);
    vector<int> mval(n, INT_MAX);
    vector<int> gval(n, 0);
    for (int u=0; u<n; u++){
        if(!cyc[u]) continue;
        int id = sccId[u];
        for(auto v : scc[id]){
            if(u!=v){
                if(d[u][v] < INF && d[v][u] < INF){
                    int cand = (int)(d[u][v] + d[v][u]);
                    coins[u].push_back(cand);
                    mval[u] = min(mval[u], cand);
                }
            }
        }
        for(auto &e : g[u]){
            if(e.first==u){
                int cand = e.second;
                coins[u].push_back(cand);
                mval[u] = min(mval[u], cand);
            }
        }
        if(coins[u].empty()){
            cyc[u] = false;
            continue;
        }
        int gtmp = 0;
        for(auto &c : coins[u]) gtmp = (gtmp==0 ? c : std::gcd(gtmp, c));
        gval[u] = gtmp;
    }

    const int L_bound = 100000;
    vector<vector<bool>> dpCoins(n, vector<bool>(L_bound+1, false));
    vector<int> T0(n, L_bound+1);
    for(int u=0; u<n; u++){
        if(!cyc[u]) continue;
        dpCoins[u][0] = true;
        for(int x=0;x<=L_bound;x++){
            if(!dpCoins[u][x]) continue;
            for(auto coin : coins[u]){
                if(x + coin <= L_bound) dpCoins[u][x+coin] = true;
            }
        }
        for(int t = mval[u]; t <= L_bound; t++){
            if(t % gval[u] != mval[u] % gval[u]) continue;
            bool ok = true;
            for(int tt = t; tt <= L_bound; tt += gval[u]){
                if(tt >= mval[u] && !dpCoins[u][tt]){
                    ok = false;
                    break;
                }
            }
            if(ok){
                T0[u] = t;
                break;
            }
        }
    }

    auto canReach = [&](int i, int j) -> bool {
        if(d[i][j] == D) return true;
        if(d[i][j] > D) return false;
        bool ok = false;
        for(int u = 0; u < n; u++){
            if(!cyc[u]) continue;
            if(d[i][u] == INF || d[u][j] == INF) continue;
            ll base = d[i][u] + d[u][j];
            if(base >= D) continue;
            ll extra = D - base;
            if(extra < mval[u]) continue;
            if(extra <= L_bound){
                if(dpCoins[u][extra]) ok = true; break;
            } else {
                if(extra >= T0[u] && ((extra - mval[u]) % gval[u] == 0)){ ok = true; break; }
            }
        }
        return ok;
    };

    vector<int> ans;
    for(int j=0;j<n;j++){
        bool all = true;
        for(int i=0;i<n;i++){
            if(!canReach(i,j)){
                all = false;
                break;
            }
        }
        if(all) ans.push_back(j);
    }

    if(ans.empty()) Cout(0), pc('\n');
    else {
        sort(ans.begin(), ans.end());
        Cout(ans.size()), pc('\n');
        for (int i = 0; i < ans.size(); i++){
            Cout(ans[i] + 1);
            pc(i+1 == ans.size()? '\n': ' ');
        }
    }
    return 0;
}
accuracy: 0.15
time_ms: 40
memory_kb: 3520
final_rank: 18

code_snippet: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#include <array>
#include <unordered_set>
#include <unordered_map>
#define rep(x, s, t) for(ll x = (s); (x) <= (t); (x)++)
#define per(x, s, t) for(ll x = (s); (x) >= (t); (x)--)
#define reps(x, s) for(ll x = 0; (x) < (ll)(s).size(); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define sz(x) ((ll)(x).size())
#define all(x) (x).begin(),(x).end()
#define outl(...) dump_func(__VA_ARGS__)
#define outf(x) cout << fixed << setprecision(16) << (x) << endl
#define pb push_back
#define fi first
#define se second
#define inf 2e18
#define eps 1e-9
const double PI = 3.1415926535897932384626433;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

struct edge{
	ll to, cost;
	edge(){}
	edge(ll a, ll b){ to = a, cost = b;}
};
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

const int mod = 1000000007;
//const int mod = 998244353;

struct mint{
	int x;
	mint(ll y = 0){if(y < 0 || y >= mod) y = (y%mod+mod)%mod; x = y;}
	mint(const mint &ope) {x = ope.x;}
	mint operator-(){return mint(-x);}
	mint operator+(const mint &ope){return mint(x) += ope;}
	mint operator-(const mint &ope){return mint(x) -= ope;}
	mint operator*(const mint &ope){return mint(x) *= ope;}
	mint operator/(const mint &ope){return mint(x) /= ope;}
	mint& operator+=(const mint &ope){x += ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator-=(const mint &ope){x += mod - ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator*=(const mint &ope){ll tmp = x; tmp *= ope.x, tmp %= mod; x = tmp; return *this;}
	mint& operator/=(const mint &ope){
		ll n = mod-2; mint mul = ope;
		while(n){if(n & 1) *this *= mul; mul *= mul; n >>= 1;}
		return *this;
	}
	mint inverse(){return mint(1) / *this;}
	bool operator ==(const mint &ope){return x == ope.x;}
	bool operator !=(const mint &ope){return x != ope.x;}
	bool operator <(const mint &ope)const{return x < ope.x;}
};
mint modpow(mint a, ll n){
	if(n == 0) return mint(1);
	if(n % 2) return a * modpow(a, n-1);
	else return modpow(a*a, n/2);
}
istream& operator >>(istream &is, mint &ope){ll t; is >> t, ope.x = t; return is;}
ostream& operator <<(ostream &os, mint &ope){return os << ope.x;}
ostream& operator <<(ostream &os, const mint &ope){return os << ope.x;}

ll modpow(ll a, ll n, ll mod){
	if(n == 0) return 1;
	if(n % 2) return ((a%mod) * (modpow(a, n-1, mod)%mod)) % mod;
	else return modpow((a*a)%mod, n/2, mod) % mod;
}

vector<mint> fact, fact_inv;
void make_fact(int n){
	fact.resize(n+1), fact_inv.resize(n+1);
	fact[0] = mint(1); rep(i, 1, n) fact[i] = fact[i-1] * mint(i);
	fact_inv[n] = fact[n].inverse(); per(i, n-1, 0) fact_inv[i] = fact_inv[i+1] * mint(i+1);
}
mint comb(int n, int k){ if(n < 0 || k < 0 || n < k) return mint(0); return fact[n] * fact_inv[k] * fact_inv[n-k];}
mint perm(int n, int k){ return comb(n, k) * fact[k]; }
template<typename T> T comb2(ll n, ll k){ T ret = 1; rep(i, 1, k) ret *= n-k+i, ret /= i; return ret;}

vector<int> prime, pvec, qrime;
void make_prime(int n){
	prime.resize(n+1);
	rep(i, 2, n){
		if(prime[i] == 0) pvec.push_back(i), prime[i] = i;
		for(auto p : pvec){ if(i*p > n || p > prime[i]) break; prime[i*p] = p;}
	}
}
void make_qrime(int n){
	qrime.resize(n+1);
	rep(i, 2, n){int ni = i / prime[i]; if(prime[i] == prime[ni]) qrime[i] = qrime[ni] * prime[i]; else qrime[i] = prime[i];}
}

bool exceed(ll x, ll y, ll m){return y > 0 && x >= m / y + 1;}
void mark(){ cout << "*" << endl; }
void yes(){ cout << "YES" << endl; }
void no(){ cout << "NO" << endl; }
ll floor(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return a/b; else return -((-a+b-1)/b); }
ll ceil(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return (a+b-1)/b; else return -((-a)/b); }
ll modulo(ll a, ll b){ b = abs(b); return a - floor(a, b) * b;}
ll sgn(ll x){ if(x > 0) return 1; if(x < 0) return -1; return 0;}
ll gcd(ll a, ll b){if(b == 0) return a; return gcd(b, a%b);}
ll lcm(ll a, ll b){return a/gcd(a, b)*b;}
ll arith(ll x){return x*(x+1)/2;}
ll arith(ll l, ll r){return arith(r) - arith(l-1);}
ll digitnum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret++; return ret;}
ll digitsum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret += x % b; return ret;}
string lltos(ll x, ll b = 10){string ret; for(;x;x/=b) ret += x % b + '0'; reverse(all(ret)); return ret;}
ll stoll(string &s, ll b = 10){ll ret = 0; for(auto c : s) ret *= b, ret += c - '0'; return ret;}
template<typename T> void uniq(T &vec){sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}
int popcount(ull x){
	x -= ((x>>1)&0x5555555555555555ULL), x = (x & 0x3333333333333333ULL) + ((x>>2) & 0x3333333333333333ULL);
	return (((x + (x>>4)) & 0x0F0F0F0F0F0F0F0FULL) * 0x0101010101010101ULL) >> 56;
}

template<class S, class T> pair<S, T>& operator+=(pair<S, T> &s, const pair<S, T> &t){s.first += t.first, s.second += t.second; return s;}
template<class S, class T> pair<S, T>& operator-=(pair<S, T> &s, const pair<S, T> &t){s.first -= t.first, s.second -= t.second; return s;}
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first-t.first, s.second-t.second);}
template<class T> T dot(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.first + s.second*t.second;}
template<class T> T cross(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.second - s.second*t.first;}

template<typename T> ostream& operator << (ostream& os, vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, deque<T>& deq){reps(i,  deq) os << deq[i] << " "; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, map<T, U>& ope){ for(auto p : ope) os << "(" << p.first << ", " << p.second << "),";return os;}
template<typename T> ostream& operator << (ostream& os, set<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, multiset<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> void outa(T a[], ll s, ll t){rep(i, s, t){ cout << a[i]; if(i < t) cout << " ";} cout << endl;}
template<typename T, size_t N> ostream& operator << (ostream& os, array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
template<typename T, size_t N> ostream& operator << (ostream& os, const array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
void dump_func(){cout << endl;}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail){cout << head; if(sizeof...(Tail) > 0) cout << " "; dump_func(std::move(tail)...);}

struct BitMatrix{
	static const int size = 1105;
	bitset<size+1> bs[size+1];
	
	BitMatrix(){}
	static BitMatrix Ident(){
		BitMatrix ret;
		rep(i, 1, size) ret.bs[i][i] = 1;
		return ret;
	}
	BitMatrix operator*(BitMatrix& ope){
		BitMatrix ret;
		bitset<size+1> bs2[size+1];
		rep(i, 1, size) rep(j, 1, size) bs2[i][j] = ope.bs[j][i];
		rep(i, 1, size) rep(j, 1, size) ret.bs[i][j] = (bs[i] & bs2[j]).any();
		return ret;
	}
	BitMatrix pow(ll n){
		if(n == 0) return Ident();
		if(n % 2) return pow(n-1) * (*this);
		else{
			BitMatrix tmp = pow(n/2);
			return tmp * tmp;
		}
	}
};
ostream& operator << (ostream& os, BitMatrix& mat) {
	rep(i, 1, BitMatrix::size){rep(j, 1, BitMatrix::size) os << mat.bs[i][j] << " "; cout << endl;}
	return os;
}
ostream& operator << (ostream& os, const BitMatrix& mat) {
	rep(i, 1, BitMatrix::size){rep(j, 1, BitMatrix::size) os << mat.bs[i][j] << " "; cout << endl;}
	return os;
}

ll n, d;
vector<P> vec[105];
vector<ll> G[1105];
ll cnt[105];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> d;
	ll k, u, w;
	rep(i, 1, n){
		cin >> k;
		rep(j, 1, k){
			cin >> u >> w;
			vec[i].pb(P(w, u));
		}
		sort(all(vec[i]));
	}
	
	ll sum = n;
	rep(i, 1, n){
		if(sz(vec[i]) == 0) continue;
		ll l = vec[i].back().fi;
		G[i].pb(sum+1);
		rep(j, 1, l-1) G[sum+j].pb(sum+j+1);
		for(auto p : vec[i]){
			ll u = p.se, w = p.fi;
			if(w == 1) G[i].pb(u);
			else G[sum+(w-1)].pb(u);
		}
		sum += l;
	}
	
	BitMatrix mat;
	rep(i, 1, sum) for(auto u : G[i]) mat.bs[u][i] = 1;
	BitMatrix pmat = mat.pow(d);
	
	rep(i, 1, n) rep(j, 1, n) if(pmat.bs[j][i]) cnt[j]++;
	vector<ll> ans;
	rep(i, 1, n) if(cnt[i] >= n) ans.pb(i);
	
	outl(sz(ans));
	outl(ans);
	
	return 0;
}
accuracy: 1
time_ms: 910
memory_kb: 18052
final_rank: 10

code_snippet: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#include <array>
#include <unordered_set>
#include <unordered_map>
#define rep(x, s, t) for(ll x = (s); (x) <= (t); (x)++)
#define per(x, s, t) for(ll x = (s); (x) >= (t); (x)--)
#define reps(x, s) for(ll x = 0; (x) < (ll)(s).size(); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define sz(x) ((ll)(x).size())
#define all(x) (x).begin(),(x).end()
#define outl(...) dump_func(__VA_ARGS__)
#define outf(x) cout << fixed << setprecision(16) << (x) << endl
#define pb push_back
#define fi first
#define se second
#define inf 2e18
#define eps 1e-9
const double PI = 3.1415926535897932384626433;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

struct edge{
	ll to, cost;
	edge(){}
	edge(ll a, ll b){ to = a, cost = b;}
};
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

const int mod = 1000000007;
//const int mod = 998244353;

struct mint{
	int x;
	mint(ll y = 0){if(y < 0 || y >= mod) y = (y%mod+mod)%mod; x = y;}
	mint(const mint &ope) {x = ope.x;}
	mint operator-(){return mint(-x);}
	mint operator+(const mint &ope){return mint(x) += ope;}
	mint operator-(const mint &ope){return mint(x) -= ope;}
	mint operator*(const mint &ope){return mint(x) *= ope;}
	mint operator/(const mint &ope){return mint(x) /= ope;}
	mint& operator+=(const mint &ope){x += ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator-=(const mint &ope){x += mod - ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator*=(const mint &ope){ll tmp = x; tmp *= ope.x, tmp %= mod; x = tmp; return *this;}
	mint& operator/=(const mint &ope){
		ll n = mod-2; mint mul = ope;
		while(n){if(n & 1) *this *= mul; mul *= mul; n >>= 1;}
		return *this;
	}
	mint inverse(){return mint(1) / *this;}
	bool operator ==(const mint &ope){return x == ope.x;}
	bool operator !=(const mint &ope){return x != ope.x;}
	bool operator <(const mint &ope)const{return x < ope.x;}
};
mint modpow(mint a, ll n){
	if(n == 0) return mint(1);
	if(n % 2) return a * modpow(a, n-1);
	else return modpow(a*a, n/2);
}
istream& operator >>(istream &is, mint &ope){ll t; is >> t, ope.x = t; return is;}
ostream& operator <<(ostream &os, mint &ope){return os << ope.x;}
ostream& operator <<(ostream &os, const mint &ope){return os << ope.x;}

ll modpow(ll a, ll n, ll mod){
	if(n == 0) return 1;
	if(n % 2) return ((a%mod) * (modpow(a, n-1, mod)%mod)) % mod;
	else return modpow((a*a)%mod, n/2, mod) % mod;
}

vector<mint> fact, fact_inv;
void make_fact(int n){
	fact.resize(n+1), fact_inv.resize(n+1);
	fact[0] = mint(1); rep(i, 1, n) fact[i] = fact[i-1] * mint(i);
	fact_inv[n] = fact[n].inverse(); per(i, n-1, 0) fact_inv[i] = fact_inv[i+1] * mint(i+1);
}
mint comb(int n, int k){ if(n < 0 || k < 0 || n < k) return mint(0); return fact[n] * fact_inv[k] * fact_inv[n-k];}
mint perm(int n, int k){ return comb(n, k) * fact[k]; }
template<typename T> T comb2(ll n, ll k){ T ret = 1; rep(i, 1, k) ret *= n-k+i, ret /= i; return ret;}

vector<int> prime, pvec, qrime;
void make_prime(int n){
	prime.resize(n+1);
	rep(i, 2, n){
		if(prime[i] == 0) pvec.push_back(i), prime[i] = i;
		for(auto p : pvec){ if(i*p > n || p > prime[i]) break; prime[i*p] = p;}
	}
}
void make_qrime(int n){
	qrime.resize(n+1);
	rep(i, 2, n){int ni = i / prime[i]; if(prime[i] == prime[ni]) qrime[i] = qrime[ni] * prime[i]; else qrime[i] = prime[i];}
}

bool exceed(ll x, ll y, ll m){return y > 0 && x >= m / y + 1;}
void mark(){ cout << "*" << endl; }
void yes(){ cout << "YES" << endl; }
void no(){ cout << "NO" << endl; }
ll floor(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return a/b; else return -((-a+b-1)/b); }
ll ceil(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return (a+b-1)/b; else return -((-a)/b); }
ll modulo(ll a, ll b){ b = abs(b); return a - floor(a, b) * b;}
ll sgn(ll x){ if(x > 0) return 1; if(x < 0) return -1; return 0;}
ll gcd(ll a, ll b){if(b == 0) return a; return gcd(b, a%b);}
ll lcm(ll a, ll b){return a/gcd(a, b)*b;}
ll arith(ll x){return x*(x+1)/2;}
ll arith(ll l, ll r){return arith(r) - arith(l-1);}
ll digitnum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret++; return ret;}
ll digitsum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret += x % b; return ret;}
string lltos(ll x, ll b = 10){string ret; for(;x;x/=b) ret += x % b + '0'; reverse(all(ret)); return ret;}
ll stoll(string &s, ll b = 10){ll ret = 0; for(auto c : s) ret *= b, ret += c - '0'; return ret;}
template<typename T> void uniq(T &vec){sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}
int popcount(ull x){
	x -= ((x>>1)&0x5555555555555555ULL), x = (x & 0x3333333333333333ULL) + ((x>>2) & 0x3333333333333333ULL);
	return (((x + (x>>4)) & 0x0F0F0F0F0F0F0F0FULL) * 0x0101010101010101ULL) >> 56;
}

template<class S, class T> pair<S, T>& operator+=(pair<S, T> &s, const pair<S, T> &t){s.first += t.first, s.second += t.second; return s;}
template<class S, class T> pair<S, T>& operator-=(pair<S, T> &s, const pair<S, T> &t){s.first -= t.first, s.second -= t.second; return s;}
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first-t.first, s.second-t.second);}
template<class T> T dot(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.first + s.second*t.second;}
template<class T> T cross(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.second - s.second*t.first;}

template<typename T> ostream& operator << (ostream& os, vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, deque<T>& deq){reps(i,  deq) os << deq[i] << " "; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, map<T, U>& ope){ for(auto p : ope) os << "(" << p.first << ", " << p.second << "),";return os;}
template<typename T> ostream& operator << (ostream& os, set<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, multiset<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> void outa(T a[], ll s, ll t){rep(i, s, t){ cout << a[i]; if(i < t) cout << " ";} cout << endl;}
template<typename T, size_t N> ostream& operator << (ostream& os, array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
template<typename T, size_t N> ostream& operator << (ostream& os, const array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
void dump_func(){cout << endl;}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail){cout << head; if(sizeof...(Tail) > 0) cout << " "; dump_func(std::move(tail)...);}

struct BitMatrix{
	static const int size = 2105;
	bitset<size+1> bs[size+1];
	
	BitMatrix(){}
	static BitMatrix Ident(){
		BitMatrix ret;
		rep(i, 1, size) ret.bs[i][i] = 1;
		return ret;
	}
	BitMatrix operator*(BitMatrix& ope){
		BitMatrix ret;
		bitset<size+1> bs2[size+1];
		rep(i, 1, size) rep(j, 1, size) bs2[i][j] = ope.bs[j][i];
		rep(i, 1, size) rep(j, 1, size) ret.bs[i][j] = (bs[i] & bs2[j]).any();
		return ret;
	}
	BitMatrix pow(ll n){
		if(n == 0) return Ident();
		if(n % 2) return pow(n-1) * (*this);
		else{
			BitMatrix tmp = pow(n/2);
			return tmp * tmp;
		}
	}
};
ostream& operator << (ostream& os, BitMatrix& mat) {
	rep(i, 1, BitMatrix::size){rep(j, 1, BitMatrix::size) os << mat.bs[i][j] << " "; cout << endl;}
	return os;
}
ostream& operator << (ostream& os, const BitMatrix& mat) {
	rep(i, 1, BitMatrix::size){rep(j, 1, BitMatrix::size) os << mat.bs[i][j] << " "; cout << endl;}
	return os;
}

ll n, d;
vector<P> vec[105];
vector<ll> G[2105];
ll cnt[105];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> d;
	ll k, u, w;
	rep(i, 1, n){
		cin >> k;
		rep(j, 1, k){
			cin >> u >> w;
			vec[i].pb(P(w, u));
		}
		sort(all(vec[i]));
	}
	
	ll sum = n;
	rep(i, 1, n){
		ll l = vec[i].back().fi;
		G[i].pb(sum+1);
		rep(j, 1, l-1) G[sum+j].pb(sum+j+1);
		for(auto p : vec[i]){
			ll u = p.se, w = p.fi;
			if(w == 1) G[i].pb(u);
			else G[sum+(w-1)].pb(u);
		}
		sum += l;
	}
	
	BitMatrix mat;
	rep(i, 1, sum) for(auto u : G[i]) mat.bs[u][i] = 1;
	BitMatrix pmat = mat.pow(d);
	
	rep(i, 1, n) rep(j, 1, n) if(pmat.bs[j][i]) cnt[j]++;
	vector<ll> ans;
	rep(i, 1, n) if(cnt[i] >= n) ans.pb(i);
	
	outl(sz(ans));
	outl(ans);
	
	return 0;
}
accuracy: 0.425
time_ms: 2610
memory_kb: 21888
final_rank: 14

code_snippet: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#include <array>
#include <unordered_set>
#include <unordered_map>
#define rep(x, s, t) for(ll x = (s); (x) <= (t); (x)++)
#define per(x, s, t) for(ll x = (s); (x) >= (t); (x)--)
#define reps(x, s) for(ll x = 0; (x) < (ll)(s).size(); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define sz(x) ((ll)(x).size())
#define all(x) (x).begin(),(x).end()
#define outl(...) dump_func(__VA_ARGS__)
#define outf(x) cout << fixed << setprecision(16) << (x) << endl
#define pb push_back
#define fi first
#define se second
#define inf 2e18
#define eps 1e-9
const double PI = 3.1415926535897932384626433;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

struct edge{
	ll to, cost;
	edge(){}
	edge(ll a, ll b){ to = a, cost = b;}
};
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

const int mod = 1000000007;
//const int mod = 998244353;

struct mint{
	int x;
	mint(ll y = 0){if(y < 0 || y >= mod) y = (y%mod+mod)%mod; x = y;}
	mint(const mint &ope) {x = ope.x;}
	mint operator-(){return mint(-x);}
	mint operator+(const mint &ope){return mint(x) += ope;}
	mint operator-(const mint &ope){return mint(x) -= ope;}
	mint operator*(const mint &ope){return mint(x) *= ope;}
	mint operator/(const mint &ope){return mint(x) /= ope;}
	mint& operator+=(const mint &ope){x += ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator-=(const mint &ope){x += mod - ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator*=(const mint &ope){ll tmp = x; tmp *= ope.x, tmp %= mod; x = tmp; return *this;}
	mint& operator/=(const mint &ope){
		ll n = mod-2; mint mul = ope;
		while(n){if(n & 1) *this *= mul; mul *= mul; n >>= 1;}
		return *this;
	}
	mint inverse(){return mint(1) / *this;}
	bool operator ==(const mint &ope){return x == ope.x;}
	bool operator !=(const mint &ope){return x != ope.x;}
	bool operator <(const mint &ope)const{return x < ope.x;}
};
mint modpow(mint a, ll n){
	if(n == 0) return mint(1);
	if(n % 2) return a * modpow(a, n-1);
	else return modpow(a*a, n/2);
}
istream& operator >>(istream &is, mint &ope){ll t; is >> t, ope.x = t; return is;}
ostream& operator <<(ostream &os, mint &ope){return os << ope.x;}
ostream& operator <<(ostream &os, const mint &ope){return os << ope.x;}

ll modpow(ll a, ll n, ll mod){
	if(n == 0) return 1;
	if(n % 2) return ((a%mod) * (modpow(a, n-1, mod)%mod)) % mod;
	else return modpow((a*a)%mod, n/2, mod) % mod;
}

vector<mint> fact, fact_inv;
void make_fact(int n){
	fact.resize(n+1), fact_inv.resize(n+1);
	fact[0] = mint(1); rep(i, 1, n) fact[i] = fact[i-1] * mint(i);
	fact_inv[n] = fact[n].inverse(); per(i, n-1, 0) fact_inv[i] = fact_inv[i+1] * mint(i+1);
}
mint comb(int n, int k){ if(n < 0 || k < 0 || n < k) return mint(0); return fact[n] * fact_inv[k] * fact_inv[n-k];}
mint perm(int n, int k){ return comb(n, k) * fact[k]; }
template<typename T> T comb2(ll n, ll k){ T ret = 1; rep(i, 1, k) ret *= n-k+i, ret /= i; return ret;}

vector<int> prime, pvec, qrime;
void make_prime(int n){
	prime.resize(n+1);
	rep(i, 2, n){
		if(prime[i] == 0) pvec.push_back(i), prime[i] = i;
		for(auto p : pvec){ if(i*p > n || p > prime[i]) break; prime[i*p] = p;}
	}
}
void make_qrime(int n){
	qrime.resize(n+1);
	rep(i, 2, n){int ni = i / prime[i]; if(prime[i] == prime[ni]) qrime[i] = qrime[ni] * prime[i]; else qrime[i] = prime[i];}
}

bool exceed(ll x, ll y, ll m){return y > 0 && x >= m / y + 1;}
void mark(){ cout << "*" << endl; }
void yes(){ cout << "YES" << endl; }
void no(){ cout << "NO" << endl; }
ll floor(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return a/b; else return -((-a+b-1)/b); }
ll ceil(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return (a+b-1)/b; else return -((-a)/b); }
ll modulo(ll a, ll b){ b = abs(b); return a - floor(a, b) * b;}
ll sgn(ll x){ if(x > 0) return 1; if(x < 0) return -1; return 0;}
ll gcd(ll a, ll b){if(b == 0) return a; return gcd(b, a%b);}
ll lcm(ll a, ll b){return a/gcd(a, b)*b;}
ll arith(ll x){return x*(x+1)/2;}
ll arith(ll l, ll r){return arith(r) - arith(l-1);}
ll digitnum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret++; return ret;}
ll digitsum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret += x % b; return ret;}
string lltos(ll x, ll b = 10){string ret; for(;x;x/=b) ret += x % b + '0'; reverse(all(ret)); return ret;}
ll stoll(string &s, ll b = 10){ll ret = 0; for(auto c : s) ret *= b, ret += c - '0'; return ret;}
template<typename T> void uniq(T &vec){sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}
int popcount(ull x){
	x -= ((x>>1)&0x5555555555555555ULL), x = (x & 0x3333333333333333ULL) + ((x>>2) & 0x3333333333333333ULL);
	return (((x + (x>>4)) & 0x0F0F0F0F0F0F0F0FULL) * 0x0101010101010101ULL) >> 56;
}

template<class S, class T> pair<S, T>& operator+=(pair<S, T> &s, const pair<S, T> &t){s.first += t.first, s.second += t.second; return s;}
template<class S, class T> pair<S, T>& operator-=(pair<S, T> &s, const pair<S, T> &t){s.first -= t.first, s.second -= t.second; return s;}
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first-t.first, s.second-t.second);}
template<class T> T dot(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.first + s.second*t.second;}
template<class T> T cross(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.second - s.second*t.first;}

template<typename T> ostream& operator << (ostream& os, vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, deque<T>& deq){reps(i,  deq) os << deq[i] << " "; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, map<T, U>& ope){ for(auto p : ope) os << "(" << p.first << ", " << p.second << "),";return os;}
template<typename T> ostream& operator << (ostream& os, set<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, multiset<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> void outa(T a[], ll s, ll t){rep(i, s, t){ cout << a[i]; if(i < t) cout << " ";} cout << endl;}
template<typename T, size_t N> ostream& operator << (ostream& os, array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
template<typename T, size_t N> ostream& operator << (ostream& os, const array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
void dump_func(){cout << endl;}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail){cout << head; if(sizeof...(Tail) > 0) cout << " "; dump_func(std::move(tail)...);}

struct BitMatrix{
	static const int size = 1105;
	bitset<size+1> bs[size+1];
	
	BitMatrix(){}
	static BitMatrix Ident(){
		BitMatrix ret;
		rep(i, 1, size) ret.bs[i][i] = 1;
		return ret;
	}
	BitMatrix operator*(BitMatrix& ope){
		BitMatrix ret;
		bitset<size+1> bs2[size+1];
		rep(i, 1, size) rep(j, 1, size) bs2[i][j] = ope.bs[j][i];
		rep(i, 1, size) rep(j, 1, size) ret.bs[i][j] = (bs[i] & bs2[j]).any();
		return ret;
	}
	BitMatrix pow(ll n){
		if(n == 0) return Ident();
		if(n % 2) return pow(n-1) * (*this);
		else{
			BitMatrix tmp = pow(n/2);
			return tmp * tmp;
		}
	}
};
ostream& operator << (ostream& os, BitMatrix& mat) {
	rep(i, 1, BitMatrix::size){rep(j, 1, BitMatrix::size) os << mat.bs[i][j] << " "; cout << endl;}
	return os;
}
ostream& operator << (ostream& os, const BitMatrix& mat) {
	rep(i, 1, BitMatrix::size){rep(j, 1, BitMatrix::size) os << mat.bs[i][j] << " "; cout << endl;}
	return os;
}

ll n, d;
vector<P> vec[105];
vector<ll> G[1105];
ll cnt[105];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> d;
	ll k, u, w;
	rep(i, 1, n){
		cin >> k;
		rep(j, 1, k){
			cin >> u >> w;
			vec[i].pb(P(w, u));
		}
		sort(all(vec[i]));
	}
	
	ll sum = n;
	rep(i, 1, n){
		ll l = vec[i].back().fi;
		G[i].pb(sum+1);
		rep(j, 1, l-1) G[sum+j].pb(sum+j+1);
		for(auto p : vec[i]){
			ll u = p.se, w = p.fi;
			if(w == 1) G[i].pb(u);
			else G[sum+(w-1)].pb(u);
		}
		sum += l;
	}
	
	BitMatrix mat;
	rep(i, 1, sum) for(auto u : G[i]) mat.bs[u][i] = 1;
	BitMatrix pmat = mat.pow(d);
	
	rep(i, 1, n) rep(j, 1, n) if(pmat.bs[j][i]) cnt[j]++;
	vector<ll> ans;
	rep(i, 1, n) if(cnt[i] >= n) ans.pb(i);
	
	outl(sz(ans));
	outl(ans);
	
	return 0;
}
accuracy: 0.425
time_ms: 350
memory_kb: 8796
final_rank: 13

code_snippet: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#include <array>
#include <unordered_set>
#include <unordered_map>
#define rep(x, s, t) for(ll x = (s); (x) <= (t); (x)++)
#define per(x, s, t) for(ll x = (s); (x) >= (t); (x)--)
#define reps(x, s) for(ll x = 0; (x) < (ll)(s).size(); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define sz(x) ((ll)(x).size())
#define all(x) (x).begin(),(x).end()
#define outl(...) dump_func(__VA_ARGS__)
#define outf(x) cout << fixed << setprecision(16) << (x) << endl
#define pb push_back
#define fi first
#define se second
#define inf 2e18
#define eps 1e-9
const double PI = 3.1415926535897932384626433;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

struct edge{
	ll to, cost;
	edge(){}
	edge(ll a, ll b){ to = a, cost = b;}
};
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

const int mod = 1000000007;
//const int mod = 998244353;

struct mint{
	int x;
	mint(ll y = 0){if(y < 0 || y >= mod) y = (y%mod+mod)%mod; x = y;}
	mint(const mint &ope) {x = ope.x;}
	mint operator-(){return mint(-x);}
	mint operator+(const mint &ope){return mint(x) += ope;}
	mint operator-(const mint &ope){return mint(x) -= ope;}
	mint operator*(const mint &ope){return mint(x) *= ope;}
	mint operator/(const mint &ope){return mint(x) /= ope;}
	mint& operator+=(const mint &ope){x += ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator-=(const mint &ope){x += mod - ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator*=(const mint &ope){ll tmp = x; tmp *= ope.x, tmp %= mod; x = tmp; return *this;}
	mint& operator/=(const mint &ope){
		ll n = mod-2; mint mul = ope;
		while(n){if(n & 1) *this *= mul; mul *= mul; n >>= 1;}
		return *this;
	}
	mint inverse(){return mint(1) / *this;}
	bool operator ==(const mint &ope){return x == ope.x;}
	bool operator !=(const mint &ope){return x != ope.x;}
	bool operator <(const mint &ope)const{return x < ope.x;}
};
mint modpow(mint a, ll n){
	if(n == 0) return mint(1);
	if(n % 2) return a * modpow(a, n-1);
	else return modpow(a*a, n/2);
}
istream& operator >>(istream &is, mint &ope){ll t; is >> t, ope.x = t; return is;}
ostream& operator <<(ostream &os, mint &ope){return os << ope.x;}
ostream& operator <<(ostream &os, const mint &ope){return os << ope.x;}

ll modpow(ll a, ll n, ll mod){
	if(n == 0) return 1;
	if(n % 2) return ((a%mod) * (modpow(a, n-1, mod)%mod)) % mod;
	else return modpow((a*a)%mod, n/2, mod) % mod;
}

vector<mint> fact, fact_inv;
void make_fact(int n){
	fact.resize(n+1), fact_inv.resize(n+1);
	fact[0] = mint(1); rep(i, 1, n) fact[i] = fact[i-1] * mint(i);
	fact_inv[n] = fact[n].inverse(); per(i, n-1, 0) fact_inv[i] = fact_inv[i+1] * mint(i+1);
}
mint comb(int n, int k){ if(n < 0 || k < 0 || n < k) return mint(0); return fact[n] * fact_inv[k] * fact_inv[n-k];}
mint perm(int n, int k){ return comb(n, k) * fact[k]; }
template<typename T> T comb2(ll n, ll k){ T ret = 1; rep(i, 1, k) ret *= n-k+i, ret /= i; return ret;}

vector<int> prime, pvec, qrime;
void make_prime(int n){
	prime.resize(n+1);
	rep(i, 2, n){
		if(prime[i] == 0) pvec.push_back(i), prime[i] = i;
		for(auto p : pvec){ if(i*p > n || p > prime[i]) break; prime[i*p] = p;}
	}
}
void make_qrime(int n){
	qrime.resize(n+1);
	rep(i, 2, n){int ni = i / prime[i]; if(prime[i] == prime[ni]) qrime[i] = qrime[ni] * prime[i]; else qrime[i] = prime[i];}
}

bool exceed(ll x, ll y, ll m){return y > 0 && x >= m / y + 1;}
void mark(){ cout << "*" << endl; }
void yes(){ cout << "YES" << endl; }
void no(){ cout << "NO" << endl; }
ll floor(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return a/b; else return -((-a+b-1)/b); }
ll ceil(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return (a+b-1)/b; else return -((-a)/b); }
ll modulo(ll a, ll b){ b = abs(b); return a - floor(a, b) * b;}
ll sgn(ll x){ if(x > 0) return 1; if(x < 0) return -1; return 0;}
ll gcd(ll a, ll b){if(b == 0) return a; return gcd(b, a%b);}
ll lcm(ll a, ll b){return a/gcd(a, b)*b;}
ll arith(ll x){return x*(x+1)/2;}
ll arith(ll l, ll r){return arith(r) - arith(l-1);}
ll digitnum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret++; return ret;}
ll digitsum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret += x % b; return ret;}
string lltos(ll x, ll b = 10){string ret; for(;x;x/=b) ret += x % b + '0'; reverse(all(ret)); return ret;}
ll stoll(string &s, ll b = 10){ll ret = 0; for(auto c : s) ret *= b, ret += c - '0'; return ret;}
template<typename T> void uniq(T &vec){sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}
int popcount(ull x){
	x -= ((x>>1)&0x5555555555555555ULL), x = (x & 0x3333333333333333ULL) + ((x>>2) & 0x3333333333333333ULL);
	return (((x + (x>>4)) & 0x0F0F0F0F0F0F0F0FULL) * 0x0101010101010101ULL) >> 56;
}

template<class S, class T> pair<S, T>& operator+=(pair<S, T> &s, const pair<S, T> &t){s.first += t.first, s.second += t.second; return s;}
template<class S, class T> pair<S, T>& operator-=(pair<S, T> &s, const pair<S, T> &t){s.first -= t.first, s.second -= t.second; return s;}
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first-t.first, s.second-t.second);}
template<class T> T dot(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.first + s.second*t.second;}
template<class T> T cross(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.second - s.second*t.first;}

template<typename T> ostream& operator << (ostream& os, vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, deque<T>& deq){reps(i,  deq) os << deq[i] << " "; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, map<T, U>& ope){ for(auto p : ope) os << "(" << p.first << ", " << p.second << "),";return os;}
template<typename T> ostream& operator << (ostream& os, set<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, multiset<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> void outa(T a[], ll s, ll t){rep(i, s, t){ cout << a[i]; if(i < t) cout << " ";} cout << endl;}
template<typename T, size_t N> ostream& operator << (ostream& os, array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
template<typename T, size_t N> ostream& operator << (ostream& os, const array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
void dump_func(){cout << endl;}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail){cout << head; if(sizeof...(Tail) > 0) cout << " "; dump_func(std::move(tail)...);}

struct BitMatrix{
	static const int size = 1105;
	bitset<size+1> bs[size+1];
	
	BitMatrix(){}
	static BitMatrix Ident(){
		BitMatrix ret;
		rep(i, 1, size) ret.bs[i][i] = 1;
		return ret;
	}
	BitMatrix operator*(BitMatrix& ope){
		BitMatrix ret;
		bitset<size+1> bs2[size+1];
		rep(i, 1, size) rep(j, 1, size) bs2[i][j] = ope.bs[j][i];
		rep(i, 1, size) rep(j, 1, size) ret.bs[i][j] = (bs[i] & bs2[j]).any();
		return ret;
	}
	BitMatrix pow(ll n){
		if(n == 0) return Ident();
		if(n % 2) return pow(n-1) * (*this);
		else{
			BitMatrix tmp = pow(n/2);
			return tmp * tmp;
		}
	}
};
ostream& operator << (ostream& os, BitMatrix& mat) {
	rep(i, 1, BitMatrix::size){rep(j, 1, BitMatrix::size) os << mat.bs[i][j] << " "; cout << endl;}
	return os;
}
ostream& operator << (ostream& os, const BitMatrix& mat) {
	rep(i, 1, BitMatrix::size){rep(j, 1, BitMatrix::size) os << mat.bs[i][j] << " "; cout << endl;}
	return os;
}

ll n, d;
vector<P> vec[105];
vector<ll> G[1105];
ll cnt[105];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> d;
	ll k, u, w;
	rep(i, 1, n){
		cin >> k;
		rep(j, 1, k){
			cin >> u >> w;
			vec[i].pb(P(w, u));
		}
		sort(all(vec[i]));
	}
	
	ll sum = n;
	rep(i, 1, n){
		ll l = vec[i].back().fi;
		rep(j, 1, l-1) G[sum+j].pb(sum+j+1);
		for(auto p : vec[i]){
			ll u = p.se, w = p.fi;
			if(w == 1) G[i].pb(u);
			else G[i].pb(sum+l-(w-2)), G[sum+l].pb(u);
		}
		sum += l;
	}
	
	BitMatrix mat;
	rep(i, 1, sum) for(auto u : G[i]) mat.bs[u][i] = 1;
	BitMatrix pmat = mat.pow(d);
	
	rep(i, 1, n) rep(j, 1, n) if(pmat.bs[j][i]) cnt[j]++;
	vector<ll> ans;
	rep(i, 1, n) if(cnt[i] >= n) ans.pb(i);
	
	outl(sz(ans));
	outl(ans);
	
	return 0;
}
accuracy: 0.375
time_ms: 360
memory_kb: 8796
final_rank: 16

code_snippet: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#include <array>
#include <unordered_set>
#include <unordered_map>
#define rep(x, s, t) for(ll x = (s); (x) <= (t); (x)++)
#define per(x, s, t) for(ll x = (s); (x) >= (t); (x)--)
#define reps(x, s) for(ll x = 0; (x) < (ll)(s).size(); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define sz(x) ((ll)(x).size())
#define all(x) (x).begin(),(x).end()
#define outl(...) dump_func(__VA_ARGS__)
#define outf(x) cout << fixed << setprecision(16) << (x) << endl
#define pb push_back
#define fi first
#define se second
#define inf 2e18
#define eps 1e-9
const double PI = 3.1415926535897932384626433;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

struct edge{
	ll to, cost;
	edge(){}
	edge(ll a, ll b){ to = a, cost = b;}
};
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};

const int mod = 1000000007;
//const int mod = 998244353;

struct mint{
	int x;
	mint(ll y = 0){if(y < 0 || y >= mod) y = (y%mod+mod)%mod; x = y;}
	mint(const mint &ope) {x = ope.x;}
	mint operator-(){return mint(-x);}
	mint operator+(const mint &ope){return mint(x) += ope;}
	mint operator-(const mint &ope){return mint(x) -= ope;}
	mint operator*(const mint &ope){return mint(x) *= ope;}
	mint operator/(const mint &ope){return mint(x) /= ope;}
	mint& operator+=(const mint &ope){x += ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator-=(const mint &ope){x += mod - ope.x; if(x >= mod) x -= mod; return *this;}
	mint& operator*=(const mint &ope){ll tmp = x; tmp *= ope.x, tmp %= mod; x = tmp; return *this;}
	mint& operator/=(const mint &ope){
		ll n = mod-2; mint mul = ope;
		while(n){if(n & 1) *this *= mul; mul *= mul; n >>= 1;}
		return *this;
	}
	mint inverse(){return mint(1) / *this;}
	bool operator ==(const mint &ope){return x == ope.x;}
	bool operator !=(const mint &ope){return x != ope.x;}
	bool operator <(const mint &ope)const{return x < ope.x;}
};
mint modpow(mint a, ll n){
	if(n == 0) return mint(1);
	if(n % 2) return a * modpow(a, n-1);
	else return modpow(a*a, n/2);
}
istream& operator >>(istream &is, mint &ope){ll t; is >> t, ope.x = t; return is;}
ostream& operator <<(ostream &os, mint &ope){return os << ope.x;}
ostream& operator <<(ostream &os, const mint &ope){return os << ope.x;}

ll modpow(ll a, ll n, ll mod){
	if(n == 0) return 1;
	if(n % 2) return ((a%mod) * (modpow(a, n-1, mod)%mod)) % mod;
	else return modpow((a*a)%mod, n/2, mod) % mod;
}

vector<mint> fact, fact_inv;
void make_fact(int n){
	fact.resize(n+1), fact_inv.resize(n+1);
	fact[0] = mint(1); rep(i, 1, n) fact[i] = fact[i-1] * mint(i);
	fact_inv[n] = fact[n].inverse(); per(i, n-1, 0) fact_inv[i] = fact_inv[i+1] * mint(i+1);
}
mint comb(int n, int k){ if(n < 0 || k < 0 || n < k) return mint(0); return fact[n] * fact_inv[k] * fact_inv[n-k];}
mint perm(int n, int k){ return comb(n, k) * fact[k]; }
template<typename T> T comb2(ll n, ll k){ T ret = 1; rep(i, 1, k) ret *= n-k+i, ret /= i; return ret;}

vector<int> prime, pvec, qrime;
void make_prime(int n){
	prime.resize(n+1);
	rep(i, 2, n){
		if(prime[i] == 0) pvec.push_back(i), prime[i] = i;
		for(auto p : pvec){ if(i*p > n || p > prime[i]) break; prime[i*p] = p;}
	}
}
void make_qrime(int n){
	qrime.resize(n+1);
	rep(i, 2, n){int ni = i / prime[i]; if(prime[i] == prime[ni]) qrime[i] = qrime[ni] * prime[i]; else qrime[i] = prime[i];}
}

bool exceed(ll x, ll y, ll m){return y > 0 && x >= m / y + 1;}
void mark(){ cout << "*" << endl; }
void yes(){ cout << "YES" << endl; }
void no(){ cout << "NO" << endl; }
ll floor(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return a/b; else return -((-a+b-1)/b); }
ll ceil(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a >= 0) return (a+b-1)/b; else return -((-a)/b); }
ll modulo(ll a, ll b){ b = abs(b); return a - floor(a, b) * b;}
ll sgn(ll x){ if(x > 0) return 1; if(x < 0) return -1; return 0;}
ll gcd(ll a, ll b){if(b == 0) return a; return gcd(b, a%b);}
ll lcm(ll a, ll b){return a/gcd(a, b)*b;}
ll arith(ll x){return x*(x+1)/2;}
ll arith(ll l, ll r){return arith(r) - arith(l-1);}
ll digitnum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret++; return ret;}
ll digitsum(ll x, ll b = 10){ll ret = 0; for(; x; x /= b) ret += x % b; return ret;}
string lltos(ll x, ll b = 10){string ret; for(;x;x/=b) ret += x % b + '0'; reverse(all(ret)); return ret;}
ll stoll(string &s, ll b = 10){ll ret = 0; for(auto c : s) ret *= b, ret += c - '0'; return ret;}
template<typename T> void uniq(T &vec){sort(all(vec)); vec.erase(unique(all(vec)), vec.end());}
int popcount(ull x){
	x -= ((x>>1)&0x5555555555555555ULL), x = (x & 0x3333333333333333ULL) + ((x>>2) & 0x3333333333333333ULL);
	return (((x + (x>>4)) & 0x0F0F0F0F0F0F0F0FULL) * 0x0101010101010101ULL) >> 56;
}

template<class S, class T> pair<S, T>& operator+=(pair<S, T> &s, const pair<S, T> &t){s.first += t.first, s.second += t.second; return s;}
template<class S, class T> pair<S, T>& operator-=(pair<S, T> &s, const pair<S, T> &t){s.first -= t.first, s.second -= t.second; return s;}
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t){return pair<S,T>(s.first-t.first, s.second-t.second);}
template<class T> T dot(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.first + s.second*t.second;}
template<class T> T cross(const pair<T, T> &s, const pair<T, T> &t){return s.first*t.second - s.second*t.first;}

template<typename T> ostream& operator << (ostream& os, vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const vector<T>& vec){reps(i, vec) os << vec[i] << " "; return os;}
template<typename T> ostream& operator << (ostream& os, list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, const list<T>& ls){for(auto x : ls) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, deque<T>& deq){reps(i,  deq) os << deq[i] << " "; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U>& ope){ os << "(" << ope.first << ", " << ope.second << ")"; return os;}
template<typename T, typename U> ostream& operator << (ostream& os, map<T, U>& ope){ for(auto p : ope) os << "(" << p.first << ", " << p.second << "),";return os;}
template<typename T> ostream& operator << (ostream& os, set<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> ostream& operator << (ostream& os, multiset<T>& ope){for(auto x : ope) os << x << " "; return os;}
template<typename T> void outa(T a[], ll s, ll t){rep(i, s, t){ cout << a[i]; if(i < t) cout << " ";} cout << endl;}
template<typename T, size_t N> ostream& operator << (ostream& os, array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
template<typename T, size_t N> ostream& operator << (ostream& os, const array<T, N>& arr){reps(i, arr) os << arr[i] << " "; return os;}
void dump_func(){cout << endl;}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail){cout << head; if(sizeof...(Tail) > 0) cout << " "; dump_func(std::move(tail)...);}

struct BitMatrix{
	static const int size = 1105;
	bitset<size+1> bs[size+1];
	
	BitMatrix(){}
	static BitMatrix Ident(){
		BitMatrix ret;
		rep(i, 1, size) ret.bs[i][i] = 1;
		return ret;
	}
	BitMatrix operator*(BitMatrix& ope){
		BitMatrix ret;
		bitset<size+1> bs2[size+1];
		rep(i, 1, size) rep(j, 1, size) bs2[i][j] = ope.bs[j][i];
		rep(i, 1, size) rep(j, 1, size) ret.bs[i][j] = (bs[i] & bs2[j]).any();
		return ret;
	}
	BitMatrix pow(ll n){
		if(n == 0) return Ident();
		if(n % 2) return pow(n-1) * (*this);
		else{
			BitMatrix tmp = pow(n/2);
			return tmp * tmp;
		}
	}
};
ostream& operator << (ostream& os, BitMatrix& mat) {
	rep(i, 1, BitMatrix::size){rep(j, 1, BitMatrix::size) os << mat.bs[i][j] << " "; cout << endl;}
	return os;
}
ostream& operator << (ostream& os, const BitMatrix& mat) {
	rep(i, 1, BitMatrix::size){rep(j, 1, BitMatrix::size) os << mat.bs[i][j] << " "; cout << endl;}
	return os;
}

ll n, d;
vector<P> vec[105];
vector<ll> G[1105];
ll cnt[105];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> d;
	ll k, u, w;
	rep(i, 1, n){
		cin >> k;
		rep(j, 1, k){
			cin >> u >> w;
			vec[i].pb(P(w, u));
		}
		sort(all(G[i]));
	}
	
	ll sum = n;
	rep(i, 1, n){
		ll l = vec[i].back().fi;
		rep(j, 1, l-1) G[sum+j].pb(sum+j+1);
		for(auto p : vec[i]){
			ll u = p.se, w = p.fi;
			if(w == 1) G[i].pb(u);
			else G[i].pb(sum+l-(w-2)), G[sum+l].pb(u);
		}
		sum += l;
	}
	
	BitMatrix mat;
	rep(i, 1, sum) for(auto u : G[i]) mat.bs[u][i] = 1;
	BitMatrix pmat = mat.pow(d);
	
	rep(i, 1, n) rep(j, 1, n) if(pmat.bs[j][i]) cnt[j]++;
	vector<ll> ans;
	rep(i, 1, n) if(cnt[i] >= n) ans.pb(i);
	
	outl(sz(ans));
	outl(ans);
	
	return 0;
}
accuracy: 0.175
time_ms: 340
memory_kb: 8684
final_rank: 17

code_snippet: #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T, typename V>
void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }
template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }
int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> sort_unique(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); }
template <typename T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
#if __cplusplus >= 201703L
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
#endif
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl
#define dbgif(cond, x) ((cond) ? cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl : cerr)
#else
#define dbg(x) (x)
#define dbgif(cond, x) 0
#endif

#include <bitset>
#include <cassert>
#include <tuple>
#include <utility>
#include <vector>

constexpr int Wmax = 1111;

std::vector<std::bitset<Wmax>>
matmul(const std::vector<std::bitset<Wmax>> &A, const std::vector<std::bitset<Wmax>> &B) {
    int H = A.size(), K = B.size();
    std::vector<std::bitset<Wmax>> C(H);
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < K; j++) {
            if (A[i][j]) C[i] |= B[j];
        }
    }
    return C;
}

std::vector<std::bitset<Wmax>> matpower(std::vector<std::bitset<Wmax>> X, long long n) {
    int D = X.size();
    std::vector<std::bitset<Wmax>> ret(D);
    for (int i = 0; i < D; i++) ret[i][i] = 1;
    while (n) {
        if (n & 1) ret = matmul(ret, X);
        X = matmul(X, X), n >>= 1;
    }
    return ret;
}


int main() {
    int N, D;
    cin >> N >> D;
    vector<vector<pint>> to(N);
    vector<int> Ks(N, 1);
    REP(a, N) {
        int K;
        cin >> K;
        while (K--) {
            int v, d;
            cin >> v >> d;
            --v;
            chmax(Ks[a], d);
            to[a].emplace_back(v, d);
        }
    }

    using BS = bitset<Wmax>;

    vector<int> AK(N + 1);
    REP(k, N) AK[k + 1] = AK[k] + Ks[k];
    dbg(AK);
    dbg(to);

    vector<BS> mat(Wmax);
    REP(s, N) {
        REP(k, Ks[s] - 1) mat[AK[s] + k + 1][AK[s] + k] = 1;

        for (auto [t, d] : to[s]) {
            mat[AK[t]][AK[s] + d - 1] = 1;
        }
    }

    mat = matpower(mat, D);

    vector<int> ret;
    REP(s, N) {
        bool good = true;
        REP(i, N) if (!mat[AK[s]][AK[i]]) good = false;
        if (good) ret.push_back(s);
    }
    cout << ret.size() << '\n';
    for (auto x : ret) cout << x + 1 << ' ';
    cout << '\n';
}
accuracy: 1
time_ms: 270
memory_kb: 3868
final_rank: 1

code_snippet: #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template <typename T, typename V>
void ndarray(vector<T>& vec, const V& val, int len) { vec.assign(len, val); }
template <typename T, typename V, typename... Args> void ndarray(vector<T>& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); }
template <typename T> bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; }
template <typename T> bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; }
int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> vector<T> sort_unique(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; }
template <typename T> int arglb(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); }
template <typename T> int argub(const std::vector<T> &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
#if __cplusplus >= 201703L
template <typename... T> istream &operator>>(istream &is, tuple<T...> &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; }
template <typename... T> ostream &operator<<(ostream &os, const tuple<T...> &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; }
#endif
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
#ifdef HITONANODE_LOCAL
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl
#define dbgif(cond, x) ((cond) ? cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << endl : cerr)
#else
#define dbg(x) (x)
#define dbgif(cond, x) 0
#endif

#include <bitset>
#include <cassert>
#include <tuple>
#include <utility>
#include <vector>

constexpr int Wmax = 1111;

std::vector<std::bitset<Wmax>>
matmul(const std::vector<std::bitset<Wmax>> &A, const std::vector<std::bitset<Wmax>> &B) {
    int H = A.size(), K = B.size();
    std::vector<std::bitset<Wmax>> C(H);
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < K; j++) {
            if (A[i][j]) C[i] |= B[j];
        }
    }
    return C;
}

std::vector<std::bitset<Wmax>> matpower(std::vector<std::bitset<Wmax>> X, long long n) {
    int D = X.size();
    std::vector<std::bitset<Wmax>> ret(D);
    for (int i = 0; i < D; i++) ret[i][i] = 1;
    while (n) {
        if (n & 1) ret = matmul(ret, X);
        X = matmul(X, X), n >>= 1;
    }
    return ret;
}


int main() {
    int N, D;
    cin >> N >> D;
    vector<vector<pint>> to(N);
    vector<int> Ks(N);
    REP(a, N) {
        int K;
        cin >> K;
        while (K--) {
            int v, d;
            cin >> v >> d;
            --v;
            chmax(Ks[a], d);
            to[a].emplace_back(v, d);
        }
    }

    using BS = bitset<Wmax>;

    vector<int> AK(N + 1);
    REP(k, N) AK[k + 1] = AK[k] + Ks[k];
    dbg(AK);
    dbg(to);

    vector<BS> mat(Wmax);
    REP(s, N) {
        REP(k, Ks[s] - 1) mat[AK[s] + k + 1][AK[s] + k] = 1;

        for (auto [t, d] : to[s]) {
            mat[AK[t]][AK[s] + d - 1] = 1;
        }
    }

    mat = matpower(mat, D);

    vector<int> ret;
    REP(s, N) {
        bool good = true;
        REP(i, N) if (!mat[AK[s]][AK[i]]) good = false;
        if (good) ret.push_back(s);
    }
    cout << ret.size() << '\n';
    for (auto x : ret) cout << x + 1 << ' ';
    cout << '\n';
}
accuracy: 0.425
time_ms: 10
memory_kb: 3868
final_rank: 11

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

// #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

#include <limits>
#include <type_traits>

namespace suisen {
// ! utility
template <typename ...Types>
using constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;
template <bool cond_v, typename Then, typename OrElse>
constexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {
    if constexpr (cond_v) {
        return std::forward<Then>(then);
    } else {
        return std::forward<OrElse>(or_else);
    }
}

// ! function
template <typename ReturnType, typename Callable, typename ...Args>
using is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;
template <typename F, typename T>
using is_uni_op = is_same_as_invoke_result<T, F, T>;
template <typename F, typename T>
using is_bin_op = is_same_as_invoke_result<T, F, T, T>;

template <typename Comparator, typename T>
using is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;

// ! integral
template <typename T, typename = constraints_t<std::is_integral<T>>>
constexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;
template <typename T, unsigned int n>
struct is_nbit { static constexpr bool value = bit_num<T> == n; };
template <typename T, unsigned int n>
static constexpr bool is_nbit_v = is_nbit<T, n>::value;

// ?
template <typename T>
struct safely_multipliable {};
template <>
struct safely_multipliable<int> { using type = long long; };
template <>
struct safely_multipliable<long long> { using type = __int128_t; };
template <>
struct safely_multipliable<unsigned int> { using type = unsigned long long; };
template <>
struct safely_multipliable<unsigned long long> { using type = __uint128_t; };
template <>
struct safely_multipliable<float> { using type = float; };
template <>
struct safely_multipliable<double> { using type = double; };
template <>
struct safely_multipliable<long double> { using type = long double; };
template <typename T>
using safely_multipliable_t = typename safely_multipliable<T>::type;

} // namespace suisen

// ! type aliases
using i128 = __int128_t;
using u128 = __uint128_t;
using ll = long long;
using uint = unsigned int;
using ull  = unsigned long long;

template <typename T> using vec  = std::vector<T>;
template <typename T> using vec2 = vec<vec <T>>;
template <typename T> using vec3 = vec<vec2<T>>;
template <typename T> using vec4 = vec<vec3<T>>;

template <typename T>
using pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <typename T, typename U>
using umap = std::unordered_map<T, U>;

// ! macros (capital: internal macro)
#define OVERLOAD2(_1,_2,name,...) name
#define OVERLOAD3(_1,_2,_3,name,...) name
#define OVERLOAD4(_1,_2,_3,_4,name,...) name

#define REP4(i,l,r,s)  for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))
#define REP3(i,l,r)    REP4(i,l,r,1)
#define REP2(i,n)      REP3(i,0,n)
#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))
#define REPINF2(i,l)   REPINF3(i,l,1)
#define REPINF1(i)     REPINF2(i,0)
#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))
#define RREP3(i,l,r)   RREP4(i,l,r,1)
#define RREP2(i,n)     RREP3(i,0,n)

#define rep(...)    OVERLOAD4(__VA_ARGS__, REP4   , REP3   , REP2   )(__VA_ARGS__)
#define rrep(...)   OVERLOAD4(__VA_ARGS__, RREP4  , RREP3  , RREP2  )(__VA_ARGS__)
#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)

#define CAT_I(a, b) a##b
#define CAT(a, b) CAT_I(a, b)
#define UNIQVAR(tag) CAT(tag, __LINE__)
#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)

#define all(iterable) (iterable).begin(), (iterable).end()
#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)

// ! I/O utilities

// pair
template <typename T, typename U>
std::ostream& operator<<(std::ostream& out, const std::pair<T, U> &a) {
    return out << a.first << ' ' << a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...> &a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return out;
    } else {
        out << std::get<N>(a);
        if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {
            out << ' ';
        }
        return operator<<<N + 1>(out, a);
    }
}
// vector
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T> &a) {
    for (auto it = a.begin(); it != a.end();) {
        out << *it;
        if (++it != a.end()) out << ' ';
    }
    return out;
}
// array
template <typename T, size_t N>
std::ostream& operator<<(std::ostream& out, const std::array<T, N> &a) {
    for (auto it = a.begin(); it != a.end();) {
        out << *it;
        if (++it != a.end()) out << ' ';
    }
    return out;
}
inline void print() { std::cout << '\n'; }
template <typename Head, typename... Tail>
inline void print(const Head &head, const Tail &...tails) {
    std::cout << head;
    if (sizeof...(tails)) std::cout << ' ';
    print(tails...);
}
template <typename Iterable>
auto print_all(const Iterable& v, std::string sep = " ", std::string end = "\n") -> decltype(std::cout << *v.begin(), void()) {
    for (auto it = v.begin(); it != v.end();) {
        std::cout << *it;
        if (++it != v.end()) std::cout << sep;
    }
    std::cout << end;
}

// pair
template <typename T, typename U>
std::istream& operator>>(std::istream& in, std::pair<T, U> &a) {
    return in >> a.first >> a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::istream& operator>>(std::istream& in, std::tuple<Args...> &a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return in;
    } else {
        return operator>><N + 1>(in >> std::get<N>(a), a);
    }
}
// vector
template <typename T>
std::istream& operator>>(std::istream& in, std::vector<T> &a) {
    for (auto it = a.begin(); it != a.end(); ++it) in >> *it;
    return in;
}
// array
template <typename T, size_t N>
std::istream& operator>>(std::istream& in, std::array<T, N> &a) {
    for (auto it = a.begin(); it != a.end(); ++it) in >> *it;
    return in;
}
template <typename ...Args>
void read(Args &...args) {
    ( std::cin >> ... >> args );
}

// ! integral utilities

// Returns pow(-1, n)
template <typename T>
constexpr inline int pow_m1(T n) {
    return -(n & 1) | 1;
}
// Returns pow(-1, n)
template <>
constexpr inline int pow_m1<bool>(bool n) {
    return -int(n) | 1;
}

// Returns floor(x / y)
template <typename T>
constexpr inline T fld(const T x, const T y) {
    return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;
}
template <typename T>
constexpr inline T cld(const T x, const T y) {
    return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;
}

template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcount(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcount(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcountll(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }
template <typename T>
constexpr inline int floor_log2(const T x) { return suisen::bit_num<T> - 1 - count_lz(x); }
template <typename T>
constexpr inline int ceil_log2(const T x)  { return floor_log2(x) + ((x & -x) != x); }
template <typename T>
constexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }
template <typename T>
constexpr inline int parity(const T x) { return popcount(x) & 1; }

struct all_subset {
    struct all_subset_iter {
        const int s; int t;
        constexpr all_subset_iter(int s) : s(s), t(s + 1) {}
        constexpr auto operator*() const { return t; }
        constexpr auto operator++() {}
        constexpr auto operator!=(std::nullptr_t) { return t ? (--t &= s, true) : false; }
    };
    int s;
    constexpr all_subset(int s) : s(s) {}
    constexpr auto begin() { return all_subset_iter(s); }
    constexpr auto end()   { return nullptr; }
};

// ! container

template <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>
auto priqueue_comp(const Comparator comparator) {
    return std::priority_queue<T, std::vector<T>, Comparator>(comparator);
}

template <typename Iterable>
auto isize(const Iterable &iterable) -> decltype(int(iterable.size())) {
    return iterable.size();
}

template <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>
auto generate_vector(int n, Gen generator) {
    std::vector<T> v(n);
    for (int i = 0; i < n; ++i) v[i] = generator(i);
    return v;
}
template <typename T>
auto generate_range_vector(T l, T r) {
    return generate_vector(r - l, [l](int i) { return l + i; });
}
template <typename T>
auto generate_range_vector(T n) {
    return generate_range_vector(0, n);
}

template <typename T>
void sort_unique_erase(std::vector<T> &a) {
    std::sort(a.begin(), a.end());
    a.erase(std::unique(a.begin(), a.end()), a.end());
}

template <typename InputIterator, typename BiConsumer>
auto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) {
    if (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr);
}
template <typename Container, typename BiConsumer>
auto foreach_adjacent_values(Container c, BiConsumer f) -> decltype(c.begin(), c.end(), void()){
    foreach_adjacent_values(c.begin(), c.end(), f);
}

// ! other utilities

// x <- min(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (y >= x) return false;
    x = y;
    return true;
}
// x <- max(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmax(T &x, const T &y) {
    if (y <= x) return false;
    x = y;
    return true;
}

namespace suisen {}
using namespace suisen;
using namespace std;

struct io_setup {
    io_setup(int precision = 20) {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(precision);
    }
} io_setup_{};

// ! code from here

constexpr int L = 1100;

using Mat = array<bitset<L>, L>;

template <bool transpose = false>
Mat mul(const Mat& A, const Mat& tB) {
    Mat C{};
    if constexpr (transpose) {
        rep(i, L) rep(j, L) C[i][j] = (A[j] & tB[i]).any();
    } else {
        rep(i, L) rep(j, L) C[i][j] = (A[i] & tB[j]).any();
    }
    return C;
}

Mat t(const Mat& A) {
    Mat B{};
    rep(i, L) rep(j, L) B[j][i] = A[i][j];
    return B;
}

Mat pow(Mat A, int l) {
    Mat res{};
    rep(i, L) res[i][i] = true;
    for (int b = l; b; b >>= 1) {
        if (b & 1) res = mul</* transpose = */true>(A, res);
        A = mul(A, t(A));
    }
    return t(res);
}

int main() {
    input(int, n, d);
    vector<vector<int>> g(n);
    rep(u, n) {
        input(int, len);
        if (len == 0) continue;
        vector<int> vs(len);
        vector<int> ds(len);
        rep(j, len) {
            input(int, v, d);
            --v;
            ds[j] = d;
            vs[j] = v;
        }
        int max_len = *max_element(all(ds));

        int siz = g.size();
        g.resize(siz + max_len - 1);
        rep(j, len) {
            if (ds[j] == 1) {
                g[u].push_back(vs[j]);
            } else {
                g[siz + ds[j] - 2].push_back(vs[j]);
            }
        }
        rep(i, max_len - 1) {
            if (i == 0) {
                g[u].push_back(siz);
            } else {
                g[siz + i - 1].push_back(siz + i);
            }
        }
    }

    int siz = g.size();
    Mat A{};
    rep(i, siz) {
        for (int j : g[i]) A[i][j] = true;
    }
    auto res = pow(A, d);

    vector<int8_t> ok(n, true);
    rep(i, n) rep(j, n) {
        ok[j] &= res[i][j];
    }

    vector<int> ans;
    rep(i, n) if (ok[i]) {
        ans.push_back(i + 1);
    }
    print(ans.size());
    print_all(ans);
    return 0;
}
accuracy: 1
time_ms: 1150
memory_kb: 4432
final_rank: 6

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

// #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

#include <limits>
#include <type_traits>

namespace suisen {
// ! utility
template <typename ...Types>
using constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;
template <bool cond_v, typename Then, typename OrElse>
constexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {
    if constexpr (cond_v) {
        return std::forward<Then>(then);
    } else {
        return std::forward<OrElse>(or_else);
    }
}

// ! function
template <typename ReturnType, typename Callable, typename ...Args>
using is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;
template <typename F, typename T>
using is_uni_op = is_same_as_invoke_result<T, F, T>;
template <typename F, typename T>
using is_bin_op = is_same_as_invoke_result<T, F, T, T>;

template <typename Comparator, typename T>
using is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;

// ! integral
template <typename T, typename = constraints_t<std::is_integral<T>>>
constexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;
template <typename T, unsigned int n>
struct is_nbit { static constexpr bool value = bit_num<T> == n; };
template <typename T, unsigned int n>
static constexpr bool is_nbit_v = is_nbit<T, n>::value;

// ?
template <typename T>
struct safely_multipliable {};
template <>
struct safely_multipliable<int> { using type = long long; };
template <>
struct safely_multipliable<long long> { using type = __int128_t; };
template <>
struct safely_multipliable<unsigned int> { using type = unsigned long long; };
template <>
struct safely_multipliable<unsigned long long> { using type = __uint128_t; };
template <>
struct safely_multipliable<float> { using type = float; };
template <>
struct safely_multipliable<double> { using type = double; };
template <>
struct safely_multipliable<long double> { using type = long double; };
template <typename T>
using safely_multipliable_t = typename safely_multipliable<T>::type;

} // namespace suisen

// ! type aliases
using i128 = __int128_t;
using u128 = __uint128_t;
using ll = long long;
using uint = unsigned int;
using ull  = unsigned long long;

template <typename T> using vec  = std::vector<T>;
template <typename T> using vec2 = vec<vec <T>>;
template <typename T> using vec3 = vec<vec2<T>>;
template <typename T> using vec4 = vec<vec3<T>>;

template <typename T>
using pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <typename T, typename U>
using umap = std::unordered_map<T, U>;

// ! macros (capital: internal macro)
#define OVERLOAD2(_1,_2,name,...) name
#define OVERLOAD3(_1,_2,_3,name,...) name
#define OVERLOAD4(_1,_2,_3,_4,name,...) name

#define REP4(i,l,r,s)  for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))
#define REP3(i,l,r)    REP4(i,l,r,1)
#define REP2(i,n)      REP3(i,0,n)
#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))
#define REPINF2(i,l)   REPINF3(i,l,1)
#define REPINF1(i)     REPINF2(i,0)
#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))
#define RREP3(i,l,r)   RREP4(i,l,r,1)
#define RREP2(i,n)     RREP3(i,0,n)

#define rep(...)    OVERLOAD4(__VA_ARGS__, REP4   , REP3   , REP2   )(__VA_ARGS__)
#define rrep(...)   OVERLOAD4(__VA_ARGS__, RREP4  , RREP3  , RREP2  )(__VA_ARGS__)
#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)

#define CAT_I(a, b) a##b
#define CAT(a, b) CAT_I(a, b)
#define UNIQVAR(tag) CAT(tag, __LINE__)
#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)

#define all(iterable) (iterable).begin(), (iterable).end()
#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)

// ! I/O utilities

// pair
template <typename T, typename U>
std::ostream& operator<<(std::ostream& out, const std::pair<T, U> &a) {
    return out << a.first << ' ' << a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...> &a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return out;
    } else {
        out << std::get<N>(a);
        if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {
            out << ' ';
        }
        return operator<<<N + 1>(out, a);
    }
}
// vector
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T> &a) {
    for (auto it = a.begin(); it != a.end();) {
        out << *it;
        if (++it != a.end()) out << ' ';
    }
    return out;
}
// array
template <typename T, size_t N>
std::ostream& operator<<(std::ostream& out, const std::array<T, N> &a) {
    for (auto it = a.begin(); it != a.end();) {
        out << *it;
        if (++it != a.end()) out << ' ';
    }
    return out;
}
inline void print() { std::cout << '\n'; }
template <typename Head, typename... Tail>
inline void print(const Head &head, const Tail &...tails) {
    std::cout << head;
    if (sizeof...(tails)) std::cout << ' ';
    print(tails...);
}
template <typename Iterable>
auto print_all(const Iterable& v, std::string sep = " ", std::string end = "\n") -> decltype(std::cout << *v.begin(), void()) {
    for (auto it = v.begin(); it != v.end();) {
        std::cout << *it;
        if (++it != v.end()) std::cout << sep;
    }
    std::cout << end;
}

// pair
template <typename T, typename U>
std::istream& operator>>(std::istream& in, std::pair<T, U> &a) {
    return in >> a.first >> a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::istream& operator>>(std::istream& in, std::tuple<Args...> &a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return in;
    } else {
        return operator>><N + 1>(in >> std::get<N>(a), a);
    }
}
// vector
template <typename T>
std::istream& operator>>(std::istream& in, std::vector<T> &a) {
    for (auto it = a.begin(); it != a.end(); ++it) in >> *it;
    return in;
}
// array
template <typename T, size_t N>
std::istream& operator>>(std::istream& in, std::array<T, N> &a) {
    for (auto it = a.begin(); it != a.end(); ++it) in >> *it;
    return in;
}
template <typename ...Args>
void read(Args &...args) {
    ( std::cin >> ... >> args );
}

// ! integral utilities

// Returns pow(-1, n)
template <typename T>
constexpr inline int pow_m1(T n) {
    return -(n & 1) | 1;
}
// Returns pow(-1, n)
template <>
constexpr inline int pow_m1<bool>(bool n) {
    return -int(n) | 1;
}

// Returns floor(x / y)
template <typename T>
constexpr inline T fld(const T x, const T y) {
    return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;
}
template <typename T>
constexpr inline T cld(const T x, const T y) {
    return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;
}

template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcount(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcount(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcountll(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }
template <typename T>
constexpr inline int floor_log2(const T x) { return suisen::bit_num<T> - 1 - count_lz(x); }
template <typename T>
constexpr inline int ceil_log2(const T x)  { return floor_log2(x) + ((x & -x) != x); }
template <typename T>
constexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }
template <typename T>
constexpr inline int parity(const T x) { return popcount(x) & 1; }

struct all_subset {
    struct all_subset_iter {
        const int s; int t;
        constexpr all_subset_iter(int s) : s(s), t(s + 1) {}
        constexpr auto operator*() const { return t; }
        constexpr auto operator++() {}
        constexpr auto operator!=(std::nullptr_t) { return t ? (--t &= s, true) : false; }
    };
    int s;
    constexpr all_subset(int s) : s(s) {}
    constexpr auto begin() { return all_subset_iter(s); }
    constexpr auto end()   { return nullptr; }
};

// ! container

template <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>
auto priqueue_comp(const Comparator comparator) {
    return std::priority_queue<T, std::vector<T>, Comparator>(comparator);
}

template <typename Iterable>
auto isize(const Iterable &iterable) -> decltype(int(iterable.size())) {
    return iterable.size();
}

template <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>
auto generate_vector(int n, Gen generator) {
    std::vector<T> v(n);
    for (int i = 0; i < n; ++i) v[i] = generator(i);
    return v;
}
template <typename T>
auto generate_range_vector(T l, T r) {
    return generate_vector(r - l, [l](int i) { return l + i; });
}
template <typename T>
auto generate_range_vector(T n) {
    return generate_range_vector(0, n);
}

template <typename T>
void sort_unique_erase(std::vector<T> &a) {
    std::sort(a.begin(), a.end());
    a.erase(std::unique(a.begin(), a.end()), a.end());
}

template <typename InputIterator, typename BiConsumer>
auto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) {
    if (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr);
}
template <typename Container, typename BiConsumer>
auto foreach_adjacent_values(Container c, BiConsumer f) -> decltype(c.begin(), c.end(), void()){
    foreach_adjacent_values(c.begin(), c.end(), f);
}

// ! other utilities

// x <- min(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (y >= x) return false;
    x = y;
    return true;
}
// x <- max(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmax(T &x, const T &y) {
    if (y <= x) return false;
    x = y;
    return true;
}

namespace suisen {}
using namespace suisen;
using namespace std;

struct io_setup {
    io_setup(int precision = 20) {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(precision);
    }
} io_setup_ {};

// ! code from here

constexpr int L = 1100;

using BS = bitset<L>;

array<BS, L> mul(const array<BS, L> &A, const array<BS, L> &tB, bool t = false) {
    array<BS, L> C{};
    rep(i, L) rep(j, L) {
        if (t) {
            C[j][i] = (A[i] & tB[j]).any();
        } else {
            C[i][j] = (A[i] & tB[j]).any();
        }
    }
    return C;
}
 
array<BS, L> t(const array<BS, L> &A) {
    array<BS, L> B{};
    rep(i, L) rep(j, L) {
        B[j][i] = A[i][j];
    }
    return B;
}
 
array<BS, L> pow(array<BS, L> A, int l) {
    array<BS, L> res{};
    rep(i, L) res[i][i] = true;
 
    for (int b = l; b; b >>= 1) {
        if (b & 1) res = mul(A, res, true);
        A = mul(A, t(A));
    }
    return t(res);
}

int main() {
    input(int, n, d);
    vector<vector<int>> g(n);
    rep(u, n) {
        input(int, len);
        if (len == 0) continue;
        vector<int> vs(len);
        vector<int> ds(len);
        rep(j, len) {
            input(int, v, d);
            --v;
            ds[j] = d;
            vs[j] = v;
        }
        int max_len = *max_element(all(ds));

        int siz = g.size();
        g.resize(siz + max_len - 1);
        rep(j, len) {
            if (ds[j] == 1) {
                g[u].push_back(vs[j]);
            } else {
                g[siz + ds[j] - 2].push_back(vs[j]);
            }
        }
        rep(i, max_len - 1) {
            if (i == 0) {
                g[u].push_back(siz);
            } else {
                g[siz + i - 1].push_back(siz + i);
            }
        }
    }

    int siz = g.size();
    array<BS, L> A{};
    rep(i, siz) {
        for (int j : g[i]) {
            A[i][j] = true;
        }
    }
    auto res = pow(A, d);

    vector<int8_t> ok(n, true);
    rep(i, n) rep(j, n) {
        if (not res[i][j]) {
            ok[j] = false;
        }
    }

    vector<int> ans;
    rep(i, n) {
        if (ok[i]) {
            ans.push_back(i + 1);
        }
    }
    print(ans.size());
    print_all(ans);
    return 0;
}
accuracy: 1
time_ms: 1160
memory_kb: 4456
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

template <int N>
class BooleanSquareMatrix {
    using BSMatrix = BooleanSquareMatrix;
    using Matrix = std::array<std::bitset<N>, N>;

public:
    void set(int r, int c) {
        rmat[r].set(c);
        cmat[c].set(r);
    }

    bool get(int r, int c) {
        return rmat[r][c];
    }

    BSMatrix matmul(const BSMatrix& B) const {
        BSMatrix res;
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                res.rmat[i][j] = res.cmat[j][i] = (rmat[i] & B.cmat[j]).any();
            }
        }
        return res;
    }

    BSMatrix pow(long long k) const {
        BSMatrix ret, A(*this);
        for (int i = 0; i < N; ++i) {
            ret.rmat[i].set(i);
            ret.cmat[i].set(i);
        }
        while (k > 0) {
            if (k & 1) ret = ret.matmul(A);
            A = A.matmul(A);
            k >>= 1;
        }
        return ret;
    }


private:
    Matrix rmat, cmat;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, D;
    cin >> N >> D;
    vector<vector<pair<int, int>>> vd(N);
    vector<int> maxd(N, 1);
    vector<int> cumd(N+1);
    rep(i,0,N) {
        int K;
        cin >> K;
        rep(j,0,K) {
            int v, d;
            cin >> v >> d;
            vd[i].push_back({v-1, d});
            chmax(maxd[i], d);
        }
        cumd[i+1] = cumd[i] + maxd[i];
    }
    BooleanSquareMatrix<1010> G;
    rep(i,0,N) {
        rep(j,cumd[i],cumd[i+1]-1) {
            G.set(j, j+1);
        }
        for (auto [v, d] : vd[i]) {
            G.set(cumd[i]+d-1, cumd[v]);
        }
    }
    G = G.pow(D);
    vector<vector<bool>> reach(N, vector<bool>(N));
    vector<int> ans;
    rep(i,0,N) {
        bool ok = true;
        rep(j,0,N) {
            if (!G.get(cumd[j], cumd[i])) {
                ok = false;
                break;
            }
        }
        if (ok) {
            ans.push_back(i);
        }
    }
    int M = ans.size();
    cout << M << endl;
    rep(i,0,M) cout << ans[i]+1 << (i<M-1 ? " " : "\n");
}
accuracy: 1
time_ms: 600
memory_kb: 4492
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

template <int N>
class BooleanSquareMatrix {
    using BSMatrix = BooleanSquareMatrix;
    using Matrix = std::array<std::bitset<N>, N>;

public:
    void set(int r, int c) {
        rmat[r].set(c);
        cmat[c].set(r);
    }

    bool get(int r, int c) {
        return rmat[r][c];
    }

    BSMatrix matmul(const BSMatrix& B) const {
        BSMatrix res;
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                res.rmat[i][j] = res.cmat[j][i] = (rmat[i] & B.cmat[j]).any();
            }
        }
        return res;
    }

    BSMatrix pow(long long k) const {
        BSMatrix ret, A(*this);
        for (int i = 0; i < N; ++i) {
            ret.rmat[i].set(i);
            ret.cmat[i].set(i);
        }
        while (k > 0) {
            if (k & 1) ret = ret.matmul(A);
            A = A.matmul(A);
            k >>= 1;
        }
        return ret;
    }


private:
    Matrix rmat, cmat;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, D;
    cin >> N >> D;
    vector<vector<pair<int, int>>> vd(N);
    vector<int> maxd(N);
    vector<int> cumd(N+1);
    rep(i,0,N) {
        int K;
        cin >> K;
        rep(j,0,K) {
            int v, d;
            cin >> v >> d;
            vd[i].push_back({v-1, d});
            chmax(maxd[i], d);
        }
        cumd[i+1] = cumd[i] + maxd[i];
    }
    BooleanSquareMatrix<1010> G;
    rep(i,0,N) {
        rep(j,cumd[i],cumd[i+1]-1) {
            G.set(j, j+1);
        }
        for (auto [v, d] : vd[i]) {
            G.set(cumd[i]+d-1, cumd[v]);
        }
    }
    G = G.pow(D);
    vector<vector<bool>> reach(N, vector<bool>(N));
    vector<int> ans;
    rep(i,0,N) {
        bool ok = true;
        rep(j,0,N) {
            if (!G.get(cumd[j], cumd[i])) {
                ok = false;
                break;
            }
        }
        if (ok) {
            ans.push_back(i);
        }
    }
    int M = ans.size();
    cout << M << endl;
    rep(i,0,M) cout << ans[i]+1 << (i<M-1 ? " " : "\n");
}
accuracy: 0.425
time_ms: 230
memory_kb: 4464
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

template <int N>
class BooleanSquareMatrix {
    using BSMatrix = BooleanSquareMatrix;
    using Matrix = std::array<std::bitset<N>, N>;

public:
    void set(int r, int c) {
        rmat[r].set(c);
        cmat[c].set(r);
    }

    bool get(int r, int c) {
        return rmat[r][c];
    }

    BSMatrix matmul(const BSMatrix& B) const {
        BSMatrix res;
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                res.rmat[i][j] = res.cmat[j][i] = (rmat[i] & B.cmat[j]).any();
            }
        }
        return res;
    }

    BSMatrix pow(long long k) const {
        BSMatrix ret, A(*this);
        for (int i = 0; i < N; ++i) {
            ret.rmat[i].set(i);
            ret.cmat[i].set(i);
        }
        while (k > 0) {
            if (k & 1) ret = ret.matmul(A);
            A = A.matmul(A);
            k >>= 1;
        }
        return ret;
    }


private:
    Matrix rmat, cmat;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N, D;
    cin >> N >> D;
    vector<vector<pair<int, int>>> vd(N);
    vector<int> maxd(N);
    vector<int> cumd(N+1);
    rep(i,0,N) {
        int K;
        cin >> K;
        rep(j,0,K) {
            int v, d;
            cin >> v >> d;
            vd[i].push_back({v-1, d});
            chmax(maxd[i], d);
        }
        cumd[i+1] = cumd[i] + maxd[i];
    }
    BooleanSquareMatrix<1010> G;
    rep(i,0,N) {
        rep(j,cumd[i],cumd[i+1]-1) {
            G.set(j, j+1);
        }
        for (auto [v, d] : vd[i]) {
            G.set(cumd[i]+d-1, cumd[v]);
        }
    }
    G = G.pow(D);
    vector<vector<bool>> reach(N, vector<bool>(N));
    vector<int> ans;
    rep(i,0,N) {
        bool ok = true;
        rep(j,0,N) {
            if (!G.get(cumd[i], cumd[j])) {
                ok = false;
                break;
            }
        }
        if (ok) {
            ans.push_back(i);
        }
    }
    int M = ans.size();
    cout << M << endl;
    rep(i,0,M) cout << ans[i]+1 << (i<M-1 ? " " : "\n");
}
accuracy: 0.125
time_ms: 200
memory_kb: 4460
final_rank: 19

code_snippet: // #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

#include <limits>
#include <type_traits>

namespace suisen {
// ! utility
template <typename ...Types>
using constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;
template <bool cond_v, typename Then, typename OrElse>
constexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {
    if constexpr (cond_v) {
        return std::forward<Then>(then);
    } else {
        return std::forward<OrElse>(or_else);
    }
}

// ! function
template <typename ReturnType, typename Callable, typename ...Args>
using is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;
template <typename F, typename T>
using is_uni_op = is_same_as_invoke_result<T, F, T>;
template <typename F, typename T>
using is_bin_op = is_same_as_invoke_result<T, F, T, T>;

template <typename Comparator, typename T>
using is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;

// ! integral
template <typename T, typename = constraints_t<std::is_integral<T>>>
constexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;
template <typename T, unsigned int n>
struct is_nbit { static constexpr bool value = bit_num<T> == n; };
template <typename T, unsigned int n>
static constexpr bool is_nbit_v = is_nbit<T, n>::value;

// ?
template <typename T>
struct safely_multipliable {};
template <>
struct safely_multipliable<int> { using type = long long; };
template <>
struct safely_multipliable<long long> { using type = __int128_t; };
template <>
struct safely_multipliable<unsigned int> { using type = unsigned long long; };
template <>
struct safely_multipliable<unsigned long long> { using type = __uint128_t; };
template <>
struct safely_multipliable<float> { using type = float; };
template <>
struct safely_multipliable<double> { using type = double; };
template <>
struct safely_multipliable<long double> { using type = long double; };
template <typename T>
using safely_multipliable_t = typename safely_multipliable<T>::type;

} // namespace suisen

// ! type aliases
using i128 = __int128_t;
using u128 = __uint128_t;
using ll = long long;
using uint = unsigned int;
using ull  = unsigned long long;

template <typename T> using vec  = std::vector<T>;
template <typename T> using vec2 = vec<vec <T>>;
template <typename T> using vec3 = vec<vec2<T>>;
template <typename T> using vec4 = vec<vec3<T>>;

template <typename T>
using pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <typename T, typename U>
using umap = std::unordered_map<T, U>;

// ! macros (capital: internal macro)
#define OVERLOAD2(_1,_2,name,...) name
#define OVERLOAD3(_1,_2,_3,name,...) name
#define OVERLOAD4(_1,_2,_3,_4,name,...) name

#define REP4(i,l,r,s)  for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))
#define REP3(i,l,r)    REP4(i,l,r,1)
#define REP2(i,n)      REP3(i,0,n)
#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))
#define REPINF2(i,l)   REPINF3(i,l,1)
#define REPINF1(i)     REPINF2(i,0)
#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))
#define RREP3(i,l,r)   RREP4(i,l,r,1)
#define RREP2(i,n)     RREP3(i,0,n)

#define rep(...)    OVERLOAD4(__VA_ARGS__, REP4   , REP3   , REP2   )(__VA_ARGS__)
#define rrep(...)   OVERLOAD4(__VA_ARGS__, RREP4  , RREP3  , RREP2  )(__VA_ARGS__)
#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)

#define CAT_I(a, b) a##b
#define CAT(a, b) CAT_I(a, b)
#define UNIQVAR(tag) CAT(tag, __LINE__)
#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)

#define all(iterable) (iterable).begin(), (iterable).end()
#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)

// ! I/O utilities

// pair
template <typename T, typename U>
std::ostream& operator<<(std::ostream& out, const std::pair<T, U> &a) {
    return out << a.first << ' ' << a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...> &a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return out;
    } else {
        out << std::get<N>(a);
        if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {
            out << ' ';
        }
        return operator<<<N + 1>(out, a);
    }
}
// vector
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T> &a) {
    for (auto it = a.begin(); it != a.end();) {
        out << *it;
        if (++it != a.end()) out << ' ';
    }
    return out;
}
// array
template <typename T, size_t N>
std::ostream& operator<<(std::ostream& out, const std::array<T, N> &a) {
    for (auto it = a.begin(); it != a.end();) {
        out << *it;
        if (++it != a.end()) out << ' ';
    }
    return out;
}
inline void print() { std::cout << '\n'; }
template <typename Head, typename... Tail>
inline void print(const Head &head, const Tail &...tails) {
    std::cout << head;
    if (sizeof...(tails)) std::cout << ' ';
    print(tails...);
}
template <typename Iterable>
auto print_all(const Iterable& v, std::string sep = " ", std::string end = "\n") -> decltype(std::cout << *v.begin(), void()) {
    for (auto it = v.begin(); it != v.end();) {
        std::cout << *it;
        if (++it != v.end()) std::cout << sep;
    }
    std::cout << end;
}

// pair
template <typename T, typename U>
std::istream& operator>>(std::istream& in, std::pair<T, U> &a) {
    return in >> a.first >> a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::istream& operator>>(std::istream& in, std::tuple<Args...> &a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return in;
    } else {
        return operator>><N + 1>(in >> std::get<N>(a), a);
    }
}
// vector
template <typename T>
std::istream& operator>>(std::istream& in, std::vector<T> &a) {
    for (auto it = a.begin(); it != a.end(); ++it) in >> *it;
    return in;
}
// array
template <typename T, size_t N>
std::istream& operator>>(std::istream& in, std::array<T, N> &a) {
    for (auto it = a.begin(); it != a.end(); ++it) in >> *it;
    return in;
}
template <typename ...Args>
void read(Args &...args) {
    ( std::cin >> ... >> args );
}

// ! integral utilities

// Returns pow(-1, n)
template <typename T>
constexpr inline int pow_m1(T n) {
    return -(n & 1) | 1;
}
// Returns pow(-1, n)
template <>
constexpr inline int pow_m1<bool>(bool n) {
    return -int(n) | 1;
}

// Returns floor(x / y)
template <typename T>
constexpr inline T fld(const T x, const T y) {
    return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;
}
template <typename T>
constexpr inline T cld(const T x, const T y) {
    return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;
}

template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcount(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcount(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcountll(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }
template <typename T>
constexpr inline int floor_log2(const T x) { return suisen::bit_num<T> - 1 - count_lz(x); }
template <typename T>
constexpr inline int ceil_log2(const T x)  { return floor_log2(x) + ((x & -x) != x); }
template <typename T>
constexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }
template <typename T>
constexpr inline int parity(const T x) { return popcount(x) & 1; }

struct all_subset {
    struct all_subset_iter {
        const int s; int t;
        constexpr all_subset_iter(int s) : s(s), t(s + 1) {}
        constexpr auto operator*() const { return t; }
        constexpr auto operator++() {}
        constexpr auto operator!=(std::nullptr_t) { return t ? (--t &= s, true) : false; }
    };
    int s;
    constexpr all_subset(int s) : s(s) {}
    constexpr auto begin() { return all_subset_iter(s); }
    constexpr auto end()   { return nullptr; }
};

// ! container

template <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>
auto priqueue_comp(const Comparator comparator) {
    return std::priority_queue<T, std::vector<T>, Comparator>(comparator);
}

template <typename Iterable>
auto isize(const Iterable &iterable) -> decltype(int(iterable.size())) {
    return iterable.size();
}

template <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>
auto generate_vector(int n, Gen generator) {
    std::vector<T> v(n);
    for (int i = 0; i < n; ++i) v[i] = generator(i);
    return v;
}
template <typename T>
auto generate_range_vector(T l, T r) {
    return generate_vector(r - l, [l](int i) { return l + i; });
}
template <typename T>
auto generate_range_vector(T n) {
    return generate_range_vector(0, n);
}

template <typename T>
void sort_unique_erase(std::vector<T> &a) {
    std::sort(a.begin(), a.end());
    a.erase(std::unique(a.begin(), a.end()), a.end());
}

template <typename InputIterator, typename BiConsumer>
auto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) {
    if (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr);
}
template <typename Container, typename BiConsumer>
auto foreach_adjacent_values(Container c, BiConsumer f) -> decltype(c.begin(), c.end(), void()){
    foreach_adjacent_values(c.begin(), c.end(), f);
}

// ! other utilities

// x <- min(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (y >= x) return false;
    x = y;
    return true;
}
// x <- max(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmax(T &x, const T &y) {
    if (y <= x) return false;
    x = y;
    return true;
}

namespace suisen {}
using namespace suisen;
using namespace std;

struct io_setup {
    io_setup(int precision = 20) {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(precision);
    }
} io_setup_ {};

// ! code from here

constexpr int L = 1100;

using BS = bitset<L>;

array<BS, L> mul(const array<BS, L> &A, const array<BS, L> &tB) {
    array<BS, L> C{};
    rep(i, L) rep(j, L) {
        C[i][j] = (A[i] & tB[j]).any();
    }
    return C;
}
 
array<BS, L> t(const array<BS, L> &A) {
    array<BS, L> B{};
    rep(i, L) rep(j, L) {
        B[j][i] = A[i][j];
    }
    return B;
}
 
array<BS, L> pow(array<BS, L> A, array<BS, L> tA, int l) {
    array<BS, L> res{};
    rep(i, L) res[i][i] = true;
 
    for (int b = l; b; b >>= 1) {
        if (b & 1) res = mul(res, tA);
        auto tmp = mul(A, tA);
        tA = mul(tA, A);
        A = move(tmp);
    }
    return res;
}

int main() {
    input(int, n, d);
    vector<vector<int>> g(n);
    rep(u, n) {
        input(int, len);
        if (len == 0) continue;
        vector<int> vs(len);
        vector<int> ds(len);
        rep(j, len) {
            input(int, v, d);
            --v;
            ds[j] = d;
            vs[j] = v;
        }
        int max_len = *max_element(all(ds));

        int siz = g.size();
        g.resize(siz + max_len - 1);
        rep(j, len) {
            if (ds[j] == 1) {
                g[u].push_back(vs[j]);
            } else {
                g[siz + ds[j] - 2].push_back(vs[j]);
            }
        }
        rep(i, max_len - 1) {
            if (i == 0) {
                g[u].push_back(siz);
            } else {
                g[siz + i - 1].push_back(siz + i);
            }
        }
    }

    int siz = g.size();
    array<BS, L> A{};
    rep(i, siz) {
        for (int j : g[i]) {
            A[i][j] = true;
        }
    }
    auto res = pow(A, t(A), d);

    vector<int8_t> ok(n, true);
    rep(i, n) rep(j, n) {
        if (not res[i][j]) {
            ok[j] = false;
        }
    }

    vector<int> ans;
    rep(i, n) {
        if (ok[i]) {
            ans.push_back(i + 1);
        }
    }
    print(ans.size());
    print_all(ans);
    return 0;
}
accuracy: 1
time_ms: 1130
memory_kb: 4584
final_rank: 7

code_snippet: // #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

#include <limits>
#include <type_traits>

namespace suisen {
// ! utility
template <typename ...Types>
using constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;
template <bool cond_v, typename Then, typename OrElse>
constexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {
    if constexpr (cond_v) {
        return std::forward<Then>(then);
    } else {
        return std::forward<OrElse>(or_else);
    }
}

// ! function
template <typename ReturnType, typename Callable, typename ...Args>
using is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;
template <typename F, typename T>
using is_uni_op = is_same_as_invoke_result<T, F, T>;
template <typename F, typename T>
using is_bin_op = is_same_as_invoke_result<T, F, T, T>;

template <typename Comparator, typename T>
using is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;

// ! integral
template <typename T, typename = constraints_t<std::is_integral<T>>>
constexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;
template <typename T, unsigned int n>
struct is_nbit { static constexpr bool value = bit_num<T> == n; };
template <typename T, unsigned int n>
static constexpr bool is_nbit_v = is_nbit<T, n>::value;

// ?
template <typename T>
struct safely_multipliable {};
template <>
struct safely_multipliable<int> { using type = long long; };
template <>
struct safely_multipliable<long long> { using type = __int128_t; };
template <>
struct safely_multipliable<unsigned int> { using type = unsigned long long; };
template <>
struct safely_multipliable<unsigned long long> { using type = __uint128_t; };
template <>
struct safely_multipliable<float> { using type = float; };
template <>
struct safely_multipliable<double> { using type = double; };
template <>
struct safely_multipliable<long double> { using type = long double; };
template <typename T>
using safely_multipliable_t = typename safely_multipliable<T>::type;

} // namespace suisen

// ! type aliases
using i128 = __int128_t;
using u128 = __uint128_t;
using ll = long long;
using uint = unsigned int;
using ull  = unsigned long long;

template <typename T> using vec  = std::vector<T>;
template <typename T> using vec2 = vec<vec <T>>;
template <typename T> using vec3 = vec<vec2<T>>;
template <typename T> using vec4 = vec<vec3<T>>;

template <typename T>
using pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <typename T, typename U>
using umap = std::unordered_map<T, U>;

// ! macros (capital: internal macro)
#define OVERLOAD2(_1,_2,name,...) name
#define OVERLOAD3(_1,_2,_3,name,...) name
#define OVERLOAD4(_1,_2,_3,_4,name,...) name

#define REP4(i,l,r,s)  for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))
#define REP3(i,l,r)    REP4(i,l,r,1)
#define REP2(i,n)      REP3(i,0,n)
#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))
#define REPINF2(i,l)   REPINF3(i,l,1)
#define REPINF1(i)     REPINF2(i,0)
#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))
#define RREP3(i,l,r)   RREP4(i,l,r,1)
#define RREP2(i,n)     RREP3(i,0,n)

#define rep(...)    OVERLOAD4(__VA_ARGS__, REP4   , REP3   , REP2   )(__VA_ARGS__)
#define rrep(...)   OVERLOAD4(__VA_ARGS__, RREP4  , RREP3  , RREP2  )(__VA_ARGS__)
#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)

#define CAT_I(a, b) a##b
#define CAT(a, b) CAT_I(a, b)
#define UNIQVAR(tag) CAT(tag, __LINE__)
#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)

#define all(iterable) (iterable).begin(), (iterable).end()
#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)

// ! I/O utilities

// pair
template <typename T, typename U>
std::ostream& operator<<(std::ostream& out, const std::pair<T, U> &a) {
    return out << a.first << ' ' << a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...> &a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return out;
    } else {
        out << std::get<N>(a);
        if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {
            out << ' ';
        }
        return operator<<<N + 1>(out, a);
    }
}
// vector
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T> &a) {
    for (auto it = a.begin(); it != a.end();) {
        out << *it;
        if (++it != a.end()) out << ' ';
    }
    return out;
}
// array
template <typename T, size_t N>
std::ostream& operator<<(std::ostream& out, const std::array<T, N> &a) {
    for (auto it = a.begin(); it != a.end();) {
        out << *it;
        if (++it != a.end()) out << ' ';
    }
    return out;
}
inline void print() { std::cout << '\n'; }
template <typename Head, typename... Tail>
inline void print(const Head &head, const Tail &...tails) {
    std::cout << head;
    if (sizeof...(tails)) std::cout << ' ';
    print(tails...);
}
template <typename Iterable>
auto print_all(const Iterable& v, std::string sep = " ", std::string end = "\n") -> decltype(std::cout << *v.begin(), void()) {
    for (auto it = v.begin(); it != v.end();) {
        std::cout << *it;
        if (++it != v.end()) std::cout << sep;
    }
    std::cout << end;
}

// pair
template <typename T, typename U>
std::istream& operator>>(std::istream& in, std::pair<T, U> &a) {
    return in >> a.first >> a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::istream& operator>>(std::istream& in, std::tuple<Args...> &a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return in;
    } else {
        return operator>><N + 1>(in >> std::get<N>(a), a);
    }
}
// vector
template <typename T>
std::istream& operator>>(std::istream& in, std::vector<T> &a) {
    for (auto it = a.begin(); it != a.end(); ++it) in >> *it;
    return in;
}
// array
template <typename T, size_t N>
std::istream& operator>>(std::istream& in, std::array<T, N> &a) {
    for (auto it = a.begin(); it != a.end(); ++it) in >> *it;
    return in;
}
template <typename ...Args>
void read(Args &...args) {
    ( std::cin >> ... >> args );
}

// ! integral utilities

// Returns pow(-1, n)
template <typename T>
constexpr inline int pow_m1(T n) {
    return -(n & 1) | 1;
}
// Returns pow(-1, n)
template <>
constexpr inline int pow_m1<bool>(bool n) {
    return -int(n) | 1;
}

// Returns floor(x / y)
template <typename T>
constexpr inline T fld(const T x, const T y) {
    return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;
}
template <typename T>
constexpr inline T cld(const T x, const T y) {
    return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;
}

template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcount(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcount(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int popcount(const T x) { return __builtin_popcountll(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x)   : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }
template <typename T>
constexpr inline int floor_log2(const T x) { return suisen::bit_num<T> - 1 - count_lz(x); }
template <typename T>
constexpr inline int ceil_log2(const T x)  { return floor_log2(x) + ((x & -x) != x); }
template <typename T>
constexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }
template <typename T>
constexpr inline int parity(const T x) { return popcount(x) & 1; }

struct all_subset {
    struct all_subset_iter {
        const int s; int t;
        constexpr all_subset_iter(int s) : s(s), t(s + 1) {}
        constexpr auto operator*() const { return t; }
        constexpr auto operator++() {}
        constexpr auto operator!=(std::nullptr_t) { return t ? (--t &= s, true) : false; }
    };
    int s;
    constexpr all_subset(int s) : s(s) {}
    constexpr auto begin() { return all_subset_iter(s); }
    constexpr auto end()   { return nullptr; }
};

// ! container

template <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>
auto priqueue_comp(const Comparator comparator) {
    return std::priority_queue<T, std::vector<T>, Comparator>(comparator);
}

template <typename Iterable>
auto isize(const Iterable &iterable) -> decltype(int(iterable.size())) {
    return iterable.size();
}

template <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>
auto generate_vector(int n, Gen generator) {
    std::vector<T> v(n);
    for (int i = 0; i < n; ++i) v[i] = generator(i);
    return v;
}
template <typename T>
auto generate_range_vector(T l, T r) {
    return generate_vector(r - l, [l](int i) { return l + i; });
}
template <typename T>
auto generate_range_vector(T n) {
    return generate_range_vector(0, n);
}

template <typename T>
void sort_unique_erase(std::vector<T> &a) {
    std::sort(a.begin(), a.end());
    a.erase(std::unique(a.begin(), a.end()), a.end());
}

template <typename InputIterator, typename BiConsumer>
auto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) {
    if (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr);
}
template <typename Container, typename BiConsumer>
auto foreach_adjacent_values(Container c, BiConsumer f) -> decltype(c.begin(), c.end(), void()){
    foreach_adjacent_values(c.begin(), c.end(), f);
}

// ! other utilities

// x <- min(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (y >= x) return false;
    x = y;
    return true;
}
// x <- max(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmax(T &x, const T &y) {
    if (y <= x) return false;
    x = y;
    return true;
}

namespace suisen {}
using namespace suisen;
using namespace std;

struct io_setup {
    io_setup(int precision = 20) {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(precision);
    }
} io_setup_ {};

// ! code from here

constexpr int L = 1100;

using BS = bitset<L>;

array<BS, L> mul(const array<BS, L> &A, const array<BS, L> &tB) {
    array<BS, L> C{};
    rep(i, L) rep(j, L) {
        C[i][j] = (A[i] & tB[j]).any();
    }
    return C;
}
 
array<BS, L> t(const array<BS, L> &A) {
    array<BS, L> B{};
    rep(i, L) rep(j, L) {
        B[j][i] = A[i][j];
    }
    return B;
}
 
array<BS, L> pow(array<BS, L> A, array<BS, L> tA, int l) {
    array<BS, L> res{};
    rep(i, L) res[i][i] = true;
 
    for (int b = l; b; b >>= 1) {
        if (b & 1) res = mul(res, tA);
        auto tmp = mul(A, tA);
        tA = mul(tA, A);
        A = move(tmp);
    }
    return res;
}

int main() {
    input(int, n, d);
    vector<vector<int>> g(n);
    rep(u, n) {
        input(int, len);
        if (len == 0) continue;
        vector<int> vs(len);
        vector<int> ds(len);
        rep(j, len) {
            input(int, v, d);
            --v;
            ds[j] = d;
            vs[j] = v;
        }
        int max_len = *max_element(all(ds));

        int siz = g.size();
        g.resize(siz + max_len - 1);
        rep(j, len) {
            if (ds[j] == 1) {
                g[u].push_back(vs[j]);
            } else {
                g[siz + ds[j] - 2].push_back(vs[j]);
            }
        }
        rep(i, max_len - 1) {
            if (i == 0) {
                g[u].push_back(siz);
            } else {
                g[siz + i].push_back(siz + i + 1);
            }
        }
    }

    int siz = g.size();
    array<BS, L> A{};
    rep(i, siz) {
        for (int j : g[i]) {
            A[i][j] = true;
        }
    }
    auto res = pow(A, t(A), d);

    vector<int8_t> ok(n, true);
    rep(i, n) rep(j, n) {
        if (not res[i][j]) {
            ok[j] = false;
        }
    }

    vector<int> ans;
    rep(i, n) {
        if (ok[i]) {
            ans.push_back(i + 1);
        }
    }
    print(ans.size());
    print_all(ans);
    return 0;
}
accuracy: 0.075
time_ms: 330
memory_kb: 4452
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using mat = array<bitset<1024>, 1000>;
mat mat_mul(mat A, mat B) {
  mat C;
  for (int i = 0; i < 1000; i++){
    C[i].reset();
  }
  for (int i = 0; i < 1000; i++){
    for (int j = 0; j < 1000; j++){
      if (A[i].test(j)){
        C[i] |= B[j];
      }
    }
  }
  return C;
} 
mat mat_pow(mat a, int L) {
  mat c;
  for (int i = 0; i < 1000; i++){
    c[i].reset();
    c[i][i] = 1;
  }
  while (L) {
    if (L & 1){
      c = mat_mul(a, c);
    }
    L >>= 1;
    if (L){
      a = mat_mul(a, a);
    }
  }
  return c;
}
int main(){
  int N, D;
  cin >> N >> D;
  vector<int> K(N);
  vector<vector<int>> v(N);
  vector<vector<int>> d(N);
  for (int i = 0; i < N; i++){
    cin >> K[i];
    v[i] = vector<int>(K[i]);
    d[i] = vector<int>(K[i]);
    for (int j = 0; j < K[i]; j++){
      cin >> v[i][j] >> d[i][j];
      v[i][j]--;
    }
  }
  vector<int> mx(N, 1);
  for (int i = 0; i < N; i++){
    for (int j = 0; j < K[i]; j++){
      mx[i] = max(mx[i], d[i][j]);
    }
  }
  vector<int> S(N + 1);
  S[0] = 0;
  for (int i = 0; i < N; i++){
    S[i + 1] = S[i] + mx[i];
  }
  mat A;
  for (int i = 0; i < 1000; i++){
    A[i].reset();
  }
  for (int i = 0; i < N; i++){
    for (int j = 0; j < mx[i] - 1; j++){
      A[S[i] + j][S[i] + j + 1] = 1;
    }
  }
  for (int i = 0; i < N; i++){
    for (int j = 0; j < K[i]; j++){
      A[S[i] + d[i][j] - 1][S[v[i][j]]] = 1;
    }
  }
  A = mat_pow(A, D);
  vector<int> f;
  for (int i = 0; i < N; i++){
    bool ok = true;
    for (int j = 0; j < N; j++){
      if (A[S[j]][S[i]] == 0){
        ok = false;
      }
    }
    if (ok){
      f.push_back(i);
    }
  }
  int M = f.size();
  cout << M << endl;
  for (int i = 0; i < M; i++){
    cout << f[i] + 1;
    if (i < M - 1){
      cout << ' ';
    }
  }
  cout << endl;
}
accuracy: 1
time_ms: 240
memory_kb: 4264
final_rank: 2

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;

    void print();
    template <class t> void print(const vector<t> &);
    template <class t, class u> void print(const pair<t, u> &);
    template <class t> void print(const t&);
    template <class Head, class... Tail> void print(const Head&, const Tail&...);

    template <class t> void println(const vector<vector<t>>&);
    template <class t> void println(const vector<t>&);
    template <class t> void println(const t&);
    template <class Head, class... Tail> void println(const Head&, const Tail&...);
    void println();
    void newline();

    void print(){
        return;
    }

    template <class t>
    void print(const vector<t>&x){
        for(const t&i:x)print(i);
    }
    template <class t, class u>
    void print(const pair<t,u>&p){
        print(p.first);
        print(p.second);
    }
    template <class t>
    void print(const t&x){
        if(was_output)cout<<" ";
        cout<<x;
        was_output = true;
    }
    template <class Head, class... Tail>
    void print(const Head&head,const Tail&...tail){
        print(head);
        print(tail...);
    }

    template<class t>
    void println(const vector<vector<t>>&x){
        for(vector<t> i:x)println(i);
    }
    template<class t>
    void println(const vector<t>&x){
        for(const t&i:x)print(i);
        println();
    }
    template <class t>
    void println(const t&x){
        print(x);
        println();
    }
    void println(){
        if(was_output){
            cout << endl;
            was_output = false;
        }
    }
    template <class Head, class... Tail>
    void println(const Head&head,const Tail&...tail){
        print(head);
        println(tail...);
    }

    void newline(){
        was_output = true;
        println();
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }

    class output_initializer{
    public:
        output_initializer(){
            ios::sync_with_stdio(false);
            cin.tie(nullptr);
            cout << setprecision(20);
        }
    };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();
}

using namespace templates;


const int N = 1010;

class bitmatrix{
public:
    vector<bitset<N>> data;
    bitmatrix():data(N){}
    static bitmatrix Identity(){
        bitmatrix res;
        rep(i,N){
            res.data[i][i] = 1;
        }
        return res;
    }
    bitmatrix rotate(){
        bitmatrix res;
        rep(i,N){
            rep(j,N){
                res.data[j][i] = data[i][j];
            }
        }
        return res;
    }
    bitmatrix operator*(bitmatrix that){
        that = that.rotate();
        bitmatrix res;
        rep(i,N){
            rep(j,N){
                res.data[i][j] = (data[i] & that.data[j]).any();
            }
        }
        return res;
    }
    bitmatrix pow(int y){
        bitmatrix res = Identity();
        bitmatrix sum = *this;
        while(y){
            if(y&1)res = res * sum;
            sum = sum * sum;
            y >>= 1;
        }
        return res;
    }
    bitset<N>& operator[](int i){
        return data[i];
    }
};

vector<int> func(){
    int n = in();
    int d = in();
    vvector<pii> edges(n);
    vector<int> adds(n+1,0);
    rep(i,n){
        int k = in();
        chmax(adds[i+1],1);
        rep(_,k){
            int v = in()-1;
            int d = in();
            edges[i].emplace_back(v,d);
            chmax(adds[i+1],d);
        }
    }
    rep(i,n){
        adds[i+1] += adds[i];
    }
    int N = adds.back();
    bitmatrix mat;
    rep(i,n){
        rep(j,adds[i],adds[i+1]-1){
            mat[j][j+1] = 1;
        }
        foreach(e,edges[i]){
            mat[adds[i]+e.second-1][adds[e.first]] = 1;
        }
    }
    mat = mat.pow(d);
    vector<int> res;
    rep(i,n){
        int cnt = 0;
        rep(j,n){
            if(mat[adds[j]][adds[i]])++cnt;
        }
        if(cnt==n)res.emplace_back(i+1);
    }
    return res;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    auto ans = func();
    println(ans.size());
    println(ans);
    return 0;
}
accuracy: 1
time_ms: 630
memory_kb: 3880
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define each(e, v) for (auto &e : v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

template <typename T>
int flg(T x, int i) {
    return (x >> i) & 1;
}

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

template <typename T>
void printn(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << '\n';
}

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <typename T>
vector<int> id_sort(const vector<T> &v, bool greater = false) {
    int n = v.size();
    vector<int> ret(n);
    iota(begin(ret), end(ret), 0);
    sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });
    return ret;
}

template <typename S, typename T>
pair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first + q.first, p.second + q.second);
}

template <typename S, typename T>
pair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first - q.first, p.second - q.second);
}

template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
    S a;
    T b;
    is >> a >> b;
    p = make_pair(a, b);
    return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
    return os << p.first << ' ' << p.second;
}

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const int MOD = 1000000007;
// const int MOD = 998244353;

int main() {
    int N, D;
    cin >> N >> D;

    vector<vector<pii>> es(N), rs(N);
    rep(i, N) {
        int K;
        cin >> K;
        while (K--) {
            int v, d;
            cin >> v >> d;
            v--;
            es[i].eb(v, d), rs[v].eb(i, d);
        }
    }

    vector<vector<bool>> ok(N, vector<bool>(N, false));

    rep(i, N) {
        vector<int> d(N, inf);
        priority_queue<pii> que;
        que.emplace(d[i] = 0, i);
        int M = inf;
        while (!empty(que)) {
            auto [x, p] = que.top();
            que.pop();
            if (x > d[p]) continue;
            each(e, es[p]) {
                if (e.first == i) {
                    chmin(M, d[p] + e.second);
                } else if (chmin(d[e.first], d[p] + e.second)) {
                    que.emplace(d[e.first], e.first); //
                }
            }
        }

        if (M == inf) continue;
        // cout << M << '\n';

        vector<vector<int>> d1(N, vector<int>(M, inf));
        priority_queue<pair<int, pii>> que1;
        que1.emplace(d1[i][0] = 0, pii(i, 0));
        while (!empty(que1)) {
            auto [x, p] = que1.top();
            auto [a, b] = p;
            que1.pop();
            each(e, rs[a]) {
                int na = e.first, nb = (b + e.second) % M;
                if (chmin(d1[na][nb], d1[a][b] + e.second)) {
                    que1.emplace(d1[na][nb], pii(na, nb)); //
                }
            }
        }
        // rep(i, N) print(d1[i]);

        vector<vector<int>> d2(N, vector<int>(M, inf));
        priority_queue<pair<int, pii>> que2;
        que2.emplace(d2[i][0] = 0, pii(i, 0));
        while (!empty(que2)) {
            auto [x, p] = que2.top();
            auto [a, b] = p;
            que2.pop();
            each(e, es[a]) {
                int na = e.first, nb = (b + e.second) % M;
                if (chmin(d2[na][nb], d2[a][b] + e.second)) {
                    que2.emplace(d2[na][nb], pii(na, nb)); //
                }
            }
        }
        // rep(i, N) print(d2[i]);

        // a->i->b
        rep(a, N) {
            rep(b, N) {
                if (ok[a][b]) continue;
                int y = D % M;
                rep(x, M) {
                    // assert((x + y) % M == D % M);
                    if (d1[a][x] + d2[b][y] <= D) {
                        ok[a][b] = true;
                        break;
                    }
                    if (y == 0) y = M;
                    y--;
                }
            }
        }
    }

    vector<int> ans;

    rep(j, N) {
        bool flag = true;
        rep(i, N) {
            if (!ok[i][j]) flag = false;
        }
        if (flag) ans.eb(j);
    }

    cout << sz(ans) << '\n';
    print(ans, 1);
}
accuracy: 1
time_ms: 2730
memory_kb: 3784
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <bitset>
using namespace std;

struct Matrix
{
    bitset<1100> row[1100], col[1100];

    void set(int i, int j){
        row[i].set(j);
        col[j].set(i);
    }
};

Matrix mul(Matrix A, Matrix B){
	Matrix res;
	for(int i = 0; i < 1100; i++){
		for(int j = 0; j < 1100; j++){
			if((A.row[i] & B.col[j]).any()) res.set(i, j);
		}
	}
	return res;
}

Matrix pow(Matrix A, int k){
	Matrix res;
	for(int i = 0; i < 1100; i++) res.set(i, i);
	while(k){
		if(k & 1) res = mul(res, A);
		A = mul(A, A);
		k >>= 1;
	}
	return res;
}

int main()
{
    int n, d;
    cin >> n >> d;
    int r = n;
    Matrix A;
    for(int i = 0; i < n; i++){
        int k;
        cin >> k;
        int v[1002];
        int e[1002];
        int f = 0;
        for(int j = 0; j < k; j++){
            cin >> v[j] >> e[j];
            v[j]--;
            f = max(f, e[j] - 1);
        }
		if(f > 0) A.set(i, r);
        for(int u = r; u < r + f - 1; u++) A.set(u, u + 1);
        for(int j = 0; j < k; j++){
			if(e[j] == 1) A.set(i, v[j]);
            else A.set(r + e[j] - 2, v[j]);
		}
        r += f;
    }
    A = pow(A, d);
    vector<int> ans;
    for(int u = 0; u < n; u++){
        bool f = true;
        for(int v = 0; v < n; v++){
            f &= A.row[v][u];
        }
        if(f) ans.push_back(u);
    }
    cout << ans.size() << endl;
    for(int u : ans) cout << u + 1 << " ";
    cout << endl;
}
accuracy: 1
time_ms: 570
memory_kb: 5296
final_rank: 5