#ifndef HIDDEN_IN_VS // 折りたたみ用 // 警告の抑制 #define _CRT_SECURE_NO_WARNINGS // ライブラリの読み込み #include using namespace std; // 型名の短縮 using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9) using pii = pair; using pll = pair; using pil = pair; using pli = pair; using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; using vb = vector; using vvb = vector; using vvvb = vector; using vc = vector; using vvc = vector; using vvvc = vector; using vd = vector; using vvd = vector; using vvvd = vector; template using priority_queue_rev = priority_queue, greater>; using Graph = vvi; // 定数の定義 const double PI = acos(-1); const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左) const vi DY = { 0, 1, 0, -1 }; int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620; // 入出力高速化 struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp; // 汎用マクロの定義 #define all(a) (a).begin(), (a).end() #define sz(x) ((int)(x).size()) #define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x)) #define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x)) #define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");} #define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順 #define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順 #define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順 #define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能) #define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能) #define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順) #define repis(i, set) for(int i = lsb(set), bset##i = set; i >= 0; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順) #define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順) #define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去 #define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了 #define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定 // 汎用関数の定義 template inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; } template inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す) template inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す) template inline T get(T set, int i) { return (set >> i) & T(1); } template inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod // 演算子オーバーロード template inline istream& operator>>(istream& is, pair& p) { is >> p.first >> p.second; return is; } template inline istream& operator>>(istream& is, vector& v) { repea(x, v) is >> x; return is; } template inline vector& operator--(vector& v) { repea(x, v) --x; return v; } template inline vector& operator++(vector& v) { repea(x, v) ++x; return v; } #endif // 折りたたみ用 #if __has_include() #include using namespace atcoder; #ifdef _MSC_VER #include "localACL.hpp" #endif //using mint = modint1000000007; using mint = modint998244353; //using mint = modint; // mint::set_mod(m); namespace atcoder { inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; } inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; } } using vm = vector; using vvm = vector; using vvvm = vector; using vvvvm = vector; using pim = pair; #endif #ifdef _MSC_VER // 手元環境(Visual Studio) #include "local.hpp" #else // 提出用(gcc) inline int popcount(int n) { return __builtin_popcount(n); } inline int popcount(ll n) { return __builtin_popcountll(n); } inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; } inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; } inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; } inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; } #define dump(...) #define dumpel(v) #define dump_list(v) #define dump_mat(v) #define input_from_file(f) #define output_to_file(f) #endif //【ビット行列】 /* * Bit_matrix(int n, int m) : O(n M / 64) * n×m 零行列で初期化する. * 制約:m ≦ M * * Bit_matrix(int n) : O(n M / 64) * n×n 単位行列で初期化する. * * Bit_matrix(vector> a, int m) : O(n M / 64) * 二次元配列 a[0..n)[0..m) の要素で初期化する. * * Bit_matrix(vi[vl] a, int m) : O(n M / 64) * 数 a[i] の第 j ビットを v[i][j] とする行列で初期化する. * * push_back(bitset col) : O(M / 64) * 最下行に col を追加する. * * A * x : O(n M / 64) * n×m 行列 A と m 次元列ベクトル x の積を返す. * * A * B : O(n m l) * n×m 行列 A と m×l 行列 B の積を返す. * * Bit_matrix pow(ll d) : O(n^3 log d) * 自身を d 乗した行列を返す. * * Bit_matrix transpose() : O(n m) * 自身を転置した行列を返す. * 制約:n ≦ M */ template struct Bit_matrix { int n, m; // 行列のサイズ(n 行 m 列) vector> v; // 行列の成分 // n×m 零行列で初期化する. Bit_matrix(int n, int m) : n(n), m(m), v(n) {} // n×n 単位行列で初期化する. Bit_matrix(int n) : n(n), m(n), v(n) { rep(i, n) v[i][i] = 1; } // 二次元配列 a[0..n)[0..m) の要素で初期化する. Bit_matrix(const vector>& a, int m) : n(sz(a)), m(m), v(a) {} // 数 a[i] の第 j ビットを v[i][j] とする行列で初期化する. Bit_matrix(const vi& a, int m) : n(sz(a)), m(m), v(n) { rep(i, n) v[i] = bitset(a[i]); } Bit_matrix(const vl& a, int m) : n(sz(a)), m(m), v(n) { rep(i, n) v[i] = bitset(a[i]); } Bit_matrix() : m(0), n(0) {} // 代入 Bit_matrix(const Bit_matrix& old) = default; Bit_matrix& operator=(const Bit_matrix& other) = default; // 比較 bool operator==(const Bit_matrix& g) const { return n == g.n && m == g.m && v == g.v; } bool operator!=(const Bit_matrix& g) const { return !(*this == g); } // アクセス inline bitset const& operator[](int i) const { return v[i]; } inline bitset& operator[](int i) { return v[i]; } // 行の追加 void push_back(const bitset& col) { v.push_back(col); n++; } // 行列ベクトル積 bitset operator*(const bitset& x) const { bitset y; rep(i, n) y[i] = (v[i] & x).count() % 2; return y; } // 積 Bit_matrix operator*(const Bit_matrix& b) const { Bit_matrix res(n, b.m); rep(i, res.n) rep(j, res.m) rep(k, m) res[i][j] = res[i][j] ^ (v[i][k] & b[k][j]); return res; } Bit_matrix& operator*=(const Bit_matrix& b) { *this = *this * b; return *this; } // 累乗 Bit_matrix pow(ll d) const { // verify : https://atcoder.jp/contests/jag2013summer-day4/tasks/icpc2013summer_day4_f Bit_matrix res(n), pow2 = *this; while (d > 0) { if (d & 1) res *= pow2; pow2 *= pow2; d /= 2; } return res; } // 転置(A^T) Bit_matrix transpose() const { Bit_matrix res(m, n); rep(i, m) rep(j, n) res[i][j] = v[j][i]; return res; } #ifdef _MSC_VER friend ostream& operator<<(ostream& os, const Bit_matrix& a) { rep(i, a.n) { os << "["; rep(j, a.m) os << a[i][j] << (j < a.m - 1 ? " " : "]"); if (i < a.n - 1) os << "\n"; } return os; } #endif }; //【線形方程式】O(n m min(n, m) / 64) /* * 与えられた n×m 行列 A と n 次元ベクトル b に対し, * 線形方程式 A x = b の特殊解 x0(m 次元ベクトル)を格納する(なければ false を返す) * また同次形 A x = 0 の解空間の基底(m 次元ベクトル)のリストを xs に格納する. */ template bool gauss_jordan_elimination(const Bit_matrix& A, const vb& b, bitset* x0 = nullptr, vector>* xs = nullptr) { // verify : https://atcoder.jp/contests/arc173/tasks/arc173_e int n = A.n, m = A.m; // v : 拡大係数行列 (A | b) vector> v(n); rep(i, n) rep(j, m) v[i][j] = A[i][j]; rep(i, n) v[i][m] = b[i]; // pivots[i] : 第 i 行のピボットが第何列にあるか vi pivots; // 注目位置を v[i][j] とする. int i = 0, j = 0; while (i < n && j <= m) { // 注目列の下方の行から 1 を見つける. int i2 = i; while (i2 < n && !v[i2][j]) i2++; // 見つからなかったら注目位置を右に移す. if (i2 == n) { j++; continue; } // 見つかったら第 i 行とその行を入れ替える. if (i != i2) swap(v[i], v[i2]); // v[i][j] をピボットに選択する. pivots.push_back(j); // 第 i 行以外の第 j 列の成分が全て 0 になるよう第 i 行を XOR する. rep(i2, n) if (v[i2][j] && i2 != i) v[i2] ^= v[i]; // 注目位置を右下に移す. i++; j++; } // 最後に見つかったピボットの位置が第 m 列ならば解なし. if (pivots.back() == m) return false; // A x = b の特殊解 x0 の構成(任意定数は全て 0 にする) if (x0 != nullptr) { x0->reset(); int rnk = sz(pivots); rep(i, rnk) (*x0)[pivots[i]] = v[i][m]; // 同次形 A x = 0 の一般解 {x} の基底の構成(任意定数を 1-hot にする) if (xs != nullptr) { xs->clear(); int i = 0; rep(j, m) { if (i < rnk && j == pivots[i]) { i++; continue; } bitset x; x[j] = 1; rep(i2, i) x[pivots[i2]] = v[i2][j]; xs->emplace_back(move(x)); } } } return true; } void WA() { auto start = chrono::system_clock::now(); int n, k; cin >> n >> k; vi p(n), a(n); cin >> p >> a; --p; constexpr int M = 2000; // constexpr int M = 20; Bit_matrix mat(n, 2 * n); vb vec(n); rep(i, n) { mat[i][i] = 1; mat[i][p[i]] = 1; mat[i][n + i] = 1; vec[i] = a[i]; } bitset x; vector> xs; bool ex = gauss_jordan_elimination(mat, vec, &x, &xs); if (!ex) EXIT("No"); dump(x); dumpel(xs); int D = sz(xs); mt19937_64 mt((int)time(NULL)); uniform_int_distribution rnd(0, D - 1); uniform_real_distribution rndf(0.f, 1.f); double temp = 1; while (1) { int pc = (int)x.count(); dump(pc); if (pc == k) EXIT("Yes"); int d = rnd(mt); auto nx = x ^ xs[d]; int npc = (int)nx.count(); int sc_dif = abs(pc - k) - abs(npc - k); dump(sc_dif, temp, exp(sc_dif / temp)); if (sc_dif || rndf(mt) < exp(sc_dif / temp)) { x = move(nx); } auto now = chrono::system_clock::now(); auto msec = chrono::duration_cast(now - start).count(); if (msec >= 1950) break; } EXIT("No"); } int x[2000]; int main() { // input_from_file("input.txt"); // output_to_file("output.txt"); auto start = chrono::system_clock::now(); int n, k; cin >> n >> k; vi p(n), a(n); cin >> p >> a; --p; vvi v_to_e(n); rep(i, n) v_to_e[i].push_back(n + i); rep(i, n) v_to_e[p[i]].push_back(n + i); mt19937_64 mt((int)time(NULL)); uniform_int_distribution rnd(0, n - 1); uniform_real_distribution rndf(0.f, 1.f); // 初期値 x[0..n) = 0 if (k < n) { dump("k < n"); int pc = accumulate(all(a), 0); rep(i, n) x[n + i] = a[i]; dump(pc); // 貪欲でちょっとマシな初期値に修正する. vi sc(2 * n, -1); priority_queue q; rep(v, n) { repe(e, v_to_e[v]) sc[v] += (2 * x[e] - 1); q.push({ sc[v], v }); } while (!q.empty()) { if (pc <= k) break; auto [s, v] = q.top(); q.pop(); if (s != sc[v]) continue; if (s <= 0) break; pc -= s; x[v] ^= 1; repe(e, v_to_e[v]) { sc[e - n] -= 2 * x[e - n] - 1; sc[p[e - n]] -= 2 * x[p[e - n]] - 1; q.push({ sc[e - n] + sc[p[e - n]] - sc[v], e - n + p[e - n] - v }); x[e] ^= 1; } } dump(pc); int ini_temp = abs(pc - k); while (1) { auto now = chrono::system_clock::now(); auto msec = chrono::duration_cast(now - start).count(); if (msec >= 1980) break; // 温度は,n で始まり,2000 msec 後に 0.1 で終わる. // double temp = n + (0.1 - n) * msec / 2000.; double temp = 0.1; // 温度設定よくわかんないので定数にする. dump("----"); // rep(i, 2 * n) cout << x[i] << " \n"[i == 2 * n - 1]; dump(pc); if (pc == k) EXIT("Yes"); int v = rnd(mt); int npc = pc - (2 * x[v] - 1); x[v] ^= 1; repe(e, v_to_e[v]) { npc -= 2 * x[e] - 1; x[e] ^= 1; } int sc_dif = abs(pc - k) - abs(npc - k); dump(sc_dif, temp, exp(sc_dif / temp)); if (sc_dif > 0 || rndf(mt) < exp(sc_dif / temp)) { pc = npc; } else { x[v] ^= 1; repe(e, v_to_e[v]) { x[e] ^= 1; } } } } // 初期値 x[0..n) = 1 else { dump("k >= n"); int pc = n + accumulate(all(a), 0); rep(i, n) { x[i] = 1; x[n + i] = a[i]; } // rep(i, 2 * n) cout << x[i] << " \n"[i == 2 * n - 1]; dump(pc); // 貪欲でちょっとマシな初期値に修正する. vi sc(2 * n, -1); priority_queue q; rep(v, n) { repe(e, v_to_e[v]) sc[v] -= (2 * x[e] - 1); q.push({ sc[v], v }); } dump(q); while (!q.empty()) { if (pc >= k) break; auto [s, v] = q.top(); q.pop(); if (s != sc[v]) continue; if (s <= 0) break; pc += s; x[v] ^= 1; repe(e, v_to_e[v]) { sc[e - n] += 2 * x[e - n] - 1; sc[p[e - n]] += 2 * x[p[e - n]] - 1; q.push({ sc[e - n] + sc[p[e - n]] - sc[v], e - n + p[e - n] - v }); x[e] ^= 1; } } // rep(i, 2 * n) cout << x[i] << " \n"[i == 2 * n - 1]; dump(pc); int ini_temp = abs(pc - k); while (1) { auto now = chrono::system_clock::now(); auto msec = chrono::duration_cast(now - start).count(); if (msec >= 1980) break; // 温度は,ini_temp で始まり,2000 msec 後に 0.1 で終わる. // double temp = ini_temp + (0.1 - ini_temp) * msec / 2000.; double temp = 0.1; dump("----"); // rep(i, 2 * n) cout << x[i] << " \n"[i == 2 * n - 1]; dump(pc); if (pc == k) EXIT("Yes"); int v = rnd(mt); int npc = pc - (2 * x[v] - 1); x[v] ^= 1; repe(e, v_to_e[v]) { npc -= 2 * x[e] - 1; x[e] ^= 1; } int sc_dif = abs(pc - k) - abs(npc - k); dump(sc_dif, temp, exp(sc_dif / temp)); if (sc_dif > 0 || rndf(mt) < exp(sc_dif / temp)) { pc = npc; } else { x[v] ^= 1; repe(e, v_to_e[v]) { x[e] ^= 1; } } } } EXIT("No"); }