problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p02901
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; using LL = long long; using P = pair<int, int>; const int Max_N = 15; int c[Max_N][Max_N]; const int INF = 1e9; int main() { int N, M; cin >> N >> M; vector<int> a(M), b(M); rep(i, M) { cin >> a[i] >> b[i]; rep(j, b[i]) cin >> c[i][j]; rep(j, b[i]) c[i][j]--; } vector<int> dp(1 << N, INF); dp[0] = 0; rep(bit, 1 << N) { rep(i, M) { bool rev = true; rep(j, b[i]) { if (!(bit & 1 << c[i][j])) { rev = false; break; } } if (!rev) continue; rep(j, 1 << b[i]) { int bit_prev = bit; rep(k, b[i]) if (j & 1 << k) bit_prev -= 1 << c[i][k]; dp[bit] = min(dp[bit], dp[bit_prev] + a[i]); } } } if (dp[(1 << N) - 1] == INF) cout << -1 << endl; else cout << dp[(1 << N) - 1] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; using LL = long long; using P = pair<int, int>; const int Max_N = 15; const int Max_M = 1e3 + 5; int c[Max_M][Max_N]; const int INF = 1e9; int main() { int N, M; cin >> N >> M; vector<int> a(M), b(M); rep(i, M) { cin >> a[i] >> b[i]; rep(j, b[i]) cin >> c[i][j]; rep(j, b[i]) c[i][j]--; } vector<int> dp(1 << N, INF); dp[0] = 0; rep(bit, 1 << N) { rep(i, M) { bool rev = true; rep(j, b[i]) { if (!(bit & 1 << c[i][j])) { rev = false; break; } } if (!rev) continue; rep(j, 1 << b[i]) { int bit_prev = bit; rep(k, b[i]) if (j & 1 << k) bit_prev -= 1 << c[i][k]; dp[bit] = min(dp[bit], dp[bit_prev] + a[i]); } } } if (dp[(1 << N) - 1] == INF) cout << -1 << endl; else cout << dp[(1 << N) - 1] << endl; return 0; }
replace
7
8
7
9
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int dp[10000]; void chmin(int &a, int b) { a = a > b ? b : a; } int main() { int N, M, ans = 0; long Inf = 1e9; cin >> N >> M; long a[M], b[M], d[M]; long c[M][12]; rep(i, M) d[i] = 0; rep(i, 10000) dp[i] = Inf; dp[0] = 0; rep(i, M) { cin >> a[i] >> b[i]; rep(j, b[i]) cin >> c[i][j]; rep(j, b[i]) d[i] += pow(2, c[i][j]); } rep(j, M) rep(i, 10000) { chmin(dp[i | d[j]], dp[i] + a[j]); } for (int i = 1; i <= N; i++) ans += pow(2, i); if (dp[ans] == Inf) cout << "-1" << endl; else cout << dp[ans] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int dp[10000]; void chmin(int &a, int b) { a = a > b ? b : a; } int main() { int N, M, ans = 0; long Inf = 1e9; cin >> N >> M; long a[M], b[M], d[M]; long c[M][12]; rep(i, M) d[i] = 0; rep(i, 10000) dp[i] = Inf; dp[0] = 0; rep(i, M) { cin >> a[i] >> b[i]; rep(j, b[i]) cin >> c[i][j]; rep(j, b[i]) d[i] += pow(2, c[i][j]); } rep(j, M) rep(i, 8192) { chmin(dp[i | d[j]], dp[i] + a[j]); } for (int i = 1; i <= N; i++) ans += pow(2, i); if (dp[ans] == Inf) cout << "-1" << endl; else cout << dp[ans] << endl; return 0; }
replace
19
20
19
20
0
p02901
C++
Time Limit Exceeded
#include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; #define llMAX numeric_limits<long long>::max() #define intMAX numeric_limits<int>::max() #define d_5 100000 #define d9_7 1000000007 #define vll vector<vector<long long>> #define vl vector<long long> #define vi vector<int> #define vii vector<vector<int>> #define pb push_back #define pf push_front int main(void) { int n, m; cin >> n >> m; vi a(m); vi b(m); vii c(m); rep(i, m) { cin >> a[i] >> b[i]; c[i] = vi(b[i]); rep(j, b[i]) { cin >> c[i][j]; c[i][j]--; } } vi dp((int)pow(2, n), 7000000); dp[0] = 0; rep(j, m) rep(i, (int)pow(2, n)) { int next = 0; rep(k, b[j]) { if ((i / ((int)pow(2, c[j][k]))) % 2 == 0) { next += (int)pow(2, c[j][k]); } } dp[i + next] = min(dp[i + next], dp[i] + a[j]); } /*rep(i,(int)pow(2,n))cout<<dp[i]<<" "; cout<<endl;*/ if (dp[(int)pow(2, n) - 1] == 7000000) { cout << -1; } else { cout << dp[(int)pow(2, n) - 1]; } return 0; }
#include "bits/stdc++.h" #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; using namespace std; #define llMAX numeric_limits<long long>::max() #define intMAX numeric_limits<int>::max() #define d_5 100000 #define d9_7 1000000007 #define vll vector<vector<long long>> #define vl vector<long long> #define vi vector<int> #define vii vector<vector<int>> #define pb push_back #define pf push_front int main(void) { int n, m; cin >> n >> m; vi a(m); vi b(m); vii c(m); rep(i, m) { cin >> a[i] >> b[i]; c[i] = vi(b[i]); rep(j, b[i]) { cin >> c[i][j]; c[i][j]--; } } vi dp((int)pow(2, n), 7000000); dp[0] = 0; rep(j, m) rep(i, (int)pow(2, n)) { int next = 0; if (dp[i] == 7000000) continue; rep(k, b[j]) { if ((i / ((int)pow(2, c[j][k]))) % 2 == 0) { next += (int)pow(2, c[j][k]); } } dp[i + next] = min(dp[i + next], dp[i] + a[j]); } /*rep(i,(int)pow(2,n))cout<<dp[i]<<" "; cout<<endl;*/ if (dp[(int)pow(2, n) - 1] == 7000000) { cout << -1; } else { cout << dp[(int)pow(2, n) - 1]; } return 0; }
insert
32
32
32
34
TLE
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 12, M = 1010; int dp[1 << N]; int mask[M], cost[M]; int n, m; int rec(int mask_) { if (__builtin_popcount(mask_) == n) { return 0; } if (dp[mask_] != -1) return dp[mask_]; int res = int(1e9); for (int i = 0; i < m; ++i) { if ((mask_ & mask[i]) == mask[i]) continue; res = min(res, cost[i] + rec(mask_ | mask[i])); } return dp[mask_] = res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; memset(dp, -1, sizeof dp); for (int i = 0; i < m; ++i) { mask[i] = 0; int x; cin >> cost[i] >> x; for (int j = 0; j < x; ++j) { int y; cin >> y; mask[i] |= (1 << y); } } int res = rec(0); cout << (res == int(1e9) ? -1 : res) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 13, M = 1010; int dp[1 << N]; int mask[M], cost[M]; int n, m; int rec(int mask_) { if (__builtin_popcount(mask_) == n) { return 0; } if (dp[mask_] != -1) return dp[mask_]; int res = int(1e9); for (int i = 0; i < m; ++i) { if ((mask_ & mask[i]) == mask[i]) continue; res = min(res, cost[i] + rec(mask_ | mask[i])); } return dp[mask_] = res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; memset(dp, -1, sizeof dp); for (int i = 0; i < m; ++i) { mask[i] = 0; int x; cin >> cost[i] >> x; for (int j = 0; j < x; ++j) { int y; cin >> y; mask[i] |= (1 << y); } } int res = rec(0); cout << (res == int(1e9) ? -1 : res) << '\n'; return 0; }
replace
4
5
4
5
0
p02901
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a(pow(2, n), 10000000); for (int i = 0; i < m; i++) { int c, d; cin >> c >> d; int n = 0; for (int j = 0; j < d; j++) { int k; cin >> k; n |= (1 << k - 1); } a.at(n) = min(a.at(n), c); } for (int h = 0; h < 12; h++) { for (int i = 0; i < pow(2, n); i++) { for (int j = i + 1; j < pow(2, n); j++) { a.at(i | j) = min(a.at(i | j), a.at(i) + a.at(j)); } } } int ans = a.at(pow(2, n) - 1); if (ans == 10000000) ans = -1; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a(pow(2, n), 10000000); for (int i = 0; i < m; i++) { int c, d; cin >> c >> d; int n = 0; for (int j = 0; j < d; j++) { int k; cin >> k; n |= (1 << k - 1); } a.at(n) = min(a.at(n), c); } for (int h = 1; h < 12; ++h) { for (int i = 0; i < pow(2, n); ++i) { if (__builtin_popcount(i) == h) { for (int j = 0; j < pow(2, n); ++j) { a.at(i | j) = min(a.at(i | j), a.at(i) + a.at(j)); } } } } int ans = a.at(pow(2, n) - 1); if (ans == 10000000) ans = -1; cout << ans << endl; }
replace
18
22
18
26
TLE
p02901
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <tuple> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; const int maxn = (int)1e5 + 1; const int inf = (int)1e9 + 7; int m, b[1111], u[1111], p[1111], a[1111]; int c[13][13]; int cnt[maxn]; string s, t; int ans = 0, sum1, sum2, sum3; ll n, k; int dp[(1 << 13)]; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a[i] >> b[i]; for (int j = 1; j <= b[i]; j++) { cin >> c[i][j]; } } fill(dp, dp + (1 << 12), inf); dp[0] = 0; for (int mask = 1; mask < (1 << n); mask++) { for (int i = 1; i <= m; i++) { int nmask = mask; for (int j = 1; j <= b[i]; j++) { if ((nmask >> (c[i][j] - 1)) & 1) { nmask &= ~(1 << c[i][j] - 1); } } /*for(int j = 0; j <= nmask; j++){ cout << ((nmask >> j) % 2); }*/ dp[mask] = min(dp[mask], dp[nmask] + a[i]); } // cout << dp[mask] << endl; } if (dp[(1 << n) - 1] == inf) { cout << -1; return 0; } cout << dp[(1 << n) - 1]; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <tuple> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; const int maxn = (int)1e5 + 1; const int inf = (int)1e9 + 7; int m, b[1111], u[1111], p[1111], a[1111]; int c[1111][13]; int cnt[maxn]; string s, t; int ans = 0, sum1, sum2, sum3; ll n, k; int dp[(1 << 13)]; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a[i] >> b[i]; for (int j = 1; j <= b[i]; j++) { cin >> c[i][j]; } } fill(dp, dp + (1 << 12), inf); dp[0] = 0; for (int mask = 1; mask < (1 << n); mask++) { for (int i = 1; i <= m; i++) { int nmask = mask; for (int j = 1; j <= b[i]; j++) { if ((nmask >> (c[i][j] - 1)) & 1) { nmask &= ~(1 << c[i][j] - 1); } } /*for(int j = 0; j <= nmask; j++){ cout << ((nmask >> j) % 2); }*/ dp[mask] = min(dp[mask], dp[nmask] + a[i]); } // cout << dp[mask] << endl; } if (dp[(1 << n) - 1] == inf) { cout << -1; return 0; } cout << dp[(1 << n) - 1]; return 0; }
replace
37
38
37
38
0
p02901
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <numeric> #include <queue> #include <sstream> #include <string.h> #include <string> #include <tuple> #include <vector> #define REP(i, x) for (int i{0}; i < (int)(x); i++) #define REPC(i, x) for (int i{0}; i <= (int)(x); i++) #define RREP(i, x) for (int i{(int)(x)-1}; i >= 0; i--) #define RREPC(i, x) for (int i{(int)(x)}; i >= 0; i--) #define REP1O(i, x) for (int i{1}; i < (int)(x); i++) #define REP1C(i, x) for (int i{1}; i <= (int)(x); i++) #define REPIT(i, x) for (auto i{(x).begin()}; i != (x).end(); i++) #define PB push_back #define MP make_pair #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() using namespace std; typedef int64_t ll; typedef double dbl; typedef vector<bool> Vb; typedef vector<char> Vc; typedef vector<double> Vd; typedef vector<int> Vi; typedef vector<ll> Vl; typedef vector<string> Vs; typedef vector<vector<int>> VVi; typedef vector<vector<ll>> VVl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } inline void swap(ll &a, ll &b) { a ^= b; b ^= a; a ^= b; } inline void swap(int &a, int &b) { a ^= b; b ^= a; a ^= b; } inline void YES() { cout << "YES" << endl; } inline void Yes() { cout << "Yes" << endl; } inline void NO() { cout << "NO" << endl; } inline void No() { cout << "No" << endl; } const int inf = 2e8; const ll linf = 1LL << 60; const int MOD = 1000000007; int n, m; Vi va, open; void init() { cin >> n >> m; va.resize(m); open.resize(m); REP(i, m) { int a, b; cin >> a >> b; va[i] = a; int open_mask = 0; REP(_i, b) { int chest; cin >> chest; open_mask |= 1 << chest - 1; } open[i] = open_mask; } } int main() { init(); Vi dp(1 << n, inf); dp[0] = 0; REP(i, m) chmin(dp[open[i]], va[i]); REP(target, 1 << n) { REP(i, target - 1) for (int j = i + 1; j < target; ++j) { if ((i | j) == target) chmin(dp[target], dp[i] + dp[j]); } } if (dp[(1 << n) - 1] == inf) cout << -1 << endl; else cout << dp[(1 << n) - 1] << endl; }
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <numeric> #include <queue> #include <sstream> #include <string.h> #include <string> #include <tuple> #include <vector> #define REP(i, x) for (int i{0}; i < (int)(x); i++) #define REPC(i, x) for (int i{0}; i <= (int)(x); i++) #define RREP(i, x) for (int i{(int)(x)-1}; i >= 0; i--) #define RREPC(i, x) for (int i{(int)(x)}; i >= 0; i--) #define REP1O(i, x) for (int i{1}; i < (int)(x); i++) #define REP1C(i, x) for (int i{1}; i <= (int)(x); i++) #define REPIT(i, x) for (auto i{(x).begin()}; i != (x).end(); i++) #define PB push_back #define MP make_pair #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() using namespace std; typedef int64_t ll; typedef double dbl; typedef vector<bool> Vb; typedef vector<char> Vc; typedef vector<double> Vd; typedef vector<int> Vi; typedef vector<ll> Vl; typedef vector<string> Vs; typedef vector<vector<int>> VVi; typedef vector<vector<ll>> VVl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } inline void swap(ll &a, ll &b) { a ^= b; b ^= a; a ^= b; } inline void swap(int &a, int &b) { a ^= b; b ^= a; a ^= b; } inline void YES() { cout << "YES" << endl; } inline void Yes() { cout << "Yes" << endl; } inline void NO() { cout << "NO" << endl; } inline void No() { cout << "No" << endl; } const int inf = 2e8; const ll linf = 1LL << 60; const int MOD = 1000000007; int n, m; Vi va, open; void init() { cin >> n >> m; va.resize(m); open.resize(m); REP(i, m) { int a, b; cin >> a >> b; va[i] = a; int open_mask = 0; REP(_i, b) { int chest; cin >> chest; open_mask |= 1 << chest - 1; } open[i] = open_mask; } } int main() { init(); Vi dp(1 << n, inf); dp[0] = 0; REP(i, m) chmin(dp[open[i]], va[i]); int target = (1 << n) - 1; REP(i, target) { for (int j = i + 1; j <= target; ++j) chmin(dp[i | j], dp[i] + dp[j]); } if (dp[(1 << n) - 1] == inf) cout << -1 << endl; else cout << dp[(1 << n) - 1] << endl; }
replace
110
116
110
115
TLE
p02901
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <set> #include <stdlib.h> #include <string> #include <vector> #define ll long long #define repp(i, a, b) for (int i = (int)a; i < (int)b; ++i) using namespace std; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } int main() { int N, M; cin >> N >> M; vector<long long> A(M); vector<vector<int>> C(M); repp(i, 0, M) { int b; cin >> A[i] >> b; repp(j, 0, b) { int c; cin >> c; C[i].push_back(c); } } const long long INF = 1LL << 32; long long dp[110][1 << 12]; Fill(dp, INF); dp[0][0] = 0; repp(i, 0, M) { int bit = 0; repp(j, 0, (int)C[i].size()) bit |= (1 << (C[i][j] - 1)); repp(j, 0, 1 << N) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); repp(j, 0, 1 << N) dp[i + 1][bit | j] = min(dp[i + 1][bit | j], dp[i][j] + A[i]); } long long result = dp[M][(1 << N) - 1] == INF ? -1 : dp[M][(1 << N) - 1]; cout << result << endl; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <set> #include <stdlib.h> #include <string> #include <vector> #define ll long long #define repp(i, a, b) for (int i = (int)a; i < (int)b; ++i) using namespace std; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } int main() { int N, M; cin >> N >> M; vector<long long> A(M); vector<vector<int>> C(M); repp(i, 0, M) { int b; cin >> A[i] >> b; repp(j, 0, b) { int c; cin >> c; C[i].push_back(c); } } const long long INF = 1LL << 32; long long dp[1010][1 << 12]; Fill(dp, INF); dp[0][0] = 0; repp(i, 0, M) { int bit = 0; repp(j, 0, (int)C[i].size()) bit |= (1 << (C[i][j] - 1)); repp(j, 0, 1 << N) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); repp(j, 0, 1 << N) dp[i + 1][bit | j] = min(dp[i + 1][bit | j], dp[i][j] + A[i]); } long long result = dp[M][(1 << N) - 1] == INF ? -1 : dp[M][(1 << N) - 1]; cout << result << endl; }
replace
37
38
37
38
0
p02901
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> #define INF 1000000000 #define MOD 1000000007 #define ALL(x) std::begin(x), std::end(x) int N, M, a[1111], b[1111], c, d[1111], T; long long memo[22][4444]; long long dfs(int i, int j) { if (j == T) return 0; if (i == M) return INF; if (memo[i][j] < 0x3f3f3f3f3f3f3f3fLL) return memo[i][j]; long long x = dfs(i + 1, j | d[i]) + a[i]; long long y = dfs(i + 1, j); return memo[i][j] = std::min(x, y); } int main(int argc, char **argv) { std::cin.tie(0); std::ios_base::sync_with_stdio(0); std::cout << std::fixed << std::setprecision(6); std::cerr << std::fixed << std::setprecision(6); std::cin >> N >> M; for (int i = 0; i < M; i++) { std::cin >> a[i] >> b[i]; d[i] = 0; for (int j = 0; j < b[i]; j++) { std::cin >> c; c--; d[i] |= 1 << c; } } T = (1 << N) - 1; memset(memo, 0x3f, sizeof(memo)); long long x = dfs(0, 0); std::cout << (x < INF ? x : -1) << std::endl; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> #define INF 1000000000 #define MOD 1000000007 #define ALL(x) std::begin(x), std::end(x) int N, M, a[1111], b[1111], c, d[1111], T; long long memo[1111][4444]; long long dfs(int i, int j) { if (j == T) return 0; if (i == M) return INF; if (memo[i][j] < 0x3f3f3f3f3f3f3f3fLL) return memo[i][j]; long long x = dfs(i + 1, j | d[i]) + a[i]; long long y = dfs(i + 1, j); return memo[i][j] = std::min(x, y); } int main(int argc, char **argv) { std::cin.tie(0); std::ios_base::sync_with_stdio(0); std::cout << std::fixed << std::setprecision(6); std::cerr << std::fixed << std::setprecision(6); std::cin >> N >> M; for (int i = 0; i < M; i++) { std::cin >> a[i] >> b[i]; d[i] = 0; for (int j = 0; j < b[i]; j++) { std::cin >> c; c--; d[i] |= 1 << c; } } T = (1 << N) - 1; memset(memo, 0x3f, sizeof(memo)); long long x = dfs(0, 0); std::cout << (x < INF ? x : -1) << std::endl; return 0; }
replace
27
28
27
28
0
p02901
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <stack> #include <vector> using namespace std; typedef long long lli; const int INF = 1e9 + 19; const int maxn = 12; const int maxm = 1005; int n, m; int c[maxn], x[maxm]; int dp[(1 << maxn) + 5]; void ReadInput() { cin >> n >> m; for (int i = 0; i < m; ++i) { int k; cin >> c[i] >> k; x[i] = 0; for (; k > 0; --k) { int a; cin >> a; --a; x[i] |= (1 << a); } } } void Solve() { fill(begin(dp), end(dp), INF); dp[0] = 0; for (int mask = 0; mask < (1 << n); ++mask) { if (dp[mask] == INF) continue; for (int i = 0; i < m; ++i) { int nmask = mask | x[i]; dp[nmask] = min(dp[nmask], dp[mask] + c[i]); } } if (dp[(1 << n) - 1] == INF) cout << "-1\n"; else cout << dp[(1 << n) - 1] << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ReadInput(); Solve(); }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <stack> #include <vector> using namespace std; typedef long long lli; const int INF = 1e9 + 19; const int maxn = 12; const int maxm = 1005; int n, m; int c[maxm], x[maxm]; int dp[(1 << maxn) + 5]; void ReadInput() { cin >> n >> m; for (int i = 0; i < m; ++i) { int k; cin >> c[i] >> k; x[i] = 0; for (; k > 0; --k) { int a; cin >> a; --a; x[i] |= (1 << a); } } } void Solve() { fill(begin(dp), end(dp), INF); dp[0] = 0; for (int mask = 0; mask < (1 << n); ++mask) { if (dp[mask] == INF) continue; for (int i = 0; i < m; ++i) { int nmask = mask | x[i]; dp[nmask] = min(dp[nmask], dp[mask] + c[i]); } } if (dp[(1 << n) - 1] == INF) cout << "-1\n"; else cout << dp[(1 << n) - 1] << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ReadInput(); Solve(); }
replace
13
14
13
14
0
p02901
C++
Runtime Error
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <chrono> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<double> vd; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<ld> vld; typedef vector<vector<int>> vvi; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<ld>> vvd; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pll> vpll; #ifdef OLBOEB mt19937 rnd(228 + 1488 + 238 + 24111997 % 322); #else mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); #endif void print_time() { #ifdef OLBOEB cout.flush(); cerr << "\ntime: " << clock() * 1.0 / CLOCKS_PER_SEC << endl; #endif } #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template <class T1, class T2> ostream &operator<<(ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; } template <class T> ostream &operator<<(ostream &out, vector<T> vec) { out << "("; for (auto &v : vec) out << v << ", "; return out << ")"; } template <class T> ostream &operator<<(ostream &out, set<T> vec) { out << "("; for (auto &v : vec) out << v << ", "; return out << ")"; } template <class L, class R> ostream &operator<<(ostream &out, map<L, R> vec) { out << "("; for (auto &v : vec) out << v << ", "; return out << ")"; } const ll MAXN = 12; const ll MASK = (1 << MAXN); const ll INF = 1e12; void upd(ll &x, ll y) { x = min(x, y); } int main() { cout << fixed << setprecision(30); ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); ll n, m; cin >> n >> m; vl costs(m); vl masks(m); for (ll i = 0; i < m; ++i) { ll cnt; cin >> costs[i] >> cnt; for (ll j = 0; j < cnt; ++j) { ll bit; cin >> bit; --bit; masks[i] |= (1LL << bit); } } vvl dp(n + 1, vl(MASK + 10, +INF)); dp[0][0] = 0; for (ll i = 0; i < m; ++i) { for (ll mask = 0; mask < MASK; ++mask) { upd(dp[i + 1][mask], dp[i][mask]); upd(dp[i + 1][mask | masks[i]], dp[i][mask] + costs[i]); } } ll ans = dp[m][(1 << n) - 1]; cout << (ans == INF ? -1 : ans) << endl; print_time(); return 0; }
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <chrono> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<double> vd; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<ld> vld; typedef vector<vector<int>> vvi; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<ld>> vvd; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pll> vpll; #ifdef OLBOEB mt19937 rnd(228 + 1488 + 238 + 24111997 % 322); #else mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); #endif void print_time() { #ifdef OLBOEB cout.flush(); cerr << "\ntime: " << clock() * 1.0 / CLOCKS_PER_SEC << endl; #endif } #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template <class T1, class T2> ostream &operator<<(ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; } template <class T> ostream &operator<<(ostream &out, vector<T> vec) { out << "("; for (auto &v : vec) out << v << ", "; return out << ")"; } template <class T> ostream &operator<<(ostream &out, set<T> vec) { out << "("; for (auto &v : vec) out << v << ", "; return out << ")"; } template <class L, class R> ostream &operator<<(ostream &out, map<L, R> vec) { out << "("; for (auto &v : vec) out << v << ", "; return out << ")"; } const ll MAXN = 12; const ll MASK = (1 << MAXN); const ll INF = 1e12; void upd(ll &x, ll y) { x = min(x, y); } int main() { cout << fixed << setprecision(30); ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); ll n, m; cin >> n >> m; vl costs(m); vl masks(m); for (ll i = 0; i < m; ++i) { ll cnt; cin >> costs[i] >> cnt; for (ll j = 0; j < cnt; ++j) { ll bit; cin >> bit; --bit; masks[i] |= (1LL << bit); } } vvl dp(m + 1, vl(MASK + 10, +INF)); dp[0][0] = 0; for (ll i = 0; i < m; ++i) { for (ll mask = 0; mask < MASK; ++mask) { upd(dp[i + 1][mask], dp[i][mask]); upd(dp[i + 1][mask | masks[i]], dp[i][mask] + costs[i]); } } ll ans = dp[m][(1 << n) - 1]; cout << (ans == INF ? -1 : ans) << endl; print_time(); return 0; }
replace
107
108
107
108
-11
p02901
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_set> #include <vector> using namespace std; #define FOR(i, N) for (int i = 0; i < (int)N; i++) #define FORIN(i, a, b) for (int i = a; i < (int)b; i++) #define ALL(x) (x).begin(), (x).end() #define MOD 1000000007 #define MAX (1 << 29) #define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__) using Pi = pair<int, int>; using ll = long long; const int INF = 1 << 28; string to_string(string s) { return s; } template <class S, class T> string to_string(pair<S, T> p) { return "{" + to_string(p.first) + "," + to_string(p.second) + "}"; } template <class S> string to_string(unordered_set<S> s) { string ret = "{"; for (S x : s) ret += to_string(x) + ","; return ret + "}"; } template <class T> string to_string(vector<T> v) { string ret = "{"; for (int i = 0; i < v.size() - 1; ++i) { ret += to_string(v[i]) + ","; } if (v.size() > 0) { ret += to_string(v.back()); } ret += "}"; return ret; } void debug() { cerr << endl; } template <class Head, class... Tail> void debug(Head head, Tail... tail) { cerr << to_string(head) << " "; debug(tail...); } void print() { cout << endl; } template <class Head, class... Tail> void print(Head head, Tail... tail) { cout << to_string(head); print(tail...); } void get() {} template <class Head, class... Tail> void get(Head &head, Tail &...tail) { cin >> head; get(tail...); } template <class T> void getv(vector<T> &vec) { for (int i = 0; i < vec.size(); ++i) cin >> vec[i]; } unordered_set<ll> enum_div(ll n) // nの約数を列挙 { std::unordered_set<ll> ret; for (ll i = 1; i * i <= n; ++i) { if (n % i == 0) { ret.insert(i); if (i * i != n) { ret.insert(n / i); } } } return ret; } int main() { int N, M; cin >> N >> M; vector<int> a(N); vector<int> dp(1 << N, 1 << 29); dp[0] = 0; FOR(i, M) { int b; cin >> a[i] >> b; int add = 0; FOR(j, b) { int c; cin >> c; add |= 1 << (c - 1); } FOR(j, 1 << N) { dp[j | add] = min(dp[j | add], dp[j] + a[i]); } } print(dp[(1 << N) - 1] == 1 << 29 ? -1 : dp[(1 << N) - 1]); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_set> #include <vector> using namespace std; #define FOR(i, N) for (int i = 0; i < (int)N; i++) #define FORIN(i, a, b) for (int i = a; i < (int)b; i++) #define ALL(x) (x).begin(), (x).end() #define MOD 1000000007 #define MAX (1 << 29) #define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__) using Pi = pair<int, int>; using ll = long long; const int INF = 1 << 28; string to_string(string s) { return s; } template <class S, class T> string to_string(pair<S, T> p) { return "{" + to_string(p.first) + "," + to_string(p.second) + "}"; } template <class S> string to_string(unordered_set<S> s) { string ret = "{"; for (S x : s) ret += to_string(x) + ","; return ret + "}"; } template <class T> string to_string(vector<T> v) { string ret = "{"; for (int i = 0; i < v.size() - 1; ++i) { ret += to_string(v[i]) + ","; } if (v.size() > 0) { ret += to_string(v.back()); } ret += "}"; return ret; } void debug() { cerr << endl; } template <class Head, class... Tail> void debug(Head head, Tail... tail) { cerr << to_string(head) << " "; debug(tail...); } void print() { cout << endl; } template <class Head, class... Tail> void print(Head head, Tail... tail) { cout << to_string(head); print(tail...); } void get() {} template <class Head, class... Tail> void get(Head &head, Tail &...tail) { cin >> head; get(tail...); } template <class T> void getv(vector<T> &vec) { for (int i = 0; i < vec.size(); ++i) cin >> vec[i]; } unordered_set<ll> enum_div(ll n) // nの約数を列挙 { std::unordered_set<ll> ret; for (ll i = 1; i * i <= n; ++i) { if (n % i == 0) { ret.insert(i); if (i * i != n) { ret.insert(n / i); } } } return ret; } int main() { int N, M; cin >> N >> M; vector<int> a(M); vector<int> dp(1 << N, 1 << 29); dp[0] = 0; FOR(i, M) { int b; cin >> a[i] >> b; int add = 0; FOR(j, b) { int c; cin >> c; add |= 1 << (c - 1); } FOR(j, 1 << N) { dp[j | add] = min(dp[j | add], dp[j] + a[i]); } } print(dp[(1 << N) - 1] == 1 << 29 ? -1 : dp[(1 << N) - 1]); return 0; }
replace
94
95
94
95
0
p02901
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f int N, M; int cost[1001]; // custo de cada chave int chave[1001]; // baus que cada chave abre int memo[1001][9000]; int total; // bits de baus int dp(int key, int baus) { if (key == M) { if (baus == total) return 0; else return INF; } // if(memo[key][baus] != -1) return memo[key][baus]; // usando essa chave int a = dp(key + 1, baus | chave[key]) + cost[key]; // sem usar essa chave int b = dp(key + 1, baus); int res = min(a, b); memo[key][baus] = res; return res; } int main() { cin >> N >> M; memset(memo, -1, sizeof(memo)); for (int i = 0; i < N; i++) { total |= 1 << i; } for (int i = 0; i < M; i++) { int custo, qtd; cin >> custo >> qtd; cost[i] = custo; int bau; for (int x = 0; x < qtd; x++) { cin >> bau; chave[i] |= 1 << (bau - 1); } } int res = dp(0, 0); if (res == INF) cout << -1 << endl; else cout << res << endl; }
#include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f int N, M; int cost[1001]; // custo de cada chave int chave[1001]; // baus que cada chave abre int memo[1001][9000]; int total; // bits de baus int dp(int key, int baus) { if (key == M) { if (baus == total) return 0; else return INF; } if (memo[key][baus] != -1) return memo[key][baus]; // usando essa chave int a = dp(key + 1, baus | chave[key]) + cost[key]; // sem usar essa chave int b = dp(key + 1, baus); int res = min(a, b); memo[key][baus] = res; return res; } int main() { cin >> N >> M; memset(memo, -1, sizeof(memo)); for (int i = 0; i < N; i++) { total |= 1 << i; } for (int i = 0; i < M; i++) { int custo, qtd; cin >> custo >> qtd; cost[i] = custo; int bau; for (int x = 0; x < qtd; x++) { cin >> bau; chave[i] |= 1 << (bau - 1); } } int res = dp(0, 0); if (res == INF) cout << -1 << endl; else cout << res << endl; }
replace
22
23
22
24
TLE
p02901
C++
Runtime Error
#include <algorithm> #include <stdio.h> #include <string.h> int a[100]; int b[100]; int c[100]; int dp[1 << 12]; int main() { int n, m, C; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", &a[i], &b[i]); for (int j = 0; j < b[i]; j++) { scanf("%d", &C); C--; c[i] |= 1 << C; } } memset(dp, 0x7f, sizeof(dp)); dp[0] = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < (1 << n); j++) { if (dp[j] >= 0) { dp[j | c[i]] = std::min(dp[j] + a[i], dp[j | c[i]]); } } } if (dp[(1 << n) - 1] == 2139062143) printf("-1"); else printf("%d", dp[(1 << n) - 1]); puts(""); return 0; }
#include <algorithm> #include <stdio.h> #include <string.h> int a[1005]; int b[1005]; int c[1005]; int dp[1 << 12]; int main() { int n, m, C; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", &a[i], &b[i]); for (int j = 0; j < b[i]; j++) { scanf("%d", &C); C--; c[i] |= 1 << C; } } memset(dp, 0x7f, sizeof(dp)); dp[0] = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < (1 << n); j++) { if (dp[j] >= 0) { dp[j | c[i]] = std::min(dp[j] + a[i], dp[j | c[i]]); } } } if (dp[(1 << n) - 1] == 2139062143) printf("-1"); else printf("%d", dp[(1 << n) - 1]); puts(""); return 0; }
replace
4
7
4
7
0
p02901
Python
Runtime Error
def read_key(): a, _ = map(int, input().split()) m = 0 for c in map(int, input().split()): m |= 1 << (c - 1) return (a, m) def main(): INF = float("inf") N, M = map(int, input().split()) keys = [read_key() for _ in range(M)] dp = [[INF] * (1 << N) for _ in range(M + 1)] dp[0][0] = 0 for i in range(M): a, m = keys[i] dpi = dp[i] dpi1 = dpi[i + 1] for j in range(1 << N): if dpi[j] == INF: continue if dpi[j] + a < dpi1[j | m]: dpi1[j | m] = dpi[j] + a if dpi[j] < dpi1[j]: dpi1[j] = dpi[j] if dp[M][(1 << N) - 1] == INF: print(-1) else: print(dp[M][(1 << N) - 1]) main()
def read_key(): a, _ = map(int, input().split()) m = 0 for c in map(int, input().split()): m |= 1 << (c - 1) return (a, m) def main(): INF = float("inf") N, M = map(int, input().split()) keys = [read_key() for _ in range(M)] dp = [[INF] * (1 << N) for _ in range(M + 1)] dp[0][0] = 0 for i in range(M): a, m = keys[i] dpi = dp[i] dpi1 = dp[i + 1] for j in range(1 << N): if dpi[j] == INF: continue if dpi[j] + a < dpi1[j | m]: dpi1[j | m] = dpi[j] + a if dpi[j] < dpi1[j]: dpi1[j] = dpi[j] if dp[M][(1 << N) - 1] == INF: print(-1) else: print(dp[M][(1 << N) - 1]) main()
replace
20
21
20
21
TypeError: 'float' object is not subscriptable
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02901/Python/s397338188.py", line 36, in <module> main() File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02901/Python/s397338188.py", line 25, in main if dpi[j] + a < dpi1[j | m]: TypeError: 'float' object is not subscriptable
p02901
Python
Time Limit Exceeded
INF = float("inf") N, M = map(int, input().split()) dp = [INF] * (2**N) dp[0] = 0 for _ in range(M): a, b = map(int, input().split()) C = [int(i) for i in input().split()] mask = sum(1 << (c - 1) for c in C) for i in range(len(dp)): dp[i | mask] = min(dp[i | mask], dp[i] + a) ans = dp[-1] print(-1 if ans == INF else ans)
INF = float("inf") N, M = map(int, input().split()) dp = [INF] * (2**N) dp[0] = 0 for _ in range(M): a, b = map(int, input().split()) C = [int(i) for i in input().split()] mask = sum(1 << (c - 1) for c in C) for i in range(len(dp)): if dp[i | mask] > dp[i] + a: dp[i | mask] = dp[i] + a ans = dp[-1] print(-1 if ans == INF else ans)
replace
12
13
12
14
TLE
p02901
Python
Time Limit Exceeded
def main(): n, m = map(int, input().split()) a = [] c = [] dp = [10**7 for j in range(2**n)] dp[0] = 0 for _ in range(m): a, b = map(int, input().split()) sc = list(map(int, input().split())) c = 0 for j in sc: c += 2 ** (j - 1) for j in range(2**n): dp[int(j | c)] = min(dp[int(j | c)], dp[j] + a) if dp[2**n - 1] == 10**7: print(-1) else: print(dp[2**n - 1]) main()
def main(): n, m = map(int, input().split()) a = [] c = [] dp = [10**7 for j in range(2**n)] dp[0] = 0 for _ in range(m): a, b = map(int, input().split()) sc = list(map(int, input().split())) c = 0 for j in sc: c += 2 ** (j - 1) for j in range(2**n): dp[j | c] = min(dp[j | c], dp[j] + a) if dp[2**n - 1] == 10**7: print(-1) else: print(dp[2**n - 1]) main()
replace
13
14
13
14
TLE
p02901
C++
Runtime Error
// // sublime text 3, // emre yazicioglu // #include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<ll> vll; typedef vector<pll> vpll; #define pb push_back #define fi first #define se second #define INF (ll)(1e18 + 7) #define MOD (ll)(1e9 + 7) const int N = 5e3 + 7; ll mn = INF, mx = -INF; ll n, m, k, s, q, t; ll a, b, c, d, e; ll ans, cur, sum, cnt; ll ar[N], p[N], dp[N][N]; string str; bool h[N]; pll pp[N]; vll v; ll f(ll cur, ll mask) { if (cur == m + 1) return (mask == (1 << n) - 1 ? 0 : INF); ll &r = dp[cur][mask]; if (~r) return r; r = INF; r = min(r, f(cur + 1, mask)); r = min(r, f(cur + 1, mask | ar[cur]) + p[cur]); return r; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); freopen("../in.txt", "r", stdin); freopen("../out.txt", "w", stdout); memset(dp, -1, sizeof dp); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> p[i] >> a; for (int j = 1; j <= a; j++) { cin >> k; ar[i] |= (1 << (k - 1)); } } ans = f(1, 0); if (ans >= INF) ans = -1; cout << ans; }
// // sublime text 3, // emre yazicioglu // #include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<ll> vll; typedef vector<pll> vpll; #define pb push_back #define fi first #define se second #define INF (ll)(1e18 + 7) #define MOD (ll)(1e9 + 7) const int N = 5e3 + 7; ll mn = INF, mx = -INF; ll n, m, k, s, q, t; ll a, b, c, d, e; ll ans, cur, sum, cnt; ll ar[N], p[N], dp[N][N]; string str; bool h[N]; pll pp[N]; vll v; ll f(ll cur, ll mask) { if (cur == m + 1) return (mask == (1 << n) - 1 ? 0 : INF); ll &r = dp[cur][mask]; if (~r) return r; r = INF; r = min(r, f(cur + 1, mask)); r = min(r, f(cur + 1, mask | ar[cur]) + p[cur]); return r; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("../in.txt","r",stdin); // freopen("../out.txt","w",stdout); memset(dp, -1, sizeof dp); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> p[i] >> a; for (int j = 1; j <= a; j++) { cin >> k; ar[i] |= (1 << (k - 1)); } } ans = f(1, 0); if (ans >= INF) ans = -1; cout << ans; }
replace
59
61
59
61
-11
p02901
C++
Runtime Error
#pragma GCC optimize("Ofast") #define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; using u8 = uint8_t; using u16 = uint16_t; using u32 = uint32_t; using u64 = uint64_t; using i8 = int8_t; using i16 = int16_t; using i32 = int32_t; using i64 = int64_t; constexpr char newl = '\n'; constexpr double eps = 1e-10; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, b) FOR(i, 0, b) #define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--) #define RF0(i, b) RFO(i, 0, b) #define fi first #define se second #define debug(x) cout << #x << ": " << x << '\n'; #define rng(a) a.begin(), a.end() #define rrng(a) a.rbegin(), a.rend() template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } template <class T> void Print(vector<T> v) { F0R(i, v.size()) { cout << v[i] << ' '; } cout << newl; } #if 1 struct GetNumberOfTrailingZeros { constexpr GetNumberOfTrailingZeros() : table() { uint64_t hash = 0x03F566ED27179461UL; for (int i = 0; i < 64; i++) { table[hash >> 58] = i; hash <<= 1; } } int Calc(int64_t x) { if (x == 0) return 64; uint64_t y = (uint64_t)(x & -x); int i = (int)((y * 0x03F566ED27179461UL) >> 58); return table[i]; } int table[64]; } gntz; // INSERT ABOVE HERE signed main() { cin.tie(0); ios_base::sync_with_stdio(false); int N, M; cin >> N >> M; vector<int> as(M); vector<int> bs(M); vector<vector<int>> cs(N); F0R(i, M) { int a, b; cin >> a >> b; as[i] = a; F0R(j, b) { int c; cin >> c; c--; cs[c].push_back(i); bs[i] |= 1 << c; } } int cl = M, ci; F0R(i, N) { if (cs[i].size() < cl) { cl = cs[i].size(); ci = i; } sort(rng(cs[i]), [&as](int l, int r) { return as[l] < as[r]; }); } int result = INT32_MAX; int all = (1 << N) - 1; auto dfs = [&](auto dfs, int bits, int cost, int ci) -> void { F0R(i, cs[ci].size()) { int nb = bits | bs[cs[ci][i]]; int nc = cost + as[cs[ci][i]]; if (result <= nc) continue; if (nb == all) { result = nc; continue; } dfs(dfs, nb, nc, gntz.Calc(~nb)); } }; dfs(dfs, 0, 0, ci); cout << (result > 10000000 ? -1 : result); } #endif
#pragma GCC optimize("Ofast") #define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; using u8 = uint8_t; using u16 = uint16_t; using u32 = uint32_t; using u64 = uint64_t; using i8 = int8_t; using i16 = int16_t; using i32 = int32_t; using i64 = int64_t; constexpr char newl = '\n'; constexpr double eps = 1e-10; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, b) FOR(i, 0, b) #define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--) #define RF0(i, b) RFO(i, 0, b) #define fi first #define se second #define debug(x) cout << #x << ": " << x << '\n'; #define rng(a) a.begin(), a.end() #define rrng(a) a.rbegin(), a.rend() template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } template <class T> void Print(vector<T> v) { F0R(i, v.size()) { cout << v[i] << ' '; } cout << newl; } #if 1 struct GetNumberOfTrailingZeros { constexpr GetNumberOfTrailingZeros() : table() { uint64_t hash = 0x03F566ED27179461UL; for (int i = 0; i < 64; i++) { table[hash >> 58] = i; hash <<= 1; } } int Calc(int64_t x) { if (x == 0) return 64; uint64_t y = (uint64_t)(x & -x); int i = (int)((y * 0x03F566ED27179461UL) >> 58); return table[i]; } int table[64]; } gntz; // INSERT ABOVE HERE signed main() { cin.tie(0); ios_base::sync_with_stdio(false); int N, M; cin >> N >> M; vector<int> as(M); vector<int> bs(M); vector<vector<int>> cs(N); F0R(i, M) { int a, b; cin >> a >> b; as[i] = a; F0R(j, b) { int c; cin >> c; c--; cs[c].push_back(i); bs[i] |= 1 << c; } } int cl = M, ci; F0R(i, N) { if (cs[i].size() == 0) { cout << -1; return 0; } if (cs[i].size() < cl) { cl = cs[i].size(); ci = i; } sort(rng(cs[i]), [&as](int l, int r) { return as[l] < as[r]; }); } int result = INT32_MAX; int all = (1 << N) - 1; auto dfs = [&](auto dfs, int bits, int cost, int ci) -> void { F0R(i, cs[ci].size()) { int nb = bits | bs[cs[ci][i]]; int nc = cost + as[cs[ci][i]]; if (result <= nc) continue; if (nb == all) { result = nc; continue; } dfs(dfs, nb, nc, gntz.Calc(~nb)); } }; dfs(dfs, 0, 0, ci); cout << (result > 10000000 ? -1 : result); } #endif
insert
87
87
87
91
0
p02901
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fst(t) std::get<0>(t) #define snd(t) std::get<1>(t) #define thd(t) std::get<2>(t) #define unless(p) if (!(p)) #define until(p) while (!(p)) using ll = std::int64_t; using P = std::tuple<int, int>; constexpr int INF = 1'000'000'000; int N, M; int A[1100], B[1100], C[1100]; int dp[1100][1 << 12]; int rec(int i, int s) { if (s + 1 == (1 << N)) { return 0; } if (i == M) { return INF; } int res = std::min(rec(i + 1, s), rec(i + 1, s | C[i]) + A[i]); return dp[i][s] = res; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cin >> N >> M; for (int i = 0; i < M; ++i) { std::cin >> A[i] >> B[i]; for (int j = 0; j < B[i]; ++j) { int c; std::cin >> c; c -= 1; C[i] |= (1 << c); } } memset(dp, -1, sizeof(dp)); int res = rec(0, 0); if (res >= INF) { res = -1; } std::cout << res << std::endl; }
#include <bits/stdc++.h> #define fst(t) std::get<0>(t) #define snd(t) std::get<1>(t) #define thd(t) std::get<2>(t) #define unless(p) if (!(p)) #define until(p) while (!(p)) using ll = std::int64_t; using P = std::tuple<int, int>; constexpr int INF = 1'000'000'000; int N, M; int A[1100], B[1100], C[1100]; int dp[1100][1 << 12]; int rec(int i, int s) { if (s + 1 == (1 << N)) { return 0; } if (i == M) { return INF; } if (dp[i][s] != -1) { return dp[i][s]; } int res = std::min(rec(i + 1, s), rec(i + 1, s | C[i]) + A[i]); return dp[i][s] = res; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cin >> N >> M; for (int i = 0; i < M; ++i) { std::cin >> A[i] >> B[i]; for (int j = 0; j < B[i]; ++j) { int c; std::cin >> c; c -= 1; C[i] |= (1 << c); } } memset(dp, -1, sizeof(dp)); int res = rec(0, 0); if (res >= INF) { res = -1; } std::cout << res << std::endl; }
insert
23
23
23
27
TLE
p02901
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long int #define rep(i, a, b) for (ll i = a; i < b; i++) #define sep(i, b, a) for (ll i = b - 1; i >= a; i--) #define vi vector<ll> #define hell 1000000007 #define pii pair<ll, ll> #define inf (ll)(1e16) #define pb push_back using namespace std; ll dp[1001][1 << 12]; ll helper(vi &a, vi &b, ll st, ll mask, ll reqmask) { if (mask == reqmask) return 0; if (st >= a.size()) return inf; ll ans; ans = helper(a, b, st + 1, mask, reqmask); ans = min(ans, a[st] + helper(a, b, st + 1, mask | b[st], reqmask)); return dp[st][mask] = ans; } void solve() { ll n, m; cin >> n >> m; vi a, b; rep(i, 0, m) { vector<bool> v(n, false); ll x, y; cin >> x >> y; rep(i, 0, y) { ll p; cin >> p; v[p - 1] = true; } ll num = 0; rep(i, 0, n) { num = num * 2; num += v[i]; } a.pb(x); b.pb(num); } ll reqmask = (1 << n) - 1; memset(dp, -1, sizeof dp); ll ans = helper(a, b, 0, 0, reqmask); if (ans == inf) ans = -1; cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t; t = 1; // cin>>t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #define ll long long int #define rep(i, a, b) for (ll i = a; i < b; i++) #define sep(i, b, a) for (ll i = b - 1; i >= a; i--) #define vi vector<ll> #define hell 1000000007 #define pii pair<ll, ll> #define inf (ll)(1e16) #define pb push_back using namespace std; ll dp[1001][1 << 12]; ll helper(vi &a, vi &b, ll st, ll mask, ll reqmask) { if (mask == reqmask) return 0; if (st >= a.size()) return inf; if (dp[st][mask] != -1) return dp[st][mask]; ll ans; ans = helper(a, b, st + 1, mask, reqmask); ans = min(ans, a[st] + helper(a, b, st + 1, mask | b[st], reqmask)); return dp[st][mask] = ans; } void solve() { ll n, m; cin >> n >> m; vi a, b; rep(i, 0, m) { vector<bool> v(n, false); ll x, y; cin >> x >> y; rep(i, 0, y) { ll p; cin >> p; v[p - 1] = true; } ll num = 0; rep(i, 0, n) { num = num * 2; num += v[i]; } a.pb(x); b.pb(num); } ll reqmask = (1 << n) - 1; memset(dp, -1, sizeof dp); ll ans = helper(a, b, 0, 0, reqmask); if (ans == inf) ans = -1; cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t; t = 1; // cin>>t; while (t--) { solve(); } return 0; }
insert
20
20
20
22
TLE
p02901
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> P; const ll MOD = 1000000007; const ll MAX_N = 500010; const ll INF = 999999999999; ll dp[1 << 20]; int main() { int n, m; cin >> n >> m; vector<int> a(m); vector<vector<int>> c(m); for (int i = 0; i < m; i++) { int A, b; cin >> A >> b; a[i] = A; for (int j = 0; j < b; j++) { int _; cin >> _; _--; c[i].push_back(_); } } queue<int> q; q.push(0); for (int i = 0; i < 1 << n; i++) dp[i] = INF; dp[0] = 0; while (!q.empty()) { int bit = q.front(); q.pop(); for (int i = 0; i < m; i++) { int nbit = 0; for (int j : c[i]) { nbit += 1 << j; } if (bit == (bit | nbit)) continue; nbit = bit | nbit; q.push(nbit); dp[nbit] = min(dp[nbit], dp[bit] + a[i]); } } if (dp[(1 << n) - 1] == INF) { cout << -1 << endl; } else { cout << dp[(1 << n) - 1] << endl; } }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> P; const ll MOD = 1000000007; const ll MAX_N = 500010; const ll INF = 999999999999; ll dp[1 << 20]; int main() { int n, m; cin >> n >> m; vector<int> a(m); vector<vector<int>> c(m); for (int i = 0; i < m; i++) { int A, b; cin >> A >> b; a[i] = A; for (int j = 0; j < b; j++) { int _; cin >> _; _--; c[i].push_back(_); } } queue<int> q; q.push(0); for (int i = 0; i < 1 << n; i++) dp[i] = INF; dp[0] = 0; while (!q.empty()) { int bit = q.front(); q.pop(); for (int i = 0; i < m; i++) { int nbit = 0; for (int j : c[i]) { nbit += 1 << j; } if (bit == (bit | nbit)) continue; nbit = bit | nbit; if (dp[nbit] > dp[bit] + a[i]) { dp[nbit] = dp[bit] + a[i]; q.push(nbit); } } } if (dp[(1 << n) - 1] == INF) { cout << -1 << endl; } else { cout << dp[(1 << n) - 1] << endl; } }
replace
68
70
68
72
TLE
p02901
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int #define INF 10000000000000 #define MK2DV(v, row, col, val) vector<vector<ll>> v(row, vector<ll>(col, val)) using namespace std; ll findmin(ll a, ll b) { if (a < b) return a; return b; } ll fuc(ll i, ll have, ll *give, ll *cost, ll m, ll n, vector<vector<ll>> &dp) { if (have == n) { return 0; } if (i == m) { return INF; } if (dp[i][have] != -1) { return dp[i][have]; } ll ans = findmin(fuc(i + 1, have, give, cost, m, n, dp), fuc(i + 1, (have | give[i]), give, cost, m, n, dp) + cost[i]); dp[i][have] = ans; return ans; } int main() { ll n, m; cin >> n >> m; ll give[m]; ll cost[m]; ll netreq = (1LL << n) - 1; MK2DV(dp, n + 5, netreq + 5, -1); for (ll i = 0; i < m; i++) { give[i] = 0; ll a, b; cin >> a >> b; cost[i] = a; for (ll j = 0; j < b; j++) { ll key; cin >> key; key--; ll mask = (1LL << key); give[i] = (give[i] | mask); } } ll ans = fuc(0, 0, give, cost, m, netreq, dp); if (ans >= INF) ans = -1; cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> #define ll long long int #define INF 10000000000000 #define MK2DV(v, row, col, val) vector<vector<ll>> v(row, vector<ll>(col, val)) using namespace std; ll findmin(ll a, ll b) { if (a < b) return a; return b; } ll fuc(ll i, ll have, ll *give, ll *cost, ll m, ll n, vector<vector<ll>> &dp) { if (have == n) { return 0; } if (i == m) { return INF; } if (dp[i][have] != -1) { return dp[i][have]; } ll ans = findmin(fuc(i + 1, have, give, cost, m, n, dp), fuc(i + 1, (have | give[i]), give, cost, m, n, dp) + cost[i]); dp[i][have] = ans; return ans; } int main() { ll n, m; cin >> n >> m; ll give[m]; ll cost[m]; ll netreq = (1LL << n) - 1; MK2DV(dp, m + 5, netreq + 5, -1); for (ll i = 0; i < m; i++) { give[i] = 0; ll a, b; cin >> a >> b; cost[i] = a; for (ll j = 0; j < b; j++) { ll key; cin >> key; key--; ll mask = (1LL << key); give[i] = (give[i] | mask); } } ll ans = fuc(0, 0, give, cost, m, netreq, dp); if (ans >= INF) ans = -1; cout << ans << '\n'; return 0; }
replace
33
34
33
34
0
p02901
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <iostream> #include <string> #include <unordered_map> #include <unordered_set> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORR(i, a, b) for (ll i = (a); i <= (b); i++) #define repR(i, n) for (ll i = n; i >= 0; i--) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define F first #define S second #define pb push_back #define pu push #define COUT(x) cout << (x) << endl #define PQ priority_queue<ll> #define PQR priority_queue<ll, vector<ll>, greater<ll>> #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define mp make_pair #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define s(x) (ll)(x).size() typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll MOD = 1000000007LL; const ll INF = 1LL << 60; using vll = vector<ll>; using vb = vector<bool>; using vvb = vector<vb>; using vvll = vector<vll>; using vstr = vector<string>; using pll = pair<ll, ll>; using vc = vector<char>; using vvc = vector<vc>; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } ll dx[4] = {0, 1, 0, -1}; ll dy[4] = {1, 0, -1, 0}; int main() { ll n, m; cin >> n >> m; vector<pll> p(m); rep(i, m) { ll a, b; cin >> a >> b; p[i].F = a; rep(j, b) { ll c; cin >> c; p[i].S |= (1 << c - 1); } } vll dp((1 << n) - 1, INF); dp[0] = 0; rep(i, (1 << n)) { rep(j, m) { ll nx = i | p[j].S; dp[nx] = min(dp[nx], dp[i] + p[j].F); } } if (dp[(1 << n) - 1] == INF) { COUT(-1); } else COUT(dp[(1 << n) - 1]); }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <iostream> #include <string> #include <unordered_map> #include <unordered_set> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORR(i, a, b) for (ll i = (a); i <= (b); i++) #define repR(i, n) for (ll i = n; i >= 0; i--) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define F first #define S second #define pb push_back #define pu push #define COUT(x) cout << (x) << endl #define PQ priority_queue<ll> #define PQR priority_queue<ll, vector<ll>, greater<ll>> #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define mp make_pair #define maxs(x, y) (x = max(x, y)) #define mins(x, y) (x = min(x, y)) #define s(x) (ll)(x).size() typedef pair<int, int> pii; typedef pair<ll, ll> pll; const ll MOD = 1000000007LL; const ll INF = 1LL << 60; using vll = vector<ll>; using vb = vector<bool>; using vvb = vector<vb>; using vvll = vector<vll>; using vstr = vector<string>; using pll = pair<ll, ll>; using vc = vector<char>; using vvc = vector<vc>; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } ll dx[4] = {0, 1, 0, -1}; ll dy[4] = {1, 0, -1, 0}; int main() { ll n, m; cin >> n >> m; vector<pll> p(m); rep(i, m) { ll a, b; cin >> a >> b; p[i].F = a; rep(j, b) { ll c; cin >> c; p[i].S |= (1 << c - 1); } } vll dp((1 << n), INF); dp[0] = 0; rep(i, (1 << n)) { rep(j, m) { ll nx = i | p[j].S; dp[nx] = min(dp[nx], dp[i] + p[j].F); } } if (dp[(1 << n) - 1] == INF) { COUT(-1); } else COUT(dp[(1 << n) - 1]); }
replace
70
71
70
71
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a[m], d[m]; fill(d, d + n, 0); for (int i = 0; i < m; i++) { int b; cin >> a[i] >> b; for (int j = 0; j < b; j++) { int c; cin >> c; d[i] += pow(2, c - 1); } } int max_state = pow(2, n); int dp[m + 1][max_state]; int inf = 1000000000; for (int i = 0; i < m + 1; i++) { for (int j = 0; j < max_state; j++) { dp[i][j] = inf; } } for (int i = 0; i < m + 1; i++) { dp[i][0] = 0; } for (int i = 1; i < m + 1; i++) { for (int j = 1; j < max_state; j++) { dp[i][j] = dp[i - 1][j]; } for (int j = 0; j < max_state; j++) { int k = j | d[i - 1]; if (dp[i][k] > dp[i - 1][j] + a[i - 1]) dp[i][k] = dp[i - 1][j] + a[i - 1]; } } if (dp[m][max_state - 1] == inf) cout << -1 << endl; else cout << dp[m][max_state - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a[m], d[m]; fill(d, d + m, 0); for (int i = 0; i < m; i++) { int b; cin >> a[i] >> b; for (int j = 0; j < b; j++) { int c; cin >> c; d[i] += pow(2, c - 1); } } int max_state = pow(2, n); int dp[m + 1][max_state]; int inf = 1000000000; for (int i = 0; i < m + 1; i++) { for (int j = 0; j < max_state; j++) { dp[i][j] = inf; } } for (int i = 0; i < m + 1; i++) { dp[i][0] = 0; } for (int i = 1; i < m + 1; i++) { for (int j = 1; j < max_state; j++) { dp[i][j] = dp[i - 1][j]; } for (int j = 0; j < max_state; j++) { int k = j | d[i - 1]; if (dp[i][k] > dp[i - 1][j] + a[i - 1]) dp[i][k] = dp[i - 1][j] + a[i - 1]; } } if (dp[m][max_state - 1] == inf) cout << -1 << endl; else cout << dp[m][max_state - 1] << endl; return 0; }
replace
7
8
7
8
-11
p02901
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdint> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <sstream> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; #define EPS (1e-7) int main() { ll N, M; cin >> N >> M; struct Key { uint32_t open; ll cost; }; constexpr size_t MAXSIZE = (1 << 13); // state, cost vector<ll> dp(MAXSIZE, -1); dp[0] = 0; vector<Key> keys(M); for (ll i = 0; i < M; i++) { ll cost, open_N; cin >> cost >> open_N; uint32_t open = 0; for (ll j = 0; j < open_N; j++) { ll x; cin >> x; x--; open |= (1 << x); } keys[i] = {open, cost}; } for (uint32_t i = 0; i < (1 << M); i++) { if (dp[i] == -1) { continue; } for (const auto &key : keys) { ll to = (i | key.open); if (dp[to] == -1) { dp[to] = dp[i] + key.cost; } else { dp[to] = min(dp[to], dp[i] + key.cost); } } } cout << dp[(1 << N) - 1] << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdint> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <sstream> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; #define EPS (1e-7) int main() { ll N, M; cin >> N >> M; struct Key { uint32_t open; ll cost; }; constexpr size_t MAXSIZE = (1 << 13); // state, cost vector<ll> dp(MAXSIZE, -1); dp[0] = 0; vector<Key> keys(M); for (ll i = 0; i < M; i++) { ll cost, open_N; cin >> cost >> open_N; uint32_t open = 0; for (ll j = 0; j < open_N; j++) { ll x; cin >> x; x--; open |= (1 << x); } keys[i] = {open, cost}; } for (uint32_t i = 0; i < (1 << N); i++) { if (dp[i] == -1) { continue; } for (const auto &key : keys) { ll to = (i | key.open); if (dp[to] == -1) { dp[to] = dp[i] + key.cost; } else { dp[to] = min(dp[to], dp[i] + key.cost); } } } cout << dp[(1 << N) - 1] << endl; return 0; }
replace
52
53
52
53
0
p02901
C++
Runtime Error
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include <prettyprint.hpp> #define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__); #else #define debug(...) 83; #endif void d_err() { cerr << endl; } template <typename H, typename... T> void d_err(H h, T... t) { cerr << h << " "; d_err(t...); } template <typename T> void print(T x) { cout << x << endl; } #define ALL(x) (x).begin(), (x).end() #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define REVFOR(i, m, n) for (int i = ((n)-1); i >= (m); --i) #define REP(i, n) FOR(i, 0, n) #define REVREP(i, n) REVFOR(i, 0, n) #define fi first #define se second #define pb push_back #define mp make_pair #define eb emplace_back #define bcnt __builtin_popcountll typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<ll, ll> Pll; typedef pair<int, int> Pin; ll INF = 1e16; int inf = 1e9; ll MOD = 1e9 + 7; int main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); int N, M; cin >> N >> M; vll A(M), C(M, 0); REP(i, M) { int b; cin >> A[i] >> b; REP(j, b) { int c; cin >> c; --c; C[i] |= (1LL << c); } } ll SMAX = (1 << N) + 5; ll dp[M + 1][SMAX]; REP(i, M + 1) REP(j, SMAX) dp[i][j] = INF; dp[0][0] = 0; REP(i, M) { REP(j, 1 << N) { REP(k, N) { if (!(C[i] >> k & 1) || j >> k & 1) continue; // i-1 -> i dp[i + 1][j | 1 << k] = min(dp[i + 1][j | 1 << k], dp[i][j] + A[i]); // i -> i dp[i + 1][j | 1 << k] = min(dp[i + 1][j | 1 << k], dp[i + 1][j]); } dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); } } print(dp[N][(1 << N) - 1] == INF ? -1 : dp[N][(1 << N) - 1]); }
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include <prettyprint.hpp> #define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__); #else #define debug(...) 83; #endif void d_err() { cerr << endl; } template <typename H, typename... T> void d_err(H h, T... t) { cerr << h << " "; d_err(t...); } template <typename T> void print(T x) { cout << x << endl; } #define ALL(x) (x).begin(), (x).end() #define FOR(i, m, n) for (int i = (m); i < (n); ++i) #define REVFOR(i, m, n) for (int i = ((n)-1); i >= (m); --i) #define REP(i, n) FOR(i, 0, n) #define REVREP(i, n) REVFOR(i, 0, n) #define fi first #define se second #define pb push_back #define mp make_pair #define eb emplace_back #define bcnt __builtin_popcountll typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<ll, ll> Pll; typedef pair<int, int> Pin; ll INF = 1e16; int inf = 1e9; ll MOD = 1e9 + 7; int main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); int N, M; cin >> N >> M; vll A(M), C(M, 0); REP(i, M) { int b; cin >> A[i] >> b; REP(j, b) { int c; cin >> c; --c; C[i] |= (1LL << c); } } ll SMAX = (1 << N) + 5; ll dp[M + 1][SMAX]; REP(i, M + 1) REP(j, SMAX) dp[i][j] = INF; dp[0][0] = 0; REP(i, M) { REP(j, 1 << N) { REP(k, N) { if (!(C[i] >> k & 1) || j >> k & 1) continue; // i-1 -> i dp[i + 1][j | 1 << k] = min(dp[i + 1][j | 1 << k], dp[i][j] + A[i]); // i -> i dp[i + 1][j | 1 << k] = min(dp[i + 1][j | 1 << k], dp[i + 1][j]); } dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); } } print(dp[M][(1 << N) - 1] == INF ? -1 : dp[M][(1 << N) - 1]); }
replace
88
89
88
89
0
p02901
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; typedef long long ll; int main(void) { int n, m, i, j, a[1003], b, c, l[1003] = {}, dp[1003][1003] = {}; const int INF = 1e9; cin >> n >> m; for (i = 0; i < m; i++) { cin >> a[i] >> b; for (j = 0; j < b; j++) { cin >> c; l[i] += (1 << (c - 1)); } } for (i = 0; i <= m; i++) for (j = 1; j < (1 << n); j++) { dp[i][j] = INF; } dp[0][0] = 0; for (i = 0; i < m; i++) { for (j = 0; j < (1 << n); j++) { dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); dp[i + 1][j | l[i]] = min(dp[i + 1][j | l[i]], dp[i][j] + a[i]); } } if (dp[m][(1 << n) - 1] == INF) { cout << -1 << endl; } else { cout << dp[m][(1 << n) - 1] << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; typedef long long ll; int main(void) { int n, m, i, j, a[1003], b, c, l[1003] = {}, dp[1003][4100] = {}; const int INF = 1e9; cin >> n >> m; for (i = 0; i < m; i++) { cin >> a[i] >> b; for (j = 0; j < b; j++) { cin >> c; l[i] += (1 << (c - 1)); } } for (i = 0; i <= m; i++) for (j = 1; j < (1 << n); j++) { dp[i][j] = INF; } dp[0][0] = 0; for (i = 0; i < m; i++) { for (j = 0; j < (1 << n); j++) { dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); dp[i + 1][j | l[i]] = min(dp[i + 1][j | l[i]], dp[i][j] + a[i]); } } if (dp[m][(1 << n) - 1] == INF) { cout << -1 << endl; } else { cout << dp[m][(1 << n) - 1] << endl; } return 0; }
replace
6
7
6
7
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define FAST \ std::ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pi acos(-1) #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define F first #define S second #define ppb pop_back #define GCD(m, n) __gcd(m, n) #define LCM(m, n) (m * n) / GCD(m, n) #define inputarr(a, n) \ for (int i = 0; i < n; ++i) \ cin >> a[i] #define initarr(a, n, x) \ for (int i = 0; i < n; ++i) \ a[i] = x #define rep(i, a, n) for (int i = a; i < n; i++) #define repe(i, a, n) for (int i = a; i <= n; i++) #define rev(i, a, b) for (int i = a; i >= b; i--) // #define int long long //////////// #define MOD 1000000007 //////// #define POD 998244353 //////// // define ll long long #define ld long double typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<vvvi> vvvvi; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<vvc> vvvc; typedef vector<pii> vpii; typedef vector<bool> vb; typedef vector<vb> vvb; typedef map<int, int> mii; typedef unordered_map<int, int> umii; typedef map<char, int> mci; #define itv for (auto it = v.begin(); it != v.end(); ++it) #define present(container, element) (container.find(element) != container.end()) #define cpresent(container, element) \ (find(ALL(container), element) != container.end()) // #define invect(data,n,commands) for(int i = 0;i<n;i++){int // tmp;cin>>tmp;data.pb(tmp);commands} #define inset(data,n,commands) for(int i // = 0;i<n;i++){int tmp;cin>>tmp;data.insert(tmp);commands} #define trav(a, x) // for(auto& a : x) #define display(x) trav(a,x) cout<<a<<" ";cout<<endl #define bs binary_search #define lb(v, val) lower_bound(ALL(v), val) #define ub(v, val) upper_bound(ALL(v), val) #define Max(x, y, z) max(x, max(y, z)) #define Min(x, y, z) min(x, min(y, z)) #define maxc(v) *max_element(ALL(v)) #define minc(v) *min_element(ALL(v)) /* #define dbg1(a) cout<<" *"<<a<<" "; #define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" " ///// #define dbg cout<<"move" */ #define vin(v, n) \ ; \ vi v(n); \ rep(i, 0, n) cin >> v[i]; #define dbg cerr << "At line " << __LINE__ << " move " << nl #define dbg1(x) cerr << "At line " << __LINE__ << " " << #x << "=" << x << nl #define dbg2(x, y) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << nl #define dbg3(x, y, z) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << " " << #z << "=" << z << nl #define prv(v) \ ; \ rep(i, 0, sz(v)) cerr << v[i] << " "; #define nl "\n" #define sz(s) (int)((s).size()) #define coutsp(k) \ cout << setprecision( \ k) // set precision (total length k icluding decimal and non decimal) #define coutfsp(k) \ cout << fixed << setprecision(k) // fixed set precision(after decimal fixing) #define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') #define INF (int)2e9 //------------------------ Global VARIABLE ------------------------------------ const int N = 12; const int M = 1000; int n, m; int a[M + 1]; vi C[M + 1]; // unordered_map<pair<int,int>,pii> dpm; int dpm[M + 1][4100]; // int valid[M+1][2050]={}; int tru = 1; //------------------------ Global VARIABLE ------------------------------------ int recurso(int n1, int vis) { if (dpm[n1][vis] != -1) return dpm[n1][vis]; if (vis == tru) return dpm[n1][vis] = 0; if (n1 <= 0) return dpm[n1][vis] = INF; // dbg2(n1,vis); int ans = INF, check = 0; int d = recurso(n1 - 1, vis); if (d != INF) ans = min(ans, d); int vis1 = vis; rep(i, 0, sz(C[n1])) vis1 |= (1 << (C[n1][i] - 1)); d = recurso(n1 - 1, vis1); if (d != INF) ans = min(ans, d + a[n1]); return dpm[n1][vis] = ans; } //------------------------------------------------------------------------------- void dquit() { cin >> n >> m; memset(dpm, -1, sizeof(dpm)); rep(i, 1, m + 1) { int b; cin >> a[i] >> b; rep(j, 0, b) { int c; cin >> c; C[i].pb(c); } } tru = (1 << n) - 1; int ans = recurso(m, 0); if (ans == INF) cout << -1; else { cout << ans; } } //------------------------------------------------------------------------------- signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif //-------------------------------- FAST // MAKE IT FAST --- //-------------------------------- /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// int t; t = 1; int co = 1; ///////---------------------------------||||| // cin>>t; ////////>>>>>>> //////----------------------------------||||| while (t--) { // cout<<"Case #"<<co<<": "; dquit(); cout << nl; // IMPORTANT FOR NEXT LINE co++; } // #ifndef ONLINE_JUDGE // cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms"; // #endif return 0; }
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define FAST \ std::ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pi acos(-1) #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define F first #define S second #define ppb pop_back #define GCD(m, n) __gcd(m, n) #define LCM(m, n) (m * n) / GCD(m, n) #define inputarr(a, n) \ for (int i = 0; i < n; ++i) \ cin >> a[i] #define initarr(a, n, x) \ for (int i = 0; i < n; ++i) \ a[i] = x #define rep(i, a, n) for (int i = a; i < n; i++) #define repe(i, a, n) for (int i = a; i <= n; i++) #define rev(i, a, b) for (int i = a; i >= b; i--) // #define int long long //////////// #define MOD 1000000007 //////// #define POD 998244353 //////// // define ll long long #define ld long double typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<vvvi> vvvvi; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<vvc> vvvc; typedef vector<pii> vpii; typedef vector<bool> vb; typedef vector<vb> vvb; typedef map<int, int> mii; typedef unordered_map<int, int> umii; typedef map<char, int> mci; #define itv for (auto it = v.begin(); it != v.end(); ++it) #define present(container, element) (container.find(element) != container.end()) #define cpresent(container, element) \ (find(ALL(container), element) != container.end()) // #define invect(data,n,commands) for(int i = 0;i<n;i++){int // tmp;cin>>tmp;data.pb(tmp);commands} #define inset(data,n,commands) for(int i // = 0;i<n;i++){int tmp;cin>>tmp;data.insert(tmp);commands} #define trav(a, x) // for(auto& a : x) #define display(x) trav(a,x) cout<<a<<" ";cout<<endl #define bs binary_search #define lb(v, val) lower_bound(ALL(v), val) #define ub(v, val) upper_bound(ALL(v), val) #define Max(x, y, z) max(x, max(y, z)) #define Min(x, y, z) min(x, min(y, z)) #define maxc(v) *max_element(ALL(v)) #define minc(v) *min_element(ALL(v)) /* #define dbg1(a) cout<<" *"<<a<<" "; #define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" " ///// #define dbg cout<<"move" */ #define vin(v, n) \ ; \ vi v(n); \ rep(i, 0, n) cin >> v[i]; #define dbg cerr << "At line " << __LINE__ << " move " << nl #define dbg1(x) cerr << "At line " << __LINE__ << " " << #x << "=" << x << nl #define dbg2(x, y) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << nl #define dbg3(x, y, z) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << " " << #z << "=" << z << nl #define prv(v) \ ; \ rep(i, 0, sz(v)) cerr << v[i] << " "; #define nl "\n" #define sz(s) (int)((s).size()) #define coutsp(k) \ cout << setprecision( \ k) // set precision (total length k icluding decimal and non decimal) #define coutfsp(k) \ cout << fixed << setprecision(k) // fixed set precision(after decimal fixing) #define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') #define INF (int)2e9 //------------------------ Global VARIABLE ------------------------------------ const int N = 12; const int M = 1000; int n, m; int a[M + 1]; vi C[M + 1]; // unordered_map<pair<int,int>,pii> dpm; int dpm[M + 1][4100]; // int valid[M+1][2050]={}; int tru = 1; //------------------------ Global VARIABLE ------------------------------------ int recurso(int n1, int vis) { if (dpm[n1][vis] != -1) return dpm[n1][vis]; if (vis == tru) return dpm[n1][vis] = 0; if (n1 <= 0) return dpm[n1][vis] = INF; // dbg2(n1,vis); int ans = INF, check = 0; int d = recurso(n1 - 1, vis); if (d != INF) ans = min(ans, d); int vis1 = vis; rep(i, 0, sz(C[n1])) vis1 |= (1 << (C[n1][i] - 1)); d = recurso(n1 - 1, vis1); if (d != INF) ans = min(ans, d + a[n1]); return dpm[n1][vis] = ans; } //------------------------------------------------------------------------------- void dquit() { cin >> n >> m; memset(dpm, -1, sizeof(dpm)); rep(i, 1, m + 1) { int b; cin >> a[i] >> b; rep(j, 0, b) { int c; cin >> c; C[i].pb(c); } } tru = (1 << n) - 1; int ans = recurso(m, 0); if (ans == INF) cout << -1; else { cout << ans; } } //------------------------------------------------------------------------------- signed main() { //-------------------------------- FAST // MAKE IT FAST --- //-------------------------------- /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// int t; t = 1; int co = 1; ///////---------------------------------||||| // cin>>t; ////////>>>>>>> //////----------------------------------||||| while (t--) { // cout<<"Case #"<<co<<": "; dquit(); cout << nl; // IMPORTANT FOR NEXT LINE co++; } // #ifndef ONLINE_JUDGE // cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms"; // #endif return 0; }
delete
184
190
184
184
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define FAST \ std::ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pi acos(-1) #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define F first #define S second #define ppb pop_back #define GCD(m, n) __gcd(m, n) #define LCM(m, n) (m * n) / GCD(m, n) #define inputarr(a, n) \ for (int i = 0; i < n; ++i) \ cin >> a[i] #define initarr(a, n, x) \ for (int i = 0; i < n; ++i) \ a[i] = x #define rep(i, a, n) for (int i = a; i < n; i++) #define repe(i, a, n) for (int i = a; i <= n; i++) #define rev(i, a, b) for (int i = a; i >= b; i--) // #define int long long //////////// #define MOD 1000000007 //////// #define POD 998244353 //////// // define ll long long #define ld long double typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<vvvi> vvvvi; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<vvc> vvvc; typedef vector<pii> vpii; typedef vector<bool> vb; typedef vector<vb> vvb; typedef map<int, int> mii; typedef unordered_map<int, int> umii; typedef map<char, int> mci; #define itv for (auto it = v.begin(); it != v.end(); ++it) #define present(container, element) (container.find(element) != container.end()) #define cpresent(container, element) \ (find(ALL(container), element) != container.end()) // #define invect(data,n,commands) for(int i = 0;i<n;i++){int // tmp;cin>>tmp;data.pb(tmp);commands} #define inset(data,n,commands) for(int i // = 0;i<n;i++){int tmp;cin>>tmp;data.insert(tmp);commands} #define trav(a, x) // for(auto& a : x) #define display(x) trav(a,x) cout<<a<<" ";cout<<endl #define bs binary_search #define lb(v, val) lower_bound(ALL(v), val) #define ub(v, val) upper_bound(ALL(v), val) #define Max(x, y, z) max(x, max(y, z)) #define Min(x, y, z) min(x, min(y, z)) #define maxc(v) *max_element(ALL(v)) #define minc(v) *min_element(ALL(v)) /* #define dbg1(a) cout<<" *"<<a<<" "; #define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" " ///// #define dbg cout<<"move" */ #define vin(v, n) \ ; \ vi v(n); \ rep(i, 0, n) cin >> v[i]; #define dbg cerr << "At line " << __LINE__ << " move " << nl #define dbg1(x) cerr << "At line " << __LINE__ << " " << #x << "=" << x << nl #define dbg2(x, y) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << nl #define dbg3(x, y, z) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << " " << #z << "=" << z << nl #define prv(v) \ ; \ rep(i, 0, sz(v)) cerr << v[i] << " "; #define nl "\n" #define sz(s) (int)((s).size()) #define coutsp(k) \ cout << setprecision( \ k) // set precision (total length k icluding decimal and non decimal) #define coutfsp(k) \ cout << fixed << setprecision(k) // fixed set precision(after decimal fixing) #define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') #define INF (int)2e9 //------------------------ Global VARIABLE ------------------------------------ const int N = 12; const int M = 1000; int n, m; int a[M + 1]; vi C[M + 1]; // unordered_map<pair<int,int>,pii> dpm; int dpm[M + 1][4100]; // int valid[M+1][2050]={}; int tru = 1; //------------------------ Global VARIABLE ------------------------------------ string N2B(int no) { string s = ""; while (no != 0) { if (no & 1) s += '1'; else s += '0'; no = no >> 1; } while (sz(s) != n) s += '0'; REVERSE(s); return s; } int B2N(string s, int m) { rep(i, 0, sz(C[m])) s[C[m][i] - 1] = '1'; REVERSE(s); int p = 1; int no = 0; rep(i, 0, sz(s)) { if (s[i] == '1') no += p; p *= 2; } return no; } //------------------------------------------------------------------- int recurso(int n1, int vis) { if (dpm[n1][vis] != -1) return dpm[n1][vis]; if (vis == tru) return dpm[n1][vis] = 0; if (n1 <= 0) return dpm[n1][vis] = INF; // dbg2(n1,vis); int ans = INF, check = 0; int d = recurso(n1 - 1, vis); if (d != INF) ans = min(ans, d); int vis1 = B2N(N2B(vis), n1); d = recurso(n1 - 1, vis1); if (d != INF) ans = min(ans, d + a[n1]); return dpm[n1][vis] = ans; } //------------------------------------------------------------------------------- void dquit() { cin >> n >> m; memset(dpm, -1, sizeof(dpm)); rep(i, 1, m + 1) { int b; cin >> a[i] >> b; rep(j, 0, b) { int c; cin >> c; C[i].pb(c); } } rep(i, 0, n) tru *= 2; tru--; int ans = recurso(m, 0); if (ans == INF) cout << -1; else { cout << ans; } } //------------------------------------------------------------------------------- signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif //-------------------------------- FAST // MAKE IT FAST --- //-------------------------------- /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// int t; t = 1; int co = 1; ///////---------------------------------||||| // cin>>t; ////////>>>>>>> //////----------------------------------||||| while (t--) { // cout<<"Case #"<<co<<": "; dquit(); cout << nl; // IMPORTANT FOR NEXT LINE co++; } // #ifndef ONLINE_JUDGE // cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms"; // #endif return 0; }
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define FAST \ std::ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define pi acos(-1) #define mp make_pair #define pb push_back #define ALL(v) v.begin(), v.end() #define SORT(v) sort(ALL(v)) #define REVERSE(v) reverse(ALL(v)) #define F first #define S second #define ppb pop_back #define GCD(m, n) __gcd(m, n) #define LCM(m, n) (m * n) / GCD(m, n) #define inputarr(a, n) \ for (int i = 0; i < n; ++i) \ cin >> a[i] #define initarr(a, n, x) \ for (int i = 0; i < n; ++i) \ a[i] = x #define rep(i, a, n) for (int i = a; i < n; i++) #define repe(i, a, n) for (int i = a; i <= n; i++) #define rev(i, a, b) for (int i = a; i >= b; i--) // #define int long long //////////// #define MOD 1000000007 //////// #define POD 998244353 //////// // define ll long long #define ld long double typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<vvvi> vvvvi; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<vvc> vvvc; typedef vector<pii> vpii; typedef vector<bool> vb; typedef vector<vb> vvb; typedef map<int, int> mii; typedef unordered_map<int, int> umii; typedef map<char, int> mci; #define itv for (auto it = v.begin(); it != v.end(); ++it) #define present(container, element) (container.find(element) != container.end()) #define cpresent(container, element) \ (find(ALL(container), element) != container.end()) // #define invect(data,n,commands) for(int i = 0;i<n;i++){int // tmp;cin>>tmp;data.pb(tmp);commands} #define inset(data,n,commands) for(int i // = 0;i<n;i++){int tmp;cin>>tmp;data.insert(tmp);commands} #define trav(a, x) // for(auto& a : x) #define display(x) trav(a,x) cout<<a<<" ";cout<<endl #define bs binary_search #define lb(v, val) lower_bound(ALL(v), val) #define ub(v, val) upper_bound(ALL(v), val) #define Max(x, y, z) max(x, max(y, z)) #define Min(x, y, z) min(x, min(y, z)) #define maxc(v) *max_element(ALL(v)) #define minc(v) *min_element(ALL(v)) /* #define dbg1(a) cout<<" *"<<a<<" "; #define dbg2(a,b) cout<<" *"<<a<<" **"<<b<<" " ///// #define dbg cout<<"move" */ #define vin(v, n) \ ; \ vi v(n); \ rep(i, 0, n) cin >> v[i]; #define dbg cerr << "At line " << __LINE__ << " move " << nl #define dbg1(x) cerr << "At line " << __LINE__ << " " << #x << "=" << x << nl #define dbg2(x, y) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << nl #define dbg3(x, y, z) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << " " << #z << "=" << z << nl #define prv(v) \ ; \ rep(i, 0, sz(v)) cerr << v[i] << " "; #define nl "\n" #define sz(s) (int)((s).size()) #define coutsp(k) \ cout << setprecision( \ k) // set precision (total length k icluding decimal and non decimal) #define coutfsp(k) \ cout << fixed << setprecision(k) // fixed set precision(after decimal fixing) #define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') #define INF (int)2e9 //------------------------ Global VARIABLE ------------------------------------ const int N = 12; const int M = 1000; int n, m; int a[M + 1]; vi C[M + 1]; // unordered_map<pair<int,int>,pii> dpm; int dpm[M + 1][4100]; // int valid[M+1][2050]={}; int tru = 1; //------------------------ Global VARIABLE ------------------------------------ string N2B(int no) { string s = ""; while (no != 0) { if (no & 1) s += '1'; else s += '0'; no = no >> 1; } while (sz(s) != n) s += '0'; REVERSE(s); return s; } int B2N(string s, int m) { rep(i, 0, sz(C[m])) s[C[m][i] - 1] = '1'; REVERSE(s); int p = 1; int no = 0; rep(i, 0, sz(s)) { if (s[i] == '1') no += p; p *= 2; } return no; } //------------------------------------------------------------------- int recurso(int n1, int vis) { if (dpm[n1][vis] != -1) return dpm[n1][vis]; if (vis == tru) return dpm[n1][vis] = 0; if (n1 <= 0) return dpm[n1][vis] = INF; // dbg2(n1,vis); int ans = INF, check = 0; int d = recurso(n1 - 1, vis); if (d != INF) ans = min(ans, d); int vis1 = B2N(N2B(vis), n1); d = recurso(n1 - 1, vis1); if (d != INF) ans = min(ans, d + a[n1]); return dpm[n1][vis] = ans; } //------------------------------------------------------------------------------- void dquit() { cin >> n >> m; memset(dpm, -1, sizeof(dpm)); rep(i, 1, m + 1) { int b; cin >> a[i] >> b; rep(j, 0, b) { int c; cin >> c; C[i].pb(c); } } rep(i, 0, n) tru *= 2; tru--; int ans = recurso(m, 0); if (ans == INF) cout << -1; else { cout << ans; } } //------------------------------------------------------------------------------- signed main() { //-------------------------------- FAST // MAKE IT FAST --- //-------------------------------- /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// int t; t = 1; int co = 1; ///////---------------------------------||||| // cin>>t; ////////>>>>>>> //////----------------------------------||||| while (t--) { // cout<<"Case #"<<co<<": "; dquit(); cout << nl; // IMPORTANT FOR NEXT LINE co++; } // #ifndef ONLINE_JUDGE // cerr<<(1000*clock())/CLOCKS_PER_SEC<<"ms"; // #endif return 0; }
delete
217
223
217
217
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; const int N = 13; const int M = 1001; int n, m, a, b, c; ii p[M]; int dp[1 << N]; int main() { freopen("in", "r", stdin); ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> a >> b; int mask = 0; for (int j = 0; j < b; ++j) { cin >> c; mask |= (1 << (c - 1)); } p[i] = {a, mask}; } memset(dp, 255, sizeof dp); dp[0] = 0; for (int ms = 0; ms < (1 << n) - 1; ++ms) { if (dp[ms] != -1) { for (int i = 0; i < m; ++i) { auto pa = p[i]; int cost = pa.first; int mm = pa.second; int nm = mm | ms; if (dp[nm] == -1 || dp[nm] > dp[ms] + cost) { dp[nm] = dp[ms] + cost; } } } } if (dp[(1 << n) - 1] == -1) { cout << -1 << endl; } else { cout << dp[(1 << n) - 1] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> ii; const int N = 13; const int M = 1001; int n, m, a, b, c; ii p[M]; int dp[1 << N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> a >> b; int mask = 0; for (int j = 0; j < b; ++j) { cin >> c; mask |= (1 << (c - 1)); } p[i] = {a, mask}; } memset(dp, 255, sizeof dp); dp[0] = 0; for (int ms = 0; ms < (1 << n) - 1; ++ms) { if (dp[ms] != -1) { for (int i = 0; i < m; ++i) { auto pa = p[i]; int cost = pa.first; int mm = pa.second; int nm = mm | ms; if (dp[nm] == -1 || dp[nm] > dp[ms] + cost) { dp[nm] = dp[ms] + cost; } } } } if (dp[(1 << n) - 1] == -1) { cout << -1 << endl; } else { cout << dp[(1 << n) - 1] << endl; } return 0; }
delete
14
15
14
14
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ull unsigned long long #define endl '\n' #define ll long long const int N = 3e2 + 5; ll a[N], dp[1 << 12]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (fopen(".INP", "r")) { freopen(".INP", "r", stdin); freopen(".OUT", "w", stdout); } ll n; cin >> n; ll m; cin >> m; dp[0] = 0; for (int i = 0; i < (1ll << n); i++) dp[i] = 1e18; for (int i = 1; i <= m; i++) { ll k; cin >> a[i] >> k; ll sum = 0; for (int j = 0; j < k; j++) { ll x; cin >> x; sum |= (1ll << (x - 1)); } for (int j = 1; j < (1ll << n); j++) { if ((sum & j) == j) { dp[j] = min(dp[j], a[i]); } } } for (int i = 1; i < (1ll << n); i++) { for (int j = 1; j <= i; j++) { if ((i & j) == j) { ll x = i; x ^= j; dp[i] = min(dp[i], dp[j] + dp[x]); } } } if (dp[(1ll << n) - 1] != 1e18) cout << dp[(1ll << n) - 1]; else cout << -1; }
#include <bits/stdc++.h> using namespace std; #define ull unsigned long long #define endl '\n' #define ll long long const int N = 1e3 + 5; ll a[N], dp[1 << 12]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (fopen(".INP", "r")) { freopen(".INP", "r", stdin); freopen(".OUT", "w", stdout); } ll n; cin >> n; ll m; cin >> m; dp[0] = 0; for (int i = 0; i < (1ll << n); i++) dp[i] = 1e18; for (int i = 1; i <= m; i++) { ll k; cin >> a[i] >> k; ll sum = 0; for (int j = 0; j < k; j++) { ll x; cin >> x; sum |= (1ll << (x - 1)); } for (int j = 1; j < (1ll << n); j++) { if ((sum & j) == j) { dp[j] = min(dp[j], a[i]); } } } for (int i = 1; i < (1ll << n); i++) { for (int j = 1; j <= i; j++) { if ((i & j) == j) { ll x = i; x ^= j; dp[i] = min(dp[i], dp[j] + dp[x]); } } } if (dp[(1ll << n) - 1] != 1e18) cout << dp[(1ll << n) - 1]; else cout << -1; }
replace
6
7
6
7
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) // [a, b) #define reprev(i, n) reprrev(i, 0, n) typedef long long ll; typedef unsigned long long ull; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } /* attention long longのシフト演算には気をつけよう タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも sizeは(int)とキャストしよう cin.tie(0); ios::sync_with_stdio(false);<- これら、printfとかと併用しない方が良さそう */ const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; /* <--------------------------------------------> */ typedef tuple<int, int, int> T; // get<K>(tuple型の変数) // 線分 ab の偏角 返り値は[-π, π] double argument(const pair<double, double> &a, const pair<double, double> &b) { double ax = a.first, ay = a.second, bx = b.first, by = b.second; return atan2(by - ay, bx - ax); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector<int> c(M, 0); vector<int> a(M, 0); rep(i, M) { cin >> a[i]; int b; cin >> b; rep(j, b) { int d; cin >> d; --d; c[i] |= (1 << d); } } const ll inf = 1LL << 60; vector<vector<ll>> dp(M + 5, vector<ll>(1 << N, inf)); dp[0][0] = 0; rep(i, M) rep(S, 1 << 1 << N) { if (dp[i][S] == inf) continue; // not use chmin(dp[i + 1][S], dp[i][S]); // use chmin(dp[i + 1][S | c[i]], dp[i][S] + a[i]); } ll ans = dp[M][(1 << N) - 1]; if (ans == inf) cout << -1 << endl; else cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) // [a, b) #define reprev(i, n) reprrev(i, 0, n) typedef long long ll; typedef unsigned long long ull; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } /* attention long longのシフト演算には気をつけよう タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも sizeは(int)とキャストしよう cin.tie(0); ios::sync_with_stdio(false);<- これら、printfとかと併用しない方が良さそう */ const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; /* <--------------------------------------------> */ typedef tuple<int, int, int> T; // get<K>(tuple型の変数) // 線分 ab の偏角 返り値は[-π, π] double argument(const pair<double, double> &a, const pair<double, double> &b) { double ax = a.first, ay = a.second, bx = b.first, by = b.second; return atan2(by - ay, bx - ax); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector<int> c(M, 0); vector<int> a(M, 0); rep(i, M) { cin >> a[i]; int b; cin >> b; rep(j, b) { int d; cin >> d; --d; c[i] |= (1 << d); } } const ll inf = 1LL << 60; vector<vector<ll>> dp(M + 5, vector<ll>(1 << N, inf)); dp[0][0] = 0; rep(i, M) rep(S, 1 << N) { // if(dp[i][S]==inf) continue; // not use chmin(dp[i + 1][S], dp[i][S]); // use chmin(dp[i + 1][S | c[i]], dp[i][S] + a[i]); } ll ans = dp[M][(1 << N) - 1]; if (ans == inf) cout << -1 << endl; else cout << ans << endl; return 0; }
replace
92
96
92
95
-6
munmap_chunk(): invalid pointer
p02901
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) #define reprev(i, n) reprrev(i, 0, n) typedef long long ll; typedef unsigned long long ull; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } /* attention long longのシフト演算には気をつけよう タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも sizeは(int)とキャストしよう ごちゃごちゃ場合分けを考える前に全探索は考えましたか? */ const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } ll power(ll x, ll n) { if (n == 0) return 1; ll res = power(x, n / 2); if (n % 2 == 0) return res * res; else return res * res * x; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; /* <--------------------------------------------> */ typedef tuple<ll, ll, ll> T; // get<K>(tuple型の変数) int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector<int> a(M); vector<int> c(M); rep(i, M) { cin >> a[i]; int b; cin >> b; int t = 0; rep(j, b) { int v; cin >> v; --v; t += 1 << (N - 1 - v); } c[i] = t; } const ll inf = 1LL << 60; vector<vector<ll>> dp(M + 5, vector<ll>(1 << N, inf)); dp[0][0] = 0; rep(i, M) rep(S, 1 << N) { chmin(dp[i + 1][S], dp[i][S]); for (int T = c[i]; T > 0; T = (T - 1) & c[i]) chmin(dp[i + 1][S], dp[i][S ^ T] + a[i]); } if (dp[M][(1 << N) - 1] == inf) cout << -1 << endl; else cout << dp[M][(1 << N) - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) #define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) #define reprev(i, n) reprrev(i, 0, n) typedef long long ll; typedef unsigned long long ull; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } /* attention long longのシフト演算には気をつけよう タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも sizeは(int)とキャストしよう ごちゃごちゃ場合分けを考える前に全探索は考えましたか? */ const ll mod = 1e9 + 7; void chmod(ll &M) { if (M >= mod) M %= mod; else if (M < 0) { M += (abs(M) / mod + 1) * mod; M %= mod; } } ll modpow(ll x, ll n) { if (n == 0) return 1; ll res = modpow(x, n / 2); if (n % 2 == 0) return res * res % mod; else return res * res % mod * x % mod; } ll power(ll x, ll n) { if (n == 0) return 1; ll res = power(x, n / 2); if (n % 2 == 0) return res * res; else return res * res * x; } int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; }; int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; }; /* <--------------------------------------------> */ typedef tuple<ll, ll, ll> T; // get<K>(tuple型の変数) int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector<int> a(M); vector<int> c(M); rep(i, M) { cin >> a[i]; int b; cin >> b; int t = 0; rep(j, b) { int v; cin >> v; --v; t += 1 << (N - 1 - v); } c[i] = t; } const ll inf = 1LL << 60; vector<vector<ll>> dp(M + 5, vector<ll>(1 << N, inf)); dp[0][0] = 0; rep(i, M) rep(S, 1 << N) { chmin(dp[i + 1][S], dp[i][S]); if ((S | c[i]) > S) continue; for (int T = c[i]; T > 0; T = (T - 1) & c[i]) chmin(dp[i + 1][S], dp[i][S ^ T] + a[i]); } if (dp[M][(1 << N) - 1] == inf) cout << -1 << endl; else cout << dp[M][(1 << N) - 1] << endl; return 0; }
insert
99
99
99
101
TLE
p02901
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <vector> #define int long long #define ll long long #define F first #define S second #define make make_pair #define pb push_back #define sz(x) (ll)(x.size()) #define all(x) x.begin(), x.end() using namespace std; typedef long double ld; const int MOD = (int)1e9 + 7; const int N = (int)1e5 * 5 + 22; const ll INF = (ll)1e18; const double eps = (double)1e-6; int n, m, a[N], b[N], c[100][1111]; int dp[N]; signed main() { #ifdef Maga freopen("input.txt", "r", stdin); #endif ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int mask = 0; mask <= (1 << n) - 1; ++mask) { dp[mask] = INF; } dp[0] = 0; for (int i = 1; i <= m; ++i) { cin >> a[i] >> b[i]; for (int j = 1; j <= b[i]; ++j) { cin >> c[i][j]; } } for (int i = 1; i <= m; ++i) { for (int mask = 0; mask <= (1 << (n)) - 1; ++mask) { int x = mask; for (int j = 1; j <= b[i]; ++j) { x = (x | (1 << (c[i][j] - 1))); } dp[x] = min(dp[mask] + a[i], dp[x]); } } if (dp[(1 << n) - 1] >= INF) cout << -1; else cout << dp[(1 << (n)) - 1]; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <vector> #define int long long #define ll long long #define F first #define S second #define make make_pair #define pb push_back #define sz(x) (ll)(x.size()) #define all(x) x.begin(), x.end() using namespace std; typedef long double ld; const int MOD = (int)1e9 + 7; const int N = (int)1e5 * 5 + 22; const ll INF = (ll)1e18; const double eps = (double)1e-6; int n, m, a[N], b[N], c[1111][1111]; int dp[N]; signed main() { #ifdef Maga freopen("input.txt", "r", stdin); #endif ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int mask = 0; mask <= (1 << n) - 1; ++mask) { dp[mask] = INF; } dp[0] = 0; for (int i = 1; i <= m; ++i) { cin >> a[i] >> b[i]; for (int j = 1; j <= b[i]; ++j) { cin >> c[i][j]; } } for (int i = 1; i <= m; ++i) { for (int mask = 0; mask <= (1 << (n)) - 1; ++mask) { int x = mask; for (int j = 1; j <= b[i]; ++j) { x = (x | (1 << (c[i][j] - 1))); } dp[x] = min(dp[mask] + a[i], dp[x]); } } if (dp[(1 << n) - 1] >= INF) cout << -1; else cout << dp[(1 << (n)) - 1]; return 0; }
replace
55
56
55
56
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> #define INF 999999999 using namespace std; typedef long long ll; typedef pair<int, int> P; int main() { int n, m; cin >> n >> m; vector<P> key; for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b; int d = 0; for (int j = 0; j < b; j++) { cin >> c; c--; d += (1 << c); } key.push_back(P(a, d)); } int dp[10][4100]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 4100; j++) dp[i][j] = INF; } dp[0][0] = 0; for (int i = 1; i <= m; i++) { for (int j = 0; j < 4096; j++) { dp[i][j | key[i - 1].second] = min(dp[i][j | key[i - 1].second], dp[i - 1][j] + key[i - 1].first); dp[i][j] = min(dp[i][j], dp[i - 1][j]); } } if (dp[m][(1 << n) - 1] == INF) cout << -1 << endl; else cout << dp[m][(1 << n) - 1] << endl; return 0; }
#include <bits/stdc++.h> #define INF 999999999 using namespace std; typedef long long ll; typedef pair<int, int> P; int main() { int n, m; cin >> n >> m; vector<P> key; for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b; int d = 0; for (int j = 0; j < b; j++) { cin >> c; c--; d += (1 << c); } key.push_back(P(a, d)); } int dp[1005][4100]; for (int i = 0; i < 1005; i++) { for (int j = 0; j < 4100; j++) dp[i][j] = INF; } dp[0][0] = 0; for (int i = 1; i <= m; i++) { for (int j = 0; j < 4096; j++) { dp[i][j | key[i - 1].second] = min(dp[i][j | key[i - 1].second], dp[i - 1][j] + key[i - 1].first); dp[i][j] = min(dp[i][j], dp[i - 1][j]); } } if (dp[m][(1 << n) - 1] == INF) cout << -1 << endl; else cout << dp[m][(1 << n) - 1] << endl; return 0; }
replace
22
24
22
24
0
p02901
C++
Runtime Error
/* * じょえチャンネル * 高評価・チャンネル登録よろしくおねがいします! * https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ */ #include <bits/stdc++.h> // #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") // here!!! // define int long long !!!!! #define int long long // define int long long !!!!! #define mod (int)((1e9) + 7) // constexpr int mod = 998244353ll; #ifdef int #define inf (int)(3e18) #else #define inf (int)(5e8) #endif #define intt long long #define itn long long #define P pair<long long, long long> #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) #define rev_rep(i, n) for (int i = n - 1; i >= 0; i--) #define REV_REP(i, n) for (int i = n; i >= 1; i--) #define ALL(v) v.begin(), v.end() #define smallpriority_queue(x) priority_queue<x, vector<x>, greater<x>> using namespace std; // Library // モッドパウ inline int modpow(int x, int y, int m = mod) { int res = 1; while (y) { if (y & 1) { res *= x; res %= m; } x = x * x % m; y /= 2; } return res; } int mypow(int x, int y) { int res = 1; while (y) { if (y % 2) { res *= x; } x = x * x; y /= 2; } return res; } // is the number (x) a prime number? bool prime(int x) { if (!x || x == 1) { return false; } for (int i = 2; i * i <= x; i++) { if (!(x % i)) { return false; } } return true; } // saidai-kouyakusuu inline int gcd(int x, int y) { if (!y) { return x; } return gcd(y, x % y); } // number of keta int keta(int x) { int ans = 0; while (x) { x /= 10; ans++; } return ans; } // sum of keta int ketasum(int x) { int ans = 0; while (x) { ans += x % 10; x /= 10; } return ans; } inline int lcm(int x, int y) { int ans = x / gcd(x, y) * y; return ans; } int twobeki(int x) { int ans = 0; while (1) { if (!(x & 1)) { ans++; x >>= 1; } else { break; } } return ans; } template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } void Yes() { cout << "Yes" << endl; } void No() { cout << "No" << endl; } void YES() { cout << "YES" << endl; } void NO() { cout << "NO" << endl; } #define fin(i) scanf("%lld", &i) #define fout(i) printf("%lld", i) #define fendl printf("\n") int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int comb(int x, int y) { if (y > x) return 0; // cout<<kai(x, y)<<' '<<modpow(kai(y, y), mod - 2)<<endl; return kai(x, y) * modpow(kai(y, y), mod - 2) % mod; } // Library-End #define vecin(v) \ for (int i = 0; i < v.size(); i++) \ scanf("%lld", &v[i]); #define vecout(v) \ { \ for (int i = 0; i < (int)v.size(); i++) \ printf("%lld ", v[i]); \ printf("\n"); \ } template <typename T> class kaageSegTree { protected: unsigned int n = 1, rank = 0; std::vector<T> node; T nodee; virtual T nodef(const T &, const T &) const = 0; public: kaageSegTree(unsigned int m, T init, T nodee) : nodee(nodee) { while (n < m) { n *= 2; rank++; } node.resize(2 * n); for (unsigned int i = n; i < 2 * n; i++) node[i] = init; } kaageSegTree(const std::vector<T> &initvec, T nodee) : nodee(nodee) { unsigned int m = initvec.size(); while (n < m) { n *= 2; rank++; } node.resize(2 * n); for (unsigned int i = n; i < 2 * n; i++) { if (i - n < m) node[i] = initvec[i - n]; } } virtual void update(int i, T x) { i += n; node[i] = x; while (i != 1) { i >>= 1; node[i] = nodef(node[2 * i], node[2 * i + 1]); } } virtual T query(int l, int r) { l += n; r += n; T ls = nodee, rs = nodee; while (l < r) { if (l & 1) { ls = nodef(ls, node[l]); l++; } if (r & 1) { r--; rs = nodef(node[r], rs); } l >>= 1; r >>= 1; } return nodef(ls, rs); } virtual T operator[](const int &x) { return node[n + x]; } void fill(T x) { std::fill(ALL(node), x); } void print() { rep(i, n) std::cout << operator[](i) << " "; std::cout << std::endl; } }; class RSQ : public kaageSegTree<int> { int nodef(const int &lhs, const int &rhs) const { return lhs + rhs; } public: RSQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, 0) {} RSQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, 0) {} }; class RMiQ : public kaageSegTree<int> { int nodef(const int &lhs, const int &rhs) const { return std::min(lhs, rhs); } public: RMiQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, inf) {} RMiQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, inf) {} }; class RMaQ : public kaageSegTree<int> { int nodef(const int &lhs, const int &rhs) const { return std::max(lhs, rhs); } public: RMaQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, -inf) {} RMaQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, -inf) {} }; template <typename T, typename U> class IntervalSegTree : public kaageSegTree<T> { protected: using kaageSegTree<T>::n; using kaageSegTree<T>::rank; using kaageSegTree<T>::node; using kaageSegTree<T>::nodef; using kaageSegTree<T>::nodee; std::vector<U> lazy; std::vector<bool> lazyflag; std::vector<int> width; virtual void lazyf(U &, const U &) = 0; virtual void updf(T &, const U &, const unsigned int &) = 0; void eval(int k) { for (int i = rank; i > 0; i--) { int nk = k >> i; if (lazyflag[nk]) { updf(node[2 * nk], lazy[nk], width[2 * nk]); updf(node[2 * nk + 1], lazy[nk], width[2 * nk + 1]); if (lazyflag[2 * nk]) lazyf(lazy[2 * nk], lazy[nk]); else lazy[2 * nk] = lazy[nk]; if (lazyflag[2 * nk + 1]) lazyf(lazy[2 * nk + 1], lazy[nk]); else lazy[2 * nk + 1] = lazy[nk]; lazyflag[2 * nk] = lazyflag[2 * nk + 1] = true; lazyflag[nk] = false; } } } public: IntervalSegTree(unsigned int m, T init, T nodee) : kaageSegTree<T>(m, init, nodee) { lazy.resize(2 * n); lazyflag.resize(2 * n); width.resize(2 * n); width[1] = n; for (unsigned int i = 2; i < 2 * n; i++) { width[i] = width[i >> 1] >> 1; } } IntervalSegTree(T nodee, const std::vector<T> &initvec) : kaageSegTree<T>(initvec, nodee) { lazy.resize(2 * n); lazyflag.resize(2 * n); width.resize(2 * n); width[1] = n; for (unsigned int i = 2; i < 2 * n; i++) { width[i] = width[i >> 1] >> 1; } } void update(int i, U x) { i += n; eval(i); updf(node[i], x, width[i]); if (lazyflag[i]) lazyf(lazy[i], x); else { lazyflag[i] = true; lazy[i] = x; } while (i /= 2) node[i] = nodef(node[2 * i], node[2 * i + 1]); } void update(int l, int r, U x) { l += n; r += n; int nl = l, nr = r; while (!(nl & 1)) nl >>= 1; while (!(nr & 1)) nr >>= 1; nr--; eval(nl); eval(nr); while (l < r) { if (l & 1) { updf(node[l], x, width[l]); if (lazyflag[l]) lazyf(lazy[l], x); else { lazyflag[l] = true; lazy[l] = x; } l++; } if (r & 1) { r--; updf(node[r], x, width[r]); if (lazyflag[r]) lazyf(lazy[r], x); else { lazyflag[r] = true; lazy[r] = x; } } l >>= 1; r >>= 1; } while (nl >>= 1) node[nl] = nodef(node[2 * nl], node[2 * nl + 1]); while (nr >>= 1) node[nr] = nodef(node[2 * nr], node[2 * nr + 1]); } T query(int l, int r) { l += n; r += n; eval(l); eval(r - 1); int ls = nodee, rs = nodee; while (l < r) { if (l & 1) { ls = nodef(ls, node[l]); l++; } if (r & 1) { r--; rs = nodef(node[r], rs); } l >>= 1; r >>= 1; } return nodef(ls, rs); } T operator[](const int &x) { eval(n + x); return node[n + x]; } T queryForAll() { return node[1]; } }; class RAQRSQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return a + b; } void lazyf(int &a, const int &b) { a += b; } void updf(int &a, const int &b, const unsigned int &width) { a += width * b; } public: RAQRSQ(int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, 0) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RAQRSQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>((int)0, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; class RAQRMiQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return std::min(a, b); } void lazyf(int &a, const int &b) { a += b; } void updf(int &a, const int &b, const unsigned int &width) { a += b; } public: RAQRMiQ(int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, inf) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RAQRMiQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>(inf, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; class RAQRMaQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return std::max(a, b); } void lazyf(int &a, const int &b) { a += b; } void updf(int &a, const int &b, const unsigned int &width) { a += b; } public: RAQRMaQ(unsigned int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, -inf) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RAQRMaQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>(-inf, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; class RUQRSQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return a + b; } void lazyf(int &a, const int &b) { a = b; } void updf(int &a, const int &b, const unsigned int &width) { a = width * b; } public: RUQRSQ(int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, 0) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RUQRSQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>((int)0, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; class RUQRMiQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return std::min(a, b); } void lazyf(int &a, const int &b) { a = b; } void updf(int &a, const int &b, const unsigned int &width) { a = b; } public: RUQRMiQ(int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, inf) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RUQRMiQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>(inf, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; class RUQRMaQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return std::max(a, b); } void lazyf(int &a, const int &b) { a = b; } void updf(int &a, const int &b, const unsigned int &width) { a = b; } public: RUQRMaQ(int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, -inf) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RUQRMaQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>(-inf, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; ////SegTree // template <class T> // class SegTree { // int n; // 葉の数 // vector<T> node; // データを格納するvector // T def; // 初期値かつ単位元 // function<T(T, T)> operation; // 区間クエリで使う処理 // function<T(T, T)> update; // 点更新で使う処理 // // // 区間[a,b)の総和。ノードk=[l,r)に着目している。 // T _query(int a, int b, int k, int l, int r) { // if (r <= a || b <= l) return def; // 交差しない // if (a <= l && r <= b) // return node[k]; // a,l,r,bの順で完全に含まれる // else { // T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 // T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 // return operation(c1, c2); // } // } // // public: // // _n:必要サイズ, _def:初期値かつ単位元, _operation:クエリ関数, // // _update:更新関数 // SegTree(size_t _n, T _def, function<T(T, T)> _operation, // function<T(T, T)> _update) // : def(_def), operation(_operation), update(_update) { // n = 1; // while (n < _n) { // n *= 2; // } // node = vector<T>(2 * n , def); // } // // // 場所i(0-indexed)の値をxで更新 // void change(int i, T x) { // i += n - 1; // node[i] = update(node[i], x); // while (i > 0) { // i = (i - 1) / 2; // node[i] = operation(node[i * 2 + 1], node[i * 2 + 2]); // } // } // // // [a, b)の区間クエリを実行 // T query(int a, int b) { // return _query(a, b, 0, 0, n); // } // // // 添字でアクセス // T operator[](int i) { // return node[i + n - 1]; // } // }; template <class T> class SegTree { int n; vector<T> node; T def; function<T(T, T)> operation; function<T(T, T)> update; public: SegTree(unsigned int _n, T _def, function<T(T, T)> _operation, function<T(T, T)> _update) : def(_def), operation(_operation), update(_update) { n = 1; while (n < _n) { n *= 2; } node = vector<T>(n * 2, def); } SegTree(vector<int> &initvec, function<T(T, T)> _operation, function<T(T, T)> _update) : operation(_operation), update(_update) { n = 1; while (n < initvec.size()) { n *= 2; } node = vector<T>(n * 2, def); for (int i = n; i < n + initvec.size(); i++) { node[i] = initvec[i - n]; } for (int i = n - 1; i >= 1; i--) { node[i] = operation(node[i * 2], node[i * 2 + 1]); } } void change(int i, T x) { i += n; node[i] = update(node[i], x); while (i >= 1) { i >>= 1; node[i] = operation(node[i * 2], node[i * 2 + 1]); } } T query(int l, int r) { l += n; r += n; T rx = def, lx = def; while (l < r) { if (l & 1) { lx = operation(lx, node[l]); l++; } if (r & 1) { r--; rx = operation(node[r], rx); } l >>= 1; r >>= 1; } return operation(lx, rx); } T operator[](const int &x) { return node[x - n]; } void fill(T x) { std::fill(ALL(node), x); } void print() { rep(i, n) std::cout << operator[](i) << " "; std::cout << std::endl; } }; #define R_MIN ([](long long a, long long b) { return min(a, b); }) #define R_MAX ([](long long a, long long b) { return max(a, b); }) #define R_SUM ([](long long a, long long b) { return a + b; }) #define NORMAL_UPDATE ([](long long a, long long b) { return b; }) #define ADD_UPDATE ([](long long a, long long b) { return a + b; }) #define MINUS_UPDATE ([](long long a, long long b) { return a - b; } class Union_Find { vector<int> par; vector<int> rankmy; vector<int> ookisa; public: Union_Find(int size) { par = vector<int>(size); rankmy = vector<int>(size); ookisa = vector<int>(size); for (int i = 0; i < size; i++) { par[i] = i; ookisa[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (rankmy[x] < rankmy[y]) { par[x] = y; ookisa[y] += ookisa[x]; ookisa[x] = 0; } else { par[y] = x; ookisa[x] += ookisa[y]; ookisa[y] = 0; if (rankmy[x] == rankmy[y]) { rankmy[x]++; } } } int size(int i) { i = find(i); return ookisa[i]; } bool same(int x, int y) { return find(x) == find(y); } }; class BIT { vector<int> data; int size = 0; public: BIT(int _size) { data = vector<int>(_size + 1); size = _size; } void add(int i, int x) { while (i <= size) { data[i] += x; i += i & -i; } } int sum(int i) { assert(i <= size); int s = 0; while (i > 0) { s += data[i]; i -= i & -i; } return s; } int lower_bound(int x) { if (x <= 0) { return 0; } else { int i = 0; int r = 1; while (r < size) r = r << 1; for (int len = r; len > 0; len = len >> 1) { if (i + len < size && data[i + len] < x) { x -= data[i + len]; i += len; } } return i + 1; } } }; // Union-Find-End int perm[2000005]; void init_perm() { perm[0] = 1; REP(i, 2000004) { perm[i] = perm[i - 1] * i % mod; } } int nCk(int x, int y) { if (y > x) { return 0; } return perm[x] * modpow(perm[x - y], mod - 2) % mod * modpow(perm[y], mod - 2) % mod; } double kyori(pair<int, int> f, pair<int, int> s) { double ans = 0; double t = fabs(f.first - s.first); double y = fabs(f.second - s.second); ans = sqrt(t * t + y * y); return ans; } inline string stringmax(string &x, string &y) { if (x.size() > y.size()) { return x; } if (x.size() < y.size()) { return y; } rep(i, x.size()) { if (x[i] > y[i]) { return x; } if (x[i] < y[i]) { return y; } } return x; } vector<int> RollingHash(string &s, string &t) { vector<int> ans; __int128 h = 0, hamod = 0, ki = 0, kim = 0, hikaku = 0, one = 0; one = 1; ki = 1000000007ll; hamod = (one << 61) - 1; kim = 1; rep(i, t.size()) { hikaku *= ki; h *= ki; kim *= ki; hikaku += t[i]; h += s[i]; hikaku %= hamod; h %= hamod; kim %= hamod; } rep(i, (int)s.size() - (int)t.size() + 1) { if (h == hikaku) { ans.emplace_back(i); } h *= ki; h %= hamod; h += s[i + (int)t.size()]; h %= hamod; h += hamod; h -= s[i] * kim % hamod; h %= hamod; } return ans; } struct edge { int to; int length; edge(int _to, int _length) { to = _to; length = _length; } }; vector<int> djkstra(vector<vector<edge>> &road, int start) { vector<int> kyo(road.size(), inf); smallpriority_queue(P) q; q.push({0, start}); kyo[start] = 0; while (q.size()) { int x = q.top().second; itn now = q.top().first; q.pop(); if (kyo[x] < now) { continue; } for (auto &i : road[x]) { if (kyo[i.to] > now + i.length) { kyo[i.to] = now + i.length; q.push({kyo[i.to], i.to}); } } } return kyo; } #define endl "\n" // interactive の時に注意!!! #define printd cout << fixed << setprecision(10) itn n, m, a[1004], b[1004], c[1004][20], dp[100004]; vector<P> v; signed main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); cin >> n >> m; fill(dp, dp + 100004, inf); dp[0] = 0; rep(i, m) { cin >> a[i] >> b[i]; int k = 0; rep(j, b[i]) { cin >> c[i][j]; c[i][j]--; k += (1ll << c[i][j]); } for (int j = (1ll << (m)) - 1; j >= 0; j--) { chmin(dp[j | k], dp[j] + a[i]); } } if (dp[(1ll << (n)) - 1] != inf) cout << dp[(1ll << (n)) - 1] << endl; else cout << -1 << endl; }
/* * じょえチャンネル * 高評価・チャンネル登録よろしくおねがいします! * https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ */ #include <bits/stdc++.h> // #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") // here!!! // define int long long !!!!! #define int long long // define int long long !!!!! #define mod (int)((1e9) + 7) // constexpr int mod = 998244353ll; #ifdef int #define inf (int)(3e18) #else #define inf (int)(5e8) #endif #define intt long long #define itn long long #define P pair<long long, long long> #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) #define rev_rep(i, n) for (int i = n - 1; i >= 0; i--) #define REV_REP(i, n) for (int i = n; i >= 1; i--) #define ALL(v) v.begin(), v.end() #define smallpriority_queue(x) priority_queue<x, vector<x>, greater<x>> using namespace std; // Library // モッドパウ inline int modpow(int x, int y, int m = mod) { int res = 1; while (y) { if (y & 1) { res *= x; res %= m; } x = x * x % m; y /= 2; } return res; } int mypow(int x, int y) { int res = 1; while (y) { if (y % 2) { res *= x; } x = x * x; y /= 2; } return res; } // is the number (x) a prime number? bool prime(int x) { if (!x || x == 1) { return false; } for (int i = 2; i * i <= x; i++) { if (!(x % i)) { return false; } } return true; } // saidai-kouyakusuu inline int gcd(int x, int y) { if (!y) { return x; } return gcd(y, x % y); } // number of keta int keta(int x) { int ans = 0; while (x) { x /= 10; ans++; } return ans; } // sum of keta int ketasum(int x) { int ans = 0; while (x) { ans += x % 10; x /= 10; } return ans; } inline int lcm(int x, int y) { int ans = x / gcd(x, y) * y; return ans; } int twobeki(int x) { int ans = 0; while (1) { if (!(x & 1)) { ans++; x >>= 1; } else { break; } } return ans; } template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } void Yes() { cout << "Yes" << endl; } void No() { cout << "No" << endl; } void YES() { cout << "YES" << endl; } void NO() { cout << "NO" << endl; } #define fin(i) scanf("%lld", &i) #define fout(i) printf("%lld", i) #define fendl printf("\n") int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int comb(int x, int y) { if (y > x) return 0; // cout<<kai(x, y)<<' '<<modpow(kai(y, y), mod - 2)<<endl; return kai(x, y) * modpow(kai(y, y), mod - 2) % mod; } // Library-End #define vecin(v) \ for (int i = 0; i < v.size(); i++) \ scanf("%lld", &v[i]); #define vecout(v) \ { \ for (int i = 0; i < (int)v.size(); i++) \ printf("%lld ", v[i]); \ printf("\n"); \ } template <typename T> class kaageSegTree { protected: unsigned int n = 1, rank = 0; std::vector<T> node; T nodee; virtual T nodef(const T &, const T &) const = 0; public: kaageSegTree(unsigned int m, T init, T nodee) : nodee(nodee) { while (n < m) { n *= 2; rank++; } node.resize(2 * n); for (unsigned int i = n; i < 2 * n; i++) node[i] = init; } kaageSegTree(const std::vector<T> &initvec, T nodee) : nodee(nodee) { unsigned int m = initvec.size(); while (n < m) { n *= 2; rank++; } node.resize(2 * n); for (unsigned int i = n; i < 2 * n; i++) { if (i - n < m) node[i] = initvec[i - n]; } } virtual void update(int i, T x) { i += n; node[i] = x; while (i != 1) { i >>= 1; node[i] = nodef(node[2 * i], node[2 * i + 1]); } } virtual T query(int l, int r) { l += n; r += n; T ls = nodee, rs = nodee; while (l < r) { if (l & 1) { ls = nodef(ls, node[l]); l++; } if (r & 1) { r--; rs = nodef(node[r], rs); } l >>= 1; r >>= 1; } return nodef(ls, rs); } virtual T operator[](const int &x) { return node[n + x]; } void fill(T x) { std::fill(ALL(node), x); } void print() { rep(i, n) std::cout << operator[](i) << " "; std::cout << std::endl; } }; class RSQ : public kaageSegTree<int> { int nodef(const int &lhs, const int &rhs) const { return lhs + rhs; } public: RSQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, 0) {} RSQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, 0) {} }; class RMiQ : public kaageSegTree<int> { int nodef(const int &lhs, const int &rhs) const { return std::min(lhs, rhs); } public: RMiQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, inf) {} RMiQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, inf) {} }; class RMaQ : public kaageSegTree<int> { int nodef(const int &lhs, const int &rhs) const { return std::max(lhs, rhs); } public: RMaQ(int size, const int &def = 0) : kaageSegTree<int>(size, def, -inf) {} RMaQ(const std::vector<int> &initvec) : kaageSegTree<int>(initvec, -inf) {} }; template <typename T, typename U> class IntervalSegTree : public kaageSegTree<T> { protected: using kaageSegTree<T>::n; using kaageSegTree<T>::rank; using kaageSegTree<T>::node; using kaageSegTree<T>::nodef; using kaageSegTree<T>::nodee; std::vector<U> lazy; std::vector<bool> lazyflag; std::vector<int> width; virtual void lazyf(U &, const U &) = 0; virtual void updf(T &, const U &, const unsigned int &) = 0; void eval(int k) { for (int i = rank; i > 0; i--) { int nk = k >> i; if (lazyflag[nk]) { updf(node[2 * nk], lazy[nk], width[2 * nk]); updf(node[2 * nk + 1], lazy[nk], width[2 * nk + 1]); if (lazyflag[2 * nk]) lazyf(lazy[2 * nk], lazy[nk]); else lazy[2 * nk] = lazy[nk]; if (lazyflag[2 * nk + 1]) lazyf(lazy[2 * nk + 1], lazy[nk]); else lazy[2 * nk + 1] = lazy[nk]; lazyflag[2 * nk] = lazyflag[2 * nk + 1] = true; lazyflag[nk] = false; } } } public: IntervalSegTree(unsigned int m, T init, T nodee) : kaageSegTree<T>(m, init, nodee) { lazy.resize(2 * n); lazyflag.resize(2 * n); width.resize(2 * n); width[1] = n; for (unsigned int i = 2; i < 2 * n; i++) { width[i] = width[i >> 1] >> 1; } } IntervalSegTree(T nodee, const std::vector<T> &initvec) : kaageSegTree<T>(initvec, nodee) { lazy.resize(2 * n); lazyflag.resize(2 * n); width.resize(2 * n); width[1] = n; for (unsigned int i = 2; i < 2 * n; i++) { width[i] = width[i >> 1] >> 1; } } void update(int i, U x) { i += n; eval(i); updf(node[i], x, width[i]); if (lazyflag[i]) lazyf(lazy[i], x); else { lazyflag[i] = true; lazy[i] = x; } while (i /= 2) node[i] = nodef(node[2 * i], node[2 * i + 1]); } void update(int l, int r, U x) { l += n; r += n; int nl = l, nr = r; while (!(nl & 1)) nl >>= 1; while (!(nr & 1)) nr >>= 1; nr--; eval(nl); eval(nr); while (l < r) { if (l & 1) { updf(node[l], x, width[l]); if (lazyflag[l]) lazyf(lazy[l], x); else { lazyflag[l] = true; lazy[l] = x; } l++; } if (r & 1) { r--; updf(node[r], x, width[r]); if (lazyflag[r]) lazyf(lazy[r], x); else { lazyflag[r] = true; lazy[r] = x; } } l >>= 1; r >>= 1; } while (nl >>= 1) node[nl] = nodef(node[2 * nl], node[2 * nl + 1]); while (nr >>= 1) node[nr] = nodef(node[2 * nr], node[2 * nr + 1]); } T query(int l, int r) { l += n; r += n; eval(l); eval(r - 1); int ls = nodee, rs = nodee; while (l < r) { if (l & 1) { ls = nodef(ls, node[l]); l++; } if (r & 1) { r--; rs = nodef(node[r], rs); } l >>= 1; r >>= 1; } return nodef(ls, rs); } T operator[](const int &x) { eval(n + x); return node[n + x]; } T queryForAll() { return node[1]; } }; class RAQRSQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return a + b; } void lazyf(int &a, const int &b) { a += b; } void updf(int &a, const int &b, const unsigned int &width) { a += width * b; } public: RAQRSQ(int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, 0) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RAQRSQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>((int)0, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; class RAQRMiQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return std::min(a, b); } void lazyf(int &a, const int &b) { a += b; } void updf(int &a, const int &b, const unsigned int &width) { a += b; } public: RAQRMiQ(int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, inf) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RAQRMiQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>(inf, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; class RAQRMaQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return std::max(a, b); } void lazyf(int &a, const int &b) { a += b; } void updf(int &a, const int &b, const unsigned int &width) { a += b; } public: RAQRMaQ(unsigned int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, -inf) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RAQRMaQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>(-inf, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; class RUQRSQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return a + b; } void lazyf(int &a, const int &b) { a = b; } void updf(int &a, const int &b, const unsigned int &width) { a = width * b; } public: RUQRSQ(int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, 0) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RUQRSQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>((int)0, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; class RUQRMiQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return std::min(a, b); } void lazyf(int &a, const int &b) { a = b; } void updf(int &a, const int &b, const unsigned int &width) { a = b; } public: RUQRMiQ(int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, inf) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RUQRMiQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>(inf, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; class RUQRMaQ : public IntervalSegTree<int, int> { int nodef(const int &a, const int &b) const { return std::max(a, b); } void lazyf(int &a, const int &b) { a = b; } void updf(int &a, const int &b, const unsigned int &width) { a = b; } public: RUQRMaQ(int size, const int &def = 0) : IntervalSegTree<int, int>(size, def, -inf) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } RUQRMaQ(const std::vector<int> &initvec) : IntervalSegTree<int, int>(-inf, initvec) { for (int i = n - 1; i > 0; i--) node[i] = nodef(node[2 * i], node[2 * i + 1]); } }; ////SegTree // template <class T> // class SegTree { // int n; // 葉の数 // vector<T> node; // データを格納するvector // T def; // 初期値かつ単位元 // function<T(T, T)> operation; // 区間クエリで使う処理 // function<T(T, T)> update; // 点更新で使う処理 // // // 区間[a,b)の総和。ノードk=[l,r)に着目している。 // T _query(int a, int b, int k, int l, int r) { // if (r <= a || b <= l) return def; // 交差しない // if (a <= l && r <= b) // return node[k]; // a,l,r,bの順で完全に含まれる // else { // T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 // T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 // return operation(c1, c2); // } // } // // public: // // _n:必要サイズ, _def:初期値かつ単位元, _operation:クエリ関数, // // _update:更新関数 // SegTree(size_t _n, T _def, function<T(T, T)> _operation, // function<T(T, T)> _update) // : def(_def), operation(_operation), update(_update) { // n = 1; // while (n < _n) { // n *= 2; // } // node = vector<T>(2 * n , def); // } // // // 場所i(0-indexed)の値をxで更新 // void change(int i, T x) { // i += n - 1; // node[i] = update(node[i], x); // while (i > 0) { // i = (i - 1) / 2; // node[i] = operation(node[i * 2 + 1], node[i * 2 + 2]); // } // } // // // [a, b)の区間クエリを実行 // T query(int a, int b) { // return _query(a, b, 0, 0, n); // } // // // 添字でアクセス // T operator[](int i) { // return node[i + n - 1]; // } // }; template <class T> class SegTree { int n; vector<T> node; T def; function<T(T, T)> operation; function<T(T, T)> update; public: SegTree(unsigned int _n, T _def, function<T(T, T)> _operation, function<T(T, T)> _update) : def(_def), operation(_operation), update(_update) { n = 1; while (n < _n) { n *= 2; } node = vector<T>(n * 2, def); } SegTree(vector<int> &initvec, function<T(T, T)> _operation, function<T(T, T)> _update) : operation(_operation), update(_update) { n = 1; while (n < initvec.size()) { n *= 2; } node = vector<T>(n * 2, def); for (int i = n; i < n + initvec.size(); i++) { node[i] = initvec[i - n]; } for (int i = n - 1; i >= 1; i--) { node[i] = operation(node[i * 2], node[i * 2 + 1]); } } void change(int i, T x) { i += n; node[i] = update(node[i], x); while (i >= 1) { i >>= 1; node[i] = operation(node[i * 2], node[i * 2 + 1]); } } T query(int l, int r) { l += n; r += n; T rx = def, lx = def; while (l < r) { if (l & 1) { lx = operation(lx, node[l]); l++; } if (r & 1) { r--; rx = operation(node[r], rx); } l >>= 1; r >>= 1; } return operation(lx, rx); } T operator[](const int &x) { return node[x - n]; } void fill(T x) { std::fill(ALL(node), x); } void print() { rep(i, n) std::cout << operator[](i) << " "; std::cout << std::endl; } }; #define R_MIN ([](long long a, long long b) { return min(a, b); }) #define R_MAX ([](long long a, long long b) { return max(a, b); }) #define R_SUM ([](long long a, long long b) { return a + b; }) #define NORMAL_UPDATE ([](long long a, long long b) { return b; }) #define ADD_UPDATE ([](long long a, long long b) { return a + b; }) #define MINUS_UPDATE ([](long long a, long long b) { return a - b; } class Union_Find { vector<int> par; vector<int> rankmy; vector<int> ookisa; public: Union_Find(int size) { par = vector<int>(size); rankmy = vector<int>(size); ookisa = vector<int>(size); for (int i = 0; i < size; i++) { par[i] = i; ookisa[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (rankmy[x] < rankmy[y]) { par[x] = y; ookisa[y] += ookisa[x]; ookisa[x] = 0; } else { par[y] = x; ookisa[x] += ookisa[y]; ookisa[y] = 0; if (rankmy[x] == rankmy[y]) { rankmy[x]++; } } } int size(int i) { i = find(i); return ookisa[i]; } bool same(int x, int y) { return find(x) == find(y); } }; class BIT { vector<int> data; int size = 0; public: BIT(int _size) { data = vector<int>(_size + 1); size = _size; } void add(int i, int x) { while (i <= size) { data[i] += x; i += i & -i; } } int sum(int i) { assert(i <= size); int s = 0; while (i > 0) { s += data[i]; i -= i & -i; } return s; } int lower_bound(int x) { if (x <= 0) { return 0; } else { int i = 0; int r = 1; while (r < size) r = r << 1; for (int len = r; len > 0; len = len >> 1) { if (i + len < size && data[i + len] < x) { x -= data[i + len]; i += len; } } return i + 1; } } }; // Union-Find-End int perm[2000005]; void init_perm() { perm[0] = 1; REP(i, 2000004) { perm[i] = perm[i - 1] * i % mod; } } int nCk(int x, int y) { if (y > x) { return 0; } return perm[x] * modpow(perm[x - y], mod - 2) % mod * modpow(perm[y], mod - 2) % mod; } double kyori(pair<int, int> f, pair<int, int> s) { double ans = 0; double t = fabs(f.first - s.first); double y = fabs(f.second - s.second); ans = sqrt(t * t + y * y); return ans; } inline string stringmax(string &x, string &y) { if (x.size() > y.size()) { return x; } if (x.size() < y.size()) { return y; } rep(i, x.size()) { if (x[i] > y[i]) { return x; } if (x[i] < y[i]) { return y; } } return x; } vector<int> RollingHash(string &s, string &t) { vector<int> ans; __int128 h = 0, hamod = 0, ki = 0, kim = 0, hikaku = 0, one = 0; one = 1; ki = 1000000007ll; hamod = (one << 61) - 1; kim = 1; rep(i, t.size()) { hikaku *= ki; h *= ki; kim *= ki; hikaku += t[i]; h += s[i]; hikaku %= hamod; h %= hamod; kim %= hamod; } rep(i, (int)s.size() - (int)t.size() + 1) { if (h == hikaku) { ans.emplace_back(i); } h *= ki; h %= hamod; h += s[i + (int)t.size()]; h %= hamod; h += hamod; h -= s[i] * kim % hamod; h %= hamod; } return ans; } struct edge { int to; int length; edge(int _to, int _length) { to = _to; length = _length; } }; vector<int> djkstra(vector<vector<edge>> &road, int start) { vector<int> kyo(road.size(), inf); smallpriority_queue(P) q; q.push({0, start}); kyo[start] = 0; while (q.size()) { int x = q.top().second; itn now = q.top().first; q.pop(); if (kyo[x] < now) { continue; } for (auto &i : road[x]) { if (kyo[i.to] > now + i.length) { kyo[i.to] = now + i.length; q.push({kyo[i.to], i.to}); } } } return kyo; } #define endl "\n" // interactive の時に注意!!! #define printd cout << fixed << setprecision(10) itn n, m, a[1004], b[1004], c[1004][20], dp[100004]; vector<P> v; signed main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); cin >> n >> m; fill(dp, dp + 100004, inf); dp[0] = 0; rep(i, m) { cin >> a[i] >> b[i]; int k = 0; rep(j, b[i]) { cin >> c[i][j]; c[i][j]--; k += (1ll << c[i][j]); } for (int j = (1ll << (n)) - 1; j >= 0; j--) { chmin(dp[j | k], dp[j] + a[i]); } } if (dp[(1ll << (n)) - 1] != inf) cout << dp[(1ll << (n)) - 1] << endl; else cout << -1 << endl; }
replace
839
840
839
840
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> #define f(i, n) for (int i = 0; i < (n); i++) #define inf (int)(3e18) #define int long long #define mod (int)(1000000007) #define intt long long using namespace std; // Library // モッドパウ int modpow(int x, int y, int m = mod) { int res = 1; while (y) { if (y % 2) { res *= x; res %= m; } x = x * x % mod; y /= 2; } return res; } int mypow(int x, int y) { int res = 1; while (y) { if (y % 2) { res *= x; } x = x * x; y /= 2; } return res; } // is the number (x) a prime number? bool prime(int x) { for (int i = 2; i * i <= x; i++) { if (!(x % i)) { return false; } } return true; } double kyori(pair<int, int> f, pair<int, int> s) { double ans = 0; double t = fabs(f.first - s.first); double y = fabs(f.second - s.second); ans = sqrt(t * t + y * y); return ans; } // saidai-kouyakusuu inline int gcd(int x, int y) { if (!y) { return x; } return gcd(y, x % y); } // Union-Find Tree class Union_Find { vector<int> par; vector<int> rankmy; public: Union_Find(int size) { par = vector<int>(size); rankmy = vector<int>(size); for (int i = 0; i < size; i++) { par[i] = i; } } int find(int x) { if (par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (rankmy[x] < rankmy[y]) { par[x] = y; } else { par[y] = x; if (rankmy[x] == rankmy[y]) { rankmy[x]++; } } } bool same(int x, int y) { return find(x) == find(y); } }; // Union-Find-End // SegTree template <class T> class SegTree { int n; // 葉の数 vector<T> data; // データを格納するvector T def; // 初期値かつ単位元 function<T(T, T)> operation; // 区間クエリで使う処理 function<T(T, T)> update; // 点更新で使う処理 // 区間[a,b)の総和。ノードk=[l,r)に着目している。 T _query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return def; // 交差しない if (a <= l && r <= b) return data[k]; // a,l,r,bの順で完全に含まれる else { T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return operation(c1, c2); } } public: // _n:必要サイズ, _def:初期値かつ単位元, _operation:クエリ関数, // _update:更新関数 SegTree(size_t _n, T _def, function<T(T, T)> _operation, function<T(T, T)> _update) : def(_def), operation(_operation), update(_update) { n = 1; while (n < _n) { n *= 2; } data = vector<T>(2 * n - 1, def); } // 場所i(0-indexed)の値をxで更新 void change(int i, T x) { i += n - 1; data[i] = update(data[i], x); while (i > 0) { i = (i - 1) / 2; data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]); } } // [a, b)の区間クエリを実行 T query(int a, int b) { return _query(a, b, 0, 0, n); } // 添字でアクセス T operator[](int i) { return data[i + n - 1]; } }; #define R_MIN ([](long long a, long long b) { return min(a, b); }) #define R_MAX ([](long long a, long long b) { return max(a, b); }) #define R_SUM ([](long long a, long long b) { return a + b; }) #define NORMAL_UPDATE ([](long long a, long long b) { return b; }) #define ADD_UPDATE ([](long long a, long long b) { return a + b; }) #define MINUS_UPDATE ([](long long a, long long b) { return a - b; } // Seg-Tree-End // dfs // vector<int> v[100004]; // bool went[100004]; // void dfs(int x) { // went[x] = true; // for (int i = 0; i < (int)v[x].size(); i++) { // if (!went[v[x][i]]) { // dfs(v[x][i]); // } // } // } // number of keta int keta(int x) { int ans = 0; while (x) { x /= 10; ans++; } return ans; } // sum of keta int ketasum(int x) { int ans = 0; while (x) { ans += x % 10; x /= 10; } return ans; } inline int lcm(int x, int y) { int ans = x / gcd(x, y) * y; return ans; } int twobeki(int x) { int ans = 0; while (1) { if (!(x & 1)) { ans++; x /= 2; } else { break; } } return ans; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int comb(int x, int y) { if (y > x) return 0; return kai(x, y) * modpow(kai(y, y), mod - 2) % mod; } int fukugen(vector<int> l) { int ans = 0; for (int i = 0; i < (int)l.size(); i++) { ans *= 10; ans += l[i]; } return ans; } int nanjyo(int n) { int ans = 0; while (n > 1) { ans++; n /= 2; } return ans; } bool intersect(pair<pair<int, int>, pair<int, int>> p1, pair<pair<int, int>, pair<int, int>> p2) { int men11 = (p2.first.first - p1.first.first) * (p2.second.second - p1.first.second) - (p2.second.first - p1.first.first) * (p2.first.second - p1.first.second); int men12 = (p2.first.first - p1.second.first) * (p2.second.second - p1.second.second) - (p2.second.first - p1.second.first) * (p2.first.second - p1.second.second); int men21 = (p1.first.first - p2.first.first) * (p1.second.second - p2.first.second) - (p1.second.first - p2.first.first) * (p1.first.second - p2.first.second); int men22 = (p1.first.first - p2.second.first) * (p1.second.second - p2.second.second) - (p1.second.first - p2.second.first) * (p1.first.second - p2.second.second); return ((signbit(men11)) ^ (signbit(men12))) && ((signbit(men21)) ^ (signbit(men22))); } template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } void Yes() { cout << "Yes" << endl; } void No() { cout << "No" << endl; } void YES() { cout << "YES" << endl; } void NO() { cout << "NO" << endl; } #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) // Library-End int n, m, a[1004], b[1004], c[1004], dp[104][10004]; signed main() { cin >> n >> m; rep(i, m) { cin >> a[i] >> b[i]; int d; rep(j, b[i]) { cin >> d; d--; c[i] += (1 << d); } } rep(i, m + 1) { rep(j, (1 << n)) { dp[i][j] = inf; } } dp[0][0] = 0; rep(i, m) { rep(j, (1 << n)) { chmin(dp[i + 1][j], dp[i][j]); int nj = j | c[i]; chmin(dp[i + 1][nj], dp[i][j] + a[i]); } } cout << (dp[m][(1 << n) - 1] < inf ? dp[m][(1 << n) - 1] : -1) << endl; }
#include <bits/stdc++.h> #define f(i, n) for (int i = 0; i < (n); i++) #define inf (int)(3e18) #define int long long #define mod (int)(1000000007) #define intt long long using namespace std; // Library // モッドパウ int modpow(int x, int y, int m = mod) { int res = 1; while (y) { if (y % 2) { res *= x; res %= m; } x = x * x % mod; y /= 2; } return res; } int mypow(int x, int y) { int res = 1; while (y) { if (y % 2) { res *= x; } x = x * x; y /= 2; } return res; } // is the number (x) a prime number? bool prime(int x) { for (int i = 2; i * i <= x; i++) { if (!(x % i)) { return false; } } return true; } double kyori(pair<int, int> f, pair<int, int> s) { double ans = 0; double t = fabs(f.first - s.first); double y = fabs(f.second - s.second); ans = sqrt(t * t + y * y); return ans; } // saidai-kouyakusuu inline int gcd(int x, int y) { if (!y) { return x; } return gcd(y, x % y); } // Union-Find Tree class Union_Find { vector<int> par; vector<int> rankmy; public: Union_Find(int size) { par = vector<int>(size); rankmy = vector<int>(size); for (int i = 0; i < size; i++) { par[i] = i; } } int find(int x) { if (par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) { return; } if (rankmy[x] < rankmy[y]) { par[x] = y; } else { par[y] = x; if (rankmy[x] == rankmy[y]) { rankmy[x]++; } } } bool same(int x, int y) { return find(x) == find(y); } }; // Union-Find-End // SegTree template <class T> class SegTree { int n; // 葉の数 vector<T> data; // データを格納するvector T def; // 初期値かつ単位元 function<T(T, T)> operation; // 区間クエリで使う処理 function<T(T, T)> update; // 点更新で使う処理 // 区間[a,b)の総和。ノードk=[l,r)に着目している。 T _query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return def; // 交差しない if (a <= l && r <= b) return data[k]; // a,l,r,bの順で完全に含まれる else { T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子 T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子 return operation(c1, c2); } } public: // _n:必要サイズ, _def:初期値かつ単位元, _operation:クエリ関数, // _update:更新関数 SegTree(size_t _n, T _def, function<T(T, T)> _operation, function<T(T, T)> _update) : def(_def), operation(_operation), update(_update) { n = 1; while (n < _n) { n *= 2; } data = vector<T>(2 * n - 1, def); } // 場所i(0-indexed)の値をxで更新 void change(int i, T x) { i += n - 1; data[i] = update(data[i], x); while (i > 0) { i = (i - 1) / 2; data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]); } } // [a, b)の区間クエリを実行 T query(int a, int b) { return _query(a, b, 0, 0, n); } // 添字でアクセス T operator[](int i) { return data[i + n - 1]; } }; #define R_MIN ([](long long a, long long b) { return min(a, b); }) #define R_MAX ([](long long a, long long b) { return max(a, b); }) #define R_SUM ([](long long a, long long b) { return a + b; }) #define NORMAL_UPDATE ([](long long a, long long b) { return b; }) #define ADD_UPDATE ([](long long a, long long b) { return a + b; }) #define MINUS_UPDATE ([](long long a, long long b) { return a - b; } // Seg-Tree-End // dfs // vector<int> v[100004]; // bool went[100004]; // void dfs(int x) { // went[x] = true; // for (int i = 0; i < (int)v[x].size(); i++) { // if (!went[v[x][i]]) { // dfs(v[x][i]); // } // } // } // number of keta int keta(int x) { int ans = 0; while (x) { x /= 10; ans++; } return ans; } // sum of keta int ketasum(int x) { int ans = 0; while (x) { ans += x % 10; x /= 10; } return ans; } inline int lcm(int x, int y) { int ans = x / gcd(x, y) * y; return ans; } int twobeki(int x) { int ans = 0; while (1) { if (!(x & 1)) { ans++; x /= 2; } else { break; } } return ans; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= i; res %= mod; } return res; } int comb(int x, int y) { if (y > x) return 0; return kai(x, y) * modpow(kai(y, y), mod - 2) % mod; } int fukugen(vector<int> l) { int ans = 0; for (int i = 0; i < (int)l.size(); i++) { ans *= 10; ans += l[i]; } return ans; } int nanjyo(int n) { int ans = 0; while (n > 1) { ans++; n /= 2; } return ans; } bool intersect(pair<pair<int, int>, pair<int, int>> p1, pair<pair<int, int>, pair<int, int>> p2) { int men11 = (p2.first.first - p1.first.first) * (p2.second.second - p1.first.second) - (p2.second.first - p1.first.first) * (p2.first.second - p1.first.second); int men12 = (p2.first.first - p1.second.first) * (p2.second.second - p1.second.second) - (p2.second.first - p1.second.first) * (p2.first.second - p1.second.second); int men21 = (p1.first.first - p2.first.first) * (p1.second.second - p2.first.second) - (p1.second.first - p2.first.first) * (p1.first.second - p2.first.second); int men22 = (p1.first.first - p2.second.first) * (p1.second.second - p2.second.second) - (p1.second.first - p2.second.first) * (p1.first.second - p2.second.second); return ((signbit(men11)) ^ (signbit(men12))) && ((signbit(men21)) ^ (signbit(men22))); } template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } void Yes() { cout << "Yes" << endl; } void No() { cout << "No" << endl; } void YES() { cout << "YES" << endl; } void NO() { cout << "NO" << endl; } #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) // Library-End int n, m, a[1004], b[1004], c[1004], dp[1004][5004]; signed main() { cin >> n >> m; rep(i, m) { cin >> a[i] >> b[i]; int d; rep(j, b[i]) { cin >> d; d--; c[i] += (1 << d); } } rep(i, m + 1) { rep(j, (1 << n)) { dp[i][j] = inf; } } dp[0][0] = 0; rep(i, m) { rep(j, (1 << n)) { chmin(dp[i + 1][j], dp[i][j]); int nj = j | c[i]; chmin(dp[i + 1][nj], dp[i][j] + a[i]); } } cout << (dp[m][(1 << n) - 1] < inf ? dp[m][(1 << n) - 1] : -1) << endl; }
replace
289
290
289
290
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } int N, M; int A[1005], B[1005]; int C[1005][15]; int main() { const int INF = 1e9; cin >> N >> M; for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; for (int j = 0; j < B[i]; j++) { cin >> C[i][j]; } } vector<vector<int>> dp(15, vector<int>(5000, INF)); for (int i = 0; i < M; i++) { dp[i][0] = 0; } for (int i = 0; i < M; i++) { int key = 0; for (int j = 0; j < B[i]; j++) { key |= (1 << (C[i][j] - 1)); } for (int bits = 0; bits < (1 << N); bits++) { chmin(dp[i + 1][bits], dp[i][bits]); chmin(dp[i + 1][bits | key], dp[i][bits] + A[i]); } } int ans = dp[M][(1 << N) - 1]; if (ans == INF) { ans = -1; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } int N, M; int A[1005], B[1005]; int C[1005][15]; int main() { const int INF = 1e9; cin >> N >> M; for (int i = 0; i < M; i++) { cin >> A[i] >> B[i]; for (int j = 0; j < B[i]; j++) { cin >> C[i][j]; } } vector<vector<int>> dp(1005, vector<int>(5000, INF)); for (int i = 0; i <= M; i++) { dp[i][0] = 0; } for (int i = 0; i < M; i++) { int key = 0; for (int j = 0; j < B[i]; j++) { key |= (1 << (C[i][j] - 1)); } for (int bits = 0; bits < (1 << N); bits++) { chmin(dp[i + 1][bits], dp[i][bits]); chmin(dp[i + 1][bits | key], dp[i][bits] + A[i]); } } int ans = dp[M][(1 << N) - 1]; if (ans == INF) { ans = -1; } cout << ans << endl; return 0; }
replace
27
29
27
29
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int L = 12; int dp[1 << L]; void solve() { int n, m; cin >> n >> m; const int MSK = 1 << n; memset(dp, INF, sizeof dp); dp[0] = 0; for (int _ = 0; _ < m; _++) { int cost, _n; cin >> cost >> _n; int k = 0; for (int __ = 0; __ < _n; __++) { int x; cin >> x; k |= 1 << (x - 1); } for (int msk = MSK; msk >= 0; msk--) { dp[msk | k] = min(dp[msk | k], dp[msk] + cost); } } if (dp[MSK - 1] == INF) dp[MSK - 1] = -1; cout << dp[MSK - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); cout << endl; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int L = 12; int dp[1 << L]; void solve() { int n, m; cin >> n >> m; const int MSK = 1 << n; memset(dp, INF, sizeof dp); dp[0] = 0; for (int _ = 0; _ < m; _++) { int cost, _n; cin >> cost >> _n; int k = 0; for (int __ = 0; __ < _n; __++) { int x; cin >> x; k |= 1 << (x - 1); } for (int msk = MSK - 1; msk >= 0; msk--) { dp[msk | k] = min(dp[msk | k], dp[msk] + cost); } } if (dp[MSK - 1] == INF) dp[MSK - 1] = -1; cout << dp[MSK - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); cout << endl; }
replace
25
26
25
26
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; #define REP(i, n) for (int i = 0; i < n; i++) #define REP2(i, a, b) for (int i = a; i <= b; i++) #define REPR(i, a, b) for (int i = a; i >= b; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define INF 2e9 #define MOD 1000000007 #define PI 3.14159265358979323846 #define vi vector<int> #define vll vector<ll> #define vi2 vector<vector<int>> #define eb emplace_back #define fi first #define se second #define ALL(v) v.begin(), v.end() #define sz(x) int(x.size()) using namespace std; using P = pair<ll, ll>; const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1}; #define INT(name) \ int name; \ cin >> name; #define VEC(type, name, n) \ vector<type> name(n); \ REP(i, n) cin >> name[i]; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { INT(N); INT(M); vector<P> vec(M); REP(i, M) { int a, b; cin >> a >> b; vec[i].fi = a; int cnt = 0; REP(j, b) { int x; cin >> x; x--; cnt += pow(2, x); } vec[i].se = cnt; } vector<vector<ll>> dp(1010, vector<ll>(2100, INF)); dp[0][0] = 0; REP(i, M) { REP(j, 1 << N) { chmin(dp[i + 1][j | vec[i].se], dp[i][j] + vec[i].fi); chmin(dp[i + 1][j], dp[i][j]); } } if (dp[M][(1 << N) - 1] < 1000000000) { cout << dp[M][(1 << N) - 1] << endl; } else { cout << -1 << endl; } }
#include <bits/stdc++.h> typedef long long ll; #define REP(i, n) for (int i = 0; i < n; i++) #define REP2(i, a, b) for (int i = a; i <= b; i++) #define REPR(i, a, b) for (int i = a; i >= b; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define INF 2e9 #define MOD 1000000007 #define PI 3.14159265358979323846 #define vi vector<int> #define vll vector<ll> #define vi2 vector<vector<int>> #define eb emplace_back #define fi first #define se second #define ALL(v) v.begin(), v.end() #define sz(x) int(x.size()) using namespace std; using P = pair<ll, ll>; const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1}; #define INT(name) \ int name; \ cin >> name; #define VEC(type, name, n) \ vector<type> name(n); \ REP(i, n) cin >> name[i]; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { INT(N); INT(M); vector<P> vec(M); REP(i, M) { int a, b; cin >> a >> b; vec[i].fi = a; int cnt = 0; REP(j, b) { int x; cin >> x; x--; cnt += pow(2, x); } vec[i].se = cnt; } vector<vector<ll>> dp(1010, vector<ll>(4200, INF)); dp[0][0] = 0; REP(i, M) { REP(j, 1 << N) { chmin(dp[i + 1][j | vec[i].se], dp[i][j] + vec[i].fi); chmin(dp[i + 1][j], dp[i][j]); } } if (dp[M][(1 << N) - 1] < 1000000000) { cout << dp[M][(1 << N) - 1] << endl; } else { cout << -1 << endl; } }
replace
60
61
60
61
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int main() { std::ios::sync_with_stdio(false); using namespace std; const int max_v = 100'000, max_n = 12, max_cost = max_v * max_n; int n, m, a, b, c; cin >> n >> m; vector<int> A(n), B(n, 0), C(1 << n, max_cost + 1); for (int i = 0; i < m; i++) { cin >> A[i] >> b; for (int j = 0; j < b; j++) { cin >> c; B[i] += (1 << (c - 1)); } } C[0] = 0; for (int i = 1; i < (1 << n); i++) { for (int j = 0; j < m; j++) C[i] = min(C[i], C[i & (~B[j])] + A[j]); } int res = C[(1 << n) - 1]; cout << (res == max_cost + 1 ? -1 : res) << "\n"; return 0; }
#include <bits/stdc++.h> #define ll long long int main() { std::ios::sync_with_stdio(false); using namespace std; const int max_v = 100'000, max_n = 12, max_cost = max_v * max_n; int n, m, a, b, c; cin >> n >> m; vector<int> A(m), B(m, 0), C(1 << n, max_cost + 1); for (int i = 0; i < m; i++) { cin >> A[i] >> b; for (int j = 0; j < b; j++) { cin >> c; B[i] += (1 << (c - 1)); } } C[0] = 0; for (int i = 1; i < (1 << n); i++) { for (int j = 0; j < m; j++) C[i] = min(C[i], C[i & (~B[j])] + A[j]); } int res = C[(1 << n) - 1]; cout << (res == max_cost + 1 ? -1 : res) << "\n"; return 0; }
replace
10
11
10
11
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // from https://atcoder.jp/contests/abc142/submissions/7731316 typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } int n, m; int a[1050], b[1050], c[1050][20]; void input() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a[i] >> b[i]; for (int j = 1; j <= b[i]; j++) cin >> c[i][j]; } } void solve() { input(); const int inf = 1e9; vector<int> dp(5000, inf); dp[0] = 0; for (int item = 1; item <= m; item++) { int now = 0; for (int j = 0; j <= b[item]; j++) { now |= (1 << (c[item][j] - 1)); } for (int bits = 0; bits < (1 << n); bits++) { chmin(dp[bits | now], dp[bits] + a[item]); } } if (dp[(1 << n) - 1] == inf) dp[(1 << n) - 1] = -1; cout << dp[(1 << n) - 1] << endl; } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; // from https://atcoder.jp/contests/abc142/submissions/7731316 typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } int n, m; int a[1050], b[1050], c[1050][20]; void input() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a[i] >> b[i]; for (int j = 1; j <= b[i]; j++) cin >> c[i][j]; } } void solve() { input(); const int inf = 1e9; vector<int> dp(5000, inf); dp[0] = 0; for (int item = 1; item <= m; item++) { int now = 0; for (int j = 1; j <= b[item]; j++) { now |= (1 << (c[item][j] - 1)); } for (int bits = 0; bits < (1 << n); bits++) { chmin(dp[bits | now], dp[bits] + a[item]); } } if (dp[(1 << n) - 1] == inf) dp[(1 << n) - 1] = -1; cout << dp[(1 << n) - 1] << endl; } int main() { solve(); return 0; }
replace
43
44
43
44
-11
p02901
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, m; int dp[1010][1 << 12]; int v[1010]; int val[1010]; int f(int pos, int mask) { if (pos == m) { if (mask == (1 << n) - 1) return 0; return 1e9; } int &ans = dp[pos][mask]; ans = 1e9; ans = min(ans, f(pos + 1, mask | v[pos]) + val[pos]); ans = min(ans, f(pos + 1, mask)); return ans; } int main() { memset(dp, -1, sizeof dp); scanf("%d %d", &n, &m); for (int i = 0; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); val[i] = a; for (int j = 0; j < b; ++j) { int x; scanf("%d", &x); x--; v[i] |= (1 << x); } } int ans = f(0, 0); if (ans == 1e9) puts("-1"); else printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int dp[1010][1 << 12]; int v[1010]; int val[1010]; int f(int pos, int mask) { if (pos == m) { if (mask == (1 << n) - 1) return 0; return 1e9; } int &ans = dp[pos][mask]; if (ans != -1) return ans; ans = 1e9; ans = min(ans, f(pos + 1, mask | v[pos]) + val[pos]); ans = min(ans, f(pos + 1, mask)); return ans; } int main() { memset(dp, -1, sizeof dp); scanf("%d %d", &n, &m); for (int i = 0; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); val[i] = a; for (int j = 0; j < b; ++j) { int x; scanf("%d", &x); x--; v[i] |= (1 << x); } } int ans = f(0, 0); if (ans == 1e9) puts("-1"); else printf("%d\n", ans); return 0; }
insert
16
16
16
18
TLE
p02901
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstdint> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; #define MP make_pair #define PB push_back #define inf 1000000007 #define mod 1000000007 #define rep(i, n) for (int i = 0; i < (int)(n); ++i) int main() { int n, m; cin >> n >> m; vector<int> a(n), b(m); vector<int> c(m); rep(i, m) { cin >> a[i] >> b[i]; rep(j, b[i]) { int k; cin >> k; k--; c[i] ^= (1 << k); } } vector<int> dp(1 << n, inf); dp[0] = 0; rep(i, 1 << n) { if (dp[i] != inf) { rep(j, m) { dp[i | c[j]] = min(dp[i | c[j]], dp[i] + a[j]); } } } if (dp[(1 << n) - 1] == inf) { cout << -1 << endl; } else { cout << dp[(1 << n) - 1] << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstdint> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; #define MP make_pair #define PB push_back #define inf 1000000007 #define mod 1000000007 #define rep(i, n) for (int i = 0; i < (int)(n); ++i) int main() { int n, m; cin >> n >> m; vector<int> a(m), b(m); vector<int> c(m); rep(i, m) { cin >> a[i] >> b[i]; rep(j, b[i]) { int k; cin >> k; k--; c[i] ^= (1 << k); } } vector<int> dp(1 << n, inf); dp[0] = 0; rep(i, 1 << n) { if (dp[i] != inf) { rep(j, m) { dp[i | c[j]] = min(dp[i | c[j]], dp[i] + a[j]); } } } if (dp[(1 << n) - 1] == inf) { cout << -1 << endl; } else { cout << dp[(1 << n) - 1] << endl; } return 0; }
replace
33
34
33
34
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> II; typedef vector<II> VII; typedef vector<int> VI; typedef vector<VI> VVI; typedef long long int LL; typedef unsigned long long int ULL; #define PB push_back #define MP make_pair #define F first #define S second #define SZ(a) (int)(a.size()) #define ALL(a) a.begin(), a.end() #define SET(a, b) memset(a, b, sizeof(a)) #define LET(x, a) __typeof(a) x(a) #define rep(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) // Works for forward as well as backward iteration #define gu getchar #define pu putchar #define si(n) scanf("%d", &n) #define dout(n) printf("%d\n", n) #define sll(n) scanf("%lld", &n) #define lldout(n) printf("%lld\n", n) #define DRT() \ int t; \ si(t); \ while (t--) #define PlUSWRAP(index, n) \ index = (index + 1) % n // index++; if(index>=n) index=0 #define MINUSWRAP(index, n) \ index = (index + n - 1) % n // index--; if(index<0) index=n-1 #define ROUNDOFFINT(d) \ d = (int)((double)d + 0.5) // Round off d to nearest integer #define FLUSHN while (gu() != '\n') #define FLUSHS while (gu() != ' ') #define TRACE #ifdef TRACE #define trace1(x) cerr << #x << ": " << x << endl; #define trace2(x, y) \ cerr << #x << ": " << x << " | " << #y << ": " << y << endl; #define trace3(x, y, z) \ cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << endl; #define trace4(a, b, c, d) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << endl; #define trace5(a, b, c, d, e) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl; #define trace6(a, b, c, d, e, f) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \ << #f << ": " << f << endl; #else #define trace1(x) #define trace2(x, y) #define trace3(x, y, z) #define trace4(a, b, c, d) #define trace5(a, b, c, d, e) #define trace6(a, b, c, d, e, f) #endif #define off \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) const LL N = 4100 + 1; LL n, m, x; LL dp[N]; LL SET_IT(LL k, LL pos) { return k | (1 << pos); } int main() { #ifndef ONLINE_JUDGE freopen("../input.txt", "r", stdin); freopen("../output.txt", "w", stdout); #endif off; cin >> n >> m; LL lmt = 1 << n; for (LL i = 0; i < N; i++) dp[i] = INT_MAX; vector<LL> cost(m); vector<LL> bitstate(m, 0); rep(i, 0, m) { LL t; cin >> cost[i] >> t; LL val = 0; while (t--) { cin >> x; val = SET_IT(val, x - 1); } bitstate[i] = val; dp[val] = min(dp[val], cost[i]); } rep(i, 0, m) { rep(j, 0, lmt) { dp[j | bitstate[i]] = min(dp[j | bitstate[i]], cost[i] + dp[j]); } } if (dp[lmt - 1] != INT_MAX) cout << dp[lmt - 1]; else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> II; typedef vector<II> VII; typedef vector<int> VI; typedef vector<VI> VVI; typedef long long int LL; typedef unsigned long long int ULL; #define PB push_back #define MP make_pair #define F first #define S second #define SZ(a) (int)(a.size()) #define ALL(a) a.begin(), a.end() #define SET(a, b) memset(a, b, sizeof(a)) #define LET(x, a) __typeof(a) x(a) #define rep(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) // Works for forward as well as backward iteration #define gu getchar #define pu putchar #define si(n) scanf("%d", &n) #define dout(n) printf("%d\n", n) #define sll(n) scanf("%lld", &n) #define lldout(n) printf("%lld\n", n) #define DRT() \ int t; \ si(t); \ while (t--) #define PlUSWRAP(index, n) \ index = (index + 1) % n // index++; if(index>=n) index=0 #define MINUSWRAP(index, n) \ index = (index + n - 1) % n // index--; if(index<0) index=n-1 #define ROUNDOFFINT(d) \ d = (int)((double)d + 0.5) // Round off d to nearest integer #define FLUSHN while (gu() != '\n') #define FLUSHS while (gu() != ' ') #define TRACE #ifdef TRACE #define trace1(x) cerr << #x << ": " << x << endl; #define trace2(x, y) \ cerr << #x << ": " << x << " | " << #y << ": " << y << endl; #define trace3(x, y, z) \ cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " \ << z << endl; #define trace4(a, b, c, d) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << endl; #define trace5(a, b, c, d, e) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl; #define trace6(a, b, c, d, e, f) \ cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \ << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \ << #f << ": " << f << endl; #else #define trace1(x) #define trace2(x, y) #define trace3(x, y, z) #define trace4(a, b, c, d) #define trace5(a, b, c, d, e) #define trace6(a, b, c, d, e, f) #endif #define off \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) const LL N = 4100 + 1; LL n, m, x; LL dp[N]; LL SET_IT(LL k, LL pos) { return k | (1 << pos); } int main() { // #ifndef ONLINE_JUDGE // freopen("../input.txt", "r", stdin); // freopen("../output.txt", "w", stdout); // #endif off; cin >> n >> m; LL lmt = 1 << n; for (LL i = 0; i < N; i++) dp[i] = INT_MAX; vector<LL> cost(m); vector<LL> bitstate(m, 0); rep(i, 0, m) { LL t; cin >> cost[i] >> t; LL val = 0; while (t--) { cin >> x; val = SET_IT(val, x - 1); } bitstate[i] = val; dp[val] = min(dp[val], cost[i]); } rep(i, 0, m) { rep(j, 0, lmt) { dp[j | bitstate[i]] = min(dp[j | bitstate[i]], cost[i] + dp[j]); } } if (dp[lmt - 1] != INT_MAX) cout << dp[lmt - 1]; else cout << -1; return 0; }
replace
86
90
86
90
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 + 5; int dp[(1 << 12) + 5][maxn], inf = 1e9; struct Key { int cost, status; }; Key key[maxn]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, cnt, box; cin >> n >> m; int bound = (1 << n); for (int i = 0; i <= m; ++i) for (int j = 0; j < bound; ++j) dp[j][i] = inf; for (int i = 1; i <= m; ++i) { cin >> key[i].cost >> cnt; for (int j = 0; j < cnt; ++j) { cin >> box; key[i].status |= (1 << (box - 1)); } } for (int i = 1; i <= m; ++i) { for (int j = 0; j <= bound; ++j) if (dp[j][i - 1] != inf) { dp[j][i] = min(dp[j][i], dp[j][i - 1]); dp[j | key[i].status][i] = min(dp[j | key[i].status][i], dp[j][i - 1] + key[i].cost); } dp[key[i].status][i] = min(dp[key[i].status][i], key[i].cost); } if (dp[(1 << n) - 1][m] == inf) cout << "-1\n"; else cout << dp[bound - 1][m] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 + 5; int dp[(1 << 13) + 5][maxn], inf = 1e9; struct Key { int cost, status; }; Key key[maxn]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, cnt, box; cin >> n >> m; int bound = (1 << n); for (int i = 0; i <= m; ++i) for (int j = 0; j < bound; ++j) dp[j][i] = inf; for (int i = 1; i <= m; ++i) { cin >> key[i].cost >> cnt; for (int j = 0; j < cnt; ++j) { cin >> box; key[i].status |= (1 << (box - 1)); } } for (int i = 1; i <= m; ++i) { for (int j = 0; j <= bound; ++j) if (dp[j][i - 1] != inf) { dp[j][i] = min(dp[j][i], dp[j][i - 1]); dp[j | key[i].status][i] = min(dp[j | key[i].status][i], dp[j][i - 1] + key[i].cost); } dp[key[i].status][i] = min(dp[key[i].status][i], key[i].cost); } if (dp[(1 << n) - 1][m] == inf) cout << "-1\n"; else cout << dp[bound - 1][m] << '\n'; return 0; }
replace
3
4
3
4
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using lint = long long; using P = pair<int, int>; using vec = vector<int>; using mat = vector<vector<int>>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define endl "\n" constexpr int MOD = 1000000007; const int INF = 1 << 30; int main() { int n, m; cin >> n >> m; vec a(m), b(m); vec c[20] = {}; rep(i, m) { cin >> a[i] >> b[i]; c[i].resize(b[i]); rep(j, b[i]) { int x; cin >> x; x--; c[i][j] = x; } } vec key(m); rep(i, m) { int k = 0; rep(j, b[i]) k |= (1 << c[i][j]); key[i] = k; } bool f = true; int key_f = 0; rep(i, m) { key_f = key_f | key[i]; } rep(i, n) if (!((key_f >> i) & 1)) f = false; if (!f) { cout << -1 << endl; exit(0); } int dp[8000]; rep(i, 8000) dp[i] = INF; dp[0] = 0; int mx = 0; rep(i, n) mx |= (1 << i); rep(i, m) { rep(flag, 1 << n) { dp[flag | key[i]] = min(dp[flag | key[i]], dp[flag] + a[i]); } } cout << dp[mx] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using lint = long long; using P = pair<int, int>; using vec = vector<int>; using mat = vector<vector<int>>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define endl "\n" constexpr int MOD = 1000000007; const int INF = 1 << 30; int main() { int n, m; cin >> n >> m; vec a(m), b(m); vec c[1050] = {}; rep(i, m) { cin >> a[i] >> b[i]; c[i].resize(b[i]); rep(j, b[i]) { int x; cin >> x; x--; c[i][j] = x; } } vec key(m); rep(i, m) { int k = 0; rep(j, b[i]) k |= (1 << c[i][j]); key[i] = k; } bool f = true; int key_f = 0; rep(i, m) { key_f = key_f | key[i]; } rep(i, n) if (!((key_f >> i) & 1)) f = false; if (!f) { cout << -1 << endl; exit(0); } int dp[8000]; rep(i, 8000) dp[i] = INF; dp[0] = 0; int mx = 0; rep(i, n) mx |= (1 << i); rep(i, m) { rep(flag, 1 << n) { dp[flag | key[i]] = min(dp[flag | key[i]], dp[flag] + a[i]); } } cout << dp[mx] << endl; return 0; }
replace
17
18
17
18
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define lint long long #define pii pair<int, int> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(c) (c).begin(), (c).end() #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MINF(a) memset(a, 0x3f, sizeof(a)) #define POW(n) (1LL << (n)) #define POPCNT(n) (__builtin_popcount(n)) #define IN(i, a, b) (a <= i && i <= b) using namespace std; template <typename T> inline bool CHMIN(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> inline bool CHMAX(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline void SORT(T &a) { sort(ALL(a)); } template <typename T> inline void REV(T &a) { reverse(ALL(a)); } template <typename T> inline void UNI(T &a) { sort(ALL(a)); a.erase(unique(ALL(a)), a.end()); } template <typename T> inline T LB(vector<T> &v, T a) { return *lower_bound(ALL(v), a); } template <typename T> inline int LBP(vector<T> &v, T a) { return lower_bound(ALL(v), a) - v.begin(); } template <typename T> inline T UB(vector<T> &v, T a) { return *upper_bound(ALL(v), a); } template <typename T> inline int UBP(vector<T> &v, T a) { return upper_bound(ALL(v), a) - v.begin(); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { REP(i, v.size()) { if (i) os << " "; os << v[i]; } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T = int> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } const lint MOD = 1000000007; const lint INF = 0x3f3f3f3f3f3f3f3f; const double EPS = 1e-10; int N, M; int a[20], b[20]; int c[1010][20]; int dp[1010][POW(12)]; void _main() { cin >> N >> M; REP(i, M) { cin >> a[i] >> b[i]; REP(j, b[i]) { cin >> c[i][j]; c[i][j]--; } } MINF(dp); dp[0][0] = 0; REP(i, M) REP(bit, POW(N)) if (dp[i][bit] < INF) { CHMIN(dp[i + 1][bit], dp[i][bit]); int newBit = bit; REP(j, b[i]) { newBit |= (1 << c[i][j]); } CHMIN(dp[i + 1][newBit], dp[i][bit] + a[i]); } cout << (dp[M][POW(N) - 1] == INF ? -1 : dp[M][POW(N) - 1]) << endl; } signed main(signed argc, char **argv) { if (argc > 1) { if (strchr(argv[1], 'i')) freopen("input.txt", "r", stdin); if (strchr(argv[1], 'o')) freopen("output.txt", "w", stdout); } cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); _main(); return 0; }
#include <bits/stdc++.h> #define int long long #define lint long long #define pii pair<int, int> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(c) (c).begin(), (c).end() #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MINF(a) memset(a, 0x3f, sizeof(a)) #define POW(n) (1LL << (n)) #define POPCNT(n) (__builtin_popcount(n)) #define IN(i, a, b) (a <= i && i <= b) using namespace std; template <typename T> inline bool CHMIN(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename T> inline bool CHMAX(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline void SORT(T &a) { sort(ALL(a)); } template <typename T> inline void REV(T &a) { reverse(ALL(a)); } template <typename T> inline void UNI(T &a) { sort(ALL(a)); a.erase(unique(ALL(a)), a.end()); } template <typename T> inline T LB(vector<T> &v, T a) { return *lower_bound(ALL(v), a); } template <typename T> inline int LBP(vector<T> &v, T a) { return lower_bound(ALL(v), a) - v.begin(); } template <typename T> inline T UB(vector<T> &v, T a) { return *upper_bound(ALL(v), a); } template <typename T> inline int UBP(vector<T> &v, T a) { return upper_bound(ALL(v), a) - v.begin(); } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { REP(i, v.size()) { if (i) os << " "; os << v[i]; } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T = int> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename V> typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); } const lint MOD = 1000000007; const lint INF = 0x3f3f3f3f3f3f3f3f; const double EPS = 1e-10; int N, M; int a[1010], b[1010]; int c[1010][20]; int dp[1010][POW(12)]; void _main() { cin >> N >> M; REP(i, M) { cin >> a[i] >> b[i]; REP(j, b[i]) { cin >> c[i][j]; c[i][j]--; } } MINF(dp); dp[0][0] = 0; REP(i, M) REP(bit, POW(N)) if (dp[i][bit] < INF) { CHMIN(dp[i + 1][bit], dp[i][bit]); int newBit = bit; REP(j, b[i]) { newBit |= (1 << c[i][j]); } CHMIN(dp[i + 1][newBit], dp[i][bit] + a[i]); } cout << (dp[M][POW(N) - 1] == INF ? -1 : dp[M][POW(N) - 1]) << endl; } signed main(signed argc, char **argv) { if (argc > 1) { if (strchr(argv[1], 'i')) freopen("input.txt", "r", stdin); if (strchr(argv[1], 'o')) freopen("output.txt", "w", stdout); } cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); _main(); return 0; }
replace
87
88
87
88
0
p02901
C++
Runtime Error
#include <algorithm> #include <stdio.h> #include <tuple> using namespace std; using ll = long long; using pii = pair<int, int>; constexpr int s = 1 << 12; int n, m, a[20], b[20], c[20], bin[20]; int dp[s]; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", a + i, b + i); for (int j = 0; j < b[i]; j++) scanf("%d", c + j), bin[i] ^= 1 << (c[j] - 1); } for (int i = 0; i < s; i++) dp[i] = 1e9; dp[0] = 0; for (int i = 0; i < m; i++) for (int j = 0; j < s; j++) dp[j | bin[i]] = min(dp[j | bin[i]], dp[j] + a[i]); if (dp[(1 << n) - 1] == ((int)1e9)) printf("-1\n"); else printf("%d\n", dp[(1 << n) - 1]); }
#include <algorithm> #include <stdio.h> #include <tuple> using namespace std; using ll = long long; using pii = pair<int, int>; constexpr int s = 1 << 12; int n, m, a[2010], b[2010], c[20], bin[2010]; int dp[s]; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", a + i, b + i); for (int j = 0; j < b[i]; j++) scanf("%d", c + j), bin[i] ^= 1 << (c[j] - 1); } for (int i = 0; i < s; i++) dp[i] = 1e9; dp[0] = 0; for (int i = 0; i < m; i++) for (int j = 0; j < s; j++) dp[j | bin[i]] = min(dp[j | bin[i]], dp[j] + a[i]); if (dp[(1 << n) - 1] == ((int)1e9)) printf("-1\n"); else printf("%d\n", dp[(1 << n) - 1]); }
replace
8
9
8
9
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using ll = long long; using namespace std; #define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++) #define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define rreps(i, n) for (int i = ((int)(n)); i > 0; i--) #define repi(i, x) \ for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++) #define all(x) (x).begin(), (x).end() #define F first #define S second #define mp make_pair #define pb push_back #define solve(a) ((a) ? "Yes" : "No") typedef vector<int> Vi; typedef vector<Vi> VVi; typedef pair<int, int> Pi; typedef vector<Pi> VPi; typedef vector<long long> V; typedef vector<V> VV; typedef pair<long long, long long> P; typedef vector<P> VP; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INFLL = 1LL << 60; const int INF = 1 << 30; const double PI = acos(-1); int main() { int n, m; int ans = 0; cin >> n >> m; int a, b; int mack = 0; int c; VPi data(n); VVi dp(m + 1, Vi(1 << n, INF)); dp[0][0] = 0; /* dp: [at][state]=yen intit : INF , [0][0]=0; find [m][2^n-1] */ rep(i, m) { mack = 0; cin >> a >> b; rep(i, b) { cin >> c; c--; mack |= (1 << c); } data[i] = mp(a, mack); } rep(i, m) { rep(j, 1 << n) { chmin(dp[i + 1][j], dp[i][j]); chmin(dp[i + 1][j | data[i].S], dp[i][j] + data[i].F); } } if (dp[m][(1 << n) - 1] == INF) { cout << -1 << endl; } else { cout << dp[m][(1 << n) - 1] << endl; } // cout<<ans<<endl; }
#include <bits/stdc++.h> using ll = long long; using namespace std; #define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++) #define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define rreps(i, n) for (int i = ((int)(n)); i > 0; i--) #define repi(i, x) \ for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++) #define all(x) (x).begin(), (x).end() #define F first #define S second #define mp make_pair #define pb push_back #define solve(a) ((a) ? "Yes" : "No") typedef vector<int> Vi; typedef vector<Vi> VVi; typedef pair<int, int> Pi; typedef vector<Pi> VPi; typedef vector<long long> V; typedef vector<V> VV; typedef pair<long long, long long> P; typedef vector<P> VP; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INFLL = 1LL << 60; const int INF = 1 << 30; const double PI = acos(-1); int main() { int n, m; int ans = 0; cin >> n >> m; int a, b; int mack = 0; int c; VPi data(m); VVi dp(m + 1, Vi(1 << n, INF)); dp[0][0] = 0; /* dp: [at][state]=yen intit : INF , [0][0]=0; find [m][2^n-1] */ rep(i, m) { mack = 0; cin >> a >> b; rep(i, b) { cin >> c; c--; mack |= (1 << c); } data[i] = mp(a, mack); } rep(i, m) { rep(j, 1 << n) { chmin(dp[i + 1][j], dp[i][j]); chmin(dp[i + 1][j | data[i].S], dp[i][j] + data[i].F); } } if (dp[m][(1 << n) - 1] == INF) { cout << -1 << endl; } else { cout << dp[m][(1 << n) - 1] << endl; } // cout<<ans<<endl; }
replace
47
48
47
48
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> #define fi first #define se second using namespace std; using ll = long long; constexpr int N = 12, INF = numeric_limits<int>::max() / 2; int n, m, a[N], b[N], unlock[N], dp[1 << N]; int go(int msk) { // msk shows not yet unlocked locks if (dp[msk] != -1) return dp[msk]; dp[msk] = INF; for (int i = 0; i < m; ++i) { int un = unlock[i] & msk; if (!un) continue; dp[msk] = min(dp[msk], a[i] + go(msk ^ un)); } return dp[msk]; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i]; for (int j = 0, x; j < b[i]; ++j) { cin >> x; --x; unlock[i] |= 1 << x; } } fill(dp, dp + (1 << N), -1); dp[0] = 0; int ans = go((1 << n) - 1); cout << (ans == INF ? -1 : ans) << '\n'; }
#include <bits/stdc++.h> #define fi first #define se second using namespace std; using ll = long long; constexpr int N = 12, M = 1009, INF = numeric_limits<int>::max() / 2; int n, m, a[M], b[M], unlock[M], dp[1 << N]; int go(int msk) { // msk shows not yet unlocked locks if (dp[msk] != -1) return dp[msk]; dp[msk] = INF; for (int i = 0; i < m; ++i) { int un = unlock[i] & msk; if (!un) continue; dp[msk] = min(dp[msk], a[i] + go(msk ^ un)); } return dp[msk]; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i]; for (int j = 0, x; j < b[i]; ++j) { cin >> x; --x; unlock[i] |= 1 << x; } } fill(dp, dp + (1 << N), -1); dp[0] = 0; int ans = go((1 << n) - 1); cout << (ans == INF ? -1 : ans) << '\n'; }
replace
6
8
6
8
0
p02901
C++
Runtime Error
#include <algorithm> #include <cstring> #include <deque> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <vector> using namespace std; #define ll long long #define endl '\n' const double INF = 1e9 + 5; pair<int, int> key[13]; // bitmask,value; int d[1001][1 << 13]; int n, m; int go(int here, int state) { if (state == (1 << n) - 1) return 0; if (here == m) // 실수했다. return INF; int &ret = d[here][state]; if (ret != -1) return ret; ret = INF; ret = min(ret, go(here + 1, state | key[here].first) + key[here].second); ret = min(ret, go(here + 1, state)); return ret; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // memset(d, -1, sizeof(d)); for (int i = 0; i < 1001; i++) for (int j = 0; j < (1 << 13); j++) d[i][j] = -1; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; int total = 0; for (int j = 0; j < b; j++) { int ci; cin >> ci; ci--; total |= (1 << ci); } key[i].first = total; key[i].second = a; } int val = go(0, 0); if (val == INF) cout << -1; else cout << val; }
#include <algorithm> #include <cstring> #include <deque> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <vector> using namespace std; #define ll long long #define endl '\n' const double INF = 1e9 + 5; pair<int, int> key[1001]; // bitmask,value; int d[1001][1 << 13]; int n, m; int go(int here, int state) { if (state == (1 << n) - 1) return 0; if (here == m) // 실수했다. return INF; int &ret = d[here][state]; if (ret != -1) return ret; ret = INF; ret = min(ret, go(here + 1, state | key[here].first) + key[here].second); ret = min(ret, go(here + 1, state)); return ret; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // memset(d, -1, sizeof(d)); for (int i = 0; i < 1001; i++) for (int j = 0; j < (1 << 13); j++) d[i][j] = -1; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; int total = 0; for (int j = 0; j < b; j++) { int ci; cin >> ci; ci--; total |= (1 << ci); } key[i].first = total; key[i].second = a; } int val = go(0, 0); if (val == INF) cout << -1; else cout << val; }
replace
14
15
14
15
0
p02901
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define INF 1e18 #define int long long #define Rep(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) Rep(i, 0, n) #define all(a) (a).begin(), (a).end() using namespace std; typedef pair<int, int> P; typedef pair<int, P> PP; const int mod = 1000000007; int n, m; int a[1010], b[1010], c[1010][15]; int dp[1010][1 << 13]; bool vis[1010][1 << 13]; int solve(int p, int bit) { if (vis[p][bit]) return dp[p][bit]; if (p == m) { if (bit == (1 << n) - 1) return dp[p][bit] = 0; else return dp[p][bit] = INF; } int res = solve(p + 1, bit); int nxt = bit; rep(i, b[p]) nxt |= (1 << c[p][i]); if (solve(p + 1, nxt) != INF) res = min(res, solve(p + 1, nxt) + a[p]); return dp[p][bit] = res; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; rep(i, m) { cin >> a[i] >> b[i]; rep(j, b[i]) cin >> c[i][j]; rep(j, b[i]) c[i][j]--; } int ans = solve(0, 0); cout << (ans == INF ? -1 : ans) << endl; }
#include <bits/stdc++.h> #define INF 1e18 #define int long long #define Rep(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) Rep(i, 0, n) #define all(a) (a).begin(), (a).end() using namespace std; typedef pair<int, int> P; typedef pair<int, P> PP; const int mod = 1000000007; int n, m; int a[1010], b[1010], c[1010][15]; int dp[1010][1 << 13]; bool vis[1010][1 << 13]; int solve(int p, int bit) { if (vis[p][bit]) return dp[p][bit]; vis[p][bit] = true; if (p == m) { if (bit == (1 << n) - 1) return dp[p][bit] = 0; else return dp[p][bit] = INF; } int res = solve(p + 1, bit); int nxt = bit; rep(i, b[p]) nxt |= (1 << c[p][i]); if (solve(p + 1, nxt) != INF) res = min(res, solve(p + 1, nxt) + a[p]); return dp[p][bit] = res; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; rep(i, m) { cin >> a[i] >> b[i]; rep(j, b[i]) cin >> c[i][j]; rep(j, b[i]) c[i][j]--; } int ans = solve(0, 0); cout << (ans == INF ? -1 : ans) << endl; }
insert
19
19
19
20
TLE
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL << 62 #define inf 1000000007 int main() { ll n, m, a[1005], b[1005], p[13]; ll cnt = 1; for (ll i = 0; i < 13; i++) { p[i] = cnt; cnt *= 2; } cin >> n >> m; for (ll i = 0; i < m; i++) { ll x, y; cin >> x >> y; a[i] = x; ll now = 0; for (ll j = 0; j < y; j++) { ll next; cin >> next; next--; now = now | p[next]; } b[i] = now; // cout <<now<<endl; } ll dp[100000]; for (ll i = 0; i < 100000; i++) { dp[i] = INF; } dp[0] = 0; for (ll i = 0; i < 100000; i++) { for (ll j = 0; j < m; j++) { ll cost = a[j]; ll op = b[j]; ll next = i | op; dp[next] = min(dp[next], dp[i] + cost); } } // cout << ll(pow(2,n))-1; if (dp[ll(pow(2, n)) - 1] == INF) { cout << -1; return 0; } cout << dp[ll(pow(2, n)) - 1]; // your code goes here return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL << 62 #define inf 1000000007 int main() { ll n, m, a[1005], b[1005], p[13]; ll cnt = 1; for (ll i = 0; i < 13; i++) { p[i] = cnt; cnt *= 2; } cin >> n >> m; for (ll i = 0; i < m; i++) { ll x, y; cin >> x >> y; a[i] = x; ll now = 0; for (ll j = 0; j < y; j++) { ll next; cin >> next; next--; now = now | p[next]; } b[i] = now; // cout <<now<<endl; } ll dp[100000]; for (ll i = 0; i < 100000; i++) { dp[i] = INF; } dp[0] = 0; for (ll i = 0; i < 100000; i++) { for (ll j = 0; j < m; j++) { ll cost = a[j]; ll op = b[j]; ll next = i | op; if (next < 100000) { dp[next] = min(dp[next], dp[i] + cost); } } } // cout << ll(pow(2,n))-1; if (dp[ll(pow(2, n)) - 1] == INF) { cout << -1; return 0; } cout << dp[ll(pow(2, n)) - 1]; // your code goes here return 0; }
replace
38
39
38
41
0
p02901
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL << 62 #define inf 1000000007 ll dp[4200]; int main() { for (ll i = 0; i < 4200; i++) { dp[i] = INF; } dp[0] = 0; ll n, m; cin >> n >> m; for (ll i = 0; i < m; i++) { ll x, y; cin >> x >> y; ll op = 0; for (ll j = 0; j < y; j++) { ll now; cin >> now; now--; now = pow(2, now); op = op | now; } for (ll j = 0; j < 4200; j++) { dp[j | op] = min(dp[j] + x, dp[j | op]); } } ll ans = 0; for (ll i = 0; i < n; i++) { ans = ans | ll(pow(2, i)); } // cout <<ans<<endl; if (dp[ans] == INF) { cout << -1; return 0; } cout << dp[ans]; // your code goes here return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL << 62 #define inf 1000000007 ll dp[4200]; int main() { for (ll i = 0; i < 4200; i++) { dp[i] = INF; } dp[0] = 0; ll n, m; cin >> n >> m; for (ll i = 0; i < m; i++) { ll x, y; cin >> x >> y; ll op = 0; for (ll j = 0; j < y; j++) { ll now; cin >> now; now--; now = pow(2, now); op = op | now; } for (ll j = 0; j < 4200; j++) { if (ll(j | op) < 4200) { dp[j | op] = min(dp[j] + x, dp[j | op]); } } } ll ans = 0; for (ll i = 0; i < n; i++) { ans = ans | ll(pow(2, i)); } // cout <<ans<<endl; if (dp[ans] == INF) { cout << -1; return 0; } cout << dp[ans]; // your code goes here return 0; }
replace
26
27
26
29
0
p02902
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define ALL(a) (a).begin(), (a).end() #define ALLR(a) (a).rbegin(), (a).rend() #define spa << " " << #define lfs << fixed << setprecision(10) << #define test cout << "test" << endl; #define fi first #define se second #define MP make_pair #define PB push_back #define EB emplace_back #define rep(i, n, m) for (ll i = n; i < (ll)(m); i++) #define rrep(i, n, m) for (ll i = n - 1; i >= (ll)(m); i--) using ll = long long; using ld = long double; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const ll INF = 1e18; using P = pair<ll, ll>; template <typename T> void chmin(T &a, T b) { if (a > b) a = b; } template <typename T> void chmax(T &a, T b) { if (a < b) a = b; } void pmod(ll &a, ll b) { a = (a + b) % MOD; } void pmod(ll &a, ll b, ll c) { a = (b + c) % MOD; } void qmod(ll &a, ll b) { a = (a * b) % MOD; } void qmod(ll &a, ll b, ll c) { a = (b * c) % MOD; } ll median(ll a, ll b, ll c) { return a + b + c - max({a, b, c}) - min({a, b, c}); } void ans1(bool x) { if (x) cout << "Yes" << endl; else cout << "No" << endl; } void ans2(bool x) { if (x) cout << "YES" << endl; else cout << "NO" << endl; } void ans3(bool x) { if (x) cout << "Yay!" << endl; else cout << ":(" << endl; } template <typename T1, typename T2> void ans(bool x, T1 y, T2 z) { if (x) cout << y << endl; else cout << z << endl; } template <typename T> void debug(vector<vector<T>> v, ll h, ll w) { for (ll i = 0; i < h; i++) { cout << v[i][0]; for (ll j = 1; j < w; j++) cout spa v[i][j]; cout << endl; } }; void debug(vector<string> v, ll h, ll w) { for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) cout << v[i][j]; cout << endl; } }; template <typename T> void debug(vector<T> v, ll n) { if (n != 0) cout << v[0]; for (ll i = 1; i < n; i++) cout spa v[i]; cout << endl; }; template <typename T> vector<vector<T>> vec(ll x, ll y, T w) { vector<vector<T>> v(x, vector<T>(y, w)); return v; } ll gcd(ll x, ll y) { ll r; while (y != 0 && (r = x % y) != 0) { x = y; y = r; } return y == 0 ? x : y; } template <typename T> vector<ll> dx = {1, 0, -1, 0, 1, 1, -1, -1}; vector<ll> dy = {0, 1, 0, -1, 1, -1, 1, -1}; template <typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } ll MAX_V = 2e5 + 5; vector<vector<ll>> G(MAX_V); // 隣接リスト(添え字は始点、値は終点) // 引数は各頂点(0-indexed)の入次数 vector<ll> tsort(vector<ll> v) { ll n = v.size(); vector<ll> ret(n); stack<ll> st; for (ll i = 0; i < n; i++) if (v[i] == 0) st.push(i); for (ll i = 0; i < n; i++) { if (st.empty()) return vector<ll>(); // 閉路がある ll x = st.top(); st.pop(); ret[x] = i; for (ll j = 0; j < G[x].size(); j++) { v[G[x][j]]--; if (v[G[x][j]] == 0) st.push(G[x][j]); } } return ret; } int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); ll res = 0, res1 = INF, res2 = -INF, buf = 0; bool judge = true; ll n, m; cin >> n >> m; vector<ll> num(n + 1); rep(i, 0, n) { ll u, v; cin >> u >> v; G[u - 1].PB(v - 1); num[v - 1]++; } auto v = tsort(num); ans(v.size() == 0, 0, -1); return 0; }
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define ALL(a) (a).begin(), (a).end() #define ALLR(a) (a).rbegin(), (a).rend() #define spa << " " << #define lfs << fixed << setprecision(10) << #define test cout << "test" << endl; #define fi first #define se second #define MP make_pair #define PB push_back #define EB emplace_back #define rep(i, n, m) for (ll i = n; i < (ll)(m); i++) #define rrep(i, n, m) for (ll i = n - 1; i >= (ll)(m); i--) using ll = long long; using ld = long double; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; const ll INF = 1e18; using P = pair<ll, ll>; template <typename T> void chmin(T &a, T b) { if (a > b) a = b; } template <typename T> void chmax(T &a, T b) { if (a < b) a = b; } void pmod(ll &a, ll b) { a = (a + b) % MOD; } void pmod(ll &a, ll b, ll c) { a = (b + c) % MOD; } void qmod(ll &a, ll b) { a = (a * b) % MOD; } void qmod(ll &a, ll b, ll c) { a = (b * c) % MOD; } ll median(ll a, ll b, ll c) { return a + b + c - max({a, b, c}) - min({a, b, c}); } void ans1(bool x) { if (x) cout << "Yes" << endl; else cout << "No" << endl; } void ans2(bool x) { if (x) cout << "YES" << endl; else cout << "NO" << endl; } void ans3(bool x) { if (x) cout << "Yay!" << endl; else cout << ":(" << endl; } template <typename T1, typename T2> void ans(bool x, T1 y, T2 z) { if (x) cout << y << endl; else cout << z << endl; } template <typename T> void debug(vector<vector<T>> v, ll h, ll w) { for (ll i = 0; i < h; i++) { cout << v[i][0]; for (ll j = 1; j < w; j++) cout spa v[i][j]; cout << endl; } }; void debug(vector<string> v, ll h, ll w) { for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) cout << v[i][j]; cout << endl; } }; template <typename T> void debug(vector<T> v, ll n) { if (n != 0) cout << v[0]; for (ll i = 1; i < n; i++) cout spa v[i]; cout << endl; }; template <typename T> vector<vector<T>> vec(ll x, ll y, T w) { vector<vector<T>> v(x, vector<T>(y, w)); return v; } ll gcd(ll x, ll y) { ll r; while (y != 0 && (r = x % y) != 0) { x = y; y = r; } return y == 0 ? x : y; } template <typename T> vector<ll> dx = {1, 0, -1, 0, 1, 1, -1, -1}; vector<ll> dy = {0, 1, 0, -1, 1, -1, 1, -1}; template <typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template <typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } ll MAX_V = 2e5 + 5; vector<vector<ll>> G(MAX_V); // 隣接リスト(添え字は始点、値は終点) // 引数は各頂点(0-indexed)の入次数 vector<ll> tsort(vector<ll> v) { ll n = v.size(); vector<ll> ret(n); stack<ll> st; for (ll i = 0; i < n; i++) if (v[i] == 0) st.push(i); for (ll i = 0; i < n; i++) { if (st.empty()) return vector<ll>(); // 閉路がある ll x = st.top(); st.pop(); ret[x] = i; for (ll j = 0; j < G[x].size(); j++) { v[G[x][j]]--; if (v[G[x][j]] == 0) st.push(G[x][j]); } } return ret; } int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); ll res = 0, res1 = INF, res2 = -INF, buf = 0; bool judge = true; ll n, m; cin >> n >> m; vector<ll> num(n); rep(i, 0, m) { ll u, v; cin >> u >> v; G[u - 1].PB(v - 1); num[v - 1]++; } auto v = tsort(num); ans(v.size() == 0, 0, -1); return 0; }
replace
137
139
137
139
0
p02902
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <fstream> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <string> #include <unordered_set> #include <vector> #define MOD_BIG 1000000007 using std::cerr; using std::cin; using std::cout; using std::endl; using std::string; using ll = long long; using std::vector; template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } ////////////////////////////////////// enum Status { Not = 0, Now, End, }; template <typename T> struct Edge { int to; int from; T weight; Edge(int from, int to, T weight) : from(from), to(to), weight(weight) {} }; template <typename T, typename D> struct Graph { int n; vector<vector<Edge<T>>> g; vector<D> data; vector<Status> s; Graph(int N) : n(N), g(N + 1), data(N + 1), s(N + 1) {} void add_edge(int from, Edge<T> e) { g[from].push_back(e); } void set_data(int node, D value) { data[node] = value; } // 幅優先探索による最短経路 // weightが全て同一の場合線形 std::pair<vector<T>, vector<vector<Edge<T>>>> bfs_spp(int start) { vector<T> d(n + 1, -1); vector<vector<Edge<T>>> route(n + 1); std::queue<int> q; d[start] = 0; q.push(start); while (!q.empty()) { int v = q.front(); q.pop(); for (auto e : g[v]) { if (d[e.to] != -1 && d[e.to] != 0) { // 到達済み continue; } d[e.to] = d[e.from] + 1; auto tmp = route[e.from]; tmp.push_back(e); route[e.to] = tmp; q.push(e.to); } } return std::make_pair(d, route); } // ダイクストラ法 // weightが非負の場合の最短経路 // 経路が存在しない場合、INFが返るのでオーバーフローに注意 std::pair<vector<T>, vector<vector<Edge<T>>>> dijkstra(int start) { using P = std::pair<T, int>; std::priority_queue<P, vector<P>, std::greater<P>> que; T T_MAX = std::numeric_limits<T>::max(); vector<T> d(n + 1, T_MAX); vector<vector<Edge<T>>> route(n + 1); d[start] = 0; que.push(P((T)0, start)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (Edge<T> e : g[v]) { if (d[e.to] > d[v] + e.weight || d[e.to] == 0) { d[e.to] = d[v] + e.weight; auto tmp = route[e.from]; tmp.push_back(e); route[e.to] = tmp; que.push(P(d[e.to], e.to)); } } } return std::make_pair(d, route); } // ベルマン-フォード法 // weightを負を含む距離として最短経路を求める // 無限ループが存在する場合、trueを1つ目に返す std::pair<bool, vector<T>> bellman_ford(int start) { T T_INF = std::numeric_limits<T>::max(); vector<T> d(n + 1, T_INF); vector<Edge<T>> E; for (int i = 0; i <= n; i++) { for (auto e : g[i]) { E.push_back(e); } } d[start] = 0; for (int i = 0; i <= n; i++) { for (auto e : E) { if (d[e.from] != T_INF && d[e.from] + e.weight < d[e.to]) { // 最初or別のstartからe.toへの最短路を発見 d[e.to] = d[e.from] + e.weight; // std::cout << "new route find, to: " << e.to << ", distance: " << // d[e.to] << std::endl; if (i == n) { // もう全ての辺はチェック済みのはずなのにまだ更新がある:無限ループあり return std::make_pair(true, d); } } } } return std::make_pair(false, d); } // あるノードに到達できるか // 逆方向はグラフ自体逆で構成することで可能 vector<bool> can_reach_from(int start) { vector<bool> reached(n + 1, false); dfs_reach_from(start, reached); return reached; } void dfs_reach_from(int now, vector<bool> &reached) { if (reached[now]) return; reached[now] = true; for (auto e : g[now]) { dfs_reach_from(e.to, reached); } } // 出力 void print() { for (auto v : g) { for (auto e : v) { std::cout << e.from << "-(" << e.weight << ")->" << e.to << ", "; } std::cout << std::endl; } } int dfs(int now, int par, int depth); std::pair<vector<T>, vector<vector<Edge<T>>>> bfs(int start, ll k); }; template <typename T, typename D> int Graph<T, D>::dfs(int now, int par, int depth) { // std::cout << "searching now: " << now << std::endl; if (g[now].empty()) { // 終端ノード } int res = 0; s[now] = Status::Now; for (auto e : g[now]) { int chi = e.to; if (s[chi] == Status::Now) { // 閉路あり } else if (s[chi] == Status::End) { // 探索済み(親が複数) ここを使う場合メモできないか考える } else { // 未探索 dfs(chi, now, depth + 1); } } s[now] = Status::End; return res; } template <typename T, typename D> std::pair<vector<T>, vector<vector<Edge<T>>>> Graph<T, D>::bfs(int start, ll k) { vector<T> d(n + 1, -1); vector<vector<Edge<T>>> route(n + 1); std::queue<int> q; d[start] = 0; q.push(start); while (!q.empty()) { int v = q.front(); if (d[v] > k) { // 更新可能性なし d[v] = -1; return std::make_pair(d, route); } q.pop(); for (auto e : g[v]) { if (d[e.to] != -1 && e.to != start) { // 到達済みがゴールにたどり着く前に出る:このスタートははずれ d[v] = -1; return std::make_pair(d, route); } d[e.to] = d[e.from] + 1; auto tmp = route[e.from]; tmp.push_back(e); route[e.to] = tmp; q.push(e.to); if (e.to == start) { return std::make_pair(d, route); } } } return std::make_pair(d, route); } ////////////////////////////////////// int main() { int N, M; cin >> N >> M; Graph<ll, ll> g(N); for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; g.add_edge(a, Edge<ll>(a, b, 1)); } ll K = std::numeric_limits<ll>::max(); vector<Edge<ll>> route; vector<bool> checked(N + 1); for (int i = 1; i <= N; i++) { if (checked[i]) { continue; } auto res = g.bfs(i, K); // cerr << res.first[i] << endl; if (res.first[i] == 0) { // このノードからつながる先にループはない うまくいかない for (auto r : res.second) { for (auto e : r) { // checked[e.to] = true; } } } else if (res.first[i] == -1) { continue; } else if (K > res.first[i]) { K = res.first[i]; route = res.second[i]; } if (i > N / 2) { bool ok = true; std::multiset<int> s; for (auto e : route) { s.insert(e.from); } for (auto n : s) { int cnt = 0; for (auto e : g.g[n]) { cnt += s.count(e.to); } if (cnt >= 2) { ok = false; break; } } if (ok) { break; } } } if (K == std::numeric_limits<ll>::max()) { cout << -1 << endl; return 0; } cout << K << endl; for (auto e : route) { cout << e.from << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <fstream> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <string> #include <unordered_set> #include <vector> #define MOD_BIG 1000000007 using std::cerr; using std::cin; using std::cout; using std::endl; using std::string; using ll = long long; using std::vector; template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } ////////////////////////////////////// enum Status { Not = 0, Now, End, }; template <typename T> struct Edge { int to; int from; T weight; Edge(int from, int to, T weight) : from(from), to(to), weight(weight) {} }; template <typename T, typename D> struct Graph { int n; vector<vector<Edge<T>>> g; vector<D> data; vector<Status> s; Graph(int N) : n(N), g(N + 1), data(N + 1), s(N + 1) {} void add_edge(int from, Edge<T> e) { g[from].push_back(e); } void set_data(int node, D value) { data[node] = value; } // 幅優先探索による最短経路 // weightが全て同一の場合線形 std::pair<vector<T>, vector<vector<Edge<T>>>> bfs_spp(int start) { vector<T> d(n + 1, -1); vector<vector<Edge<T>>> route(n + 1); std::queue<int> q; d[start] = 0; q.push(start); while (!q.empty()) { int v = q.front(); q.pop(); for (auto e : g[v]) { if (d[e.to] != -1 && d[e.to] != 0) { // 到達済み continue; } d[e.to] = d[e.from] + 1; auto tmp = route[e.from]; tmp.push_back(e); route[e.to] = tmp; q.push(e.to); } } return std::make_pair(d, route); } // ダイクストラ法 // weightが非負の場合の最短経路 // 経路が存在しない場合、INFが返るのでオーバーフローに注意 std::pair<vector<T>, vector<vector<Edge<T>>>> dijkstra(int start) { using P = std::pair<T, int>; std::priority_queue<P, vector<P>, std::greater<P>> que; T T_MAX = std::numeric_limits<T>::max(); vector<T> d(n + 1, T_MAX); vector<vector<Edge<T>>> route(n + 1); d[start] = 0; que.push(P((T)0, start)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (Edge<T> e : g[v]) { if (d[e.to] > d[v] + e.weight || d[e.to] == 0) { d[e.to] = d[v] + e.weight; auto tmp = route[e.from]; tmp.push_back(e); route[e.to] = tmp; que.push(P(d[e.to], e.to)); } } } return std::make_pair(d, route); } // ベルマン-フォード法 // weightを負を含む距離として最短経路を求める // 無限ループが存在する場合、trueを1つ目に返す std::pair<bool, vector<T>> bellman_ford(int start) { T T_INF = std::numeric_limits<T>::max(); vector<T> d(n + 1, T_INF); vector<Edge<T>> E; for (int i = 0; i <= n; i++) { for (auto e : g[i]) { E.push_back(e); } } d[start] = 0; for (int i = 0; i <= n; i++) { for (auto e : E) { if (d[e.from] != T_INF && d[e.from] + e.weight < d[e.to]) { // 最初or別のstartからe.toへの最短路を発見 d[e.to] = d[e.from] + e.weight; // std::cout << "new route find, to: " << e.to << ", distance: " << // d[e.to] << std::endl; if (i == n) { // もう全ての辺はチェック済みのはずなのにまだ更新がある:無限ループあり return std::make_pair(true, d); } } } } return std::make_pair(false, d); } // あるノードに到達できるか // 逆方向はグラフ自体逆で構成することで可能 vector<bool> can_reach_from(int start) { vector<bool> reached(n + 1, false); dfs_reach_from(start, reached); return reached; } void dfs_reach_from(int now, vector<bool> &reached) { if (reached[now]) return; reached[now] = true; for (auto e : g[now]) { dfs_reach_from(e.to, reached); } } // 出力 void print() { for (auto v : g) { for (auto e : v) { std::cout << e.from << "-(" << e.weight << ")->" << e.to << ", "; } std::cout << std::endl; } } int dfs(int now, int par, int depth); std::pair<vector<T>, vector<vector<Edge<T>>>> bfs(int start, ll k); }; template <typename T, typename D> int Graph<T, D>::dfs(int now, int par, int depth) { // std::cout << "searching now: " << now << std::endl; if (g[now].empty()) { // 終端ノード } int res = 0; s[now] = Status::Now; for (auto e : g[now]) { int chi = e.to; if (s[chi] == Status::Now) { // 閉路あり } else if (s[chi] == Status::End) { // 探索済み(親が複数) ここを使う場合メモできないか考える } else { // 未探索 dfs(chi, now, depth + 1); } } s[now] = Status::End; return res; } template <typename T, typename D> std::pair<vector<T>, vector<vector<Edge<T>>>> Graph<T, D>::bfs(int start, ll k) { vector<T> d(n + 1, -1); vector<vector<Edge<T>>> route(n + 1); std::queue<int> q; d[start] = 0; q.push(start); while (!q.empty()) { int v = q.front(); if (d[v] > k) { // 更新可能性なし d[v] = -1; return std::make_pair(d, route); } q.pop(); for (auto e : g[v]) { if (d[e.to] != -1 && e.to != start) { // 到達済みがゴールにたどり着く前に出る:このスタートははずれ d[v] = -1; return std::make_pair(d, route); } d[e.to] = d[e.from] + 1; auto tmp = route[e.from]; tmp.push_back(e); route[e.to] = tmp; q.push(e.to); if (e.to == start) { return std::make_pair(d, route); } } } return std::make_pair(d, route); } ////////////////////////////////////// int main() { int N, M; cin >> N >> M; Graph<ll, ll> g(N); for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; g.add_edge(a, Edge<ll>(a, b, 1)); } ll K = std::numeric_limits<ll>::max(); vector<Edge<ll>> route; vector<bool> checked(N + 1); for (int i = 1; i <= N; i++) { if (checked[i]) { continue; } auto res = g.bfs(i, K); // cerr << res.first[i] << endl; if (res.first[i] == 0) { // このノードからつながる先にループはない うまくいかない for (auto r : res.second) { for (auto e : r) { // checked[e.to] = true; } } } else if (res.first[i] == -1) { continue; } else if (K > res.first[i]) { K = res.first[i]; route = res.second[i]; } if (route.size() > 2) { bool ok = true; std::multiset<int> s; for (auto e : route) { s.insert(e.from); } for (auto n : s) { int cnt = 0; for (auto e : g.g[n]) { cnt += s.count(e.to); } if (cnt >= 2) { ok = false; break; } } if (ok) { break; } } } if (K == std::numeric_limits<ll>::max()) { cout << -1 << endl; return 0; } cout << K << endl; for (auto e : route) { cout << e.from << endl; } return 0; }
replace
268
269
268
269
TLE
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) // const ll mod = 1000000007; int N, M; vector<int> pathes[1005]; bool visiting[1005]; bool visited[1005]; int indexes[1005]; bool appeared[1005]; vector<int> circuit; vector<int> ans; int endpoint = -1; bool dfs(int now) { if (endpoint != -1) return false; visiting[now] = true; visited[now] = true; for (auto to : pathes[now]) { if (visiting[to]) { circuit.push_back(now); endpoint = to; visiting[now] = false; return true; } if (visited[to]) continue; if (dfs(to)) { circuit.push_back(now); visiting[now] = false; if (endpoint == now) return false; return true; // return true; } } visiting[now] = false; return false; } int main() { // cout.precision(10); cin >> N >> M; for (int i = 1; i <= M; i++) { int a, b; cin >> a >> b; pathes[a].push_back(b); } for (int i = 1; i <= N; i++) { if (visited[i]) continue; if (dfs(i)) break; } if (circuit.empty()) { cout << -1 << endl; return 0; } reverse(circuit.begin(), circuit.end()); for (auto v : circuit) cerr << v << " "; cerr << endl; bool ok = false; int n = circuit.size() - 1; for (auto to : pathes[circuit[n]]) { if (to == circuit[0]) ok = true; } if (!ok) { cerr << "NG" << endl; for (int i = 1; i <= 1e8; i++) cout << i << endl; } for (int i = 1; i <= N; i++) indexes[i] = -1; for (int i = 0; i < circuit.size(); i++) { indexes[circuit[i]] = i; } for (int i = 0; i < circuit.size(); i++) { appeared[circuit[i]] = true; int maxi = -1; for (auto to : pathes[circuit[i]]) { if (indexes[to] == -1) continue; if (!appeared[to]) continue; if (indexes[to] < i) chmax(maxi, indexes[to]); } if (maxi != -1) { cerr << "B" << endl; for (int j = maxi; j <= i; j++) if (appeared[circuit[j]]) ans.push_back(circuit[j]); cout << ans.size() << endl; for (auto to : ans) cout << to << endl; return 0; } if (i + 1 == circuit.size()) { cerr << "A" << endl; for (int j = 0; j < circuit.size(); j++) if (appeared[circuit[j]]) ans.push_back(circuit[j]); cout << ans.size() << endl; for (auto to : ans) cout << to << endl; return 0; } maxi = -1; for (auto to : pathes[circuit[i]]) { if (indexes[to] == -1) continue; if (indexes[to] > i) chmax(maxi, indexes[to]); } if (maxi != -1) i = maxi - 1; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) // const ll mod = 1000000007; int N, M; vector<int> pathes[1005]; bool visiting[1005]; bool visited[1005]; int indexes[1005]; bool appeared[1005]; vector<int> circuit; vector<int> ans; int endpoint = -1; bool dfs(int now) { if (endpoint != -1) return false; visiting[now] = true; visited[now] = true; for (auto to : pathes[now]) { if (endpoint != -1) break; if (visiting[to]) { circuit.push_back(now); endpoint = to; visiting[now] = false; return true; } if (visited[to]) continue; if (dfs(to)) { circuit.push_back(now); visiting[now] = false; if (endpoint == now) return false; return true; // return true; } } visiting[now] = false; return false; } int main() { // cout.precision(10); cin >> N >> M; for (int i = 1; i <= M; i++) { int a, b; cin >> a >> b; pathes[a].push_back(b); } for (int i = 1; i <= N; i++) { if (visited[i]) continue; if (dfs(i)) break; } if (circuit.empty()) { cout << -1 << endl; return 0; } reverse(circuit.begin(), circuit.end()); for (auto v : circuit) cerr << v << " "; cerr << endl; bool ok = false; int n = circuit.size() - 1; for (auto to : pathes[circuit[n]]) { if (to == circuit[0]) ok = true; } if (!ok) { cerr << "NG" << endl; for (int i = 1; i <= 1e8; i++) cout << i << endl; } for (int i = 1; i <= N; i++) indexes[i] = -1; for (int i = 0; i < circuit.size(); i++) { indexes[circuit[i]] = i; } for (int i = 0; i < circuit.size(); i++) { appeared[circuit[i]] = true; int maxi = -1; for (auto to : pathes[circuit[i]]) { if (indexes[to] == -1) continue; if (!appeared[to]) continue; if (indexes[to] < i) chmax(maxi, indexes[to]); } if (maxi != -1) { cerr << "B" << endl; for (int j = maxi; j <= i; j++) if (appeared[circuit[j]]) ans.push_back(circuit[j]); cout << ans.size() << endl; for (auto to : ans) cout << to << endl; return 0; } if (i + 1 == circuit.size()) { cerr << "A" << endl; for (int j = 0; j < circuit.size(); j++) if (appeared[circuit[j]]) ans.push_back(circuit[j]); cout << ans.size() << endl; for (auto to : ans) cout << to << endl; return 0; } maxi = -1; for (auto to : pathes[circuit[i]]) { if (indexes[to] == -1) continue; if (indexes[to] > i) chmax(maxi, indexes[to]); } if (maxi != -1) i = maxi - 1; } return 0; }
insert
40
40
40
42
TLE
p02902
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1000000000 #define P pair<int, int> #define pb push_back #define fi first #define se second int n, m, st, d[1010]; vector<int> v[1010], vg[1010], ans; P es[1010]; int mid = INF, mi; queue<int> q; void dfs(int x) { if (d[x] == 0) return; for (int i : vg[x]) { if (d[i] == d[x] - 1) { ans.pb(i); dfs(i); break; } } } int main() { cin >> n >> m; rep(i, m) { int a, b; cin >> a >> b; a--, b--; v[a].pb(b); vg[b].pb(a); es[i] = {b, a}; } rep(i, m) { P e = es[i]; fill(d, d + n, INF); d[e.fi] = 0; q.push(e.fi); while (!q.empty()) { int p = q.front(); q.pop(); for (int j : v[p]) { if (d[j] > d[p] + 1) { d[j] = d[p] + 1; q.push(j); } } } if (mid > d[e.se]) { mid = d[e.se]; mi = i; } } if (mid == INF) { puts("-1"); return 0; } P e = es[mi]; fill(d, d + n, INF); d[e.fi] = 0; q.push(e.fi); while (!q.empty()) { int p = q.front(); q.pop(); for (int j : v[p]) { if (d[j] > d[p] + 1) { d[j] = d[p] + 1; q.push(j); } } } ans.pb(e.se); dfs(e.se); cout << ans.size() << endl; for (int i : ans) { cout << i + 1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define INF 1000000000 #define P pair<int, int> #define pb push_back #define fi first #define se second int n, m, st, d[1010]; vector<int> v[1010], vg[1010], ans; P es[2010]; int mid = INF, mi; queue<int> q; void dfs(int x) { if (d[x] == 0) return; for (int i : vg[x]) { if (d[i] == d[x] - 1) { ans.pb(i); dfs(i); break; } } } int main() { cin >> n >> m; rep(i, m) { int a, b; cin >> a >> b; a--, b--; v[a].pb(b); vg[b].pb(a); es[i] = {b, a}; } rep(i, m) { P e = es[i]; fill(d, d + n, INF); d[e.fi] = 0; q.push(e.fi); while (!q.empty()) { int p = q.front(); q.pop(); for (int j : v[p]) { if (d[j] > d[p] + 1) { d[j] = d[p] + 1; q.push(j); } } } if (mid > d[e.se]) { mid = d[e.se]; mi = i; } } if (mid == INF) { puts("-1"); return 0; } P e = es[mi]; fill(d, d + n, INF); d[e.fi] = 0; q.push(e.fi); while (!q.empty()) { int p = q.front(); q.pop(); for (int j : v[p]) { if (d[j] > d[p] + 1) { d[j] = d[p] + 1; q.push(j); } } } ans.pb(e.se); dfs(e.se); cout << ans.size() << endl; for (int i : ans) { cout << i + 1 << endl; } return 0; }
replace
10
11
10
11
0
p02902
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define REP(i, n) for (int i = 0; i < n; ++i) int main() { int n, m; cin >> n >> m; vector<vector<int>> g(n); vector<int> d(n); vector<pair<int, int>> e(m); REP(i, m) { int a, b; cin >> a >> b; a--; b--; d[b]++; g[a].push_back(b); e[i] = {a, b}; } queue<int> q; REP(i, n) { if (d[i] == 0) { q.push(i); } } while (!q.empty()) { int p = q.front(); q.pop(); REP(i, g[p].size()) { if (--d[g[p][i]] == 0) q.push(g[p][i]); } } if (*max_element(d.begin(), d.end()) == 0) { cout << -1 << endl; return 0; } vector<vector<int>> dist(n, vector<int>(n, 1e9 + 1)), prev(n, vector<int>(n, -1)); REP(i, n) { dist[i][i] = 0; q.push(i); while (!q.empty()) { int p = q.front(); q.pop(); for (auto a : g[p]) { if (dist[i][a] > dist[i][p] + 1) { q.push(a); dist[i][a] = dist[i][p] + 1; prev[i][a] = p; } } } } vector<int> res; REP(i, m) { if (dist[e[i].second][e[i].first] != 1e9 + 1) { int pos = e[i].first; while (pos != e[i].second) { res.push_back(pos); pos = prev[e[i].second][pos]; } res.push_back(pos); } } reverse(res.begin(), res.end()); set<int> st(res.begin(), res.end()); { set<pair<int, int>> s(e.begin(), e.end()); REP(i, res.size()) { assert(s.count({res[i], res[(i + 1) % res.size()]})); } } while (1) { bool f = false; REP(i, m) { if (st.count(e[i].first) && st.count(e[i].second)) { int a = find(res.begin(), res.end(), e[i].first) - res.begin(), b = find(res.begin(), res.end(), e[i].second) - res.begin(); if ((a + 1) % res.size() == b) continue; vector<int> next; if (a < b) { for (int i = 0; i <= a; ++i) next.push_back(res[i]); for (int i = b; i < res.size(); ++i) next.push_back(res[i]); } else { for (int i = b; i <= a; ++i) next.push_back(res[i]); } res = next; st = set<int>(res.begin(), res.end()); f = true; } } if (!f) break; } cout << res.size() << endl; for (auto a : res) cout << a + 1 << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define REP(i, n) for (int i = 0; i < n; ++i) int main() { int n, m; cin >> n >> m; vector<vector<int>> g(n); vector<int> d(n); vector<pair<int, int>> e(m); REP(i, m) { int a, b; cin >> a >> b; a--; b--; d[b]++; g[a].push_back(b); e[i] = {a, b}; } queue<int> q; REP(i, n) { if (d[i] == 0) { q.push(i); } } while (!q.empty()) { int p = q.front(); q.pop(); REP(i, g[p].size()) { if (--d[g[p][i]] == 0) q.push(g[p][i]); } } if (*max_element(d.begin(), d.end()) == 0) { cout << -1 << endl; return 0; } vector<vector<int>> dist(n, vector<int>(n, 1e9 + 1)), prev(n, vector<int>(n, -1)); REP(i, n) { dist[i][i] = 0; q.push(i); while (!q.empty()) { int p = q.front(); q.pop(); for (auto a : g[p]) { if (dist[i][a] > dist[i][p] + 1) { q.push(a); dist[i][a] = dist[i][p] + 1; prev[i][a] = p; } } } } vector<int> res; REP(i, m) { if (dist[e[i].second][e[i].first] != 1e9 + 1) { int pos = e[i].first; while (pos != e[i].second) { res.push_back(pos); pos = prev[e[i].second][pos]; } res.push_back(pos); break; } } reverse(res.begin(), res.end()); set<int> st(res.begin(), res.end()); { set<pair<int, int>> s(e.begin(), e.end()); REP(i, res.size()) { assert(s.count({res[i], res[(i + 1) % res.size()]})); } } while (1) { bool f = false; REP(i, m) { if (st.count(e[i].first) && st.count(e[i].second)) { int a = find(res.begin(), res.end(), e[i].first) - res.begin(), b = find(res.begin(), res.end(), e[i].second) - res.begin(); if ((a + 1) % res.size() == b) continue; vector<int> next; if (a < b) { for (int i = 0; i <= a; ++i) next.push_back(res[i]); for (int i = b; i < res.size(); ++i) next.push_back(res[i]); } else { for (int i = b; i <= a; ++i) next.push_back(res[i]); } res = next; st = set<int>(res.begin(), res.end()); f = true; } } if (!f) break; } cout << res.size() << endl; for (auto a : res) cout << a + 1 << endl; }
insert
66
66
66
67
-6
0f9492a3-53f9-4b80-a2cc-a26678af1926.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02902/C++/s927197528.cpp:73: int main(): Assertion `s.count({res[i], res[(i + 1) % res.size()]})' failed.
p02902
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; /* result can always be reduced to a cycle lets say we have a cycle, i posit that even if there are extra stuff, we can still reduce it extra forward edge-> we can take one of the two dfs down -> find a cycle (if none then no answer) run through the elements in the cycle, chopping off ones run through once-> establish a set & order. run again, checking if the set has extra edge, if so, chop it off and repeat */ const int N = 1000; int n, m; vector<int> g[N]; vector<int> reduce_cycle(vector<int> v) { vector<int> res = v; int in[N] = {}; int cnt = 1; for (int e : v) { in[e] = cnt++; } int len = v.size(); for (int i = 0; i < v.size(); ++i) { int e = v[i]; for (int to : g[e]) { if (to == v[(i + 1) % len]) continue; if (in[to]) { // chop it off vector<int> nxt; if (in[e] < in[to]) { nxt.assign(res.begin(), res.begin() + in[e]); nxt.insert(nxt.end(), res.begin() + in[to] - 1, res.end()); } else { nxt.assign(res.begin() + in[to] - 1, res.begin() + in[e]); } res.swap(nxt); return reduce_cycle(res); } } } return res; } int col[N]; vector<int> find_cycle(int cur, vector<int> &path) { path.push_back(cur); col[cur] = 1; vector<int> res; for (int a : g[cur]) { if (col[a] == 1) { auto it = find(path.begin(), path.end(), a); assert(it != path.end()); res.assign(it, path.end()); } else { res = find_cycle(a, path); } if (res.size()) break; } path.pop_back(); col[cur] = 2; return res; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; g[x].push_back(y); } vector<int> ans; for (int i = 1; i <= n; ++i) { if (!col[i]) { vector<int> path; auto res = find_cycle(i, path); if (res.size()) { ans = reduce_cycle(res); break; } } } if (ans.size()) { cout << ans.size() << '\n'; for (int a : ans) { cout << a << '\n'; } } else { cout << -1 << '\n'; } }
#include <bits/stdc++.h> using namespace std; /* result can always be reduced to a cycle lets say we have a cycle, i posit that even if there are extra stuff, we can still reduce it extra forward edge-> we can take one of the two dfs down -> find a cycle (if none then no answer) run through the elements in the cycle, chopping off ones run through once-> establish a set & order. run again, checking if the set has extra edge, if so, chop it off and repeat */ const int N = 1000 + 5; int n, m; vector<int> g[N]; vector<int> reduce_cycle(vector<int> v) { vector<int> res = v; int in[N] = {}; int cnt = 1; for (int e : v) { in[e] = cnt++; } int len = v.size(); for (int i = 0; i < v.size(); ++i) { int e = v[i]; for (int to : g[e]) { if (to == v[(i + 1) % len]) continue; if (in[to]) { // chop it off vector<int> nxt; if (in[e] < in[to]) { nxt.assign(res.begin(), res.begin() + in[e]); nxt.insert(nxt.end(), res.begin() + in[to] - 1, res.end()); } else { nxt.assign(res.begin() + in[to] - 1, res.begin() + in[e]); } res.swap(nxt); return reduce_cycle(res); } } } return res; } int col[N]; vector<int> find_cycle(int cur, vector<int> &path) { path.push_back(cur); col[cur] = 1; vector<int> res; for (int a : g[cur]) { if (col[a] == 1) { auto it = find(path.begin(), path.end(), a); assert(it != path.end()); res.assign(it, path.end()); } else { res = find_cycle(a, path); } if (res.size()) break; } path.pop_back(); col[cur] = 2; return res; } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; g[x].push_back(y); } vector<int> ans; for (int i = 1; i <= n; ++i) { if (!col[i]) { vector<int> path; auto res = find_cycle(i, path); if (res.size()) { ans = reduce_cycle(res); break; } } } if (ans.size()) { cout << ans.size() << '\n'; for (int a : ans) { cout << a << '\n'; } } else { cout << -1 << '\n'; } }
replace
20
21
20
21
0
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using i64 = long long; #define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++) #define all(x) x.begin(), x.end() #define let auto const template <typename... Types> struct dynarr : std::vector<Types...> { using std::vector<Types...>::vector; using size_type = typename std::vector<Types...>::size_type; auto &&operator[](size_type i) { return this->at(i); } auto &&operator[](size_type i) const { return this->at(i); } }; vector<vector<i64>> aG; vector<vector<i64>> G; vector<vector<i64>> rG; vector<i64> res; vector<i64> cnt; vector<i64> ok; i64 AA = 0; bool dfs(i64 v, i64 f) { // cout << v << " " << f << endl; for (i64 x : aG[v]) { cnt[x]++; } for (i64 x : G[v]) { cnt[x]--; if (cnt[x] == 1 && ok[x] == 1) { res.push_back(x); res.push_back(v); return true; } if (cnt[x] == 0 && dfs(x, v)) { res.push_back(v); return true; } cnt[x]++; } for (i64 x : aG[v]) { cnt[x]--; } return false; } int main() { i64 N, M; cin >> N >> M; aG.resize(N); G.resize(N); rG.resize(N); cnt.resize(N); rep(i, 0, M) { i64 a, b; cin >> a >> b; a--; b--; aG[a].push_back(b); aG[b].push_back(a); G[a].push_back(b); rG[b].push_back(a); } rep(i, 0, N) { cnt.assign(N, 0); ok.assign(N, 0); for (i64 x : rG[i]) ok[x] = 1; if (dfs(i, -1)) { cout << res.size() << endl; for (auto x : res) { cout << x + 1 << endl; } return 0; } } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; using i64 = long long; #define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++) #define all(x) x.begin(), x.end() #define let auto const template <typename... Types> struct dynarr : std::vector<Types...> { using std::vector<Types...>::vector; using size_type = typename std::vector<Types...>::size_type; auto &&operator[](size_type i) { return this->at(i); } auto &&operator[](size_type i) const { return this->at(i); } }; vector<vector<i64>> aG; vector<vector<i64>> G; vector<vector<i64>> rG; vector<i64> res; vector<i64> cnt; vector<i64> ok; i64 AA = 0; bool dfs(i64 v, i64 f) { // cout << v << " " << f << endl; for (i64 x : aG[v]) { cnt[x]++; } for (i64 x : G[v]) { cnt[x]--; if (cnt[x] == 1 && ok[x] == 1) { res.push_back(x); res.push_back(v); return true; } if (cnt[x] == 0 && dfs(x, v)) { res.push_back(v); return true; } cnt[x]++; } for (i64 x : aG[v]) { cnt[x]--; } cnt[v] = 1e9; return false; } int main() { i64 N, M; cin >> N >> M; aG.resize(N); G.resize(N); rG.resize(N); cnt.resize(N); rep(i, 0, M) { i64 a, b; cin >> a >> b; a--; b--; aG[a].push_back(b); aG[b].push_back(a); G[a].push_back(b); rG[b].push_back(a); } rep(i, 0, N) { cnt.assign(N, 0); ok.assign(N, 0); for (i64 x : rG[i]) ok[x] = 1; if (dfs(i, -1)) { cout << res.size() << endl; for (auto x : res) { cout << x + 1 << endl; } return 0; } } cout << -1 << endl; }
insert
45
45
45
46
TLE
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; struct Edge { int from, to; }; struct Node { int num; vector<int> visited; }; using Graph = vector<vector<Edge>>; int main() { int n, m; cin >> n >> m; Graph graph(n + 1, vector<Edge>()); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; graph[a].push_back({a, b}); } vector<int> ans; for (int i = 1; i <= n; ++i) { queue<Node> que; deque<bool> visited(n + 1); for (auto e : graph[i]) { que.push({e.to, vector<int>({e.to})}); visited[e.to] = true; } while (!que.empty()) { auto cur = que.front(); que.pop(); if (cur.num == i) { if (ans.size() == 0 || cur.visited.size() < ans.size()) { ans = cur.visited; } break; } for (auto e : graph[cur.num]) { if (visited[e.to]) continue; auto v = cur.visited; v.push_back(e.to); que.push({e.to, v}); } } } if (ans.size() == 0) cout << -1 << endl; else { cout << ans.size() << endl; for (auto e : ans) { cout << e << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; struct Edge { int from, to; }; struct Node { int num; vector<int> visited; }; using Graph = vector<vector<Edge>>; int main() { int n, m; cin >> n >> m; Graph graph(n + 1, vector<Edge>()); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; graph[a].push_back({a, b}); } vector<int> ans; for (int i = 1; i <= n; ++i) { queue<Node> que; deque<bool> visited(n + 1); for (auto e : graph[i]) { que.push({e.to, vector<int>({e.to})}); visited[e.to] = true; } while (!que.empty()) { auto cur = que.front(); que.pop(); if (cur.num == i) { if (ans.size() == 0 || cur.visited.size() < ans.size()) { ans = cur.visited; } break; } for (auto e : graph[cur.num]) { if (visited[e.to]) continue; auto v = cur.visited; v.push_back(e.to); que.push({e.to, v}); visited[e.to] = true; } } } if (ans.size() == 0) cout << -1 << endl; else { cout << ans.size() << endl; for (auto e : ans) { cout << e << endl; } } return 0; }
insert
49
49
49
50
TLE
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int n, m; struct edge { int v, nxt; } e[2019][2]; int head[2019][2], in[2019], out[2019], tot[2] = {1, 1}, vst[2019]; void ae(int u, int v, int f) { if (!f) { in[v]++; out[u]++; } e[tot[f]][f] = {v, head[u][f]}; head[u][f] = tot[f]++; } queue<int> q; int ans[2000], tmp[2000], ptr = 2000, ptr1 = 0; int kkk = 2; void dfs(int u, int len) { if (len >= ptr) return; vst[u] = kkk; tmp[len] = u; bool ok = 0; for (int i = head[u][0]; i; i = e[i][0].nxt) { int v = e[i][0].v; if (vst[v] != 1 && vst[v] != kkk) { dfs(v, len + 1); ok = 1; } } if (!ok) { if (len < ptr) { ptr = len + 1; for (int i = 0; i < ptr; i++) { ans[i] = tmp[i]; } } } vst[u] = kkk - 1; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; ae(u, v, 0); ae(v, u, 1); } int visit = 0; for (int i = 1; i <= n; i++) { if (in[i] == 0 || out[i] == 0) q.push(i), vst[i] = 1; } while (!q.empty()) { int u = q.front(); q.pop(); if (in[u] == 0) { for (int i = head[u][0]; i; i = e[i][0].nxt) { int v = e[i][0].v; in[v]--; if (in[v] == 0 && !vst[v]) { vst[v] = 1; q.push(v); } } } else { for (int i = head[u][1]; i; i = e[i][1].nxt) { int v = e[i][1].v; out[v]--; if (out[v] == 0 && !vst[v]) { vst[v] = 1; q.push(v); } } } } for (int i = 1; i <= n; i++) if (in[i] == out[i] && in[i] == 0) visit++; if (visit == n) { cout << -1 << endl; return 0; } int x = 1; bool ok = 0; for (; x <= n; x++) { if (vst[x] != 1) { kkk++; dfs(x, 0); } } if (ptr == 0 || ptr == 2000) { cout << -1 << endl; return 0; } cout << ptr << endl; for (int i = 0; i < ptr; i++) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; struct edge { int v, nxt; } e[2019][2]; int head[2019][2], in[2019], out[2019], tot[2] = {1, 1}, vst[2019]; void ae(int u, int v, int f) { if (!f) { in[v]++; out[u]++; } e[tot[f]][f] = {v, head[u][f]}; head[u][f] = tot[f]++; } queue<int> q; int ans[2000], tmp[2000], ptr = 2000, ptr1 = 0; int kkk = 2; void dfs(int u, int len) { if (len >= ptr) return; vst[u] = kkk; tmp[len] = u; bool ok = 0; for (int i = head[u][0]; i; i = e[i][0].nxt) { int v = e[i][0].v; if (vst[v] == kkk) break; if (vst[v] != 1 && vst[v] != kkk) { dfs(v, len + 1); ok = 1; } } if (!ok) { if (len < ptr) { ptr = len + 1; for (int i = 0; i < ptr; i++) { ans[i] = tmp[i]; } } } vst[u] = kkk - 1; } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; ae(u, v, 0); ae(v, u, 1); } int visit = 0; for (int i = 1; i <= n; i++) { if (in[i] == 0 || out[i] == 0) q.push(i), vst[i] = 1; } while (!q.empty()) { int u = q.front(); q.pop(); if (in[u] == 0) { for (int i = head[u][0]; i; i = e[i][0].nxt) { int v = e[i][0].v; in[v]--; if (in[v] == 0 && !vst[v]) { vst[v] = 1; q.push(v); } } } else { for (int i = head[u][1]; i; i = e[i][1].nxt) { int v = e[i][1].v; out[v]--; if (out[v] == 0 && !vst[v]) { vst[v] = 1; q.push(v); } } } } for (int i = 1; i <= n; i++) if (in[i] == out[i] && in[i] == 0) visit++; if (visit == n) { cout << -1 << endl; return 0; } int x = 1; bool ok = 0; for (; x <= n; x++) { if (vst[x] != 1) { kkk++; dfs(x, 0); } } if (ptr == 0 || ptr == 2000) { cout << -1 << endl; return 0; } cout << ptr << endl; for (int i = 0; i < ptr; i++) { cout << ans[i] << endl; } return 0; }
insert
26
26
26
28
TLE
p02902
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <time.h> #include <unistd.h> #include <vector> #ifndef ONLINE_JUDGE #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #else #define DEBUG(x) \ do { \ } while (0); #endif #define pb push_back #define mp make_pair #define X first #define Y second #define FOR(i, A, N) for (int(i) = (A); (i) < (N); (i)++) #define REP(i, N) for (int(i) = 0; (i) < (N); (i)++) using namespace std; typedef long long ll; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } int n, m; class CycleFinder { public: map<int, int> state; map<int, vector<int>> nxt; vector<int> cycle; set<int> vertices; vector<int> stk; void dfs(int u) { state[u] = 1; stk.pb(u); for (int v : nxt[u]) { if (cycle.size() == 0 && state[v] == 1) { bool write = false; for (int j = 0; j < stk.size(); j++) { if (stk[j] == v) { write = true; } if (write) cycle.pb(stk[j]); } } else if (!state[v]) { dfs(v); } } stk.pop_back(); state[u] = 2; } vector<int> find() { vector<int> ord; for (int i : vertices) ord.pb(i); random_shuffle(ord.begin(), ord.end()); for (int i : ord) if (!state[i]) { dfs(i); } return cycle; } }; int main() { scanf("%d%d", &n, &m); CycleFinder initial; vector<pair<int, int>> edges; REP(i, n) { initial.state[i] = 0; initial.vertices.insert(i); initial.nxt[i] = vector<int>{}; } REP(i, m) { int a, b; scanf("%d%d", &a, &b); a--; b--; edges.pb({a, b}); initial.nxt[a].pb(b); } vector<int> initCycle = initial.find(); if (initCycle.size() == 0) { printf("-1\n"); return 0; } vector<int> currCycle = initCycle; vector<int> nxtCycle; for (int t = 0; t < 3000; t++) { CycleFinder cf; set<int> has; for (int i : currCycle) { has.insert(i); cf.state[i] = 0; cf.vertices.insert(i); cf.nxt[i] = vector<int>{}; } for (auto e : edges) { if (has.count(e.first) && has.count(e.second)) { cf.nxt[e.first].pb(e.second); } } for (int i : currCycle) { random_shuffle(cf.nxt[i].begin(), cf.nxt[i].end()); } currCycle = cf.find(); } printf("%d\n", currCycle.size()); for (int x : currCycle) printf("%d\n", x + 1); return 0; }
#include <algorithm> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <time.h> #include <unistd.h> #include <vector> #ifndef ONLINE_JUDGE #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #else #define DEBUG(x) \ do { \ } while (0); #endif #define pb push_back #define mp make_pair #define X first #define Y second #define FOR(i, A, N) for (int(i) = (A); (i) < (N); (i)++) #define REP(i, N) for (int(i) = 0; (i) < (N); (i)++) using namespace std; typedef long long ll; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } int n, m; class CycleFinder { public: map<int, int> state; map<int, vector<int>> nxt; vector<int> cycle; set<int> vertices; vector<int> stk; void dfs(int u) { state[u] = 1; stk.pb(u); for (int v : nxt[u]) { if (cycle.size() == 0 && state[v] == 1) { bool write = false; for (int j = 0; j < stk.size(); j++) { if (stk[j] == v) { write = true; } if (write) cycle.pb(stk[j]); } } else if (!state[v]) { dfs(v); } } stk.pop_back(); state[u] = 2; } vector<int> find() { vector<int> ord; for (int i : vertices) ord.pb(i); random_shuffle(ord.begin(), ord.end()); for (int i : ord) if (!state[i]) { dfs(i); } return cycle; } }; int main() { scanf("%d%d", &n, &m); CycleFinder initial; vector<pair<int, int>> edges; REP(i, n) { initial.state[i] = 0; initial.vertices.insert(i); initial.nxt[i] = vector<int>{}; } REP(i, m) { int a, b; scanf("%d%d", &a, &b); a--; b--; edges.pb({a, b}); initial.nxt[a].pb(b); } vector<int> initCycle = initial.find(); if (initCycle.size() == 0) { printf("-1\n"); return 0; } vector<int> currCycle = initCycle; vector<int> nxtCycle; for (int t = 0; t < 1000; t++) { CycleFinder cf; set<int> has; for (int i : currCycle) { has.insert(i); cf.state[i] = 0; cf.vertices.insert(i); cf.nxt[i] = vector<int>{}; } for (auto e : edges) { if (has.count(e.first) && has.count(e.second)) { cf.nxt[e.first].pb(e.second); } } for (int i : currCycle) { random_shuffle(cf.nxt[i].begin(), cf.nxt[i].end()); } currCycle = cf.find(); } printf("%d\n", currCycle.size()); for (int x : currCycle) printf("%d\n", x + 1); return 0; }
replace
104
105
104
105
TLE
p02902
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const int INF = 1001001001; int n; vector<int> to[1005]; vector<int> bfs(int sv) { vector<int> dist(n, INF), pre(n, -1); queue<int> q; dist[sv] = 0; q.push(sv); while (!q.empty()) { int v = q.front(); q.pop(); for (int u : to[v]) { if (dist[u] != INF) continue; pre[u] = v; dist[u] = dist[v] + 1; q.push(u); } } pair<int, int> best(INF, -1); rep(v, n) { if (v == sv) continue; for (int u : to[v]) { // svに戻ってくる場合(サイクルとなっている場合) if (u == sv) { best = min(best, make_pair(dist[v], v)); } } } // cout<<"Debug"<<best.first<<endl; if (best.first == INF) return vector<int>(n + 1); int v = best.second; vector<int> res; while (v != -1) { res.push_back(v); v = pre[v]; } return res; } int main() { int m; cin >> n >> m; rep(i, m) { int a, b; cin >> a >> b; a--, b--; to[a].push_back(b); } vector<int> ans(n + 1); rep(i, m) { auto now = bfs(i); if (now.size() < ans.size()) ans = now; } if (ans.size() == n + 1) { cout << -1 << endl; return 0; } cout << ans.size() << endl; for (int v : ans) { cout << v + 1 << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const int INF = 1001001001; int n; vector<int> to[1005]; vector<int> bfs(int sv) { vector<int> dist(n, INF), pre(n, -1); queue<int> q; dist[sv] = 0; q.push(sv); while (!q.empty()) { int v = q.front(); q.pop(); for (int u : to[v]) { if (dist[u] != INF) continue; pre[u] = v; dist[u] = dist[v] + 1; q.push(u); } } pair<int, int> best(INF, -1); rep(v, n) { if (v == sv) continue; for (int u : to[v]) { // svに戻ってくる場合(サイクルとなっている場合) if (u == sv) { best = min(best, make_pair(dist[v], v)); } } } // cout<<"Debug"<<best.first<<endl; if (best.first == INF) return vector<int>(n + 1); int v = best.second; vector<int> res; while (v != -1) { res.push_back(v); v = pre[v]; } return res; } int main() { int m; cin >> n >> m; rep(i, m) { int a, b; cin >> a >> b; a--, b--; to[a].push_back(b); } vector<int> ans(n + 1); rep(i, n) { auto now = bfs(i); if (now.size() < ans.size()) ans = now; } if (ans.size() == n + 1) { cout << -1 << endl; return 0; } cout << ans.size() << endl; for (int v : ans) { cout << v + 1 << endl; } return 0; }
replace
59
60
59
60
0
p02902
C++
Runtime Error
#include <algorithm> // minmax, sort, swap #include <bitset> #include <cassert> #include <cmath> #include <cstdio> // printf, scanf #include <deque> // deque #include <functional> // function<void(int)> #include <iomanip> // cout<<setprecision(n) #include <iostream> // cin, cout, cerr #include <map> // key-value pairs sorted by keys #include <numeric> // iota #include <queue> // queue, priority_queue #include <set> // set #include <string> // string, stoi, to_string #include <vector> // vector #ifdef DEBUG #include "debug.hpp" #else #define debug(...) #endif #define int long long // at least int64 > 9*10^18 #define EL '\n' #define rep(i, n) for (int i = 0; i < (n); i++) #define print(i) std::cout << (i) << '\n' #define all(v) (v).begin(), (v).end() /* libraries */ signed main() { int n, m; std::cin >> n >> m; std::vector<std::vector<int>> g(n); rep(i, m) { int a, b; std::cin >> a >> b; a--; b--; g[a].emplace_back(b); } // find cycle std::vector<int> cycle; { std::vector<int> went(n, -1); auto dfs = [&](auto f, int i) -> int { for (auto to : g[i]) { // cycle if (went[to] == went[i]) { cycle.emplace_back(i); return to; } if (went[to] != -1) continue; went[to] = went[i]; int x = f(f, to); if (x == -2) return -2; if (x != -1) { cycle.emplace_back(i); if (x == i) return -2; return x; } } return -1; }; rep(i, n) { went[i] = i; if (dfs(dfs, i) == -2) break; } } if (cycle.size() == 0) { print(-1); return 0; } while (true) { debug(cycle); std::set<int> cs(all(cycle)); std::set<std::pair<int, int>> set; rep(i, n) if (cs.count(i)) for (auto to : g[i]) if (cs.count(to)) set.emplace(i, to); debug(set.size()); int o = set.size(); rep(i, cycle.size()) set.erase(std::make_pair(cycle[(i + 1) % cycle.size()], cycle[i])); assert(o - cycle.size() == set.size()); debug(set.size()); if (set.empty()) break; auto e = set.begin(); int s = e->first, t = e->second; std::vector<int> went(n, 1); for (auto x : cycle) went[x] = -1; cycle.clear(); auto dfs = [&](auto f, int i) -> int { if (i == s) { cycle.emplace_back(i); return 0; } for (auto to : g[i]) { if (went[to] != -1) continue; went[to] = went[i]; int x = f(f, to); if (x != -1) { cycle.emplace_back(i); return x; } } return -1; }; went[t] = 0; dfs(dfs, t); } if (cycle.size() == 0) { print(-1); return 0; } print(cycle.size()); for (auto x : cycle) print(x + 1); return 0; }
#include <algorithm> // minmax, sort, swap #include <bitset> #include <cassert> #include <cmath> #include <cstdio> // printf, scanf #include <deque> // deque #include <functional> // function<void(int)> #include <iomanip> // cout<<setprecision(n) #include <iostream> // cin, cout, cerr #include <map> // key-value pairs sorted by keys #include <numeric> // iota #include <queue> // queue, priority_queue #include <set> // set #include <string> // string, stoi, to_string #include <vector> // vector #ifdef DEBUG #include "debug.hpp" #else #define debug(...) #endif #define int long long // at least int64 > 9*10^18 #define EL '\n' #define rep(i, n) for (int i = 0; i < (n); i++) #define print(i) std::cout << (i) << '\n' #define all(v) (v).begin(), (v).end() /* libraries */ signed main() { int n, m; std::cin >> n >> m; std::vector<std::vector<int>> g(n); rep(i, m) { int a, b; std::cin >> a >> b; a--; b--; g[a].emplace_back(b); } // find cycle std::vector<int> cycle; { std::vector<int> went(n, -1); auto dfs = [&](auto f, int i) -> int { for (auto to : g[i]) { // cycle if (went[to] == went[i]) { cycle.emplace_back(i); return to; } if (went[to] != -1) continue; went[to] = went[i]; int x = f(f, to); if (x == -2) return -2; if (x != -1) { cycle.emplace_back(i); if (x == i) return -2; return x; } } went[i] = -2; return -1; }; rep(i, n) { went[i] = i; if (dfs(dfs, i) == -2) break; } } if (cycle.size() == 0) { print(-1); return 0; } while (true) { debug(cycle); std::set<int> cs(all(cycle)); std::set<std::pair<int, int>> set; rep(i, n) if (cs.count(i)) for (auto to : g[i]) if (cs.count(to)) set.emplace(i, to); debug(set.size()); int o = set.size(); rep(i, cycle.size()) set.erase(std::make_pair(cycle[(i + 1) % cycle.size()], cycle[i])); assert(o - cycle.size() == set.size()); debug(set.size()); if (set.empty()) break; auto e = set.begin(); int s = e->first, t = e->second; std::vector<int> went(n, 1); for (auto x : cycle) went[x] = -1; cycle.clear(); auto dfs = [&](auto f, int i) -> int { if (i == s) { cycle.emplace_back(i); return 0; } for (auto to : g[i]) { if (went[to] != -1) continue; went[to] = went[i]; int x = f(f, to); if (x != -1) { cycle.emplace_back(i); return x; } } return -1; }; went[t] = 0; dfs(dfs, t); } if (cycle.size() == 0) { print(-1); return 0; } print(cycle.size()); for (auto x : cycle) print(x + 1); return 0; }
insert
64
64
64
65
0
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define INF 1e18 #define int long long #define Rep(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) Rep(i, 0, n) #define all(a) (a).begin(), (a).end() using namespace std; typedef pair<int, int> P; typedef pair<int, P> PP; const int mod = 1000000007; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> G(n); rep(i, m) { int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); } int len = n + 1; vector<int> ans; for (int s = 0; s < n; s++) { vector<int> d(n, -1); vector<int> pre(n, -1); queue<int> que; d[s] = 0; que.push(s); while (!que.empty()) { int t = que.front(); que.pop(); for (auto nxt : G[t]) { if (d[nxt] == -1) { d[nxt] = d[t] + 1; pre[nxt] = t; que.push(nxt); } } for (int t = 0; t < n; t++) { if (s == t || d[t] == -1) continue; for (auto p : G[t]) { if (p == s) { vector<int> v({s}); int now = t; while (now != s) { v.push_back(now); now = pre[now]; } if (len > v.size()) { len = v.size(); ans = v; } } } } } } sort(all(ans)); if (len == n + 1) cout << -1 << endl; else { cout << len << endl; rep(i, len) cout << ans[i] + 1 << endl; } }
#include <bits/stdc++.h> #define INF 1e18 #define int long long #define Rep(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) Rep(i, 0, n) #define all(a) (a).begin(), (a).end() using namespace std; typedef pair<int, int> P; typedef pair<int, P> PP; const int mod = 1000000007; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> G(n); rep(i, m) { int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); } int len = n + 1; vector<int> ans; for (int s = 0; s < n; s++) { vector<int> d(n, -1); vector<int> pre(n, -1); queue<int> que; d[s] = 0; que.push(s); while (!que.empty()) { int t = que.front(); que.pop(); for (auto nxt : G[t]) { if (d[nxt] == -1) { d[nxt] = d[t] + 1; pre[nxt] = t; que.push(nxt); } } } for (int t = 0; t < n; t++) { if (s == t || d[t] == -1) continue; for (auto p : G[t]) { if (p == s) { vector<int> v({s}); int now = t; while (now != s) { v.push_back(now); now = pre[now]; } if (len > v.size()) { len = v.size(); ans = v; } } } } } sort(all(ans)); if (len == n + 1) cout << -1 << endl; else { cout << len << endl; rep(i, len) cout << ans[i] + 1 << endl; } }
replace
42
57
42
57
TLE
p02902
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstring> //memset(dp,0,sizeof(dp)) #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> #define ll long long #define rep(i, n) for (int i = 0; i < (n); i++) #define repp(i, n) for (int i = n - 1; i >= 0; i--) #define fi first #define se second #define pb push_back #define ppb pop_back() #define ALL(a) (a).begin(), (a).end() using namespace std; template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } typedef pair<int, int> P; typedef pair<ll, ll> Pll; signed main() { int n, m; cin >> n >> m; vector<int> to[n]; rep(i, m) { int a, b; cin >> a >> b; a--; b--; to[a].pb(b); } bool ok = 0; vector<int> sta; rep(i, n) { bool bl = 0; vector<bool> vis(n, 0); vis[i] = 1; queue<int> q; q.push(i); while (!q.empty()) { if (bl) break; int v = q.front(); q.pop(); for (int u : to[v]) { if (u == i) { // exist loop sta.pb(i); ok = 1; bl = 1; break; } q.push(u); vis[u] = 1; } } } if (!ok) { cout << -1 << endl; return 0; } vector<P> vp; vector<int> v[n]; for (int st : sta) { // st->stの最短経路を持って復元 int d[n]; rep(i, n) d[i] = 1001001; priority_queue<P, vector<P>, greater<P>> que; d[st] = 0; int okk = 0; que.push(P(0, st)); int prev[n]; while (!que.empty()) { if (okk == 1) { d[st] = 100100; } okk++; P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < to[v].size(); ++i) { int e = to[v][i]; if (d[e] > d[v] + 1) { d[e] = d[v] + 1; prev[e] = v; que.push(P(d[e], e)); } } } vp.pb({d[st], st}); int stt = st; int cnt = d[st]; rep(i, cnt) { v[stt].pb(prev[st]); st = prev[st]; } reverse(ALL(v[stt])); } sort(ALL(vp)); P p = vp[0]; cout << p.fi << endl; for (int u : v[p.se]) { cout << ++u << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstring> //memset(dp,0,sizeof(dp)) #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> #define ll long long #define rep(i, n) for (int i = 0; i < (n); i++) #define repp(i, n) for (int i = n - 1; i >= 0; i--) #define fi first #define se second #define pb push_back #define ppb pop_back() #define ALL(a) (a).begin(), (a).end() using namespace std; template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } typedef pair<int, int> P; typedef pair<ll, ll> Pll; signed main() { int n, m; cin >> n >> m; vector<int> to[n]; rep(i, m) { int a, b; cin >> a >> b; a--; b--; to[a].pb(b); } bool ok = 0; vector<int> sta; rep(i, n) { bool bl = 0; vector<bool> vis(n, 0); vis[i] = 1; queue<int> q; q.push(i); while (!q.empty()) { if (bl) break; int v = q.front(); q.pop(); for (int u : to[v]) { if (u == i) { // exist loop sta.pb(i); ok = 1; bl = 1; break; } if (vis[u]) continue; q.push(u); vis[u] = 1; } } } if (!ok) { cout << -1 << endl; return 0; } vector<P> vp; vector<int> v[n]; for (int st : sta) { // st->stの最短経路を持って復元 int d[n]; rep(i, n) d[i] = 1001001; priority_queue<P, vector<P>, greater<P>> que; d[st] = 0; int okk = 0; que.push(P(0, st)); int prev[n]; while (!que.empty()) { if (okk == 1) { d[st] = 100100; } okk++; P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < to[v].size(); ++i) { int e = to[v][i]; if (d[e] > d[v] + 1) { d[e] = d[v] + 1; prev[e] = v; que.push(P(d[e], e)); } } } vp.pb({d[st], st}); int stt = st; int cnt = d[st]; rep(i, cnt) { v[stt].pb(prev[st]); st = prev[st]; } reverse(ALL(v[stt])); } sort(ALL(vp)); P p = vp[0]; cout << p.fi << endl; for (int u : v[p.se]) { cout << ++u << endl; } return 0; }
insert
65
65
65
67
TLE
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef long double ld; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define F first #define S second constexpr char ln = '\n'; const int mx = 100010; const ll mod = 1e9 + 7; int main() { int n, m; cin >> n >> m; vector<int> v[n]; vector<vector<int>> dist(n, vector<int>(n, inf)); rep(i, m) { int x, y; cin >> x >> y; x--; y--; v[x].emplace_back(y); } rep(i, n) { dist[i][i] = 0; queue<int> q; q.push(i); while (!q.empty()) { auto p = q.front(); q.pop(); for (auto to : v[p]) { if (dist[i][to] > dist[i][p] + 1) { dist[i][to] = dist[i][p] + 1; q.push(to); } } } } int st = -1, gl = -1, c = inf; rep(i, n) rep(j, i) { if (dist[i][j] + dist[j][i] < c && dist[j][i] == 1) { st = i; gl = j; c = dist[i][j] + dist[j][i]; } } if (c == inf) { cout << -1 << ln; return 0; } int pos = st; vector<int> a; a.emplace_back(pos); while (pos != gl) { for (auto to : v[pos]) { if (dist[pos][gl] == dist[to][gl] - 1 && dist[st][pos] + 1 == dist[st][to]) { pos = to; a.emplace_back(pos); break; } } } cout << a.size() << ln; for (auto ans : a) { cout << ans + 1 << ln; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef long double ld; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) #define F first #define S second constexpr char ln = '\n'; const int mx = 100010; const ll mod = 1e9 + 7; int main() { int n, m; cin >> n >> m; vector<int> v[n]; vector<vector<int>> dist(n, vector<int>(n, inf)); rep(i, m) { int x, y; cin >> x >> y; x--; y--; v[x].emplace_back(y); } rep(i, n) { dist[i][i] = 0; queue<int> q; q.push(i); while (!q.empty()) { auto p = q.front(); q.pop(); for (auto to : v[p]) { if (dist[i][to] > dist[i][p] + 1) { dist[i][to] = dist[i][p] + 1; q.push(to); } } } } int st = -1, gl = -1, c = inf; rep(i, n) rep(j, i) { if (dist[i][j] + dist[j][i] < c && dist[j][i] == 1) { st = i; gl = j; c = dist[i][j] + dist[j][i]; } } if (c == inf) { cout << -1 << ln; return 0; } int pos = st; vector<int> a; a.emplace_back(pos); while (pos != gl) { for (auto to : v[pos]) { if (dist[pos][gl] - 1 == dist[to][gl] && dist[st][pos] + 1 == dist[st][to]) { pos = to; a.emplace_back(pos); break; } } } cout << a.size() << ln; for (auto ans : a) { cout << ans + 1 << ln; } return 0; }
replace
63
64
63
64
TLE
p02902
C++
Runtime Error
// #undef _DEBUG // #pragma GCC optimize("Ofast") // 不動小数点の計算高速化 // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName // 最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name /*@formatter:off*/ //[-n, n)にアクセスできる // また、外部関数resizeに渡せる // sizeは[-n, n)でnを返す template <class T> class mvec { vector<T> v; int n; public: mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} auto &operator[](int i) { return v[i + n]; } auto size() { return n; } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; //[]でboolは参照を返さないため特殊化が必要 template <> struct mvec<bool> { vector<bool> v; int n; mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, bool val) : n(n), v(n * 2, val) {} auto operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { int spa = 3; for (auto &&v : a) { spa = max(spa, (int)(to_string(v).size()) + 1); } int n = (int)a.size(); os << endl; for (int i = -n; i < n; i++) { int need = spa - ((int)to_string(i).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(a[i]).size())); } while (need--) { os << " "; } os << i; } os << endl; int i = -n; for (auto &&v : a) { int need = spa - ((int)to_string(v).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(i).size())); } while (need--) { os << " "; } os << v; i++; } return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #define mvvt0(t) mvec<mvec<t>> #define mvvt1(t, a) mvec<mvec<t>> a #define mvvt2(t, a, b) mvec<mvec<t>> a(b) #define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c)) #define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d)) #define mvvi(...) \ over4(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__) template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); } template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) { return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...)); } #define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__) #ifdef _DEBUG string message; // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; // #define vector debtor // 区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapu unordered_map<uint64_t, ll> #define umapip unordered_map<ll, P> template <class T, class U, class X> auto count(unordered_map<T, U> &a, X k) { return a.find(k) != a.end(); } #else #define endl '\n' // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapu __gnu_pbds::gp_hash_table<uint64_t, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> template <class T, class U, class W, class X> auto count(__gnu_pbds::gp_hash_table<T, U, W> &a, X k) { return a.find(k) != a.end(); } #endif /*@formatter:on*/ struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; /*@formatter:off*/ template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } // 衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &r) const { return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &r) const { return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順 */ } bool operator==(const T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } //@マクロ省略系 型,構造 #define double long double using dou = double; const double eps = 1e-9; // 基本コメントアウト /* struct epsdou { double v; epsdou(double v = 0) : v(v) {} template<class T> epsdou &operator+=(T b) { v += (double) b; return (*this); } template<class T> epsdou &operator-=(T b) { v -= (double) b; return (*this); } template<class T> epsdou &operator*=(T b) { v *= (double) b; return (*this); } template<class T> epsdou &operator/=(T b) { v /= (double) b; return (*this); } epsdou operator+(epsdou b) { return v + (double) b; } epsdou operator-(epsdou b) { return v - (double) b; } epsdou operator*(epsdou b) { return v * (double) b; } epsdou operator/(epsdou b) { return v / (double) b; } epsdou operator-() const { return epsdou(-v); } template<class T> bool operator<(T b) { return v < (double) b; } template<class T> bool operator>(T b) { return v > (double) b; } template<class T> bool operator==(T b) { return fabs(v - (double) b) <= eps; } template<class T> bool operator<=(T b) { return v < (double) b || fabs(v - b) <= eps; } template<class T> bool operator>=(T b) { return v > (double) b || fabs(v - b) <= eps; } operator double() { return v; }};istream &operator>>(istream &iss, epsdou &a) { iss >> a.v; return iss;}ostream &operator<<(ostream &os, epsdou &a) { os << a.v; return os;} #define eps_conr_t(o) template<class T> epsdou operator o(T b, epsdou a){return a.v o (dou)b;} #define eps_conl_t(o) template<class T> epsdou operator o(epsdou a, T b){return a.v o (dou)b;} eps_conl_t(+)eps_conl_t(-)eps_conl_t(*)eps_conl_t(/)eps_conr_t(+)eps_conr_t(-)eps_conr_t(*)eps_conr_t(/) #undef double #define double epsdou */ #define ull unsigned long long using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using mvp = mvec<P>; using mvt = mvec<T>; #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam1(ret) [&](auto &v) { return ret; } #define lam2(v, ret) [&](auto &v) { return ret; } #define lam(...) over2(__VA_ARGS__, lam2, lam1)(__VA_ARGS__) #define lamr(right) [&](auto &p) { return p right; } // マクロ省略系 コンテナ using vi = vector<ll>; using vb = vector<bool>; using vs = vector<string>; using vd = vector<double>; using vc = vector<char>; using vp = vector<P>; using vt = vector<T>; // #define V vector #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vvi(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) #define vvt(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__) #define vv(type, ...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__) template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } #define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__) #define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__) #define vns(name, ...) auto name = make_v<string>(__VA_ARGS__) #define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__) #define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__) #define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__) #define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setp = set<P>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap // 任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> // マクロ 繰り返し // ↓@オーバーロード隔離 #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) // 逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) // ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss2(i, I) for (int i = 0; i < I; i++) #define repss4(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss6(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss8(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss10(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) // i,j,k...をnまで見る #define reps2(i, n) repss2(i, n) #define reps3(i, j, n) repss4(i, j, n, n) #define reps4(i, j, k, n) repss6(i, j, k, n, n, n) #define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n) template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_2(i, a) repss2(i, sz(a)) // 正方形である必要はない // 直前を持つのとどっちが早いか #define repv_3(i, j, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \ nex_repv2(i, j, I, J, a)) // 箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_4(i, j, k, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \ i = 0, j = 0, k = 0; \ i < I; nex_repv3(i, j, k, I, J, K, a)) #define repv_5(i, j, k, l, a) \ repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_6(i, j, k, l, m, a) \ repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define forrm1(st) \ for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it) #define forrm3(k, v, st) \ for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it) #define fors1(st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors2(v, st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors3(v, st, r) \ for (auto &&fors_it = st.begin(); fors_it != st.end() && (*fors_it) < r; \ ++fors_it) #define fors4(v, st, l, r) \ for (auto &&fors_it = st.lower_bound(l); \ fors_it != st.end() && (*fors_it) < r; ++fors_it) #define forslr3(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr4(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr5(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr6(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> vector<U> to1d(vector<vector<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(vector<vector<vector<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<vector<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<vector<vector<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define fora_init_2(a, A) ; #define fora_init_3(fora_i, a, A) auto &&a = A[fora_i]; #define fora_init_4(a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_5(fora_i, a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_7(fora_i, a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define fora_init_9(fora_i, a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define fora_init(...) \ over9(__VA_ARGS__, fora_init_9, fora_init_8, fora_init_7, fora_init_6, \ fora_init_5, fora_init_4, fora_init_3, fora_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_3(forr_i, a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_5(forr_i, a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_7(forr_i, a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init_9(forr_i, a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init(...) \ over9(__VA_ARGS__, forr_init_9, forr_init_8, forr_init_7, forr_init_6, \ forr_init_5, forr_init_4, forr_init_3, forr_init_2)(__VA_ARGS__) #define forp_init(k, v, ...) \ auto &&k = (*forp_it).fi; \ auto &&v = (*forp_it).se; #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define forrm_init(k, v, ...) \ auto &&k = (*forrm_it).fi; \ auto &&v = (*forrm_it).se; #define fors_init(v, ...) auto &&v = (*fors_it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init(...) \ over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__); #define fora_2(a, A) for (auto &&a : A) #define fora_3(fora_i, a, A) rep(fora_i, sz(A)) #define fora_4(a, b, A, B) rep(fora_i, sz(A)) #define fora_5(fora_i, a, b, A, B) rep(fora_i, sz(A)) #define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_7(fora_i, a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define fora_9(fora_i, a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_3(forr_i, a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_5(forr_i, a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_7(forr_i, a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) #define forr_9(forr_i, a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) // ↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__) #define repss(...) \ over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \ repss2)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #define fora(...) \ over9(__VA_ARGS__, fora_9, fora_8, fora_7, fora_6, fora_5, fora_4, fora_3, \ fora_2)(__VA_ARGS__) #define forr(...) \ over9(__VA_ARGS__, forr_9, forr_8, forr_7, forr_6, forr_5, forr_4, forr_3, \ forr_2)(__VA_ARGS__) // #define forr(v, a) for(auto&& v : makeReversedRange(a)) // 参照を取らない #define forv(a, b) for (auto a : to1d(b)) // インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) // 値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #define form(...) \ over5(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define forrm(...) \ over5(__VA_ARGS__, forrm5, forrm4, forrm3, forrm2, forrm1)(__VA_ARGS__) #define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__) #define forp(k, v, st) \ for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it) // マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const dou dinf = (dou)linf * linf; const char infc = '{'; const string infs = "{"; const double PI = 3.1415926535897932384626433832795029L; // マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの // 埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:on*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { os << endl; for (auto &&v : m) os << v << endl; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, mset<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora(v,a)os<<v<<endl;return os;} /*@formatter:off*/ template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // template<typename W, typename H> void resize(vector<W> &vec, const H head) { // vec.resize(head); } template<typename W, typename H, typename ... T> void // resize(vector<W> &vec, const H &head, const T ... tail) {vec.resize(head);for // (auto &v: vec)resize(v, tail...);} template <typename T, typename F> bool all_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool all_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (!all_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool any_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool any_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (any_of2(v[i], f)) return true; } return false; } template <typename T, typename F> bool none_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool none_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (none_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool find_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll find_if2(vector<T> &v, F f) { rep(i, sz(v)) { if (find_if2(v[i], f)) return i; } return sz(v); } template <typename T, typename F> bool rfind_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll rfind_if2(vector<T> &v, F f) { rer(i, sz(v) - 1) { if (rfind_if2(v[i], f)) return i; } return -1; } template <class T> bool contains(string &s, const T &v) { return s.find(v) != string::npos; } template <typename T> bool contains(vector<T> &v, const T &val) { return std::find(v.begin(), v.end(), val) != v.end(); } template <typename T, typename F> bool contains_if2(vector<T> &v, F f) { return find_if(v.begin(), v.end(), f) != v.end(); } template <typename T, typename F> ll count_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll count_if2(vector<T> &vec, F f) { ll ret = 0; fora(v, vec) ret += count_if2(v, f); return ret; } template <typename T, typename F> void for_each2(T &v, F f) { f(v); } template <typename T, typename F> void for_each2(vector<T> &vec, F f) { fora(v, vec) for_each2(v, f); } template <typename W> ll count_od(vector<W> &a) { return count_if2(a, [](ll v) { return v & 1; }); } template <typename W> ll count_ev(vector<W> &a) { return count_if2(a, [](ll v) { return !(v & 1); }); } // 削除した後のvectorを返す template <typename T, typename F> vector<T> erase_if2(vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> erase_if2(vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = erase_if2(v[i], f); } return res; } template <typename T, typename F> vector<T> l_erase_if2(vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> ll l_rfind_if(vector<T> &v, F f) { rer(i, sz(v) - 1) { if (f(v[i])) return i; } return -1; } template <typename T, typename F> bool l_contains_if(vector<T> &v, F f) { rer(i, sz(v) - 1) { if (f(v[i])) return true; } return false; } template <class A, class B, class C> auto t_all_of(A a, B b, C c) { return std::all_of(a, b, c); } template <class A, class B, class C> auto t_any_of(A a, B b, C c) { return std::any_of(a, b, c); } template <class A, class B, class C> auto t_none_of(A a, B b, C c) { return std::none_of(a, b, c); } template <class A, class B, class C> auto t_find_if(A a, B b, C c) { return std::find_if(a, b, c); } template <class A, class B, class C> auto t_count_if(A a, B b, C c) { return std::count_if(a, b, c); } #define all_of_s__2(a, right) (t_all_of(all(a), lamr(right))) #define all_of_s__3(a, v, siki) (t_all_of(all(a), [&](auto v) { return siki; })) #define all_of_s(...) over3(__VA_ARGS__, all_of_s__3, all_of_s__2)(__VA_ARGS__) // all_of(A, %2); // all_of(A, a, a%2); #define all_of__2(a, right) all_of2(a, lamr(right)) #define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; }) #define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__) #define all_of_f(a, f) all_of2(a, f) #define any_of_s__2(a, right) (t_any_of(all(a), lamr(right))) #define any_of_s__3(a, v, siki) (t_any_of(all(a), [&](auto v) { return siki; })) #define any_of_s(...) over3(__VA_ARGS__, any_of_s__3, any_of_s__2)(__VA_ARGS__) #define any_of__2(a, right) any_of2(a, lamr(right)) #define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; }) #define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__) #define any_of_f(a, f) any_of2(a, f) #define none_of_s__2(a, right) (t_none_of(all(a), lamr(right))) #define none_of_s__3(a, v, siki) \ (t_none_of(all(a), [&](auto v) { return siki; })) #define none_of_s(...) \ over3(__VA_ARGS__, none_of_s__3, none_of_s__2)(__VA_ARGS__) #define none_of__2(a, right) none_of2(a, lamr(right)) #define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; }) #define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__) #define none_of_f(a, f) none_of2(a, f) #define find_if_s__2(a, right) (t_find_if(all(a), lamr(right)) - a.begin()) #define find_if_s__3(a, v, siki) \ (t_find_if(all(a), [&](auto v) { return siki; }) - a.begin()) #define find_if_s(...) \ over3(__VA_ARGS__, find_if_s__3, find_if_s__2)(__VA_ARGS__) #define find_if__2(a, right) find_if2(a, lamr(right)) #define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; }) #define find_if(...) over3(__VA_ARGS__, find_if__3, find_if__2)(__VA_ARGS__) #define find_if_f(a, f) find_if2(a, f) #define rfind_if_s__2(a, right) l_rfind_if(a, lamr(right)) #define rfind_if_s__3(a, v, siki) l_rfind_if(a, [&](auto v) { return siki; }) #define rfind_if_s(...) \ over3(__VA_ARGS__, rfind_if_s__3, rfind_if_s__2)(__VA_ARGS__) #define rfind_if__2(a, right) rfind_if2(a, lamr(right)) #define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; }) #define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if_s__2(a, right) l_contains_if(a, lamr(right)) #define contains_if_s__3(a, v, siki) \ l_contains_if(a, [&](auto v) { return siki; }) #define contains_if_s(...) \ over3(__VA_ARGS__, contains_if_s__3, contains_if_s__2)(__VA_ARGS__) #define contains_if__2(a, right) contains_if2(a, lamr(right)) #define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; }) #define contains_if(...) \ over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__) #define contains_if_f(a, f) contains_if2(a, f) #define count_if_s__2(a, right) (t_count_if(all(a), lamr(right))) #define count_if_s__3(a, v, siki) \ (t_count_if(all(a), [&](auto v) { return siki; })) #define count_if_s(...) \ over3(__VA_ARGS__, count_if_s__3, count_if_s__2)(__VA_ARGS__) #define count_if__2(a, right) count_if2(a, lamr(right)) #define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; }) #define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__) #define count_if_f(a, f) count_if2(a, f) #define for_each_s__2(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each_s__3(a, v, siki) \ do { \ fora(v, a) { siki; } \ } while (0) #define for_each_s(...) \ over3(__VA_ARGS__, for_each_s__3, for_each_s__2)(__VA_ARGS__) #define for_each__2(a, right) for_each2(a, lamr(right)) #define for_each__3(a, v, siki) for_each2(a, [&](auto v) { return siki; }) #define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__) #define for_each_f(a, f) for_each2(a, f); #define erase_if_s__2(a, right) l_erase_if2(a, lamr(right)) #define erase_if_s__3(a, v, siki) l_erase_if2(a, [&](auto v) { return siki; }) #define erase_if_s(...) \ over3(__VA_ARGS__, erase_if_s__3, erase_if_s__2)(__VA_ARGS__) #define erase_if__2(a, right) erase_if2(a, lamr(right)) #define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; }) #define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__) #define erase_if_f(a, f) erase_if2(a, f) #define entry_if_s__2(a, right) l_entry_if2(a, lamr(right)) #define entry_if_s__3(a, v, siki) l_entry_if2(a, [&](auto v) { return siki; }) #define entry_if_s(...) \ over3(__VA_ARGS__, entry_if_s__3, entry_if_s__2)(__VA_ARGS__) #define entry_if__2(a, right) entry_if2(a, lamr(right)) #define entry_if__3(a, v, siki) entry_if2(a, [&](auto v) { return siki; }) #define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__) #define entry_if_f(a, f) entry_if2(a, f) template <class T, class U, class W> void replace(vector<W> &a, T key, U v) { rep(i, sz(a)) if (a[i] == key) a[i] = v; } template <class T, class U, class W> void replace(vector<vector<W>> &A, T key, U v) { rep(i, sz(A)) replace(A[i], key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } // template<class T> void replace(T &a) { replace(a, '#'); } void replace(str &a) { int dec = 0; if ('a' <= a[0] && a[0] <= 'z') dec = 'a'; if ('A' <= a[0] && a[0] <= 'Z') dec = 'A'; fora(v, a) v -= dec; } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } void sort(int &a, int &b) { if (a > b) swap(a, b); } void sort(int &a, int &b, int &c) { sort(a, b); sort(a, c); sort(b, c); } void rsort(int &a, int &b) { if (a < b) swap(a, b); } void rsort(int &a, int &b, int &c) { rsort(a, b); rsort(a, c); rsort(b, c); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> // 例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <class... T, class U> auto sorted(U head, T... a) { sort(head, a...); return head; } template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) { rep(i, N) a[i] = v; } template <typename A, size_t N, size_t O, typename T> void fill(A (&a)[N][O], const T &v) { rep(i, N) rep(j, O) a[i][j] = v; } template <typename A, size_t N, size_t O, size_t P, typename T> void fill(A (&a)[N][O][P], const T &v) { rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T> void fill(A (&a)[N][O][P][Q], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, typename T> void fill(A (&a)[N][O][P][Q][R], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S, typename T> void fill(A (&a)[N][O][P][Q][R][S], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) a[i][j][k][l][m][n] = v; } template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <class W, class T> void fill(mvec<W> &xx, const T v) { fora(x, xx) fill(x, v); } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ over6(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ over6(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #ifdef _DEBUG template <class T> void err2(T &&head) { cerr << head; } template <class T, class... U> void err2(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); } template <class T, class... U> void err(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); cerr << "" << endl; } template <class T> void err(T &&head) { cerr << head << endl; } void err() { cerr << "" << endl; } template <class T> string out_m2(vector<T> &a, ll W = inf) { stringstream ss; if (W == inf) W = min(sz(a), 12ll); if (sz(a) == 0) return ss.str(); rep(i, W) { ss << a[i]; if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } template <class T> string out_m2(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1) ss << " *|"; else ss << " " << key << "|"; rep(w, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3) ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]), 12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } template <class T> string out_m2(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) { H = min({H, sz(a), 6ll}); W = min({W, sz(a[0]), 6ll}); stringstream ss; ss << endl; // if (key == inf)ss << " *|"; else ss << " " << key << "|"; if (key == inf) ss << " *|"; else { ss << std::right << std::setw(2) << key; ss << "|"; } rep(w, -W, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 8 + 3) ss << "_"; ss << "" << endl; rep(h, -H, H) { ss << std::right << std::setw(2) << h << "|"; int NW = min(sz(a[h]), 6ll); rep(w, -NW, NW) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 6; H = min(H, sz(a)); rep(i, -H, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } /*@formatter:off*/ template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m(vector<T> &a, ll W = inf) { stringstream ss; if (W == inf) W = min(sz(a), 12ll); if (sz(a) == 0) return ss.str(); rep(i, W) { ss << a[i] << " "; } ss << "" << endl; return ss.str(); } /*@formatter:off*/ template <class T> string out_m(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1) ss << " *|"; else ss << " " << key << "|"; rep(w, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3) ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]), 12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } ss << endl; return ss.str(); } template <class T> string out_m(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 5; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m(a[i], W, U, i); } ss << endl; return ss.str(); } string out_m(int a) { stringstream ss; ss << a << endl; return ss.str(); } template <class T> string out_m(T &a) { stringstream ss; ss << a << endl; return ss.str(); } template <class T> void outv(vector<T> &a, ll W = inf) { cout << out_m(a, W) << endl; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) { cout << out_m(a, H, W, key) << endl; } template <class T> void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) { cout << out_m(a, H, W, U) << endl; } #else template <class T> void outv(vector<T> &a, ll W = inf) { rep(i, min(W, sz(a))) { cout << a[i] << " "; } cout << "" << endl; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) { rep(i, min(H, sz(a))) { outv(a[i], W); } } template <class T> void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) { ; } #define err(...) ; #endif template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } // テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } template <class T> void na(set<T> &a, ll n) { rep(i, n) a.insert(in()); } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T> void nad(set<T> &a, ll n) { rep(i, n) a.insert(in() - 1); } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } template <class T, class U> void na2(set<T> &a, set<U> &b, ll n) { rep(i, n) { a.insert(in()); b.insert(in()); } } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--; } #define dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; // デバッグ #define sp << " " << #define deb1(x) debugName(x) << " = " << out_m2(x) #define deb_2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__) #define deb_3(x, ...) deb1(x) << ", " << deb_2(__VA_ARGS__) #define deb_4(x, ...) deb1(x) << ", " << deb_3(__VA_ARGS__) #define deb5(x, ...) deb1(x) << ", " << deb_4(__VA_ARGS__) #define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__) #define deb7(x, ...) deb1(x) << ", " << deb6(__VA_ARGS__) #define deb8(x, ...) deb1(x) << ", " << deb7(__VA_ARGS__) #define deb9(x, ...) deb1(x) << ", " << deb8(__VA_ARGS__) #define deb10(x, ...) deb1(x) << ", " << deb9(__VA_ARGS__) #ifdef _DEBUG #define deb(...) \ cerr << over10(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb_4, \ deb_3, deb_2, deb1)(__VA_ARGS__) \ << endl #define base_keta 8 void print_n_base(int x, int base) { cerr << bitset<base_keta>(x) << endl; } template <class T> void print_n_base(vector<T> X, int base) { cerr << endl; for (auto &&x : X) { print_n_base(x, base); } cerr << endl; } // n進数 #define deb2(x) \ cerr << debugName(x) << " = "; \ print_n_base(x, 2); #define deb3(x) \ cerr << debugName(x) << " = "; \ print_n_base(x, 3); #define deb4(x) \ cerr << debugName(x) << " = "; \ print_n_base(x, 4); #else #define deb(...) ; #define deb2(...) ; #define deb3(...) ; #define deb4(...) ; #endif #define debugline(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n' //@formatter:off // よく使うクラス、構造体 // graphでredefineしているため、書き換えたら向こうも書き換える struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; // 連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } // つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; using bint = __int128; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } __int128 to_bint(string &s) { __int128 ret = 0; for (ll i = 0; i < s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } void operator>>(istream &iss, bint &v) { string S; iss >> S; v = 0; rep(i, sz(S)) { v *= 10; v += S[i] - '0'; } } bint max(bint a, signed b) { return max(a, (bint)b); } bint max(signed a, bint b) { return max((bint)a, b); } bint max(bint a, ll b) { return max(a, (bint)b); } bint max(ll a, bint b) { return max((bint)a, b); } bint min(bint a, signed b) { return min(a, (bint)b); } bint min(signed a, bint b) { return min((bint)a, b); } bint min(bint a, ll b) { return min(a, (bint)b); } bint min(ll a, bint b) { return min((bint)a, b); } // エラー void ole() { #ifdef _DEBUG debugline("ole"); exit(0); #endif string a = "a"; rep(i, 30) a += a; rep(i, 1 << 17) cout << a << endl; cout << "OLE 出力長制限超過" << endl; exit(0); } void re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } // 便利関数 // テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } ll rand(ll max) { return rand(0, max); } template <class T> T rand(vector<T> &A) { return A[rand(sz(A) - 1)]; } // 重複することがある template <class T> vector<T> ranv(vector<T> &A, int N) { vector<T> ret(N); rep(i, N) { ret[i] = rand(A); } return ret; } template <class T> vector<T> ranv_unique(vector<T> &A, int N) { vector<T> ret(N); umapi was; rep(j, N) { int i; while (1) { i = rand(sz(A) - 1); if (was.find(i) == was.end()) break; } ret[j] = A[i]; was[i] = 1; } return ret; } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } // 単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } // 便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class T> vector<pair<T, int>> run_length(vector<T> &a) { vector<pair<T, int>> ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } vector<pair<char, ll>> run_length(string &a) { vector<pair<char, ll>> ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } template <class T, class F> T mgr(T ok, T ng, F f, int deb_ = 0) { bool han = true; if (deb_) { if (ok < ng) while (ng - ok > 1) { T mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else while (ok - ng > 1) { T mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } } else { if (ok < ng) while (ng - ok > 1) { T mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; } else while (ok - ng > 1) { T mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; } } return ok; } template <class T, class F> T mgr(signed ok, T ng, F f) { return mgr((T)ok, ng, f); } template <class T, class F> T mgr(T ok, signed ng, F f) { return mgr(ok, (T)ng, f); } //[l, r)の中で,f(i)がtrueとなる範囲を返す okはそこに含まれる template <class F> P mgr(int l, int r, F f, int ok) { if (f(ok) == 0) { out("f(ok) must true"); re(); } return mp(mgr(ok, l - 1, f), mgr(ok, r, f) + 1); } template <class F> dou mgrd(dou ok, dou ng, F f, int kai = 100) { bool han = true; if (ok < ng) rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } // エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } // getid(find())を返す 1次元にする template <typename W, typename T> int findi(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return i * sz(a[0]) + j; return -1; } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } vi count(vi &a) { int ma = 0; fora(v, a) { if (ma < v) ma = v; } vi res(ma + 1); fora(v, a) { res[v]++; } return res; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } //'a' = 'A' = 0 として集計 既に-'a'されていても動く vi count(str &a, int l, int r) { vi cou(26); char c = 'a'; if ('A' <= a[l] && a[l] <= 'Z') c = 'A'; if ('a' <= a[l] && a[l] <= 'z') c = 'a'; else c = 0; rep(i, l, r)++ cou[a[i] - c]; return cou; } vi count(str &a, int r = inf) { return count(a, 0, min(r, sz(a))); } #define couif count_if // algorythm ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template <class T> void rev(vector<T> &a) { reverse(all(a)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll getr(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 上から何桁目か ll getl(ll a, ll ket) { int sketa = keta(a); return getr(a, sketa - 1 - ket); } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } // 変換系 //[v] := iとなるようなvectorを返す // 存在しない物は-1 template <class T> auto keys(T a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> constexpr T min(T a, signed b) { return a < b ? a : b; } template <class T> constexpr T max(T a, signed b) { return a < b ? b : a; } template <class T> constexpr T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> constexpr T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<T>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> T mul(vector<T> &v, ll t = inf) { T ret = v[0]; rep(i, 1, min(t, sz(v))) ret *= v[i]; return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret // += sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // imoは0-indexed // ruiは1-indexed template <class T> vector<T> imo(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } // kと同じものの数 template <class T, class U> vi imo(vector<T> &a, U k) { vector<T> ret = a; rep(i, sz(ret)) ret[i] = a[i] == k; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } template <class T> vector<T> imox(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i]; return ret; } // 漸化的に最小を持つ template <class T> vector<T> imi(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chmi(ret[i - 1], ret[i]); return ret; } template <class T> vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chma(ret[i - 1], ret[i]); return ret; } template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN(T), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int ma = MIN(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -linf; ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX(T), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int mi = MAX(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = linf; ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ostream &operator<<(ostream &os, ruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } template <class T> vector<T> ruiv(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i]; return ret; } template <class T> ruiC<T> ruic() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return ret; } vector<ll> ruiv(string &a) { if (sz(a) == 0) return vi(1); ll dec = ('0' <= a[0] && a[0] <= '9') ? '0' : 0; vector<ll> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i] - dec; return ret; } ruiC<ll> ruic(string &a) { vector<ll> ret = ruiv(a); return ruiC<ll>(ret); } // kと同じものの数 template <class T, class U> vi ruiv(T &a, U k) { vi ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k); return ret; } template <class T, class U> ruiC<ll> ruic(T &a, U k) { vi ret = ruiv(a, k); return ruiC<ll>(ret); } template <class T> struct ruiC2 { int H; vector<ruiC<T>> rui; ruiC2(vector<vector<T>> &ru) : rui(sz(ru)), H(sz(ru)) { for (int h = 0; h < H; h++) { rui[h] = ruic(ru[h]); } } // WについてHを返す vector<T> operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } vector<T> res(H); for (int h = 0; h < H; h++) res[h] = rui[h](l, r); return res; } // HについてWを返す ruiC<T> operator[](ll h) { assert(h < H); return rui[h]; } // vector<T> operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } /*0から順に追加される必要がある*/ // T back() { return rui.back(); } // ll size() { return rui.size(); } // auto begin(){return rui.begin();} // auto end(){return rui.end();} }; // a~zを0~25として // rui(l,r)でvector(26文字について, l~rのcの個数) // rui[h] ruic()を返す ruiC2<ll> ruicou(str &a) { str s = a; replace(s); vector<ruiC<ll>> res(26); vvi(cou, 26, sz(s)); rep(i, sz(s)) { cou[s[i]][i] = 1; } return ruiC2<ll>(cou); } ruiC2<ll> ruicou(vi &a) { int H = max(a) + 1; vector<ruiC<ll>> res(H); vvi(cou, H, sz(a)); rep(i, sz(a)) { cou[a[i]][i] = 1; } return ruiC2<ll>(cou); } /*@formatter:off*/ // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { vector<T> rui; ruixC(vector<T> &ru) : rui(ru) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruiXc "; deb(l, r); assert(0); } return rui[r] ^ rui[l]; } T operator[](ll i) { return rui[i]; } T back() { return rui.back(); } ll size() { return rui.size(); } }; template <class T> vector<T> ruix(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i]; return ret; } template <class T> ruixC<ll> ruixc(vector<T> &a) { vi ret = ruix(a); return ruixC<ll>(ret); } template <class T> vector<T> ruim(vector<T> &a) { vector<T> res(a.size() + 1, 1); rep(i, a.size()) res[i + 1] = res[i] * a[i]; return res; } // 漸化的に最小を1indexで持つ template <class T> vector<T> ruimi(vector<T> &a) { ll n = sz(a); vector<T> ret(n + 1); rep(i, 1, n) { ret[i] = a[i - 1]; chmi(ret[i + 1], ret[i]); } return ret; } // template<class T> T *rrui(vector<T> &a) { // 右から左にかけての半開区間 (-1 n-1] template <class T> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } // 掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } /*@formatter:off*/ template <class T> T *rruim(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; res[len - 1] = 1; rer(i, len - 1) res[i - 1] = res[i] * a[i]; return res; } template <class T, class U> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U = int> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U = int> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool san(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return san(0, i, sz(a)) && san(0, j, sz(a)); } #define inside ins ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } // 添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } template <class T> T min(vector<vector<vector<T>>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, min(a[i])); return res; } template <class T> T max(vector<vector<vector<T>>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, max(a[i])); return res; } template <class T> T min(vector<vector<vector<vector<T>>>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, min(a[i])); return res; } template <class T> T max(vector<vector<vector<vector<T>>>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, max(a[i])); return res; } template <class T> T min(vector<vector<vector<vector<vector<T>>>>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, min(a[i])); return res; } template <class T> T max(vector<vector<vector<vector<vector<T>>>>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, max(a[i])); return res; } template <class T> T min(vector<vector<vector<vector<vector<vector<T>>>>>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, min(a[i])); return res; } template <class T> T max(vector<vector<vector<vector<vector<vector<T>>>>>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, max(a[i])); return res; } // pow周りの仕様 // powiを使うと整数型 // powbを使うとbint型 // powを使うと powlに変換され long doubleが返る #ifdef _DEBUG auto pow(ll a, ll k) { static bool was = 1; if (was) { message += "if integer use *powi* it's very fast\n"; } was = 0; return powl(a, k); } // 上のメッセージを出すための関数 auto pow(signed a, ll k) { return pow((ll)a, k); } auto pow(signed a, signed k) { return pow((ll)a, (ll)k); } #else #define pow powl #endif // 整数型のpow int powi(int a, int k) { if (a == 2) return 1ll << k; int res = 1; int x = a; while (k) { if (k & 1) res *= x; x *= x; k >>= 1; } return res; } // define pow powlより上に動かすとバグる bint pow(bint a, ll k) { bint res = 1; bint x = a; while (k) { if (k & 1) res *= x; x *= x; k >>= 1; } return res; } bint pow(bint a, signed k) { return pow(a, (ll)k); } bint powb(int a, int b) { return pow((bint)a, b); } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); // オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); // オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll bit(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 // bitsetに置き換える感覚 i が立っていたら i bit目を立てる ll bit_bool(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll // 最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } // 最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } // 初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) // aにある物をtrueとする vb bool_(vi &a, int n) { vb ret(max(max(a) + 1, n)); rep(i, sz(a)) ret[a[i]] = true; return ret; } char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } // 基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } // 要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) // 狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } // 存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class... T> void fin(T... s) { out(s...); exit(0); } // 便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll sig0(int t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } bint sig0(bint t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } // 第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // v * v >= aとなる最小のvを返す ll sqrt(ll a) { if (a < 0) { debugline("sqrt"); deb(a); ole(); } ll res = (ll)std::sqrt(a); while (res * res < a) ++res; return res; } double log(double e, double x) { return log(x) / log(e); } // b * res <= aを満たす [l, r)を返す P drange_ika(int a, int b) { assert(b); if (a >= 0) { if (b > 0) return mp(-linf, a / b + 1); else return mp(-(a / -b), linf + 1); } else { if (b > 0) return mp(-linf, -ceil(-a, b) + 1); else return mp(ceil(-a, -b), linf + 1); } } /*@formatter:off*/ // 機能拡張 #define dtie(a, b) \ int a, b; \ tie(a, b) template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> void operator+=(mset<T> &a, vector<T> &v) { for (auto &&u : v) a.insert(u); } template <class T> void operator+=(set<T> &a, vector<T> &v) { for (auto &&u : v) a.insert(u); } template <class T> void operator+=(vector<T> &a, set<T> &v) { for (auto &&u : v) a.emplace_back(u); } template <class T> void operator+=(vector<T> &a, mset<T> &v) { for (auto &&u : v) a.emplace_back(u); } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <typename T> void entry(vector<T> &v, unsigned ll s, unsigned ll e) { erase(v, e, sz(v)); erase(v, 0, s); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } template <class T> T poll(set<T> &S) { T ret = *S.begin(); S.erase(S.begin()); return ret; } template <class T> T poll(mset<T> &S) { T ret = *S.begin(); S.erase(S.begin()); return ret; } template <class T> T poll_back(set<T> &S) { T ret = *S.rbegin(); S.erase(S.rbegin()); return ret; } template <class T> T poll_back(mset<T> &S) { T ret = *S.rbegin(); S.erase(S.rbegin()); return ret; } template <class T> T peek(set<T> &S) { T ret = *S.begin(); return ret; } template <class T> T peek(mset<T> &S) { T ret = *S.begin(); return ret; } template <class T> T peek_back(set<T> &S) { T ret = *S.rbegin(); return ret; } template <class T> T peek_back(mset<T> &S) { T ret = *S.rbegin(); return ret; } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } ll stoi(string &s) { return stol(s); } void yn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; } void Yn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; } void YN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; } void fyn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; exit(0); } void fYn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; exit(0); } void fYN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; exit(0); } void fAb(bool a) { if (a) cout << "Alice" << endl; else cout << "Bob"; } void Possible(bool a) { if (a) cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { if (a) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } void fPossible(bool a) { Possible(a); exit(0); } void fPOSSIBLE(bool a) { POSSIBLE(a); exit(0); } template <typename T> class fixed_point : T { public: explicit constexpr fixed_point(T &&t) noexcept : T(std::forward<T>(t)) {} template <typename... Args> constexpr decltype(auto) operator()(Args &&...args) const { return T::operator()(*this, std::forward<Args>(args)...); } }; template <typename T> static inline constexpr decltype(auto) fix(T &&t) noexcept { return fixed_point<T>{std::forward<T>(t)}; } // 未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) { int N = sz(a); vector<vector<T>> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } // 長さn毎に分割する template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) { int N = sz(a); vector<vector<T>> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } vi inds_(vi &a) { int n = sz(a); vb was(n); vi res(n); rep(i, n) { assert(!was[a[i]]); res[a[i]] = i; was[a[i]] = true; } return res; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; #define pre prev #define nex next // gra mll pr // 上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] // 右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) // グラフ内で #undef getid // #define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) // #define set_shuffle() std::random_device seed_gen;std::mt19937 // engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), // engine); 1980 開始からtime ms経っていたらtrue bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う // 全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } /*@formatter:on*/ // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; /*@formatter:off*/ #undef getid #define type_id_graph #ifdef type_id_graph #define forg(gi, ve) \ for (ll gi = 0, forglim = ve.size(), f, t, c, ty, id; \ gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, \ ty = ve[gi].ty, id = ve[gi].id, true); \ ++gi) #define fort(gi, ve) \ for (ll gi = 0, f, t, c, ty, id, wasp = (p != -1 && ve[gi].t == p) ? 1 : 0; \ (wasp = wasp ? 1 \ : (p != -1 && ve[gi].t == p) ? 1 \ : 0, \ gi + wasp < ve.size()) && \ (tie(f, t, c, ty, id) = \ wasp ? make_tuple(ve[gi + 1].f, ve[gi + 1].t, ve[gi + 1].c, \ ve[gi + 1].ty, ve[gi + 1].id) \ : make_tuple(ve[gi].f, ve[gi].t, ve[gi].c, ve[gi].ty, \ ve[gi].id), \ true); \ ++gi) #define fore(gi, ve) \ for (ll gi = 0, forglim = ve.size(), f, t, c, ty, id; \ gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, \ ty = ve[gi].ty, id = ve[gi].id, true); \ ++gi) template <class T> struct edge_ { int f, t, ty, id; T c; edge_(int f, int t, T c = 1, int ty = -1, int id = -1) : f(f), t(t), c(c), ty(ty), id(id) {} bool operator<(const edge_ &b) const { return c < b.c; } bool operator>(const edge_ &b) const { return c > b.c; } }; template <class T> ostream &operator<<(ostream &os, edge_<T> &e) { os << e.f << " " << e.t << " " << e.c << " " << e.ty << " " << e.id; return os; } template <typename T> class graph { public: vector<vector<edge_<T>>> g; vector<edge_<T>> edges; int n; explicit graph(int n) : n(n) { g.resize(n); } void clear() { g.clear(), edges.clear(); } void resize(int n) { this->n = n; g.resize(n); } int size() { return n; } vector<edge_<T>> &operator[](int i) { return g[i]; } virtual void add(int f, int t, T c, int ty, int id) = 0; virtual void set_edges() = 0; }; template <typename T = ll> class digraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; explicit digraph(int n) : graph<T>(n) {} void add(int f, int t, T c = 1, int ty = -1, int eid = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digraph add"); deb(f, t, c); ole(); } int id = sz(edges); if (eid != -1) id = eid; g[f].emplace_back(f, t, c, ty, id); edges.emplace_back(f, t, c, ty, id); // edgesを使わないなら消せる } void ing(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); } } void set_edges() override { if (sz(edges)) return; rep(i, n) fora(e, g[i]) edges.push_back(e); } }; template <class T = int> class undigraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; explicit undigraph(int n) : graph<T>(n) {} // f < t void add(int f, int t, T c = 1, int ty = -1, int eid = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("undigraph add"); deb(f, t, c); ole(); } int id = sz(edges); if (eid != -1) id = eid; g[f].emplace_back(f, t, c, ty, id); g[t].emplace_back(t, f, c, ty, id); edges.emplace_back(f, t, c, ty, id); // edgesを使わないなら消せる edges.emplace_back(t, f, c, ty, id); } void add(edge_<T> &e) { int f = e.f, t = e.t, ty = e.ty, id = e.id; T c = e.c; add(f, t, c, ty, id); } void ing(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); } } void set_edges() override { if (sz(edges)) return; rep(i, n) fora(e, g[i]) edges.push_back(e); } }; #else #define forg(gi, ve) \ for (ll gi = 0, forglim = ve.size(), f, t, c; \ gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi) #define fort(gi, ve) \ for (ll gi = 0, f, t, c, wasp = (p != -1 && ve[gi].t == p) ? 1 : 0; \ (wasp = wasp ? 1 \ : (p != -1 && ve[gi].t == p) ? 1 \ : 0, \ gi + wasp < ve.size()) && \ (tie(f, t, c) = \ wasp ? make_tuple(ve[gi + 1].f, ve[gi + 1].t, ve[gi + 1].c) \ : make_tuple(ve[gi].f, ve[gi].t, ve[gi].c), \ true); \ ++gi) #define fore(gi, ve) \ for (ll gi = 0, forglim = ve.size(), f, t, c; \ gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi) template <class T> struct edge_ { int f, t; T c; edge_(int f, int t, T c = 1) : f(f), t(t), c(c) {} bool operator<(const edge_ &b) const { return c < b.c; } bool operator>(const edge_ &b) const { return c > b.c; } }; template <class T> ostream &operator<<(ostream &os, edge_<T> &e) { os << e.f << " " << e.t << " " << e.c; return os; } template <typename T> class graph { public: vector<vector<edge_<T>>> g; vector<edge_<T>> edges; int n; explicit graph(int n) : n(n) { g.resize(n); } void clear() { g.clear(), edges.clear(); } void resize(int n) { this->n = n; g.resize(n); } int size() { return n; } vector<edge_<T>> &operator[](int i) { return g[i]; } virtual void add(int f, int t, T c) = 0; virtual void set_edges() = 0; }; template <typename T = ll> class digraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; explicit digraph(int n) : graph<T>(n) {} void add(int f, int t, T c = 1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digraph add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); edges.emplace_back(f, t, c); // edgesを使わないなら消せる } void ing(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); } } void set_edges() override { if (sz(edges)) return; rep(i, n) fora(e, g[i]) edges.push_back(e); } }; template <class T = int> class undigraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; explicit undigraph(int n) : graph<T>(n) {} // f < t void add(int f, int t, T c = 1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("undigraph add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); g[t].emplace_back(t, f, c); edges.emplace_back(f, t, c); // edgesを使わないなら消せる edges.emplace_back(t, f, c); } void add(edge_<T> &e) { int f = e.f, t = e.t; T c = e.c; add(f, t, c); } void ing(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); } } void set_edges() override { if (sz(edges)) return; rep(i, n) fora(e, g[i]) edges.push_back(e); } }; #endif #define dijkstra_path dis_path template <class T> vi dis_path(digraph<T> &g, vector<T> &dis, int s, int t, int init_value) { assert(dis[t] != init_value); auto rg = rev(g); int now = t; vi path; path.push_back(now); T cost = 0; while (now != s) { rep(gi, sz(rg[now])) { int m = rg[now][gi].t; if (dis[m] == init_value) continue; if (dis[m] + rg[now][gi].c + cost == dis[t]) { cost += rg[now][gi].c; now = m; break; } } path.push_back(now); } rev(path); return path; } template <class T> vi dis_path(undigraph<T> &g, vector<T> &dis, int s, int t, int init_value) { assert(dis[t] != init_value); int now = t; vi path; path.push_back(now); T cost = 0; while (now != s) { rep(gi, sz(g[now])) { int m = g[now][gi].t; if (dis[m] == init_value) continue; if (dis[m] + g[now][gi].c + cost == dis[t]) { cost += g[now][gi].c; now = m; break; } } path.push_back(now); } rev(path); return path; } template <class T> vi dis_path(digraph<T> &g, vector<vector<T>> &dis, int s, int t, int init_value) { return dis_path(g, dis[s], s, t, init_value); } template <class T> vi dis_path(undigraph<T> &g, vector<vector<T>> &dis, int s, int t, int init_value) { return dis_path(g, dis[s], s, t, init_value); } // O(N^2) template <class T> vector<T> dijkstra_mitu(graph<T> &g, int s, int init_value) { if (!(0 <= s && s < g.n)) { debugline("dijkstra_mitu"); deb(s, g.n); ole(); } int n = g.n; int initValue = MAX(int); vector<T> dis(n, initValue); dis[s] = 0; vb used(n); while (true) { int v = -1; rep(u, n) { if (!used[u] && (v == -1 || dis[u] < dis[v])) v = u; } if (v == -1) break; if (dis[v] == initValue) break; used[v] = true; rep(u, sz(g[v])) { auto e = g[v][u]; dis[e.t] = min(dis[e.t], dis[v] + e.c); } } /*基本、たどり着かないなら-1*/ for (auto &&d : dis) { if (d == initValue) { d = init_value; } } return dis; } template <typename T> struct radixheap { vector<pair<u64, T>> v[65]; u64 size, last; radixheap() : size(0), last(0) {} bool empty() const { return size == 0; } int getbit(int a) { return a ? 64 - __builtin_clzll(a) : 0; } void push(u64 key, const T &value) { ++size; v[getbit(key ^ last)].emplace_back(key, value); } pair<u64, T> pop() { if (v[0].empty()) { int idx = 1; while (v[idx].empty()) ++idx; last = min_element(begin(v[idx]), end(v[idx]))->first; for (auto &p : v[idx]) v[getbit(p.first ^ last)].emplace_back(p); v[idx].clear(); } --size; auto ret = v[0].back(); v[0].pop_back(); return ret; } }; /*radix_heap こっちの方が早い*/ // O((N+M) log N) vi dijkstra(graph<int> &g, int s, int init_value) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } /*O((N+M) log N) vs O(N^2)*/ if ((g.n + sz(g.edges)) * log2(N) > g.n * g.n) { return dijkstra_mitu(g, s, init_value); } int initValue = MAX(int); vi dis(g.n, initValue); radixheap<int> q; dis[s] = 0; q.push(0, s); while (!q.empty()) { int nowc, i; tie(nowc, i) = q.pop(); if (dis[i] != nowc) continue; for (auto &&e : g.g[i]) { int to = e.t; int c = nowc + e.c; if (dis[to] > c) { dis[to] = c; q.push(dis[to], to); } } } /*基本、たどり着かないなら-1*/ for (auto &&d : dis) if (d == initValue) d = init_value; return dis; } template <class T> vector<T> dijkstra_normal(graph<T> &g, int s, int init_value) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } if ((g.n + sz(g.edges)) * 20 > g.n * g.n) { return dijkstra_mitu(g, s, init_value); } T initValue = MAX(T); vector<T> dis(g.n, initValue); priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q; dis[s] = 0; q.emplace(0, s); while (q.size()) { T nowc = q.top().fi; int i = q.top().se; q.pop(); if (dis[i] != nowc) continue; for (auto &&e : g.g[i]) { int to = e.t; T c = nowc + e.c; if (dis[to] > c) { dis[to] = c; q.emplace(dis[to], to); } } } /*基本、たどり着かないなら-1*/ for (auto &&d : dis) if (d == initValue) d = init_value; return dis; } template <class T> vector<T> dijkstra_01(graph<T> &g, int s, int init_value) { int N = g.n; vi dis(N, linf); dis[s] = 0; deque<int> q; q.push_back(s); vb was(N); while (!q.empty()) { int f = q.front(); q.pop_front(); if (was[f]) continue; was[f] = true; fora(e, g[f]) { if (dis[e.t] > dis[f] + e.c) { if (e.c) { dis[e.t] = dis[f] + 1; q.push_back(e.t); } else { dis[e.t] = dis[f]; q.push_front(e.t); } } } } rep(i, N) if (dis[i] == linf) dis[i] = init_value; return dis; } // dijkstra_cou<mint> : 数える型で書く return vp(dis,cou) template <class COU, class T = int> auto dijkstra_cou(const graph<T> &g, int s, int init_value) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } err("count by type COU "); err("int or mint"); T initValue = MAX(T); vector<T> dis(g.n, initValue); vector<COU> cou(g.n); cou[s] = 1; priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q; dis[s] = 0; q.emplace(0, s); while (q.size()) { T nowc = q.top().fi; int i = q.top().se; q.pop(); if (dis[i] != nowc) continue; for (auto &&e : g.g[i]) { int to = e.t; T c = nowc + e.c; if (dis[to] > c) { dis[to] = c; cou[to] = cou[e.f]; q.emplace(dis[to], to); } else if (dis[to] == c) { cou[to] += cou[e.f]; } } } /*基本、たどり着かないなら-1*/ for (auto &&d : dis) if (d == initValue) d = init_value; return vtop(dis, cou); } // 密グラフの時、warshallに投げる template <class T> vector<vector<T>> dijkstra_all(const graph<T> &g, int init_value) { int n = g.n; assert(n < 1e4); if (n * n < (n + sz(g.edges)) * 14) { /*O(N^3) vs O(N (N+M)log N)*/ return warshall(g, init_value); } vector<vector<T>> dis(n); rep(i, n) { dis[i] = dijkstra(g, i, init_value); } return dis; } // コストを無限に減らせる := -linf // たどり着けない := linf template <class T> vector<T> bell(graph<T> &g, int s) { if (g.n >= 1e4) { cout << "bell size too big" << endl; exit(0); } vector<T> res(g.n, linf); res[s] = 0; vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく */ fix([&](auto ds, int p, int i) -> void { if (can[i]) return; can[i] = true; forg(gi, g[i]) if (t != p) ds(i, t); })(-1, 0); vector<edge_<T>> es; fora(e, g.edges) { if (can[e.f]) es += e; } rep(i, g.n) { bool upd = false; fora(e, es) { if (res[e.f] != linf && res[e.t] > res[e.f] + e.c) { upd = true; res[e.t] = res[e.f] + e.c; } } if (!upd) break; } rep(i, g.n * 2) { bool upd = 0; fora(e, g.edges) { if (res[e.f] != linf && res[e.t] != -linf && res[e.t] > res[e.f] + e.c) { upd = 1; res[e.t] = -linf; } } if (!upd) break; } return res; } // コストを無限に増やせる := linf // たどり着けない := -linf template <class T> vector<T> bell_far(graph<T> &g, int s) { if (g.n >= 1e4) { cout << "bell_far size too big" << endl; exit(0); } vector<T> res(g.n, linf); res[s] = 0; vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく*/ fix([&](auto ds, int p, int i) -> void { if (can[i]) return; can[i] = true; forg(gi, g[i]) if (t != p) ds(i, t); })(-1, 0); vector<edge_<T>> es; fora(e, g.edges) { if (can[e.f]) es += e; } rep(i, g.n) { bool upd = false; fora(e, es) { if (res[e.f] != linf && res[e.t] > res[e.f] - e.c) { /*-c*/ upd = true; res[e.t] = res[e.f] - e.c; /*-c*/ } } if (!upd) break; } rep(i, g.n * 2) { bool upd = 0; fora(e, g.edges) { if (res[e.f] != linf && res[e.t] != -linf && res[e.t] > res[e.f] - e.c) { /*-c*/ upd = 1; res[e.t] = -linf; } } if (!upd) break; } rep(i, g.n) res[i] *= -1; return res; } template <class T> vector<vector<T>> warshall(graph<T> &g, int init_value) { int n = g.n; assert(n < 1e4); vector<vector<T>> dis(n, vector<T>(n, linf)); rep(i, n) fora(e, g.g[i]) { if (dis[e.f][e.t] > e.c) { dis[e.f][e.t] = e.c; } } rep(i, n) dis[i][i] = 0; rep(k, n) rep(i, n) rep(j, n) { if (dis[i][j] > dis[i][k] + dis[k][j]) { dis[i][j] = dis[i][k] + dis[k][j]; } } rep(i, n) rep(j, n) if (dis[i][j] == linf) dis[i][j] = init_value; return dis; } template <class T> class MinOp { public: T operator()(T a, T b) { return min(a, b); } }; template <typename OpFunc> struct SparseTable { OpFunc op; signed size; vector<signed> lg; vector<vector<pair<signed, signed>>> table; void init(vector<pair<signed, signed>> &array, OpFunc opfunc) { signed n = array.size(); op = opfunc; lg.assign(n + 1, 0); for (signed i = 1; i <= n; i++) { lg[i] = 31 - __builtin_clz(i); } table.assign(lg[n] + 1, array); for (signed i = 1; i <= lg[n]; i++) { for (signed j = 0; j < n; j++) { if (j + (1 << (i - 1)) < n) { table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]); } else { table[i][j] = table[i - 1][j]; } } } } pair<signed, signed> query(signed l, signed r) { assert(l < r); return op(table[lg[r - l]][l], table[lg[r - l]][r - (1 << lg[r - l])]); } }; struct PMORMQ { vector<signed> a; SparseTable<MinOp<pair<signed, signed>>> sparse_table; vector<vector<vector<signed>>> lookup_table; vector<signed> block_type; signed block_size, n_block; void init(vector<signed> &array) { a = array; signed n = a.size(); block_size = std::max(1, (31 - __builtin_clz(n)) / 2); while (n % block_size != 0) { a.push_back(a.back() + 1); n++; } n_block = n / block_size; vector<pair<signed, signed>> b(n_block, make_pair(INT_MAX, INT_MAX)); for (signed i = 0; i < n; i++) { b[i / block_size] = min(b[i / block_size], make_pair(a[i], i)); } sparse_table.init(b, MinOp<pair<signed, signed>>()); block_type.assign(n_block, 0); for (signed i = 0; i < n_block; i++) { signed cur = 0; for (signed j = 0; j < block_size - 1; j++) { signed ind = i * block_size + j; if (a[ind] < a[ind + 1]) { cur |= 1 << j; } } block_type[i] = cur; } lookup_table.assign( 1 << (block_size - 1), vector<vector<signed>>(block_size, vector<signed>(block_size + 1))); for (signed i = 0; i < (1 << (block_size - 1)); i++) { for (signed j = 0; j < block_size; j++) { signed res = 0; signed cur = 0; signed pos = j; for (signed k = j + 1; k <= block_size; k++) { lookup_table[i][j][k] = pos; if (i & (1 << (k - 1))) { cur++; } else { cur--; } if (res > cur) { pos = k; res = cur; } } } } } signed query(signed l, signed r) { assert(l < r); signed lb = l / block_size; signed rb = r / block_size; if (lb == rb) { return lb * block_size + lookup_table[block_type[lb]][l % block_size][r % block_size]; } signed pl = lb * block_size + lookup_table[block_type[lb]][l % block_size][block_size]; signed pr = rb * block_size + lookup_table[block_type[rb]][0][r % block_size]; signed pos = pl; if (r % block_size > 0 && a[pl] > a[pr]) { pos = pr; } if (lb + 1 == rb) { return pos; } signed spv = sparse_table.query(lb + 1, rb).second; if (a[pos] > a[spv]) { return spv; } return pos; } }; template <class T = int> class tree : public undigraph<T> { PMORMQ rmq; int cnt; vector<signed> id_, in; bool never = true; bool never_hld = true; void dfs(int x, int p, int d, int dis = 0) { id_[cnt] = x; par_[x] = p; rmq_dep.push_back(d); disv[x] = dis; in[x] = cnt++; forg(gi, g[x]) { if (t == p) { continue; } dfs(t, x, d + 1, dis + c); id_[cnt] = x; rmq_dep.push_back(d); cnt++; } } void precalc() { never = false; cnt = 0; rmq_dep.clear(); disv.assign(n, 0); in.assign(n, -1); id_.assign(2 * n - 1, -1); par_.assign(n, -1); dfs(root, -1, 0); rmq.init(rmq_dep); #ifdef _DEBUG if (n >= 100) return; cerr << "---tree---" << endl; rep(i, n) { if (!(i == root || sz(g[i]) > 1)) continue; cerr << i << " -> "; vi ts; forg(gi, g[i]) { if (t != par_[i]) ts.push_back(t); } rep(i, sz(ts) - 1) cerr << ts[i] << ", "; if (sz(ts)) cerr << ts.back() << endl; } cerr << endl; #endif } int pos; void hld_build() { never_hld = false; if (never) precalc(); g.resize(n); vid.resize(n, -1); head.resize(n); heavy.resize(n, -1); depth.resize(n); inv.resize(n); subl.resize(n); subr.resize(n); dfs(root, -1); t = 0; dfs_hld(root); #ifdef _DEBUG if (n >= 100) return; cerr << "---hld_index---" << endl; vi inds; rep(i, n) if (sz(g[i])) inds.push_back(i); rep(i, sz(inds)) { str s = tos(bel(inds[i])); cerr << std::right << std::setw(sz(s) + (i ? 1 : 0)) << inds[i]; } cerr << endl; rep(i, sz(inds)) { cerr << bel(inds[i]) << " "; } cerr << endl << endl; cerr << "---hld_edge_index---" << endl; fora(e, edges) { if (e.f <= e.t) cerr << e.f << "-" << e.t << " " << bel(e) << endl; } cerr << endl << endl; cerr << "!!query!! edge or not edge carefull!!" << endl; #endif } int dfs(int curr, int prev) { int sub = 1, max_sub = 0; heavy[curr] = -1; forg(i, g[curr]) { int next = t; if (next != prev) { depth[next] = depth[curr] + 1; int sub_next = dfs(next, curr); sub += sub_next; if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next; } } return sub; } int t = 0; #ifndef __CLION_IDE__ void dfs_hld(int v = 0) { vid[v] = subl[v] = t; t++; inv[subl[v]] = v; if (0 <= heavy[v]) { head[heavy[v]] = head[v]; dfs_hld(heavy[v]); } forg(i, g[v]) if (depth[v] < depth[t]) if (t != heavy[v]) { head[t] = t; dfs_hld(t); } subr[v] = t; } #endif //__CLION_IDE__ vector<signed> rmq_dep; vi par_, depth, disv; vi childs; vi par_id_; // 親への辺のidを持つ vvi(ids_); // 隣接で辺のidを持つ public: using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; int root; //(f,t)と(t,f)は同じidを持ち、[0,n-1]でadd順に割り振られる // 部分木の [左端、右端) index // 部分木の辺に加算する場合 // add(subl[i],subr[i],x) // add(sub[i],sub[i+1],-x) vector<int> vid, head, heavy, inv, subl, subr; tree(int n_, int root = 0) : undigraph<T>(n_), root(root) { n = n_; } void root_change(int roo) { root = roo; precalc(); } // 葉になりえない頂点を根にする void root_change_mid() { #ifdef _DEBUG message += "N>2 (mid)\n"; #endif assert(n > 2); rep(i, n) { if (sz(g[i]) > 1) { root_change(i); return; } } } int lca(int a, int b) { if (never) precalc(); int x = in[a]; int y = in[b]; if (x > y) { swap(x, y); } int pos = rmq.query(x, y + 1); return id_[pos]; } int dis(int a, int b) { if (never) precalc(); int x = in[a]; int y = in[b]; if (x > y) { swap(x, y); } int pos = rmq.query(x, y + 1); int p = id_[pos]; return disv[a] + disv[b] - disv[p] * 2; } int dep(int a) { if (never) precalc(); return disv[a]; } int par(int a) { if (never) precalc(); return par_[a]; } bool isleaf(int i) { if (never) precalc(); return (sz(g[i]) == 1 && i != root); } bool leaf(int i) { return isleaf(i); } vi child(int r) { vi res; res.push_back(r); queue<int> q; q.push(r); while (!q.empty()) { int i = q.front(); res.push_back(i); q.pop(); forg(gi, g[i]) { if (t != par(i)) q.push(t); } } return res; } vb child_ex(int r) { vb res(n); res[r] = true; queue<int> q; q.push(r); while (!q.empty()) { int i = q.front(); res[i] = true; q.pop(); forg(gi, g[i]) { if (t != par(i)) q.push(t); } } return res; } int dfs_count_subtree(int p, int i) { childs[i] = 1; fort(gi, g[i]) { childs[i] += dfs_count_subtree(i, t); } return childs[i]; } #define count_child count_subtree int count_subtree(int f) { if (sz(childs) == 0) { if (never) precalc(); childs.resize(n); dfs_count_subtree(-1, root); } return childs[f]; } // fからtの辺を切った時のtの大きさ int count_subtree(int f, int t) { if (par(f) == t) { return n - count_subtree(f); } else { return count_subtree(t); } } vi path(int a, int b) { vi res; for_each_l(a, b, [&](int i) { res.push_back(i); }); return res; } // idはedgesに使えない ↓edge(id)とする // pathに含まれる辺のid達を返す vi pathe(int a, int b) { #ifdef _DEBUG static bool was = 0; if (!was) message += "can't edges[id]. must edge(id)\n"; was = 1; #endif if (sz(par_id_) == 0) { ids_.resize(n); par_id_.resize(n); rep(i, 0, sz(edges), 2) { ids_[edges[i].f].emplace_back(i >> 1); ids_[edges[i].t].emplace_back(i >> 1); } if (never) precalc(); /*par_を呼ぶため*/ rep(i, n) { int pa = par_[i]; forg(gi, g[i]) { if (t == pa) { par_id_[i] = ids_[i][gi]; } } } } int u = lca(a, b); vi res; if (a != u) { do { res.emplace_back(par_id_[a]); a = par_[a]; } while (a != u); } vi rev; if (b != u) { do { rev.emplace_back(par_id_[b]); b = par_[b]; } while (b != u); } rer(i, sz(rev) - 1) { res.emplace_back(rev[i]); } return res; } // 親の辺のidを返す int par_id(int i) { if (sz(par_id_) == 0) { pathe(0, 0); } return par_id_[i]; } // fのi番目の辺のidを返す int ids(int f, int i) { if (sz(ids_) == 0) { pathe(0, 0); } return ids_[f][i]; } // idから辺を返す edge_<T> edge(int id) { return edges[id << 1]; } /*O(N) hldを使わず木を普通にたどる liteの意*/ void for_each_l(int u, int v, function<void(int)> fnode) { int r = lca(u, v); while (u != r) { fnode(u); u = par_[u]; } fnode(r); vi a; while (v != r) { a.push_back(v); v = par_[v]; } while (sz(a)) { fnode(a.back()); a.pop_back(); } } void for_each_edge_l(int u, int v, function<void(edge_<int> &)> fedge) { int r = lca(u, v); while (u != r) { forg(gi, g[u]) { if (t == par_[u]) { fedge(g[u][gi]); u = par_[u]; break; } } } vector<edge_<int>> qs; while (v != r) { forg(gi, g[v]) { if (t == par_[v]) { qs.push_back(g[v][gi]); v = par_[v]; break; } } } while (sz(qs)) { fedge(qs.back()); qs.pop_back(); } } // Fは半開 (u,v)は木の頂点 // 中ではhldの頂点を見るため、seg木のupdateはhldのindexで行なう void for_each_ /*[l,r)*/ (int u, int v, const function<void(int, int)> &f) { if (never_hld) hld_build(); while (1) { if (vid[u] > vid[v]) swap(u, v); int l = max(vid[head[v]], vid[u]); int r = vid[v] + 1; f(l, r); if (head[u] != head[v]) v = par_[head[v]]; else break; } } void for_each_edge /*[l,r) O(log(N)) 辺を頂点として扱っている 上と同じ感じで使える*/ (int u, int v, const function<void(int, int)> &f) { if (never_hld) hld_build(); while (1) { if (vid[u] > vid[v]) swap(u, v); if (head[u] != head[v]) { int l = vid[head[v]]; int r = vid[v] + 1; f(l, r); v = par_[head[v]]; } else { if (u != v) { int l = vid[u] + 1; int r = vid[v] + 1; f(l, r); } break; } } } int bel(int v) { /*hld内での頂点番号を返す*/ if (never_hld) hld_build(); return vid[v]; } // 下の頂点に辺のクエリを持たせている int bel( int f, int t) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/ if (never_hld) hld_build(); return depth[f] > depth[t] ? vid[f] : vid[t]; } int bel( edge_<T> & e) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/ if (never_hld) hld_build(); return depth[e.f] > depth[e.t] ? vid[e.f] : vid[e.t]; } template <class... U> int operator()(U... args) { return bel(args...); } // path l -> r += v template <class S> void seg_add(S &seg, int lhei, int rhei, int v) { for_each_(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); }); } template <class S> void seg_update(S &seg, int lhei, int rhei, int v) { for_each_(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); }); } template <class S> T seg_get(S &seg, int lhei, int rhei) { T res = seg.e; for_each_(lhei, rhei, [&](int l, int r) { res = seg.f(res, seg.get(l, r)); }); return res; } template <class S> void seg_add_edge(S &seg, int lhei, int rhei, int v) { for_each_edge(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); }); } template <class S> void seg_update_edge(S &seg, int lhei, int rhei, int v) { for_each_edge(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); }); } template <class S> T seg_get_edge(S &seg, int lhei, int rhei) { T res = seg.e; for_each_edge(lhei, rhei, [&](int l, int r) { res = seg.f(res, seg.get(l, r)); }); return res; } // 単体 edgeは上で処理できる template <class S> void seg_add(S &seg, int i, int v) { seg.add(bel(i), v); } template <class S> void seg_update(S &seg, int i, int v) { seg.update(bel(i), v); } template <class S> T seg_get(S &seg, int i) { return seg[i]; } template <class S> void seg_del(S &seg, int i) { seg.del(bel(i)); } // 部分木iに対するクエリ template <class S> void seg_add_sub(S &seg, int i, int v) { if (never_hld) hld_build(); seg.add(subl[i], subr[i], v); } template <class S> void seg_update_sub(S &seg, int i, int v) { if (never_hld) hld_build(); seg.update(subl[i], subr[i], v); } template <class S> T seg_get_sub(S &seg, int i, int v) { if (never_hld) hld_build(); return seg.get(subl[i], subr[i], v); } template <class S> void seg_add_sub_edge(S &seg, int i, int v) { if (never_hld) hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/ seg.add(subl[i] + 1, subr[i], v); } template <class S> void seg_update_sub_edge(S &seg, int i, int v) { if (never_hld) hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/ seg.update(subl[i] + 1, subr[i], v); } template <class S> T seg_get_sub_edge(S &seg, int i, int v) { if (never_hld) hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/ return seg.get(subl[i] + 1, subr[i], v); } }; // 辺が多いのでedgesを持たない // cost oo, ox, xo, xx 渡す template <class T = int> class grid_k6 : public undigraph<T> { vi costs; public: using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; int H, W; vector<vector<char>> ba; char wall; void add(int f, int t, T c = 1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("grid_k6 add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); g[t].emplace_back(t, f, c); // undigraphと違い、edgesを持たない } int getid(int h, int w) { assert(ins(h, w, H, W)); return W * h + w; } int getid(P p) { return getid(p.first, p.second); } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } int operator()(P p) { return getid(p); } // 辺は無い grid_k6(int H, int W) : H(H), W(W), undigraph<T>(H * W) {} grid_k6(vector<vector<char>> ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall) con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } /*o -> o, o -> x, x -> o, x-> x*/ grid_k6(vector<vector<char>> ba, int oo, int ox, int xo, int xx, char wall = '#') : H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])), costs({oo, ox, xo, xx}), ba(ba), wall(wall) { rep(h, H) { rep(w, W) { add(h, w, h + 1, w); add(h, w, h - 1, w); add(h, w, h, w + 1); add(h, w, h, w - 1); } } } void add(int fh, int fw, int th, int tw) { if (ins(fh, fw, H, W) && ins(th, tw, H, W)) { int cm = 0; if (ba[fh][fw] == wall) { cm += 2; } if (ba[th][tw] == wall) { cm++; } int f = getid(fh, fw); int t = getid(th, tw); g[f].emplace_back(f, t, costs[cm]); } } void set_edges() { rep(i, n) fora(e, g[i]) if (e.f < e.t) edges.push_back(e); } }; // 辺によりメモリを大量消費ためedgesを消している // 頂点10^6でメモリを190MB(制限の8割)使う // 左上から右下に移動できる template <class T = int> class digrid_k6 : public digraph<T> { public: using digraph<T>::g; using digraph<T>::n; using digraph<T>::edges; int H, W; void add(int f, int t, T c = 1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digrid_k6 add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); /*digraphと違いedgesを持たない*/ } int getid(int h, int w) { if (!ins(h, w, H, W)) return -1; return W * h + w; } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } digrid_k6(int H, int W) : H(H), W(W), digraph<T>(H * W) {} digrid_k6(vector<vector<char>> ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), digraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall) con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } void add(int fh, int fw, int th, int tw) { add(getid(fh, fw), getid(th, tw)); } void set_edges() { rep(i, n) fora(e, g[i]) edges.push_back(e); } }; // edgesを持たない // dijkstra(g,0)[t] とかける (t+n-1等とする必要はない) template <class T = int> class segdi : public digraph<T> { int getid(int k) { if (k >= len * 2 - 1) { return k; } else if (k < len - 1) { return k + len; } else { return k - len + 1; } } void add(int f, int t, T c = 1) { f = getid(f); t = getid(t); if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("segdi add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); /*digraphと違いedgesを持たない*/ } int mid; int len; public: using digraph<T>::g; using digraph<T>::n; using digraph<T>::edges; // 頂点が足りなくなったらresize segdi(int n_) : digraph<T>(1) { int nn = 1; while (nn < n_) nn *= 2; n_ = nn; len = n_; this->resize(n_ + (n_ - 1) * 2 + n_); mid = n_ + (n_ - 1) * 2; int ad = len * 2 - 1; rep(i, len - 1) { add(i, i * 2 + 1, 0); add(i, i * 2 + 2, 0); if (i * 2 + 1 >= len - 1) { add(i * 2 + 1, i + ad, 0); add(i * 2 + 2, i + ad, 0); } else { add(i * 2 + 1 + ad, i + ad, 0); add(i * 2 + 2 + ad, i + ad, 0); } } } void dfs(vi &list, int nl, int nr, int k, int l, int r) { if (r <= nl || nr <= l) return; if (l <= nl && nr <= r) { list += k; } else { dfs(list, nl, (nl + nr) / 2, k * 2 + 1, l, r); dfs(list, (nl + nr) / 2, nr, k * 2 + 2, l, r); } } void rekkyo(vi &list, int l, int r) { l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { list.push_back(l); } if (!(r & 1)) { list.push_back(r - 1); } l >>= 1; r = (r - 1) >> 1; } } // 半開 void add(int fl, int fr, int tl, int tr, int cost) { /*fは下側*/ int ad = 2 * len - 1; if (mid >= n) { this->resize(n + 100); } { int l = fl, r = fr; l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { if (l - len + 1 < 0) add(l + ad, mid, cost); else add(l, mid, cost); } if (!(r & 1)) { if (r - 1 - len + 1 < 0) add(r - 1 + ad, mid, cost); else add(r - 1, mid, cost); } l >>= 1; r = (r - 1) >> 1; } } { int l = tl, r = tr; l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { add(mid, l, 0); } if (!(r & 1)) { add(mid, r - 1, 0); } l >>= 1; r = (r - 1) >> 1; } } mid++; } }; // edgesを持たない // dijkstra(g,0)[t] とかける (t+n-1等とする必要はない) template <class T = int> class segun : public undigraph<T> { int getid(int k) { if (k >= len * 2 - 1) { return k; } else if (k < len - 1) { return k + len; } else { return k - len + 1; } } void add(int f, int t, T c = 1) { f = getid(f); t = getid(t); if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("segun add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); /*digraphと違いedgesを持たない*/ g[t].emplace_back(t, f, c); /*digraphと違いedgesを持たない*/ } int mid; int len; public: using digraph<T>::g; using digraph<T>::n; using digraph<T>::edges; // 頂点が足りなくなったらresize segun(int n_) : digraph<T>(1) { int nn = 1; while (nn < n_) nn *= 2; n_ = nn; len = n_; this->resize(n_ + (n_ - 1) * 2 + n_); mid = n_ + (n_ - 1) * 2; int ad = len * 2 - 1; rep(i, len - 1) { add(i, i * 2 + 1, 0); add(i, i * 2 + 2, 0); if (i * 2 + 1 >= len - 1) { add(i * 2 + 1, i + ad, 0); add(i * 2 + 2, i + ad, 0); } else { add(i * 2 + 1 + ad, i + ad, 0); add(i * 2 + 2 + ad, i + ad, 0); } } } void dfs(vi &list, int nl, int nr, int k, int l, int r) { if (r <= nl || nr <= l) return; if (l <= nl && nr <= r) { list += k; } else { dfs(list, nl, (nl + nr) / 2, k * 2 + 1, l, r); dfs(list, (nl + nr) / 2, nr, k * 2 + 2, l, r); } } void rekkyo(vi &list, int l, int r) { l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { list.push_back(l); } if (!(r & 1)) { list.push_back(r - 1); } l >>= 1; r = (r - 1) >> 1; } } // 半開 void add(int fl, int fr, int tl, int tr, int cost) { /*fは下側*/ int ad = 2 * len - 1; if (mid >= n) { this->resize(n + 100); } { int l = fl, r = fr; l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { if (l - len + 1 < 0) add(l + ad, mid, cost); else add(l, mid, cost); } if (!(r & 1)) { if (r - 1 - len + 1 < 0) add(r - 1 + ad, mid, cost); else add(r - 1, mid, cost); } l >>= 1; r = (r - 1) >> 1; } } { int l = tl, r = tr; l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { add(mid, l, 0); } if (!(r & 1)) { add(mid, r - 1, 0); } l >>= 1; r = (r - 1) >> 1; } } mid++; } }; #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define o_getid(a, b, name, ...) name #define getid(...) o_getid(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define unionfind unionfind_graph struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; // 連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } template <class T> unionfind(graph<T> &g) : n(g.n), trees(g.n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } add(g); } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } template <class T> void add(graph<T> &g) { fora(e, g.edges) { unite(e.f, e.t); } } template <class T> void unite(graph<T> &g) { add(g); } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } // つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off //@出力 template <class T> ostream &operator<<(ostream &os, digraph<T> &g) { os << endl << g.n << " " << sz(g.edges) << endl; fore(gi, g.edges) { os << f << " " << t << " " << c << endl; } return os; } template <class T> ostream &operator<<(ostream &os, undigraph<T> &g) { os << endl << g.n << " " << sz(g.edges) / 2 << endl; fore(gi, g.edges) { if (f < t) os << f << " " << t << " " << c << endl; } return os; } //@判定 template <class T> bool nibu(graph<T> &g) { int size = 0; rep(i, g.n) size += sz(g.g[i]); if (size == 0) return true; unionfind uf(g.n * 2); rep(i, g.n) fora(e, g.g[i]) uf.unite(e.f, e.t + g.n), uf.unite(e.f + g.n, e.t); rep(i, g.n) if (uf.same(i, i + g.n)) return 0; return true; } // 頂点ではなく辺の数に依存した計算量 O(E) template <class T> bool nibu_sub(graph<T> &g) { umapi col; /*0なら無色 */ queue<int> q; /*色は01か11 */ fora(e, g.edegs) { /*fとtの色を持つか否かは同じ*/ if (col[e.f] == 0) q.push(e.f); while (!q.empty()) { int f = q.front(); q.pop(); int fc = col[f]; forg(gi, g[f]) { int &tc = col[t]; /*fcには色がある*/ if (fc == tc) return false; /*違う色*/ if (tc) continue; /*無色*/ tc = fc ^ 2; q.push(tc); } } } return true; } // 二部グラフを色分けした際の頂点数を返す template <class T> vp nibug(graph<T> &g) { vp cg; if (!nibu(g)) { debugline("nibu"); ole(); } int n = g.size(); vb was(n); queue<P> q; rep(i, n) { if (was[i]) continue; q.push(mp(i, 1)); was[i] = 1; int red = 0; int coun = 0; while (q.size()) { int now = q.front().fi; int col = q.front().se; red += col; coun++; q.pop(); forg(gi, g[now]) { if (was[t]) continue; q.push(mp(t, col ^ 1)); was[t] = 1; } } cg.push_back(mp(red, coun - red)); } return cg; } // 連結グラフが与えられる 閉路があるか template <class T> bool close(undigraph<T> &g) { int n = 0; int e = 0; rep(i, g.n) { if (sz(g[i])) n++; forg(gi, g[i]) { e++; } } return (e >> 1) >= n; } template <class T> bool close(undigraph<T> &g, int v) { unionfind uf(g.n); rep(i, g.n) { forg(gi, g[i]) { if (f < t) break; if (f == t && f == v) return true; if (uf.same(f, v) && uf.same(t, v)) return true; uf.unite(f, t); } } return false; } template <class T> bool close(digraph<T> &g) { vi res; return topo(res, g); } //@変形 // 条件(f!=0等 f,t,cが使える)を満たすsubgraphをg2に代入 #define sub_di(g, g2, zhouken) \ { \ g2.resize(g.n); \ fore(gi, g.edges) { \ if (zhouken) { \ g2.add(f, t, c); \ } \ } \ } #define sub_un(g, g2, zhouken) \ { \ g2.resize(g.n); \ fore(gi, g.edges) { \ bool ok = zhouken; /*片方がアウトなら駄目という扱い*/ \ swap(f, t); \ ok &= zhouken; \ if (ok && f < t) { \ g2.add(f, t, c); \ } \ } \ } // 誘導部分グラフ(両辺がVに含まれる辺を含む最大のグラフを返す) yuudou #define sub_di_guided(g, g2, V) \ { \ vi ex(max(V)); \ fora(v, V) { ex[v] = 1; } \ sub_di(g, g2, ex[f] && ex[t]); \ } #define sub_un_guided(g, g2, V) \ { \ vi ex(max(V)); \ fora(v, V) { ex[v] = 1; } \ sub_un(g, g2, ex[f] && ex[t]); \ } #define sub_tree sub_un // 閉路がなければtrue bool topo(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n) for (auto &&e : g[i]) nyu[e.t]++; queue<int> st; rep(i, n) if (nyu[i] == 0) st.push(i); while (st.size()) { int v = st.front(); st.pop(); res.push_back(v); fora(e, g[v]) if (--nyu[e.t] == 0) st.push(e.t); } return res.size() == n; } // 辞書順最小トポロジカルソート bool topos(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n) for (auto &&e : g[i]) nyu[e.t]++; /*小さい順*/ priority_queue<int, vector<int>, greater<int>> q; rep(i, n) if (nyu[i] == 0) q.push(i); while (q.size()) { int i = q.top(); q.pop(); res.push_back(i); fora(e, g[i]) if (--nyu[e.t] == 0) q.push(e.t); } return res.size() == n; } template <class T> digraph<T> rev(digraph<T> &g) { digraph<T> r(g.n); rep(i, g.n) { forg(gi, g[i]) { r.add(t, f, c); } } return r; } // lc,rcは子を持つ中で一番左、右 //(g,ind,l,r) template <class T> tree<T> get_bfs_tree(tree<T> &g, vi &ind, vi &l, vi &r) { if (sz(ind)) { cerr << "ind must be empty" << endl; exit(0); } int N = sz(g); ind.resize(N); l.resize(N, inf); r.resize(N, -1); tree<T> h(N); queue<P> q; q.emplace(-1, 0); int c = 0; while (sz(q)) { int p = q.front().first; int i = q.front().second; q.pop(); ind[i] = c; if (~p) chmi(l[ind[p]], c); if (~p) chma(r[ind[p]], c); c++; forg(gi, g[i]) { if (t != p) q.emplace(i, t); } } fora(e, g.edges) { if (e.f < e.t) { h.add(ind[e.f], ind[e.t], e.c); } } rep(i, N) { if (l[i] == inf) l[i] = -1; } return h; } // lc,rcは子を持つ中で一番左、右 // たとえばl[lc[x]は2段下の最左 //(g,ind,l,r,lc,rc) template <class T> tree<T> get_bfs_tree(tree<T> &g, vi &ind, vi &l, vi &r, vi &lc, vi &rc) { if (sz(ind)) { cerr << "ind must be empty" << endl; exit(0); } int N = sz(g); ind.resize(N); l.resize(N, inf); lc.resize(N, inf); r.resize(N, -1); rc.resize(N, -1); tree<T> h(N); queue<P> q; q.emplace(-1, 0); int c = 0; while (sz(q)) { int p = q.front().first; int i = q.front().second; q.pop(); ind[i] = c; if (~p) { chmi(l[ind[p]], c); chma(r[ind[p]], c); if (sz(g[i]) > 1) { chmi(lc[ind[p]], c); chma(rc[ind[p]], c); } } c++; forg(gi, g[i]) { if (t != p) q.emplace(i, t); } } fora(e, g.edges) { if (e.f < e.t) { h.add(ind[e.f], ind[e.t], e.c); } } rep(i, N) { if (l[i] == inf) l[i] = -1; if (lc[i] == inf) lc[i] = -1; } return h; } //@集計 template <class T> vi indegree(graph<T> &g) { vi ret(g.size()); rep(i, g.size()) { forg(gi, g[i]) { ret[t]++; } } return ret; } template <class T> vi outdegree(graph<T> &g) { vi ret(g.size()); rep(i, g.size()) { ret[i] = g[i].size(); } return ret; } #define kansetu articulation // private /*private*/ P farthest____(tree<> &E, int cur, int pre, int d, vi &D) { D[cur] = d; P r = {d, cur}; forg(gi, E[cur]) if (t != pre) { P v = farthest____(E, t, cur, d + 1, D); r = max(r, v); } return r; } // dagでなければ-1を返す int diameter(digraph<> &g) { vi per; if (!topo(per, g)) return -1; int n = g.n; vi dp(n, 1); fora(v, per) { forg(gi, g[v]) { chma(dp[t], dp[f] + 1); } } return max(dp); } // iから最も離れた距離 vi diameters(tree<> &E) { /* diameter,center*/ vi D[3]; D[0].resize(E.size()); D[1].resize(E.size()); auto v1 = farthest____(E, 0, 0, 0, D[0]); auto v2 = farthest____(E, v1.second, v1.second, 0, D[0]); farthest____(E, v2.second, v2.second, 0, D[1]); int i; rep(i, D[0].size()) D[2].push_back(max(D[0][i], D[1][i])); return D[2]; } // iに対応するjと距離 vp diameters_p(tree<> &E) { /* diameter,center*/ vector<int> D[3]; D[0].resize(E.size()); D[1].resize(E.size()); auto v1 = farthest____(E, 0, 0, 0, D[0]); auto v2 = farthest____(E, v1.second, v1.second, 0, D[0]); farthest____(E, v2.second, v2.second, 0, D[1]); int i; vp res(E.size()); rep(i, D[0].size()) { if (D[0][i] > D[1][i]) res[i] = mp(v1.second, D[0][i]); else res[i] = mp(v2.second, D[1][i]); } return res; } int diameter(tree<> &E) { vi d = diameters(E); return max(d); } // 最も離れた二点を返す P diameter_p(tree<> &E) { auto d = diameters_p(E); int dis = -1; int l = -1, r = -1; rep(i, sz(d)) { if (chma(dis, d[i].se)) { l = i; r = d[i].fi; } } return mp(l, r); } //@列挙 取得 // 閉路がある時linfを返す template <class T> int longest_path(digraph<T> &g) { vi top; if (!topo(top, g)) { return linf; } int n = sz(top); vi dp(n, 0); for (auto &&i : top) { forg(gi, g[i]) { chma(dp[t], dp[i] + 1); } } return max(dp); } template <class T> vi longest_path_v(digraph<T> &g) { vi top; if (!topo(top, g)) { return vi(); } int n = sz(top); vi dp(n, 0); vi pre(n, -1); for (auto &&i : top) { forg(gi, g[i]) { if (chma(dp[t], dp[i] + 1)) { pre[t] = i; } } } int s = std::max_element(dp.begin(), dp.end()) - dp.begin(); vi path; while (s != -1) { path.push_back(s); s = pre[s]; } std::reverse(path.begin(), path.end()); return path; } // 橋を列挙する (取り除くと連結でなくなる辺) template <class T> vp bridge(graph<T> &G) { static bool was; vp brid; vi articulation; vi ord(G.n), low(G.n); vb vis(G.n); function<void(int, int, int)> dfs = [&](int v, int p, int k) { vis[v] = true; ord[v] = k++; low[v] = ord[v]; bool isArticulation = false; int ct = 0; for (int i = 0; i < G[v].size(); i++) { if (!vis[G[v][i].t]) { ct++; dfs(G[v][i].t, v, k); low[v] = min(low[v], low[G[v][i].t]); if (~p && ord[v] <= low[G[v][i].t]) isArticulation = true; if (ord[v] < low[G[v][i].t]) brid.push_back(make_pair(min(v, G[v][i].t), max(v, G[v][i].t))); } else if (G[v][i].t != p) { low[v] = min(low[v], ord[G[v][i].t]); } } if (p == -1 && ct > 1) isArticulation = true; if (isArticulation) articulation.push_back(v); }; int k = 0; rep(i, G.n) { if (!vis[i]) dfs(i, -1, k); } sort(brid.begin(), brid.end()); return brid; } // 間接点を列挙する (取り除くと連結でなくなる点) template <class T> vi articulation(undigraph<T> &G) { static bool was; vp bridge; vi arti; vi ord(G.n), low(G.n); vb vis(G.n); function<void(int, int, int)> dfs = [&](int v, int p, int k) { vis[v] = true; ord[v] = k++; low[v] = ord[v]; bool isArticulation = false; int ct = 0; for (int i = 0; i < G[v].size(); i++) { if (!vis[G[v][i].t]) { ct++; dfs(G[v][i].t, v, k); low[v] = min(low[v], low[G[v][i].t]); if (~p && ord[v] <= low[G[v][i].t]) isArticulation = true; if (ord[v] < low[G[v][i].t]) bridge.push_back(make_pair(min(v, G[v][i].t), max(v, G[v][i].t))); } else if (G[v][i].t != p) { low[v] = min(low[v], ord[G[v][i].t]); } } if (p == -1 && ct > 1) isArticulation = true; if (isArticulation) arti.push_back(v); }; int k = 0; rep(i, G.n) { if (!vis[i]) dfs(i, -1, k); } sort(arti.begin(), arti.end()); return arti; } // 閉路パスを一つ返す vi close_path(digraph<> &g) { int n = g.n; vi state(n); vi path; rep(i, n) if (!state[i]) { if (fix([&](auto dfs, int v) -> bool { if (state[v]) { if (state[v] == 1) { path.erase(path.begin(), find(path.begin(), path.end(), v)); return true; } return false; } path.push_back(v); state[v] = 1; forg(gi, g[v]) { if (dfs(t)) return true; } state[v] = -1; path.pop_back(); return false; })(i)) { return path; } } return vi(); } #ifdef type_id_graph /*@formatter:on*/ vi close_path_id(digraph<> &g) { auto D = close_path(g); D += D[0]; vi ret; int cur = D[0]; rep(i, sz(D) - 1) { forg(gi, g[D[i]]) { if (t == D[i + 1]) { ret += id; break; } } } return ret; // int n = g.n; // vi state(sz(g.edges)); // vi path; // rep(i, n) if (!state[i]) { // if (fix([&](auto dfs, int v) -> bool { // forg(gi, g[v]) { // if (state[id]) { // if (state[id] == 1) { // path.erase(path.begin(), find(path.begin(), // path.end(), id)); return true; // } // return false; // } // path.push_back(id); // state[id] = 1; // if (dfs(t)) { // return true; // } // state[v] = -1; // path.pop_back(); // } // return false; // })(i)) { return path; } // } // return vi(); } /*@formatter:off*/ #endif vi close_path_min(digraph<> &g) { int n = g.n; vvi(dis, n); rep(i, n) dis[i] = dijkstra(g, i, linf); int mind = linf; int f = 0, t = 0; rep(i, n) { rep(j, n) { if (i == j) continue; if (chmi(mind, dis[i][j] + dis[j][i])) { f = i; t = j; } } } vi path; auto add = [&](int f, int t) { int now = f; while (now != t) { rep(i, n) { if (dis[now][i] == 1 && dis[f][i] + dis[i][t] == dis[f][t]) { path.push_back(i); now = i; break; } } } }; add(f, t); add(t, f); return path; } // iを含む最短閉路 https://atcoder.jp/contests/abc022/tasks/abc022_c /*閉路が1つしかない場合、その閉路に含まれる頂点を1としたvectorを返す*/; template <class T> vi get_close1(digraph<T> &g) { int n = g.n; queue<int> q; vi d = outdegree(g); vi res(n, 1); rep(i, n) { if (d[i] == 0) { q += i; res[i] = 0; } } auto rg = rev(g); while (q.size()) { auto now = q.front(); q.pop(); forg(gi, rg[now]) { if (--d[t] == 0) { q += t; res[t] = 0; } } } return res; } //@アルゴリズム template <class T> int krus(undigraph<T> &g) { int res = 0; unionfind uf(g.n); if (sz(g.edges) == 0) g.set_edges(); int i = 0; auto E = g.edges; sort(E); fora(e, E) { if (uf.unite(e.f, e.t)) { res += e.c; } } return res; } template <class T> vector<edge_<T>> krus_ed(undigraph<T> &g) { unionfind uf(g.n); if (sz(g.edges) == 0) g.set_edges(); int i = 0; auto E = g.edges; sort(E); vector<edge_<T>> res; fora(e, E) { if (uf.unite(e.f, e.t)) { res.push_back(e); } } return res; } template <class T> tree<T> krus_tr(undigraph<T> &g) { tree<T> res(g.n); unionfind uf(g.n); if (sz(g.edges) == 0) g.set_edges(); int i = 0; auto E = g.edges; sort(E); fora(e, E) { if (uf.unite(e.f, e.t)) { res.add(e.f, e.t); } } return res; } template <class T> int krus(vector<edge_<T>> &g) { int n = 0; fora(e, g) { chma(n, max(e.f, e.t) + 1); }; int res = 0; unionfind uf(n); auto E = g; sort(E); fora(e, E) { if (uf.unite(e.f, e.t)) { res += e.c; } } return res; } template <class T> vector<edge_<T>> krus_ed(vector<edge_<T>> &g) { int n = 0; fora(e, g) { chma(n, max(e.f, e.t) + 1); }; vector<edge_<T>> res; unionfind uf(n); auto E = g; sort(E); fora(e, E) { if (uf.unite(e.f, e.t)) { res += e; } } return res; }; template <class T> vi krus_i(vector<edge_<T>> &g) { int n = 0; fora(e, g) { chma(n, max(e.f, e.t) + 1); }; vi res; unionfind uf(n); auto E = g; sort(E); int i = 0; fora(e, E) { if (uf.unite(e.f, e.t)) { res += i; } i++; } return res; } //@実験 digraph<> rang_di(int n, int m, bool zibun = 0, bool taju = 0) { umapp was; digraph<> g(n); was[mp(-1, -2)] = 1; while (m) { int f = -1, t = -2; while (f < 0 || (!taju && was[mp(f, t)])) { f = rand(0, n - 1); t = rand(0, n - 1); if (!zibun && f == t) f = -1; } g.add(f, t); was[mp(f, t)] = 1; m--; } return g; } digraph<> perfect_di(int n, bool zibun = 0) { digraph<> g(n); rep(i, n) { rep(j, n) { if (!zibun && i == j) con; g.add(i, j); } } return g; } undigraph<> rang_un(int n, int m, bool zibun = 0, bool taju = 0) { umapp was; undigraph<> g(n); was[mp(-1, -2)] = 1; while (m) { int f = -1, t = -2; while (f < 0 || (!taju && was[mp(min(f, t), max(f, t))])) { f = rand(0, n - 1); t = rand(0, n - 1); if (!zibun && f == t) f = -1; } g.add(f, t); was[mp(min(f, t), max(f, t))] = 1; m--; } return g; } undigraph<> perfect_un(int n, bool zibun = 0) { undigraph<> g(n); rep(i, n) { rep(j, i, n) { if (!zibun && i == j) con; g.add(i, j); } } return g; } /*頂点数がkの木を一つ返す サイズが0の木が帰ったら終了*/ tree<int> next_tree(int k) { assert(2 <= k && k < 11); static str name; static ifstream ina; static int rem; static vp edges; static int pk = -1; /*前回見たk*/ if (pk != k) { if (~pk) ina.close(); edges.clear(); pk = k; name = (k == 6) ? "C:\\Users\\kaout\\Desktop\\trees_sizek\\nazeka6.txt" : "C:\\Users\\kaout\\Desktop\\trees_sizek\\tree_size" + tos(k) + ".txt"; ina = ifstream(name); rem = pow(k, k - 2); /*Cayleyの定理*/ rep(i, k) rep(j, i + 1, k) edges.emplace_back(i, j); pk = k; } tree<int> g(k); if (rem == 0) { g.resize(0); return g; } int m; ina >> m; while (m) { int lb = lbit(m); int id = log2(lb); g.add(edges[id].first, edges[id].second); m ^= lb; } rem--; return g; } undigraph<int> next_undi(int k) { assert(2 <= k && k < 9); static str name; static ifstream ina; static int rem; static vp edges; static vi lims = {-1, -1, 1, 4, 38, 728, 26704, 1866256}; static int pk = -1; /*前回見たk*/ if (pk != k) { if (~pk) ina.close(); edges.clear(); pk = k; name = (k == 6) ? "C:\\Users\\kaout\\Desktop\\undi_sizek\\roku.txt" : "C:\\Users\\kaout\\Desktop\\undi_sizek\\undi_size" + tos(k) + ".txt"; ina = ifstream(name); rem = lims[k]; rep(i, k) rep(j, i + 1, k) edges.emplace_back(i, j); pk = k; } undigraph<int> g(k); if (rem == 0) { g.resize(0); return g; } int m; ina >> m; while (m) { int lb = lbit(m); int id = log2(lb); g.add(edges[id].first, edges[id].second); m ^= lb; } rem--; return g; } vector<tree<int>> trees(int k) { vector<tree<int>> res; while (1) { tree<int> g = next_tree(k); if (sz(g) == 0) break; res.push_back(g); } return res; } vector<undigraph<int>> undis(int k) { vector<undigraph<int>> res; while (1) { undigraph<int> g = next_undi(k); if (sz(g) == 0) break; res.push_back(g); } return res; } template <class T> vector<vector<int>> table(graph<T> &g, int init_value) { vvi(res, g.n, g.n, init_value); rep(i, g.n) { forg(gi, g[i]) { res[i][t] = c; } } return res; } #ifdef type_id_graph template <class T> vector<vector<edge_<T>>> type_list(digraph<T> &g) { vector<vector<edge_<T>>> res; rep(i, g.n) { forg(gi, g[i]) { res[ty].push_back(g[i][gi]); } } return res; } template <class T> vector<vector<edge_<T>>> type_list(undigraph<T> &g, int types = -1) { int tn = types; if (types == -1) tn = g.n; rep(i, g.n) { forg(gi, g[i]) { chma(tn, ty); } } vector<vector<edge_<T>>> res(tn + 1); vi was(g.n); rep(i, g.n) { forg(gi, g[i]) { if (f < t) res[ty].push_back(g[i][gi]); else if (f == t && !was[f]) { res[ty].push_back(g[i][gi]); was[f] = 1; } } } return res; } // idは 00 11 22のようにedgesに持たれている template <class T> vi krus_id(undigraph<T> &g) { unionfind uf(g.n); if (sz(g.edges) == 0) g.set_edges(); int i = 0; auto E = g.edges; sort(E); vi res; fora(e, E) { if (uf.unite(e.f, e.t)) { res.push_back(e.id); } } return res; } // graの方が早い #endif // type,idが使いたい場合はgraty /*@formatter:on*/ void solve() { in(N, M); digraph<> g(2 * k5); ; g.ing(N, M); if (M == 0) { fin(-1); } auto E = close_path_id(g); if (sz(E) == 0) { fin(-1); } auto cut = [&]() { vb ee = bool_(E, M); vi ev(N); vi V; fora(e, E) { /*@formatter:off*/ fora_init(e, E); /*@formatter:on*/ int f = g.edges[e].f, t = g.edges[e].t; ev[f] = 1; ev[t] = 1; V += f; } fore(gi, g.edges) { if (ev[f] && ev[t] && !ee[id]) { int a = find(V, f); int b = find(V, t); if (a < b) { erase(E, a, b); insert(E, a, id); } else { entry(E, b, a); E += id; } return true; } } return false; }; while (cut()) ; vi ev(N); fora(e, E) { /*@formatter:off*/ fora_init(e, E); /*@formatter:on*/ int f = g.edges[e].f, t = g.edges[e].t; ev[f] = 1; ev[t] = 1; } rep(i, N) { if (ev[i] == 0) con; int cou = 0; forg(gi, g[i]) { cou += ev[f] && ev[t]; } if (cou != 1) fin(-1); } out(sz(E)); fora(e, E) { /*@formatter:off*/ fora_init(e, E); /*@formatter:on*/ out(g.edges[e].f + 1); } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } if (sz(message)) { cerr << "****************************" << endl; cerr << message << endl; cerr << "****************************" << endl; } #endif return 0; };
// #undef _DEBUG // #pragma GCC optimize("Ofast") // 不動小数点の計算高速化 // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName // 最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name /*@formatter:off*/ //[-n, n)にアクセスできる // また、外部関数resizeに渡せる // sizeは[-n, n)でnを返す template <class T> class mvec { vector<T> v; int n; public: mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} auto &operator[](int i) { return v[i + n]; } auto size() { return n; } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; //[]でboolは参照を返さないため特殊化が必要 template <> struct mvec<bool> { vector<bool> v; int n; mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, bool val) : n(n), v(n * 2, val) {} auto operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { int spa = 3; for (auto &&v : a) { spa = max(spa, (int)(to_string(v).size()) + 1); } int n = (int)a.size(); os << endl; for (int i = -n; i < n; i++) { int need = spa - ((int)to_string(i).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(a[i]).size())); } while (need--) { os << " "; } os << i; } os << endl; int i = -n; for (auto &&v : a) { int need = spa - ((int)to_string(v).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(i).size())); } while (need--) { os << " "; } os << v; i++; } return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #define mvvt0(t) mvec<mvec<t>> #define mvvt1(t, a) mvec<mvec<t>> a #define mvvt2(t, a, b) mvec<mvec<t>> a(b) #define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c)) #define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d)) #define mvvi(...) \ over4(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__) template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); } template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) { return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...)); } #define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__) #ifdef _DEBUG string message; // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; // #define vector debtor // 区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapu unordered_map<uint64_t, ll> #define umapip unordered_map<ll, P> template <class T, class U, class X> auto count(unordered_map<T, U> &a, X k) { return a.find(k) != a.end(); } #else #define endl '\n' // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapu __gnu_pbds::gp_hash_table<uint64_t, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> template <class T, class U, class W, class X> auto count(__gnu_pbds::gp_hash_table<T, U, W> &a, X k) { return a.find(k) != a.end(); } #endif /*@formatter:on*/ struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; /*@formatter:off*/ template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } // 衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &r) const { return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &r) const { return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順 */ } bool operator==(const T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } //@マクロ省略系 型,構造 #define double long double using dou = double; const double eps = 1e-9; // 基本コメントアウト /* struct epsdou { double v; epsdou(double v = 0) : v(v) {} template<class T> epsdou &operator+=(T b) { v += (double) b; return (*this); } template<class T> epsdou &operator-=(T b) { v -= (double) b; return (*this); } template<class T> epsdou &operator*=(T b) { v *= (double) b; return (*this); } template<class T> epsdou &operator/=(T b) { v /= (double) b; return (*this); } epsdou operator+(epsdou b) { return v + (double) b; } epsdou operator-(epsdou b) { return v - (double) b; } epsdou operator*(epsdou b) { return v * (double) b; } epsdou operator/(epsdou b) { return v / (double) b; } epsdou operator-() const { return epsdou(-v); } template<class T> bool operator<(T b) { return v < (double) b; } template<class T> bool operator>(T b) { return v > (double) b; } template<class T> bool operator==(T b) { return fabs(v - (double) b) <= eps; } template<class T> bool operator<=(T b) { return v < (double) b || fabs(v - b) <= eps; } template<class T> bool operator>=(T b) { return v > (double) b || fabs(v - b) <= eps; } operator double() { return v; }};istream &operator>>(istream &iss, epsdou &a) { iss >> a.v; return iss;}ostream &operator<<(ostream &os, epsdou &a) { os << a.v; return os;} #define eps_conr_t(o) template<class T> epsdou operator o(T b, epsdou a){return a.v o (dou)b;} #define eps_conl_t(o) template<class T> epsdou operator o(epsdou a, T b){return a.v o (dou)b;} eps_conl_t(+)eps_conl_t(-)eps_conl_t(*)eps_conl_t(/)eps_conr_t(+)eps_conr_t(-)eps_conr_t(*)eps_conr_t(/) #undef double #define double epsdou */ #define ull unsigned long long using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using mvp = mvec<P>; using mvt = mvec<T>; #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam1(ret) [&](auto &v) { return ret; } #define lam2(v, ret) [&](auto &v) { return ret; } #define lam(...) over2(__VA_ARGS__, lam2, lam1)(__VA_ARGS__) #define lamr(right) [&](auto &p) { return p right; } // マクロ省略系 コンテナ using vi = vector<ll>; using vb = vector<bool>; using vs = vector<string>; using vd = vector<double>; using vc = vector<char>; using vp = vector<P>; using vt = vector<T>; // #define V vector #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vvi(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) #define vvt(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__) #define vv(type, ...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__) template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } #define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__) #define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__) #define vns(name, ...) auto name = make_v<string>(__VA_ARGS__) #define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__) #define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__) #define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__) #define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setp = set<P>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap // 任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> // マクロ 繰り返し // ↓@オーバーロード隔離 #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) // 逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) // ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss2(i, I) for (int i = 0; i < I; i++) #define repss4(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss6(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss8(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss10(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) // i,j,k...をnまで見る #define reps2(i, n) repss2(i, n) #define reps3(i, j, n) repss4(i, j, n, n) #define reps4(i, j, k, n) repss6(i, j, k, n, n, n) #define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n) template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_2(i, a) repss2(i, sz(a)) // 正方形である必要はない // 直前を持つのとどっちが早いか #define repv_3(i, j, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \ nex_repv2(i, j, I, J, a)) // 箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_4(i, j, k, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \ i = 0, j = 0, k = 0; \ i < I; nex_repv3(i, j, k, I, J, K, a)) #define repv_5(i, j, k, l, a) \ repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_6(i, j, k, l, m, a) \ repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define forrm1(st) \ for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it) #define forrm3(k, v, st) \ for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it) #define fors1(st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors2(v, st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors3(v, st, r) \ for (auto &&fors_it = st.begin(); fors_it != st.end() && (*fors_it) < r; \ ++fors_it) #define fors4(v, st, l, r) \ for (auto &&fors_it = st.lower_bound(l); \ fors_it != st.end() && (*fors_it) < r; ++fors_it) #define forslr3(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr4(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr5(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr6(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> vector<U> to1d(vector<vector<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(vector<vector<vector<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<vector<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(vector<vector<vector<vector<vector<vector<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define fora_init_2(a, A) ; #define fora_init_3(fora_i, a, A) auto &&a = A[fora_i]; #define fora_init_4(a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_5(fora_i, a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_7(fora_i, a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define fora_init_9(fora_i, a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define fora_init(...) \ over9(__VA_ARGS__, fora_init_9, fora_init_8, fora_init_7, fora_init_6, \ fora_init_5, fora_init_4, fora_init_3, fora_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_3(forr_i, a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_5(forr_i, a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_7(forr_i, a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init_9(forr_i, a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init(...) \ over9(__VA_ARGS__, forr_init_9, forr_init_8, forr_init_7, forr_init_6, \ forr_init_5, forr_init_4, forr_init_3, forr_init_2)(__VA_ARGS__) #define forp_init(k, v, ...) \ auto &&k = (*forp_it).fi; \ auto &&v = (*forp_it).se; #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define forrm_init(k, v, ...) \ auto &&k = (*forrm_it).fi; \ auto &&v = (*forrm_it).se; #define fors_init(v, ...) auto &&v = (*fors_it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init(...) \ over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__); #define fora_2(a, A) for (auto &&a : A) #define fora_3(fora_i, a, A) rep(fora_i, sz(A)) #define fora_4(a, b, A, B) rep(fora_i, sz(A)) #define fora_5(fora_i, a, b, A, B) rep(fora_i, sz(A)) #define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_7(fora_i, a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define fora_9(fora_i, a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_3(forr_i, a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_5(forr_i, a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_7(forr_i, a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) #define forr_9(forr_i, a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) // ↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__) #define repss(...) \ over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \ repss2)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #define fora(...) \ over9(__VA_ARGS__, fora_9, fora_8, fora_7, fora_6, fora_5, fora_4, fora_3, \ fora_2)(__VA_ARGS__) #define forr(...) \ over9(__VA_ARGS__, forr_9, forr_8, forr_7, forr_6, forr_5, forr_4, forr_3, \ forr_2)(__VA_ARGS__) // #define forr(v, a) for(auto&& v : makeReversedRange(a)) // 参照を取らない #define forv(a, b) for (auto a : to1d(b)) // インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) // 値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #define form(...) \ over5(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define forrm(...) \ over5(__VA_ARGS__, forrm5, forrm4, forrm3, forrm2, forrm1)(__VA_ARGS__) #define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__) #define forp(k, v, st) \ for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it) // マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const dou dinf = (dou)linf * linf; const char infc = '{'; const string infs = "{"; const double PI = 3.1415926535897932384626433832795029L; // マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの // 埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:on*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { os << endl; for (auto &&v : m) os << v << endl; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, mset<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora(v,a)os<<v<<endl;return os;} /*@formatter:off*/ template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // template<typename W, typename H> void resize(vector<W> &vec, const H head) { // vec.resize(head); } template<typename W, typename H, typename ... T> void // resize(vector<W> &vec, const H &head, const T ... tail) {vec.resize(head);for // (auto &v: vec)resize(v, tail...);} template <typename T, typename F> bool all_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool all_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (!all_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool any_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool any_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (any_of2(v[i], f)) return true; } return false; } template <typename T, typename F> bool none_of2(T &v, F f) { return f(v); } template <typename T, typename F> bool none_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (none_of2(v[i], f)) return false; } return true; } template <typename T, typename F> bool find_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll find_if2(vector<T> &v, F f) { rep(i, sz(v)) { if (find_if2(v[i], f)) return i; } return sz(v); } template <typename T, typename F> bool rfind_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll rfind_if2(vector<T> &v, F f) { rer(i, sz(v) - 1) { if (rfind_if2(v[i], f)) return i; } return -1; } template <class T> bool contains(string &s, const T &v) { return s.find(v) != string::npos; } template <typename T> bool contains(vector<T> &v, const T &val) { return std::find(v.begin(), v.end(), val) != v.end(); } template <typename T, typename F> bool contains_if2(vector<T> &v, F f) { return find_if(v.begin(), v.end(), f) != v.end(); } template <typename T, typename F> ll count_if2(T &v, F f) { return f(v); } template <typename T, typename F> ll count_if2(vector<T> &vec, F f) { ll ret = 0; fora(v, vec) ret += count_if2(v, f); return ret; } template <typename T, typename F> void for_each2(T &v, F f) { f(v); } template <typename T, typename F> void for_each2(vector<T> &vec, F f) { fora(v, vec) for_each2(v, f); } template <typename W> ll count_od(vector<W> &a) { return count_if2(a, [](ll v) { return v & 1; }); } template <typename W> ll count_ev(vector<W> &a) { return count_if2(a, [](ll v) { return !(v & 1); }); } // 削除した後のvectorを返す template <typename T, typename F> vector<T> erase_if2(vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> erase_if2(vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = erase_if2(v[i], f); } return res; } template <typename T, typename F> vector<T> l_erase_if2(vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> ll l_rfind_if(vector<T> &v, F f) { rer(i, sz(v) - 1) { if (f(v[i])) return i; } return -1; } template <typename T, typename F> bool l_contains_if(vector<T> &v, F f) { rer(i, sz(v) - 1) { if (f(v[i])) return true; } return false; } template <class A, class B, class C> auto t_all_of(A a, B b, C c) { return std::all_of(a, b, c); } template <class A, class B, class C> auto t_any_of(A a, B b, C c) { return std::any_of(a, b, c); } template <class A, class B, class C> auto t_none_of(A a, B b, C c) { return std::none_of(a, b, c); } template <class A, class B, class C> auto t_find_if(A a, B b, C c) { return std::find_if(a, b, c); } template <class A, class B, class C> auto t_count_if(A a, B b, C c) { return std::count_if(a, b, c); } #define all_of_s__2(a, right) (t_all_of(all(a), lamr(right))) #define all_of_s__3(a, v, siki) (t_all_of(all(a), [&](auto v) { return siki; })) #define all_of_s(...) over3(__VA_ARGS__, all_of_s__3, all_of_s__2)(__VA_ARGS__) // all_of(A, %2); // all_of(A, a, a%2); #define all_of__2(a, right) all_of2(a, lamr(right)) #define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; }) #define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__) #define all_of_f(a, f) all_of2(a, f) #define any_of_s__2(a, right) (t_any_of(all(a), lamr(right))) #define any_of_s__3(a, v, siki) (t_any_of(all(a), [&](auto v) { return siki; })) #define any_of_s(...) over3(__VA_ARGS__, any_of_s__3, any_of_s__2)(__VA_ARGS__) #define any_of__2(a, right) any_of2(a, lamr(right)) #define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; }) #define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__) #define any_of_f(a, f) any_of2(a, f) #define none_of_s__2(a, right) (t_none_of(all(a), lamr(right))) #define none_of_s__3(a, v, siki) \ (t_none_of(all(a), [&](auto v) { return siki; })) #define none_of_s(...) \ over3(__VA_ARGS__, none_of_s__3, none_of_s__2)(__VA_ARGS__) #define none_of__2(a, right) none_of2(a, lamr(right)) #define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; }) #define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__) #define none_of_f(a, f) none_of2(a, f) #define find_if_s__2(a, right) (t_find_if(all(a), lamr(right)) - a.begin()) #define find_if_s__3(a, v, siki) \ (t_find_if(all(a), [&](auto v) { return siki; }) - a.begin()) #define find_if_s(...) \ over3(__VA_ARGS__, find_if_s__3, find_if_s__2)(__VA_ARGS__) #define find_if__2(a, right) find_if2(a, lamr(right)) #define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; }) #define find_if(...) over3(__VA_ARGS__, find_if__3, find_if__2)(__VA_ARGS__) #define find_if_f(a, f) find_if2(a, f) #define rfind_if_s__2(a, right) l_rfind_if(a, lamr(right)) #define rfind_if_s__3(a, v, siki) l_rfind_if(a, [&](auto v) { return siki; }) #define rfind_if_s(...) \ over3(__VA_ARGS__, rfind_if_s__3, rfind_if_s__2)(__VA_ARGS__) #define rfind_if__2(a, right) rfind_if2(a, lamr(right)) #define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; }) #define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if_s__2(a, right) l_contains_if(a, lamr(right)) #define contains_if_s__3(a, v, siki) \ l_contains_if(a, [&](auto v) { return siki; }) #define contains_if_s(...) \ over3(__VA_ARGS__, contains_if_s__3, contains_if_s__2)(__VA_ARGS__) #define contains_if__2(a, right) contains_if2(a, lamr(right)) #define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; }) #define contains_if(...) \ over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__) #define contains_if_f(a, f) contains_if2(a, f) #define count_if_s__2(a, right) (t_count_if(all(a), lamr(right))) #define count_if_s__3(a, v, siki) \ (t_count_if(all(a), [&](auto v) { return siki; })) #define count_if_s(...) \ over3(__VA_ARGS__, count_if_s__3, count_if_s__2)(__VA_ARGS__) #define count_if__2(a, right) count_if2(a, lamr(right)) #define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; }) #define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__) #define count_if_f(a, f) count_if2(a, f) #define for_each_s__2(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each_s__3(a, v, siki) \ do { \ fora(v, a) { siki; } \ } while (0) #define for_each_s(...) \ over3(__VA_ARGS__, for_each_s__3, for_each_s__2)(__VA_ARGS__) #define for_each__2(a, right) for_each2(a, lamr(right)) #define for_each__3(a, v, siki) for_each2(a, [&](auto v) { return siki; }) #define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__) #define for_each_f(a, f) for_each2(a, f); #define erase_if_s__2(a, right) l_erase_if2(a, lamr(right)) #define erase_if_s__3(a, v, siki) l_erase_if2(a, [&](auto v) { return siki; }) #define erase_if_s(...) \ over3(__VA_ARGS__, erase_if_s__3, erase_if_s__2)(__VA_ARGS__) #define erase_if__2(a, right) erase_if2(a, lamr(right)) #define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; }) #define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__) #define erase_if_f(a, f) erase_if2(a, f) #define entry_if_s__2(a, right) l_entry_if2(a, lamr(right)) #define entry_if_s__3(a, v, siki) l_entry_if2(a, [&](auto v) { return siki; }) #define entry_if_s(...) \ over3(__VA_ARGS__, entry_if_s__3, entry_if_s__2)(__VA_ARGS__) #define entry_if__2(a, right) entry_if2(a, lamr(right)) #define entry_if__3(a, v, siki) entry_if2(a, [&](auto v) { return siki; }) #define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__) #define entry_if_f(a, f) entry_if2(a, f) template <class T, class U, class W> void replace(vector<W> &a, T key, U v) { rep(i, sz(a)) if (a[i] == key) a[i] = v; } template <class T, class U, class W> void replace(vector<vector<W>> &A, T key, U v) { rep(i, sz(A)) replace(A[i], key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } // template<class T> void replace(T &a) { replace(a, '#'); } void replace(str &a) { int dec = 0; if ('a' <= a[0] && a[0] <= 'z') dec = 'a'; if ('A' <= a[0] && a[0] <= 'Z') dec = 'A'; fora(v, a) v -= dec; } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } void sort(int &a, int &b) { if (a > b) swap(a, b); } void sort(int &a, int &b, int &c) { sort(a, b); sort(a, c); sort(b, c); } void rsort(int &a, int &b) { if (a < b) swap(a, b); } void rsort(int &a, int &b, int &c) { rsort(a, b); rsort(a, c); rsort(b, c); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> // 例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <class... T, class U> auto sorted(U head, T... a) { sort(head, a...); return head; } template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) { rep(i, N) a[i] = v; } template <typename A, size_t N, size_t O, typename T> void fill(A (&a)[N][O], const T &v) { rep(i, N) rep(j, O) a[i][j] = v; } template <typename A, size_t N, size_t O, size_t P, typename T> void fill(A (&a)[N][O][P], const T &v) { rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T> void fill(A (&a)[N][O][P][Q], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, typename T> void fill(A (&a)[N][O][P][Q][R], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S, typename T> void fill(A (&a)[N][O][P][Q][R][S], const T &v) { rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) a[i][j][k][l][m][n] = v; } template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <class W, class T> void fill(mvec<W> &xx, const T v) { fora(x, xx) fill(x, v); } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ over6(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ over6(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #ifdef _DEBUG template <class T> void err2(T &&head) { cerr << head; } template <class T, class... U> void err2(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); } template <class T, class... U> void err(T &&head, U &&...tail) { cerr << head << " "; err2(tail...); cerr << "" << endl; } template <class T> void err(T &&head) { cerr << head << endl; } void err() { cerr << "" << endl; } template <class T> string out_m2(vector<T> &a, ll W = inf) { stringstream ss; if (W == inf) W = min(sz(a), 12ll); if (sz(a) == 0) return ss.str(); rep(i, W) { ss << a[i]; if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } template <class T> string out_m2(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1) ss << " *|"; else ss << " " << key << "|"; rep(w, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3) ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]), 12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } template <class T> string out_m2(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) { H = min({H, sz(a), 6ll}); W = min({W, sz(a[0]), 6ll}); stringstream ss; ss << endl; // if (key == inf)ss << " *|"; else ss << " " << key << "|"; if (key == inf) ss << " *|"; else { ss << std::right << std::setw(2) << key; ss << "|"; } rep(w, -W, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 8 + 3) ss << "_"; ss << "" << endl; rep(h, -H, H) { ss << std::right << std::setw(2) << h << "|"; int NW = min(sz(a[h]), 6ll); rep(w, -NW, NW) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 6; H = min(H, sz(a)); rep(i, -H, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } /*@formatter:off*/ template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m(vector<T> &a, ll W = inf) { stringstream ss; if (W == inf) W = min(sz(a), 12ll); if (sz(a) == 0) return ss.str(); rep(i, W) { ss << a[i] << " "; } ss << "" << endl; return ss.str(); } /*@formatter:off*/ template <class T> string out_m(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1) ss << " *|"; else ss << " " << key << "|"; rep(w, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3) ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]), 12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } ss << endl; return ss.str(); } template <class T> string out_m(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 5; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m(a[i], W, U, i); } ss << endl; return ss.str(); } string out_m(int a) { stringstream ss; ss << a << endl; return ss.str(); } template <class T> string out_m(T &a) { stringstream ss; ss << a << endl; return ss.str(); } template <class T> void outv(vector<T> &a, ll W = inf) { cout << out_m(a, W) << endl; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) { cout << out_m(a, H, W, key) << endl; } template <class T> void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) { cout << out_m(a, H, W, U) << endl; } #else template <class T> void outv(vector<T> &a, ll W = inf) { rep(i, min(W, sz(a))) { cout << a[i] << " "; } cout << "" << endl; } template <class T> void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) { rep(i, min(H, sz(a))) { outv(a[i], W); } } template <class T> void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) { ; } #define err(...) ; #endif template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } // テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } template <class T> void na(set<T> &a, ll n) { rep(i, n) a.insert(in()); } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T> void nad(set<T> &a, ll n) { rep(i, n) a.insert(in() - 1); } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } template <class T, class U> void na2(set<T> &a, set<U> &b, ll n) { rep(i, n) { a.insert(in()); b.insert(in()); } } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--; } #define dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; // デバッグ #define sp << " " << #define deb1(x) debugName(x) << " = " << out_m2(x) #define deb_2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__) #define deb_3(x, ...) deb1(x) << ", " << deb_2(__VA_ARGS__) #define deb_4(x, ...) deb1(x) << ", " << deb_3(__VA_ARGS__) #define deb5(x, ...) deb1(x) << ", " << deb_4(__VA_ARGS__) #define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__) #define deb7(x, ...) deb1(x) << ", " << deb6(__VA_ARGS__) #define deb8(x, ...) deb1(x) << ", " << deb7(__VA_ARGS__) #define deb9(x, ...) deb1(x) << ", " << deb8(__VA_ARGS__) #define deb10(x, ...) deb1(x) << ", " << deb9(__VA_ARGS__) #ifdef _DEBUG #define deb(...) \ cerr << over10(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb_4, \ deb_3, deb_2, deb1)(__VA_ARGS__) \ << endl #define base_keta 8 void print_n_base(int x, int base) { cerr << bitset<base_keta>(x) << endl; } template <class T> void print_n_base(vector<T> X, int base) { cerr << endl; for (auto &&x : X) { print_n_base(x, base); } cerr << endl; } // n進数 #define deb2(x) \ cerr << debugName(x) << " = "; \ print_n_base(x, 2); #define deb3(x) \ cerr << debugName(x) << " = "; \ print_n_base(x, 3); #define deb4(x) \ cerr << debugName(x) << " = "; \ print_n_base(x, 4); #else #define deb(...) ; #define deb2(...) ; #define deb3(...) ; #define deb4(...) ; #endif #define debugline(x) \ cerr << x << " " \ << "(L:" << __LINE__ << ")" << '\n' //@formatter:off // よく使うクラス、構造体 // graphでredefineしているため、書き換えたら向こうも書き換える struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; // 連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } // つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; using bint = __int128; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } __int128 to_bint(string &s) { __int128 ret = 0; for (ll i = 0; i < s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } void operator>>(istream &iss, bint &v) { string S; iss >> S; v = 0; rep(i, sz(S)) { v *= 10; v += S[i] - '0'; } } bint max(bint a, signed b) { return max(a, (bint)b); } bint max(signed a, bint b) { return max((bint)a, b); } bint max(bint a, ll b) { return max(a, (bint)b); } bint max(ll a, bint b) { return max((bint)a, b); } bint min(bint a, signed b) { return min(a, (bint)b); } bint min(signed a, bint b) { return min((bint)a, b); } bint min(bint a, ll b) { return min(a, (bint)b); } bint min(ll a, bint b) { return min((bint)a, b); } // エラー void ole() { #ifdef _DEBUG debugline("ole"); exit(0); #endif string a = "a"; rep(i, 30) a += a; rep(i, 1 << 17) cout << a << endl; cout << "OLE 出力長制限超過" << endl; exit(0); } void re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } // 便利関数 // テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } ll rand(ll max) { return rand(0, max); } template <class T> T rand(vector<T> &A) { return A[rand(sz(A) - 1)]; } // 重複することがある template <class T> vector<T> ranv(vector<T> &A, int N) { vector<T> ret(N); rep(i, N) { ret[i] = rand(A); } return ret; } template <class T> vector<T> ranv_unique(vector<T> &A, int N) { vector<T> ret(N); umapi was; rep(j, N) { int i; while (1) { i = rand(sz(A) - 1); if (was.find(i) == was.end()) break; } ret[j] = A[i]; was[i] = 1; } return ret; } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } // 単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } // 便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class T> vector<pair<T, int>> run_length(vector<T> &a) { vector<pair<T, int>> ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } vector<pair<char, ll>> run_length(string &a) { vector<pair<char, ll>> ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); } } return ret; } template <class T, class F> T mgr(T ok, T ng, F f, int deb_ = 0) { bool han = true; if (deb_) { if (ok < ng) while (ng - ok > 1) { T mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else while (ok - ng > 1) { T mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } } else { if (ok < ng) while (ng - ok > 1) { T mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; } else while (ok - ng > 1) { T mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; } } return ok; } template <class T, class F> T mgr(signed ok, T ng, F f) { return mgr((T)ok, ng, f); } template <class T, class F> T mgr(T ok, signed ng, F f) { return mgr(ok, (T)ng, f); } //[l, r)の中で,f(i)がtrueとなる範囲を返す okはそこに含まれる template <class F> P mgr(int l, int r, F f, int ok) { if (f(ok) == 0) { out("f(ok) must true"); re(); } return mp(mgr(ok, l - 1, f), mgr(ok, r, f) + 1); } template <class F> dou mgrd(dou ok, dou ng, F f, int kai = 100) { bool han = true; if (ok < ng) rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } // エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } // getid(find())を返す 1次元にする template <typename W, typename T> int findi(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return i * sz(a[0]) + j; return -1; } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } vi count(vi &a) { int ma = 0; fora(v, a) { if (ma < v) ma = v; } vi res(ma + 1); fora(v, a) { res[v]++; } return res; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } //'a' = 'A' = 0 として集計 既に-'a'されていても動く vi count(str &a, int l, int r) { vi cou(26); char c = 'a'; if ('A' <= a[l] && a[l] <= 'Z') c = 'A'; if ('a' <= a[l] && a[l] <= 'z') c = 'a'; else c = 0; rep(i, l, r)++ cou[a[i] - c]; return cou; } vi count(str &a, int r = inf) { return count(a, 0, min(r, sz(a))); } #define couif count_if // algorythm ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template <class T> void rev(vector<T> &a) { reverse(all(a)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll getr(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 上から何桁目か ll getl(ll a, ll ket) { int sketa = keta(a); return getr(a, sketa - 1 - ket); } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } // 変換系 //[v] := iとなるようなvectorを返す // 存在しない物は-1 template <class T> auto keys(T a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> constexpr T min(T a, signed b) { return a < b ? a : b; } template <class T> constexpr T max(T a, signed b) { return a < b ? b : a; } template <class T> constexpr T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> constexpr T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<T>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> T mul(vector<T> &v, ll t = inf) { T ret = v[0]; rep(i, 1, min(t, sz(v))) ret *= v[i]; return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret // += sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // imoは0-indexed // ruiは1-indexed template <class T> vector<T> imo(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } // kと同じものの数 template <class T, class U> vi imo(vector<T> &a, U k) { vector<T> ret = a; rep(i, sz(ret)) ret[i] = a[i] == k; rep(i, sz(ret) - 1) ret[i + 1] += ret[i]; return ret; } template <class T> vector<T> imox(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i]; return ret; } // 漸化的に最小を持つ template <class T> vector<T> imi(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } template <class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chmi(ret[i - 1], ret[i]); return ret; } template <class T> vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, 1) chma(ret[i - 1], ret[i]); return ret; } template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN(T), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int ma = MIN(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -linf; ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX(T), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int mi = MAX(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = linf; ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ostream &operator<<(ostream &os, ruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } template <class T> vector<T> ruiv(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i]; return ret; } template <class T> ruiC<T> ruic() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return ret; } vector<ll> ruiv(string &a) { if (sz(a) == 0) return vi(1); ll dec = ('0' <= a[0] && a[0] <= '9') ? '0' : 0; vector<ll> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + a[i] - dec; return ret; } ruiC<ll> ruic(string &a) { vector<ll> ret = ruiv(a); return ruiC<ll>(ret); } // kと同じものの数 template <class T, class U> vi ruiv(T &a, U k) { vi ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k); return ret; } template <class T, class U> ruiC<ll> ruic(T &a, U k) { vi ret = ruiv(a, k); return ruiC<ll>(ret); } template <class T> struct ruiC2 { int H; vector<ruiC<T>> rui; ruiC2(vector<vector<T>> &ru) : rui(sz(ru)), H(sz(ru)) { for (int h = 0; h < H; h++) { rui[h] = ruic(ru[h]); } } // WについてHを返す vector<T> operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } vector<T> res(H); for (int h = 0; h < H; h++) res[h] = rui[h](l, r); return res; } // HについてWを返す ruiC<T> operator[](ll h) { assert(h < H); return rui[h]; } // vector<T> operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } /*0から順に追加される必要がある*/ // T back() { return rui.back(); } // ll size() { return rui.size(); } // auto begin(){return rui.begin();} // auto end(){return rui.end();} }; // a~zを0~25として // rui(l,r)でvector(26文字について, l~rのcの個数) // rui[h] ruic()を返す ruiC2<ll> ruicou(str &a) { str s = a; replace(s); vector<ruiC<ll>> res(26); vvi(cou, 26, sz(s)); rep(i, sz(s)) { cou[s[i]][i] = 1; } return ruiC2<ll>(cou); } ruiC2<ll> ruicou(vi &a) { int H = max(a) + 1; vector<ruiC<ll>> res(H); vvi(cou, H, sz(a)); rep(i, sz(a)) { cou[a[i]][i] = 1; } return ruiC2<ll>(cou); } /*@formatter:off*/ // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { vector<T> rui; ruixC(vector<T> &ru) : rui(ru) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruiXc "; deb(l, r); assert(0); } return rui[r] ^ rui[l]; } T operator[](ll i) { return rui[i]; } T back() { return rui.back(); } ll size() { return rui.size(); } }; template <class T> vector<T> ruix(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i]; return ret; } template <class T> ruixC<ll> ruixc(vector<T> &a) { vi ret = ruix(a); return ruixC<ll>(ret); } template <class T> vector<T> ruim(vector<T> &a) { vector<T> res(a.size() + 1, 1); rep(i, a.size()) res[i + 1] = res[i] * a[i]; return res; } // 漸化的に最小を1indexで持つ template <class T> vector<T> ruimi(vector<T> &a) { ll n = sz(a); vector<T> ret(n + 1); rep(i, 1, n) { ret[i] = a[i - 1]; chmi(ret[i + 1], ret[i]); } return ret; } // template<class T> T *rrui(vector<T> &a) { // 右から左にかけての半開区間 (-1 n-1] template <class T> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } // 掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } /*@formatter:off*/ template <class T> T *rruim(vector<T> &a) { ll len = a.size(); T *body = (T *)malloc((len + 1) * sizeof(T)); T *res = body + 1; res[len - 1] = 1; rer(i, len - 1) res[i - 1] = res[i] * a[i]; return res; } template <class T, class U> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U = int> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U = int> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool san(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return san(0, i, sz(a)) && san(0, j, sz(a)); } #define inside ins ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } // 添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } template <class T> T min(vector<vector<vector<T>>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, min(a[i])); return res; } template <class T> T max(vector<vector<vector<T>>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, max(a[i])); return res; } template <class T> T min(vector<vector<vector<vector<T>>>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, min(a[i])); return res; } template <class T> T max(vector<vector<vector<vector<T>>>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, max(a[i])); return res; } template <class T> T min(vector<vector<vector<vector<vector<T>>>>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, min(a[i])); return res; } template <class T> T max(vector<vector<vector<vector<vector<T>>>>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, max(a[i])); return res; } template <class T> T min(vector<vector<vector<vector<vector<vector<T>>>>>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, min(a[i])); return res; } template <class T> T max(vector<vector<vector<vector<vector<vector<T>>>>>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, max(a[i])); return res; } // pow周りの仕様 // powiを使うと整数型 // powbを使うとbint型 // powを使うと powlに変換され long doubleが返る #ifdef _DEBUG auto pow(ll a, ll k) { static bool was = 1; if (was) { message += "if integer use *powi* it's very fast\n"; } was = 0; return powl(a, k); } // 上のメッセージを出すための関数 auto pow(signed a, ll k) { return pow((ll)a, k); } auto pow(signed a, signed k) { return pow((ll)a, (ll)k); } #else #define pow powl #endif // 整数型のpow int powi(int a, int k) { if (a == 2) return 1ll << k; int res = 1; int x = a; while (k) { if (k & 1) res *= x; x *= x; k >>= 1; } return res; } // define pow powlより上に動かすとバグる bint pow(bint a, ll k) { bint res = 1; bint x = a; while (k) { if (k & 1) res *= x; x *= x; k >>= 1; } return res; } bint pow(bint a, signed k) { return pow(a, (ll)k); } bint powb(int a, int b) { return pow((bint)a, b); } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); // オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); // オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll bit(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 // bitsetに置き換える感覚 i が立っていたら i bit目を立てる ll bit_bool(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll // 最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } // 最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } // 初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) // aにある物をtrueとする vb bool_(vi &a, int n) { vb ret(max(max(a) + 1, n)); rep(i, sz(a)) ret[a[i]] = true; return ret; } char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } // 基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } // 要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) // 狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } // 存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class... T> void fin(T... s) { out(s...); exit(0); } // 便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll sig0(int t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } bint sig0(bint t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } // 第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // v * v >= aとなる最小のvを返す ll sqrt(ll a) { if (a < 0) { debugline("sqrt"); deb(a); ole(); } ll res = (ll)std::sqrt(a); while (res * res < a) ++res; return res; } double log(double e, double x) { return log(x) / log(e); } // b * res <= aを満たす [l, r)を返す P drange_ika(int a, int b) { assert(b); if (a >= 0) { if (b > 0) return mp(-linf, a / b + 1); else return mp(-(a / -b), linf + 1); } else { if (b > 0) return mp(-linf, -ceil(-a, b) + 1); else return mp(ceil(-a, -b), linf + 1); } } /*@formatter:off*/ // 機能拡張 #define dtie(a, b) \ int a, b; \ tie(a, b) template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> void operator+=(mset<T> &a, vector<T> &v) { for (auto &&u : v) a.insert(u); } template <class T> void operator+=(set<T> &a, vector<T> &v) { for (auto &&u : v) a.insert(u); } template <class T> void operator+=(vector<T> &a, set<T> &v) { for (auto &&u : v) a.emplace_back(u); } template <class T> void operator+=(vector<T> &a, mset<T> &v) { for (auto &&u : v) a.emplace_back(u); } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <typename T> void entry(vector<T> &v, unsigned ll s, unsigned ll e) { erase(v, e, sz(v)); erase(v, 0, s); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } template <class T> T poll(set<T> &S) { T ret = *S.begin(); S.erase(S.begin()); return ret; } template <class T> T poll(mset<T> &S) { T ret = *S.begin(); S.erase(S.begin()); return ret; } template <class T> T poll_back(set<T> &S) { T ret = *S.rbegin(); S.erase(S.rbegin()); return ret; } template <class T> T poll_back(mset<T> &S) { T ret = *S.rbegin(); S.erase(S.rbegin()); return ret; } template <class T> T peek(set<T> &S) { T ret = *S.begin(); return ret; } template <class T> T peek(mset<T> &S) { T ret = *S.begin(); return ret; } template <class T> T peek_back(set<T> &S) { T ret = *S.rbegin(); return ret; } template <class T> T peek_back(mset<T> &S) { T ret = *S.rbegin(); return ret; } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } ll stoi(string &s) { return stol(s); } void yn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; } void Yn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; } void YN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; } void fyn(bool a) { if (a) cout << "yes" << endl; else cout << "no" << endl; exit(0); } void fYn(bool a) { if (a) cout << "Yes" << endl; else cout << "No" << endl; exit(0); } void fYN(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; exit(0); } void fAb(bool a) { if (a) cout << "Alice" << endl; else cout << "Bob"; } void Possible(bool a) { if (a) cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { if (a) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } void fPossible(bool a) { Possible(a); exit(0); } void fPOSSIBLE(bool a) { POSSIBLE(a); exit(0); } template <typename T> class fixed_point : T { public: explicit constexpr fixed_point(T &&t) noexcept : T(std::forward<T>(t)) {} template <typename... Args> constexpr decltype(auto) operator()(Args &&...args) const { return T::operator()(*this, std::forward<Args>(args)...); } }; template <typename T> static inline constexpr decltype(auto) fix(T &&t) noexcept { return fixed_point<T>{std::forward<T>(t)}; } // 未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) { int N = sz(a); vector<vector<T>> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } // 長さn毎に分割する template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) { int N = sz(a); vector<vector<T>> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } vi inds_(vi &a) { int n = sz(a); vb was(n); vi res(n); rep(i, n) { assert(!was[a[i]]); res[a[i]] = i; was[a[i]] = true; } return res; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; #define pre prev #define nex next // gra mll pr // 上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] // 右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) // グラフ内で #undef getid // #define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) // #define set_shuffle() std::random_device seed_gen;std::mt19937 // engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), // engine); 1980 開始からtime ms経っていたらtrue bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う // 全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } /*@formatter:on*/ // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; /*@formatter:off*/ #undef getid #define type_id_graph #ifdef type_id_graph #define forg(gi, ve) \ for (ll gi = 0, forglim = ve.size(), f, t, c, ty, id; \ gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, \ ty = ve[gi].ty, id = ve[gi].id, true); \ ++gi) #define fort(gi, ve) \ for (ll gi = 0, f, t, c, ty, id, wasp = (p != -1 && ve[gi].t == p) ? 1 : 0; \ (wasp = wasp ? 1 \ : (p != -1 && ve[gi].t == p) ? 1 \ : 0, \ gi + wasp < ve.size()) && \ (tie(f, t, c, ty, id) = \ wasp ? make_tuple(ve[gi + 1].f, ve[gi + 1].t, ve[gi + 1].c, \ ve[gi + 1].ty, ve[gi + 1].id) \ : make_tuple(ve[gi].f, ve[gi].t, ve[gi].c, ve[gi].ty, \ ve[gi].id), \ true); \ ++gi) #define fore(gi, ve) \ for (ll gi = 0, forglim = ve.size(), f, t, c, ty, id; \ gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, \ ty = ve[gi].ty, id = ve[gi].id, true); \ ++gi) template <class T> struct edge_ { int f, t, ty, id; T c; edge_(int f, int t, T c = 1, int ty = -1, int id = -1) : f(f), t(t), c(c), ty(ty), id(id) {} bool operator<(const edge_ &b) const { return c < b.c; } bool operator>(const edge_ &b) const { return c > b.c; } }; template <class T> ostream &operator<<(ostream &os, edge_<T> &e) { os << e.f << " " << e.t << " " << e.c << " " << e.ty << " " << e.id; return os; } template <typename T> class graph { public: vector<vector<edge_<T>>> g; vector<edge_<T>> edges; int n; explicit graph(int n) : n(n) { g.resize(n); } void clear() { g.clear(), edges.clear(); } void resize(int n) { this->n = n; g.resize(n); } int size() { return n; } vector<edge_<T>> &operator[](int i) { return g[i]; } virtual void add(int f, int t, T c, int ty, int id) = 0; virtual void set_edges() = 0; }; template <typename T = ll> class digraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; explicit digraph(int n) : graph<T>(n) {} void add(int f, int t, T c = 1, int ty = -1, int eid = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digraph add"); deb(f, t, c); ole(); } int id = sz(edges); if (eid != -1) id = eid; g[f].emplace_back(f, t, c, ty, id); edges.emplace_back(f, t, c, ty, id); // edgesを使わないなら消せる } void ing(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); } } void set_edges() override { if (sz(edges)) return; rep(i, n) fora(e, g[i]) edges.push_back(e); } }; template <class T = int> class undigraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; explicit undigraph(int n) : graph<T>(n) {} // f < t void add(int f, int t, T c = 1, int ty = -1, int eid = -1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("undigraph add"); deb(f, t, c); ole(); } int id = sz(edges); if (eid != -1) id = eid; g[f].emplace_back(f, t, c, ty, id); g[t].emplace_back(t, f, c, ty, id); edges.emplace_back(f, t, c, ty, id); // edgesを使わないなら消せる edges.emplace_back(t, f, c, ty, id); } void add(edge_<T> &e) { int f = e.f, t = e.t, ty = e.ty, id = e.id; T c = e.c; add(f, t, c, ty, id); } void ing(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); } } void set_edges() override { if (sz(edges)) return; rep(i, n) fora(e, g[i]) edges.push_back(e); } }; #else #define forg(gi, ve) \ for (ll gi = 0, forglim = ve.size(), f, t, c; \ gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi) #define fort(gi, ve) \ for (ll gi = 0, f, t, c, wasp = (p != -1 && ve[gi].t == p) ? 1 : 0; \ (wasp = wasp ? 1 \ : (p != -1 && ve[gi].t == p) ? 1 \ : 0, \ gi + wasp < ve.size()) && \ (tie(f, t, c) = \ wasp ? make_tuple(ve[gi + 1].f, ve[gi + 1].t, ve[gi + 1].c) \ : make_tuple(ve[gi].f, ve[gi].t, ve[gi].c), \ true); \ ++gi) #define fore(gi, ve) \ for (ll gi = 0, forglim = ve.size(), f, t, c; \ gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi) template <class T> struct edge_ { int f, t; T c; edge_(int f, int t, T c = 1) : f(f), t(t), c(c) {} bool operator<(const edge_ &b) const { return c < b.c; } bool operator>(const edge_ &b) const { return c > b.c; } }; template <class T> ostream &operator<<(ostream &os, edge_<T> &e) { os << e.f << " " << e.t << " " << e.c; return os; } template <typename T> class graph { public: vector<vector<edge_<T>>> g; vector<edge_<T>> edges; int n; explicit graph(int n) : n(n) { g.resize(n); } void clear() { g.clear(), edges.clear(); } void resize(int n) { this->n = n; g.resize(n); } int size() { return n; } vector<edge_<T>> &operator[](int i) { return g[i]; } virtual void add(int f, int t, T c) = 0; virtual void set_edges() = 0; }; template <typename T = ll> class digraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; explicit digraph(int n) : graph<T>(n) {} void add(int f, int t, T c = 1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digraph add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); edges.emplace_back(f, t, c); // edgesを使わないなら消せる } void ing(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); } } void set_edges() override { if (sz(edges)) return; rep(i, n) fora(e, g[i]) edges.push_back(e); } }; template <class T = int> class undigraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; explicit undigraph(int n) : graph<T>(n) {} // f < t void add(int f, int t, T c = 1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("undigraph add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); g[t].emplace_back(t, f, c); edges.emplace_back(f, t, c); // edgesを使わないなら消せる edges.emplace_back(t, f, c); } void add(edge_<T> &e) { int f = e.f, t = e.t; T c = e.c; add(f, t, c); } void ing(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); } } void set_edges() override { if (sz(edges)) return; rep(i, n) fora(e, g[i]) edges.push_back(e); } }; #endif #define dijkstra_path dis_path template <class T> vi dis_path(digraph<T> &g, vector<T> &dis, int s, int t, int init_value) { assert(dis[t] != init_value); auto rg = rev(g); int now = t; vi path; path.push_back(now); T cost = 0; while (now != s) { rep(gi, sz(rg[now])) { int m = rg[now][gi].t; if (dis[m] == init_value) continue; if (dis[m] + rg[now][gi].c + cost == dis[t]) { cost += rg[now][gi].c; now = m; break; } } path.push_back(now); } rev(path); return path; } template <class T> vi dis_path(undigraph<T> &g, vector<T> &dis, int s, int t, int init_value) { assert(dis[t] != init_value); int now = t; vi path; path.push_back(now); T cost = 0; while (now != s) { rep(gi, sz(g[now])) { int m = g[now][gi].t; if (dis[m] == init_value) continue; if (dis[m] + g[now][gi].c + cost == dis[t]) { cost += g[now][gi].c; now = m; break; } } path.push_back(now); } rev(path); return path; } template <class T> vi dis_path(digraph<T> &g, vector<vector<T>> &dis, int s, int t, int init_value) { return dis_path(g, dis[s], s, t, init_value); } template <class T> vi dis_path(undigraph<T> &g, vector<vector<T>> &dis, int s, int t, int init_value) { return dis_path(g, dis[s], s, t, init_value); } // O(N^2) template <class T> vector<T> dijkstra_mitu(graph<T> &g, int s, int init_value) { if (!(0 <= s && s < g.n)) { debugline("dijkstra_mitu"); deb(s, g.n); ole(); } int n = g.n; int initValue = MAX(int); vector<T> dis(n, initValue); dis[s] = 0; vb used(n); while (true) { int v = -1; rep(u, n) { if (!used[u] && (v == -1 || dis[u] < dis[v])) v = u; } if (v == -1) break; if (dis[v] == initValue) break; used[v] = true; rep(u, sz(g[v])) { auto e = g[v][u]; dis[e.t] = min(dis[e.t], dis[v] + e.c); } } /*基本、たどり着かないなら-1*/ for (auto &&d : dis) { if (d == initValue) { d = init_value; } } return dis; } template <typename T> struct radixheap { vector<pair<u64, T>> v[65]; u64 size, last; radixheap() : size(0), last(0) {} bool empty() const { return size == 0; } int getbit(int a) { return a ? 64 - __builtin_clzll(a) : 0; } void push(u64 key, const T &value) { ++size; v[getbit(key ^ last)].emplace_back(key, value); } pair<u64, T> pop() { if (v[0].empty()) { int idx = 1; while (v[idx].empty()) ++idx; last = min_element(begin(v[idx]), end(v[idx]))->first; for (auto &p : v[idx]) v[getbit(p.first ^ last)].emplace_back(p); v[idx].clear(); } --size; auto ret = v[0].back(); v[0].pop_back(); return ret; } }; /*radix_heap こっちの方が早い*/ // O((N+M) log N) vi dijkstra(graph<int> &g, int s, int init_value) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } /*O((N+M) log N) vs O(N^2)*/ if ((g.n + sz(g.edges)) * log2(N) > g.n * g.n) { return dijkstra_mitu(g, s, init_value); } int initValue = MAX(int); vi dis(g.n, initValue); radixheap<int> q; dis[s] = 0; q.push(0, s); while (!q.empty()) { int nowc, i; tie(nowc, i) = q.pop(); if (dis[i] != nowc) continue; for (auto &&e : g.g[i]) { int to = e.t; int c = nowc + e.c; if (dis[to] > c) { dis[to] = c; q.push(dis[to], to); } } } /*基本、たどり着かないなら-1*/ for (auto &&d : dis) if (d == initValue) d = init_value; return dis; } template <class T> vector<T> dijkstra_normal(graph<T> &g, int s, int init_value) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } if ((g.n + sz(g.edges)) * 20 > g.n * g.n) { return dijkstra_mitu(g, s, init_value); } T initValue = MAX(T); vector<T> dis(g.n, initValue); priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q; dis[s] = 0; q.emplace(0, s); while (q.size()) { T nowc = q.top().fi; int i = q.top().se; q.pop(); if (dis[i] != nowc) continue; for (auto &&e : g.g[i]) { int to = e.t; T c = nowc + e.c; if (dis[to] > c) { dis[to] = c; q.emplace(dis[to], to); } } } /*基本、たどり着かないなら-1*/ for (auto &&d : dis) if (d == initValue) d = init_value; return dis; } template <class T> vector<T> dijkstra_01(graph<T> &g, int s, int init_value) { int N = g.n; vi dis(N, linf); dis[s] = 0; deque<int> q; q.push_back(s); vb was(N); while (!q.empty()) { int f = q.front(); q.pop_front(); if (was[f]) continue; was[f] = true; fora(e, g[f]) { if (dis[e.t] > dis[f] + e.c) { if (e.c) { dis[e.t] = dis[f] + 1; q.push_back(e.t); } else { dis[e.t] = dis[f]; q.push_front(e.t); } } } } rep(i, N) if (dis[i] == linf) dis[i] = init_value; return dis; } // dijkstra_cou<mint> : 数える型で書く return vp(dis,cou) template <class COU, class T = int> auto dijkstra_cou(const graph<T> &g, int s, int init_value) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } err("count by type COU "); err("int or mint"); T initValue = MAX(T); vector<T> dis(g.n, initValue); vector<COU> cou(g.n); cou[s] = 1; priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q; dis[s] = 0; q.emplace(0, s); while (q.size()) { T nowc = q.top().fi; int i = q.top().se; q.pop(); if (dis[i] != nowc) continue; for (auto &&e : g.g[i]) { int to = e.t; T c = nowc + e.c; if (dis[to] > c) { dis[to] = c; cou[to] = cou[e.f]; q.emplace(dis[to], to); } else if (dis[to] == c) { cou[to] += cou[e.f]; } } } /*基本、たどり着かないなら-1*/ for (auto &&d : dis) if (d == initValue) d = init_value; return vtop(dis, cou); } // 密グラフの時、warshallに投げる template <class T> vector<vector<T>> dijkstra_all(const graph<T> &g, int init_value) { int n = g.n; assert(n < 1e4); if (n * n < (n + sz(g.edges)) * 14) { /*O(N^3) vs O(N (N+M)log N)*/ return warshall(g, init_value); } vector<vector<T>> dis(n); rep(i, n) { dis[i] = dijkstra(g, i, init_value); } return dis; } // コストを無限に減らせる := -linf // たどり着けない := linf template <class T> vector<T> bell(graph<T> &g, int s) { if (g.n >= 1e4) { cout << "bell size too big" << endl; exit(0); } vector<T> res(g.n, linf); res[s] = 0; vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく */ fix([&](auto ds, int p, int i) -> void { if (can[i]) return; can[i] = true; forg(gi, g[i]) if (t != p) ds(i, t); })(-1, 0); vector<edge_<T>> es; fora(e, g.edges) { if (can[e.f]) es += e; } rep(i, g.n) { bool upd = false; fora(e, es) { if (res[e.f] != linf && res[e.t] > res[e.f] + e.c) { upd = true; res[e.t] = res[e.f] + e.c; } } if (!upd) break; } rep(i, g.n * 2) { bool upd = 0; fora(e, g.edges) { if (res[e.f] != linf && res[e.t] != -linf && res[e.t] > res[e.f] + e.c) { upd = 1; res[e.t] = -linf; } } if (!upd) break; } return res; } // コストを無限に増やせる := linf // たどり着けない := -linf template <class T> vector<T> bell_far(graph<T> &g, int s) { if (g.n >= 1e4) { cout << "bell_far size too big" << endl; exit(0); } vector<T> res(g.n, linf); res[s] = 0; vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく*/ fix([&](auto ds, int p, int i) -> void { if (can[i]) return; can[i] = true; forg(gi, g[i]) if (t != p) ds(i, t); })(-1, 0); vector<edge_<T>> es; fora(e, g.edges) { if (can[e.f]) es += e; } rep(i, g.n) { bool upd = false; fora(e, es) { if (res[e.f] != linf && res[e.t] > res[e.f] - e.c) { /*-c*/ upd = true; res[e.t] = res[e.f] - e.c; /*-c*/ } } if (!upd) break; } rep(i, g.n * 2) { bool upd = 0; fora(e, g.edges) { if (res[e.f] != linf && res[e.t] != -linf && res[e.t] > res[e.f] - e.c) { /*-c*/ upd = 1; res[e.t] = -linf; } } if (!upd) break; } rep(i, g.n) res[i] *= -1; return res; } template <class T> vector<vector<T>> warshall(graph<T> &g, int init_value) { int n = g.n; assert(n < 1e4); vector<vector<T>> dis(n, vector<T>(n, linf)); rep(i, n) fora(e, g.g[i]) { if (dis[e.f][e.t] > e.c) { dis[e.f][e.t] = e.c; } } rep(i, n) dis[i][i] = 0; rep(k, n) rep(i, n) rep(j, n) { if (dis[i][j] > dis[i][k] + dis[k][j]) { dis[i][j] = dis[i][k] + dis[k][j]; } } rep(i, n) rep(j, n) if (dis[i][j] == linf) dis[i][j] = init_value; return dis; } template <class T> class MinOp { public: T operator()(T a, T b) { return min(a, b); } }; template <typename OpFunc> struct SparseTable { OpFunc op; signed size; vector<signed> lg; vector<vector<pair<signed, signed>>> table; void init(vector<pair<signed, signed>> &array, OpFunc opfunc) { signed n = array.size(); op = opfunc; lg.assign(n + 1, 0); for (signed i = 1; i <= n; i++) { lg[i] = 31 - __builtin_clz(i); } table.assign(lg[n] + 1, array); for (signed i = 1; i <= lg[n]; i++) { for (signed j = 0; j < n; j++) { if (j + (1 << (i - 1)) < n) { table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]); } else { table[i][j] = table[i - 1][j]; } } } } pair<signed, signed> query(signed l, signed r) { assert(l < r); return op(table[lg[r - l]][l], table[lg[r - l]][r - (1 << lg[r - l])]); } }; struct PMORMQ { vector<signed> a; SparseTable<MinOp<pair<signed, signed>>> sparse_table; vector<vector<vector<signed>>> lookup_table; vector<signed> block_type; signed block_size, n_block; void init(vector<signed> &array) { a = array; signed n = a.size(); block_size = std::max(1, (31 - __builtin_clz(n)) / 2); while (n % block_size != 0) { a.push_back(a.back() + 1); n++; } n_block = n / block_size; vector<pair<signed, signed>> b(n_block, make_pair(INT_MAX, INT_MAX)); for (signed i = 0; i < n; i++) { b[i / block_size] = min(b[i / block_size], make_pair(a[i], i)); } sparse_table.init(b, MinOp<pair<signed, signed>>()); block_type.assign(n_block, 0); for (signed i = 0; i < n_block; i++) { signed cur = 0; for (signed j = 0; j < block_size - 1; j++) { signed ind = i * block_size + j; if (a[ind] < a[ind + 1]) { cur |= 1 << j; } } block_type[i] = cur; } lookup_table.assign( 1 << (block_size - 1), vector<vector<signed>>(block_size, vector<signed>(block_size + 1))); for (signed i = 0; i < (1 << (block_size - 1)); i++) { for (signed j = 0; j < block_size; j++) { signed res = 0; signed cur = 0; signed pos = j; for (signed k = j + 1; k <= block_size; k++) { lookup_table[i][j][k] = pos; if (i & (1 << (k - 1))) { cur++; } else { cur--; } if (res > cur) { pos = k; res = cur; } } } } } signed query(signed l, signed r) { assert(l < r); signed lb = l / block_size; signed rb = r / block_size; if (lb == rb) { return lb * block_size + lookup_table[block_type[lb]][l % block_size][r % block_size]; } signed pl = lb * block_size + lookup_table[block_type[lb]][l % block_size][block_size]; signed pr = rb * block_size + lookup_table[block_type[rb]][0][r % block_size]; signed pos = pl; if (r % block_size > 0 && a[pl] > a[pr]) { pos = pr; } if (lb + 1 == rb) { return pos; } signed spv = sparse_table.query(lb + 1, rb).second; if (a[pos] > a[spv]) { return spv; } return pos; } }; template <class T = int> class tree : public undigraph<T> { PMORMQ rmq; int cnt; vector<signed> id_, in; bool never = true; bool never_hld = true; void dfs(int x, int p, int d, int dis = 0) { id_[cnt] = x; par_[x] = p; rmq_dep.push_back(d); disv[x] = dis; in[x] = cnt++; forg(gi, g[x]) { if (t == p) { continue; } dfs(t, x, d + 1, dis + c); id_[cnt] = x; rmq_dep.push_back(d); cnt++; } } void precalc() { never = false; cnt = 0; rmq_dep.clear(); disv.assign(n, 0); in.assign(n, -1); id_.assign(2 * n - 1, -1); par_.assign(n, -1); dfs(root, -1, 0); rmq.init(rmq_dep); #ifdef _DEBUG if (n >= 100) return; cerr << "---tree---" << endl; rep(i, n) { if (!(i == root || sz(g[i]) > 1)) continue; cerr << i << " -> "; vi ts; forg(gi, g[i]) { if (t != par_[i]) ts.push_back(t); } rep(i, sz(ts) - 1) cerr << ts[i] << ", "; if (sz(ts)) cerr << ts.back() << endl; } cerr << endl; #endif } int pos; void hld_build() { never_hld = false; if (never) precalc(); g.resize(n); vid.resize(n, -1); head.resize(n); heavy.resize(n, -1); depth.resize(n); inv.resize(n); subl.resize(n); subr.resize(n); dfs(root, -1); t = 0; dfs_hld(root); #ifdef _DEBUG if (n >= 100) return; cerr << "---hld_index---" << endl; vi inds; rep(i, n) if (sz(g[i])) inds.push_back(i); rep(i, sz(inds)) { str s = tos(bel(inds[i])); cerr << std::right << std::setw(sz(s) + (i ? 1 : 0)) << inds[i]; } cerr << endl; rep(i, sz(inds)) { cerr << bel(inds[i]) << " "; } cerr << endl << endl; cerr << "---hld_edge_index---" << endl; fora(e, edges) { if (e.f <= e.t) cerr << e.f << "-" << e.t << " " << bel(e) << endl; } cerr << endl << endl; cerr << "!!query!! edge or not edge carefull!!" << endl; #endif } int dfs(int curr, int prev) { int sub = 1, max_sub = 0; heavy[curr] = -1; forg(i, g[curr]) { int next = t; if (next != prev) { depth[next] = depth[curr] + 1; int sub_next = dfs(next, curr); sub += sub_next; if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next; } } return sub; } int t = 0; #ifndef __CLION_IDE__ void dfs_hld(int v = 0) { vid[v] = subl[v] = t; t++; inv[subl[v]] = v; if (0 <= heavy[v]) { head[heavy[v]] = head[v]; dfs_hld(heavy[v]); } forg(i, g[v]) if (depth[v] < depth[t]) if (t != heavy[v]) { head[t] = t; dfs_hld(t); } subr[v] = t; } #endif //__CLION_IDE__ vector<signed> rmq_dep; vi par_, depth, disv; vi childs; vi par_id_; // 親への辺のidを持つ vvi(ids_); // 隣接で辺のidを持つ public: using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; int root; //(f,t)と(t,f)は同じidを持ち、[0,n-1]でadd順に割り振られる // 部分木の [左端、右端) index // 部分木の辺に加算する場合 // add(subl[i],subr[i],x) // add(sub[i],sub[i+1],-x) vector<int> vid, head, heavy, inv, subl, subr; tree(int n_, int root = 0) : undigraph<T>(n_), root(root) { n = n_; } void root_change(int roo) { root = roo; precalc(); } // 葉になりえない頂点を根にする void root_change_mid() { #ifdef _DEBUG message += "N>2 (mid)\n"; #endif assert(n > 2); rep(i, n) { if (sz(g[i]) > 1) { root_change(i); return; } } } int lca(int a, int b) { if (never) precalc(); int x = in[a]; int y = in[b]; if (x > y) { swap(x, y); } int pos = rmq.query(x, y + 1); return id_[pos]; } int dis(int a, int b) { if (never) precalc(); int x = in[a]; int y = in[b]; if (x > y) { swap(x, y); } int pos = rmq.query(x, y + 1); int p = id_[pos]; return disv[a] + disv[b] - disv[p] * 2; } int dep(int a) { if (never) precalc(); return disv[a]; } int par(int a) { if (never) precalc(); return par_[a]; } bool isleaf(int i) { if (never) precalc(); return (sz(g[i]) == 1 && i != root); } bool leaf(int i) { return isleaf(i); } vi child(int r) { vi res; res.push_back(r); queue<int> q; q.push(r); while (!q.empty()) { int i = q.front(); res.push_back(i); q.pop(); forg(gi, g[i]) { if (t != par(i)) q.push(t); } } return res; } vb child_ex(int r) { vb res(n); res[r] = true; queue<int> q; q.push(r); while (!q.empty()) { int i = q.front(); res[i] = true; q.pop(); forg(gi, g[i]) { if (t != par(i)) q.push(t); } } return res; } int dfs_count_subtree(int p, int i) { childs[i] = 1; fort(gi, g[i]) { childs[i] += dfs_count_subtree(i, t); } return childs[i]; } #define count_child count_subtree int count_subtree(int f) { if (sz(childs) == 0) { if (never) precalc(); childs.resize(n); dfs_count_subtree(-1, root); } return childs[f]; } // fからtの辺を切った時のtの大きさ int count_subtree(int f, int t) { if (par(f) == t) { return n - count_subtree(f); } else { return count_subtree(t); } } vi path(int a, int b) { vi res; for_each_l(a, b, [&](int i) { res.push_back(i); }); return res; } // idはedgesに使えない ↓edge(id)とする // pathに含まれる辺のid達を返す vi pathe(int a, int b) { #ifdef _DEBUG static bool was = 0; if (!was) message += "can't edges[id]. must edge(id)\n"; was = 1; #endif if (sz(par_id_) == 0) { ids_.resize(n); par_id_.resize(n); rep(i, 0, sz(edges), 2) { ids_[edges[i].f].emplace_back(i >> 1); ids_[edges[i].t].emplace_back(i >> 1); } if (never) precalc(); /*par_を呼ぶため*/ rep(i, n) { int pa = par_[i]; forg(gi, g[i]) { if (t == pa) { par_id_[i] = ids_[i][gi]; } } } } int u = lca(a, b); vi res; if (a != u) { do { res.emplace_back(par_id_[a]); a = par_[a]; } while (a != u); } vi rev; if (b != u) { do { rev.emplace_back(par_id_[b]); b = par_[b]; } while (b != u); } rer(i, sz(rev) - 1) { res.emplace_back(rev[i]); } return res; } // 親の辺のidを返す int par_id(int i) { if (sz(par_id_) == 0) { pathe(0, 0); } return par_id_[i]; } // fのi番目の辺のidを返す int ids(int f, int i) { if (sz(ids_) == 0) { pathe(0, 0); } return ids_[f][i]; } // idから辺を返す edge_<T> edge(int id) { return edges[id << 1]; } /*O(N) hldを使わず木を普通にたどる liteの意*/ void for_each_l(int u, int v, function<void(int)> fnode) { int r = lca(u, v); while (u != r) { fnode(u); u = par_[u]; } fnode(r); vi a; while (v != r) { a.push_back(v); v = par_[v]; } while (sz(a)) { fnode(a.back()); a.pop_back(); } } void for_each_edge_l(int u, int v, function<void(edge_<int> &)> fedge) { int r = lca(u, v); while (u != r) { forg(gi, g[u]) { if (t == par_[u]) { fedge(g[u][gi]); u = par_[u]; break; } } } vector<edge_<int>> qs; while (v != r) { forg(gi, g[v]) { if (t == par_[v]) { qs.push_back(g[v][gi]); v = par_[v]; break; } } } while (sz(qs)) { fedge(qs.back()); qs.pop_back(); } } // Fは半開 (u,v)は木の頂点 // 中ではhldの頂点を見るため、seg木のupdateはhldのindexで行なう void for_each_ /*[l,r)*/ (int u, int v, const function<void(int, int)> &f) { if (never_hld) hld_build(); while (1) { if (vid[u] > vid[v]) swap(u, v); int l = max(vid[head[v]], vid[u]); int r = vid[v] + 1; f(l, r); if (head[u] != head[v]) v = par_[head[v]]; else break; } } void for_each_edge /*[l,r) O(log(N)) 辺を頂点として扱っている 上と同じ感じで使える*/ (int u, int v, const function<void(int, int)> &f) { if (never_hld) hld_build(); while (1) { if (vid[u] > vid[v]) swap(u, v); if (head[u] != head[v]) { int l = vid[head[v]]; int r = vid[v] + 1; f(l, r); v = par_[head[v]]; } else { if (u != v) { int l = vid[u] + 1; int r = vid[v] + 1; f(l, r); } break; } } } int bel(int v) { /*hld内での頂点番号を返す*/ if (never_hld) hld_build(); return vid[v]; } // 下の頂点に辺のクエリを持たせている int bel( int f, int t) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/ if (never_hld) hld_build(); return depth[f] > depth[t] ? vid[f] : vid[t]; } int bel( edge_<T> & e) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/ if (never_hld) hld_build(); return depth[e.f] > depth[e.t] ? vid[e.f] : vid[e.t]; } template <class... U> int operator()(U... args) { return bel(args...); } // path l -> r += v template <class S> void seg_add(S &seg, int lhei, int rhei, int v) { for_each_(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); }); } template <class S> void seg_update(S &seg, int lhei, int rhei, int v) { for_each_(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); }); } template <class S> T seg_get(S &seg, int lhei, int rhei) { T res = seg.e; for_each_(lhei, rhei, [&](int l, int r) { res = seg.f(res, seg.get(l, r)); }); return res; } template <class S> void seg_add_edge(S &seg, int lhei, int rhei, int v) { for_each_edge(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); }); } template <class S> void seg_update_edge(S &seg, int lhei, int rhei, int v) { for_each_edge(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); }); } template <class S> T seg_get_edge(S &seg, int lhei, int rhei) { T res = seg.e; for_each_edge(lhei, rhei, [&](int l, int r) { res = seg.f(res, seg.get(l, r)); }); return res; } // 単体 edgeは上で処理できる template <class S> void seg_add(S &seg, int i, int v) { seg.add(bel(i), v); } template <class S> void seg_update(S &seg, int i, int v) { seg.update(bel(i), v); } template <class S> T seg_get(S &seg, int i) { return seg[i]; } template <class S> void seg_del(S &seg, int i) { seg.del(bel(i)); } // 部分木iに対するクエリ template <class S> void seg_add_sub(S &seg, int i, int v) { if (never_hld) hld_build(); seg.add(subl[i], subr[i], v); } template <class S> void seg_update_sub(S &seg, int i, int v) { if (never_hld) hld_build(); seg.update(subl[i], subr[i], v); } template <class S> T seg_get_sub(S &seg, int i, int v) { if (never_hld) hld_build(); return seg.get(subl[i], subr[i], v); } template <class S> void seg_add_sub_edge(S &seg, int i, int v) { if (never_hld) hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/ seg.add(subl[i] + 1, subr[i], v); } template <class S> void seg_update_sub_edge(S &seg, int i, int v) { if (never_hld) hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/ seg.update(subl[i] + 1, subr[i], v); } template <class S> T seg_get_sub_edge(S &seg, int i, int v) { if (never_hld) hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/ return seg.get(subl[i] + 1, subr[i], v); } }; // 辺が多いのでedgesを持たない // cost oo, ox, xo, xx 渡す template <class T = int> class grid_k6 : public undigraph<T> { vi costs; public: using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; int H, W; vector<vector<char>> ba; char wall; void add(int f, int t, T c = 1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("grid_k6 add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); g[t].emplace_back(t, f, c); // undigraphと違い、edgesを持たない } int getid(int h, int w) { assert(ins(h, w, H, W)); return W * h + w; } int getid(P p) { return getid(p.first, p.second); } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } int operator()(P p) { return getid(p); } // 辺は無い grid_k6(int H, int W) : H(H), W(W), undigraph<T>(H * W) {} grid_k6(vector<vector<char>> ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall) con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } /*o -> o, o -> x, x -> o, x-> x*/ grid_k6(vector<vector<char>> ba, int oo, int ox, int xo, int xx, char wall = '#') : H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])), costs({oo, ox, xo, xx}), ba(ba), wall(wall) { rep(h, H) { rep(w, W) { add(h, w, h + 1, w); add(h, w, h - 1, w); add(h, w, h, w + 1); add(h, w, h, w - 1); } } } void add(int fh, int fw, int th, int tw) { if (ins(fh, fw, H, W) && ins(th, tw, H, W)) { int cm = 0; if (ba[fh][fw] == wall) { cm += 2; } if (ba[th][tw] == wall) { cm++; } int f = getid(fh, fw); int t = getid(th, tw); g[f].emplace_back(f, t, costs[cm]); } } void set_edges() { rep(i, n) fora(e, g[i]) if (e.f < e.t) edges.push_back(e); } }; // 辺によりメモリを大量消費ためedgesを消している // 頂点10^6でメモリを190MB(制限の8割)使う // 左上から右下に移動できる template <class T = int> class digrid_k6 : public digraph<T> { public: using digraph<T>::g; using digraph<T>::n; using digraph<T>::edges; int H, W; void add(int f, int t, T c = 1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digrid_k6 add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); /*digraphと違いedgesを持たない*/ } int getid(int h, int w) { if (!ins(h, w, H, W)) return -1; return W * h + w; } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } digrid_k6(int H, int W) : H(H), W(W), digraph<T>(H * W) {} digrid_k6(vector<vector<char>> ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), digraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall) con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } void add(int fh, int fw, int th, int tw) { add(getid(fh, fw), getid(th, tw)); } void set_edges() { rep(i, n) fora(e, g[i]) edges.push_back(e); } }; // edgesを持たない // dijkstra(g,0)[t] とかける (t+n-1等とする必要はない) template <class T = int> class segdi : public digraph<T> { int getid(int k) { if (k >= len * 2 - 1) { return k; } else if (k < len - 1) { return k + len; } else { return k - len + 1; } } void add(int f, int t, T c = 1) { f = getid(f); t = getid(t); if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("segdi add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); /*digraphと違いedgesを持たない*/ } int mid; int len; public: using digraph<T>::g; using digraph<T>::n; using digraph<T>::edges; // 頂点が足りなくなったらresize segdi(int n_) : digraph<T>(1) { int nn = 1; while (nn < n_) nn *= 2; n_ = nn; len = n_; this->resize(n_ + (n_ - 1) * 2 + n_); mid = n_ + (n_ - 1) * 2; int ad = len * 2 - 1; rep(i, len - 1) { add(i, i * 2 + 1, 0); add(i, i * 2 + 2, 0); if (i * 2 + 1 >= len - 1) { add(i * 2 + 1, i + ad, 0); add(i * 2 + 2, i + ad, 0); } else { add(i * 2 + 1 + ad, i + ad, 0); add(i * 2 + 2 + ad, i + ad, 0); } } } void dfs(vi &list, int nl, int nr, int k, int l, int r) { if (r <= nl || nr <= l) return; if (l <= nl && nr <= r) { list += k; } else { dfs(list, nl, (nl + nr) / 2, k * 2 + 1, l, r); dfs(list, (nl + nr) / 2, nr, k * 2 + 2, l, r); } } void rekkyo(vi &list, int l, int r) { l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { list.push_back(l); } if (!(r & 1)) { list.push_back(r - 1); } l >>= 1; r = (r - 1) >> 1; } } // 半開 void add(int fl, int fr, int tl, int tr, int cost) { /*fは下側*/ int ad = 2 * len - 1; if (mid >= n) { this->resize(n + 100); } { int l = fl, r = fr; l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { if (l - len + 1 < 0) add(l + ad, mid, cost); else add(l, mid, cost); } if (!(r & 1)) { if (r - 1 - len + 1 < 0) add(r - 1 + ad, mid, cost); else add(r - 1, mid, cost); } l >>= 1; r = (r - 1) >> 1; } } { int l = tl, r = tr; l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { add(mid, l, 0); } if (!(r & 1)) { add(mid, r - 1, 0); } l >>= 1; r = (r - 1) >> 1; } } mid++; } }; // edgesを持たない // dijkstra(g,0)[t] とかける (t+n-1等とする必要はない) template <class T = int> class segun : public undigraph<T> { int getid(int k) { if (k >= len * 2 - 1) { return k; } else if (k < len - 1) { return k + len; } else { return k - len + 1; } } void add(int f, int t, T c = 1) { f = getid(f); t = getid(t); if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("segun add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); /*digraphと違いedgesを持たない*/ g[t].emplace_back(t, f, c); /*digraphと違いedgesを持たない*/ } int mid; int len; public: using digraph<T>::g; using digraph<T>::n; using digraph<T>::edges; // 頂点が足りなくなったらresize segun(int n_) : digraph<T>(1) { int nn = 1; while (nn < n_) nn *= 2; n_ = nn; len = n_; this->resize(n_ + (n_ - 1) * 2 + n_); mid = n_ + (n_ - 1) * 2; int ad = len * 2 - 1; rep(i, len - 1) { add(i, i * 2 + 1, 0); add(i, i * 2 + 2, 0); if (i * 2 + 1 >= len - 1) { add(i * 2 + 1, i + ad, 0); add(i * 2 + 2, i + ad, 0); } else { add(i * 2 + 1 + ad, i + ad, 0); add(i * 2 + 2 + ad, i + ad, 0); } } } void dfs(vi &list, int nl, int nr, int k, int l, int r) { if (r <= nl || nr <= l) return; if (l <= nl && nr <= r) { list += k; } else { dfs(list, nl, (nl + nr) / 2, k * 2 + 1, l, r); dfs(list, (nl + nr) / 2, nr, k * 2 + 2, l, r); } } void rekkyo(vi &list, int l, int r) { l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { list.push_back(l); } if (!(r & 1)) { list.push_back(r - 1); } l >>= 1; r = (r - 1) >> 1; } } // 半開 void add(int fl, int fr, int tl, int tr, int cost) { /*fは下側*/ int ad = 2 * len - 1; if (mid >= n) { this->resize(n + 100); } { int l = fl, r = fr; l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { if (l - len + 1 < 0) add(l + ad, mid, cost); else add(l, mid, cost); } if (!(r & 1)) { if (r - 1 - len + 1 < 0) add(r - 1 + ad, mid, cost); else add(r - 1, mid, cost); } l >>= 1; r = (r - 1) >> 1; } } { int l = tl, r = tr; l += len - 1; r += len - 1; while (l < r) { if (!(l & 1)) { add(mid, l, 0); } if (!(r & 1)) { add(mid, r - 1, 0); } l >>= 1; r = (r - 1) >> 1; } } mid++; } }; #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define o_getid(a, b, name, ...) name #define getid(...) o_getid(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define unionfind unionfind_graph struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; // 連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } template <class T> unionfind(graph<T> &g) : n(g.n), trees(g.n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } add(g); } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } template <class T> void add(graph<T> &g) { fora(e, g.edges) { unite(e.f, e.t); } } template <class T> void unite(graph<T> &g) { add(g); } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } // つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off //@出力 template <class T> ostream &operator<<(ostream &os, digraph<T> &g) { os << endl << g.n << " " << sz(g.edges) << endl; fore(gi, g.edges) { os << f << " " << t << " " << c << endl; } return os; } template <class T> ostream &operator<<(ostream &os, undigraph<T> &g) { os << endl << g.n << " " << sz(g.edges) / 2 << endl; fore(gi, g.edges) { if (f < t) os << f << " " << t << " " << c << endl; } return os; } //@判定 template <class T> bool nibu(graph<T> &g) { int size = 0; rep(i, g.n) size += sz(g.g[i]); if (size == 0) return true; unionfind uf(g.n * 2); rep(i, g.n) fora(e, g.g[i]) uf.unite(e.f, e.t + g.n), uf.unite(e.f + g.n, e.t); rep(i, g.n) if (uf.same(i, i + g.n)) return 0; return true; } // 頂点ではなく辺の数に依存した計算量 O(E) template <class T> bool nibu_sub(graph<T> &g) { umapi col; /*0なら無色 */ queue<int> q; /*色は01か11 */ fora(e, g.edegs) { /*fとtの色を持つか否かは同じ*/ if (col[e.f] == 0) q.push(e.f); while (!q.empty()) { int f = q.front(); q.pop(); int fc = col[f]; forg(gi, g[f]) { int &tc = col[t]; /*fcには色がある*/ if (fc == tc) return false; /*違う色*/ if (tc) continue; /*無色*/ tc = fc ^ 2; q.push(tc); } } } return true; } // 二部グラフを色分けした際の頂点数を返す template <class T> vp nibug(graph<T> &g) { vp cg; if (!nibu(g)) { debugline("nibu"); ole(); } int n = g.size(); vb was(n); queue<P> q; rep(i, n) { if (was[i]) continue; q.push(mp(i, 1)); was[i] = 1; int red = 0; int coun = 0; while (q.size()) { int now = q.front().fi; int col = q.front().se; red += col; coun++; q.pop(); forg(gi, g[now]) { if (was[t]) continue; q.push(mp(t, col ^ 1)); was[t] = 1; } } cg.push_back(mp(red, coun - red)); } return cg; } // 連結グラフが与えられる 閉路があるか template <class T> bool close(undigraph<T> &g) { int n = 0; int e = 0; rep(i, g.n) { if (sz(g[i])) n++; forg(gi, g[i]) { e++; } } return (e >> 1) >= n; } template <class T> bool close(undigraph<T> &g, int v) { unionfind uf(g.n); rep(i, g.n) { forg(gi, g[i]) { if (f < t) break; if (f == t && f == v) return true; if (uf.same(f, v) && uf.same(t, v)) return true; uf.unite(f, t); } } return false; } template <class T> bool close(digraph<T> &g) { vi res; return topo(res, g); } //@変形 // 条件(f!=0等 f,t,cが使える)を満たすsubgraphをg2に代入 #define sub_di(g, g2, zhouken) \ { \ g2.resize(g.n); \ fore(gi, g.edges) { \ if (zhouken) { \ g2.add(f, t, c); \ } \ } \ } #define sub_un(g, g2, zhouken) \ { \ g2.resize(g.n); \ fore(gi, g.edges) { \ bool ok = zhouken; /*片方がアウトなら駄目という扱い*/ \ swap(f, t); \ ok &= zhouken; \ if (ok && f < t) { \ g2.add(f, t, c); \ } \ } \ } // 誘導部分グラフ(両辺がVに含まれる辺を含む最大のグラフを返す) yuudou #define sub_di_guided(g, g2, V) \ { \ vi ex(max(V)); \ fora(v, V) { ex[v] = 1; } \ sub_di(g, g2, ex[f] && ex[t]); \ } #define sub_un_guided(g, g2, V) \ { \ vi ex(max(V)); \ fora(v, V) { ex[v] = 1; } \ sub_un(g, g2, ex[f] && ex[t]); \ } #define sub_tree sub_un // 閉路がなければtrue bool topo(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n) for (auto &&e : g[i]) nyu[e.t]++; queue<int> st; rep(i, n) if (nyu[i] == 0) st.push(i); while (st.size()) { int v = st.front(); st.pop(); res.push_back(v); fora(e, g[v]) if (--nyu[e.t] == 0) st.push(e.t); } return res.size() == n; } // 辞書順最小トポロジカルソート bool topos(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n) for (auto &&e : g[i]) nyu[e.t]++; /*小さい順*/ priority_queue<int, vector<int>, greater<int>> q; rep(i, n) if (nyu[i] == 0) q.push(i); while (q.size()) { int i = q.top(); q.pop(); res.push_back(i); fora(e, g[i]) if (--nyu[e.t] == 0) q.push(e.t); } return res.size() == n; } template <class T> digraph<T> rev(digraph<T> &g) { digraph<T> r(g.n); rep(i, g.n) { forg(gi, g[i]) { r.add(t, f, c); } } return r; } // lc,rcは子を持つ中で一番左、右 //(g,ind,l,r) template <class T> tree<T> get_bfs_tree(tree<T> &g, vi &ind, vi &l, vi &r) { if (sz(ind)) { cerr << "ind must be empty" << endl; exit(0); } int N = sz(g); ind.resize(N); l.resize(N, inf); r.resize(N, -1); tree<T> h(N); queue<P> q; q.emplace(-1, 0); int c = 0; while (sz(q)) { int p = q.front().first; int i = q.front().second; q.pop(); ind[i] = c; if (~p) chmi(l[ind[p]], c); if (~p) chma(r[ind[p]], c); c++; forg(gi, g[i]) { if (t != p) q.emplace(i, t); } } fora(e, g.edges) { if (e.f < e.t) { h.add(ind[e.f], ind[e.t], e.c); } } rep(i, N) { if (l[i] == inf) l[i] = -1; } return h; } // lc,rcは子を持つ中で一番左、右 // たとえばl[lc[x]は2段下の最左 //(g,ind,l,r,lc,rc) template <class T> tree<T> get_bfs_tree(tree<T> &g, vi &ind, vi &l, vi &r, vi &lc, vi &rc) { if (sz(ind)) { cerr << "ind must be empty" << endl; exit(0); } int N = sz(g); ind.resize(N); l.resize(N, inf); lc.resize(N, inf); r.resize(N, -1); rc.resize(N, -1); tree<T> h(N); queue<P> q; q.emplace(-1, 0); int c = 0; while (sz(q)) { int p = q.front().first; int i = q.front().second; q.pop(); ind[i] = c; if (~p) { chmi(l[ind[p]], c); chma(r[ind[p]], c); if (sz(g[i]) > 1) { chmi(lc[ind[p]], c); chma(rc[ind[p]], c); } } c++; forg(gi, g[i]) { if (t != p) q.emplace(i, t); } } fora(e, g.edges) { if (e.f < e.t) { h.add(ind[e.f], ind[e.t], e.c); } } rep(i, N) { if (l[i] == inf) l[i] = -1; if (lc[i] == inf) lc[i] = -1; } return h; } //@集計 template <class T> vi indegree(graph<T> &g) { vi ret(g.size()); rep(i, g.size()) { forg(gi, g[i]) { ret[t]++; } } return ret; } template <class T> vi outdegree(graph<T> &g) { vi ret(g.size()); rep(i, g.size()) { ret[i] = g[i].size(); } return ret; } #define kansetu articulation // private /*private*/ P farthest____(tree<> &E, int cur, int pre, int d, vi &D) { D[cur] = d; P r = {d, cur}; forg(gi, E[cur]) if (t != pre) { P v = farthest____(E, t, cur, d + 1, D); r = max(r, v); } return r; } // dagでなければ-1を返す int diameter(digraph<> &g) { vi per; if (!topo(per, g)) return -1; int n = g.n; vi dp(n, 1); fora(v, per) { forg(gi, g[v]) { chma(dp[t], dp[f] + 1); } } return max(dp); } // iから最も離れた距離 vi diameters(tree<> &E) { /* diameter,center*/ vi D[3]; D[0].resize(E.size()); D[1].resize(E.size()); auto v1 = farthest____(E, 0, 0, 0, D[0]); auto v2 = farthest____(E, v1.second, v1.second, 0, D[0]); farthest____(E, v2.second, v2.second, 0, D[1]); int i; rep(i, D[0].size()) D[2].push_back(max(D[0][i], D[1][i])); return D[2]; } // iに対応するjと距離 vp diameters_p(tree<> &E) { /* diameter,center*/ vector<int> D[3]; D[0].resize(E.size()); D[1].resize(E.size()); auto v1 = farthest____(E, 0, 0, 0, D[0]); auto v2 = farthest____(E, v1.second, v1.second, 0, D[0]); farthest____(E, v2.second, v2.second, 0, D[1]); int i; vp res(E.size()); rep(i, D[0].size()) { if (D[0][i] > D[1][i]) res[i] = mp(v1.second, D[0][i]); else res[i] = mp(v2.second, D[1][i]); } return res; } int diameter(tree<> &E) { vi d = diameters(E); return max(d); } // 最も離れた二点を返す P diameter_p(tree<> &E) { auto d = diameters_p(E); int dis = -1; int l = -1, r = -1; rep(i, sz(d)) { if (chma(dis, d[i].se)) { l = i; r = d[i].fi; } } return mp(l, r); } //@列挙 取得 // 閉路がある時linfを返す template <class T> int longest_path(digraph<T> &g) { vi top; if (!topo(top, g)) { return linf; } int n = sz(top); vi dp(n, 0); for (auto &&i : top) { forg(gi, g[i]) { chma(dp[t], dp[i] + 1); } } return max(dp); } template <class T> vi longest_path_v(digraph<T> &g) { vi top; if (!topo(top, g)) { return vi(); } int n = sz(top); vi dp(n, 0); vi pre(n, -1); for (auto &&i : top) { forg(gi, g[i]) { if (chma(dp[t], dp[i] + 1)) { pre[t] = i; } } } int s = std::max_element(dp.begin(), dp.end()) - dp.begin(); vi path; while (s != -1) { path.push_back(s); s = pre[s]; } std::reverse(path.begin(), path.end()); return path; } // 橋を列挙する (取り除くと連結でなくなる辺) template <class T> vp bridge(graph<T> &G) { static bool was; vp brid; vi articulation; vi ord(G.n), low(G.n); vb vis(G.n); function<void(int, int, int)> dfs = [&](int v, int p, int k) { vis[v] = true; ord[v] = k++; low[v] = ord[v]; bool isArticulation = false; int ct = 0; for (int i = 0; i < G[v].size(); i++) { if (!vis[G[v][i].t]) { ct++; dfs(G[v][i].t, v, k); low[v] = min(low[v], low[G[v][i].t]); if (~p && ord[v] <= low[G[v][i].t]) isArticulation = true; if (ord[v] < low[G[v][i].t]) brid.push_back(make_pair(min(v, G[v][i].t), max(v, G[v][i].t))); } else if (G[v][i].t != p) { low[v] = min(low[v], ord[G[v][i].t]); } } if (p == -1 && ct > 1) isArticulation = true; if (isArticulation) articulation.push_back(v); }; int k = 0; rep(i, G.n) { if (!vis[i]) dfs(i, -1, k); } sort(brid.begin(), brid.end()); return brid; } // 間接点を列挙する (取り除くと連結でなくなる点) template <class T> vi articulation(undigraph<T> &G) { static bool was; vp bridge; vi arti; vi ord(G.n), low(G.n); vb vis(G.n); function<void(int, int, int)> dfs = [&](int v, int p, int k) { vis[v] = true; ord[v] = k++; low[v] = ord[v]; bool isArticulation = false; int ct = 0; for (int i = 0; i < G[v].size(); i++) { if (!vis[G[v][i].t]) { ct++; dfs(G[v][i].t, v, k); low[v] = min(low[v], low[G[v][i].t]); if (~p && ord[v] <= low[G[v][i].t]) isArticulation = true; if (ord[v] < low[G[v][i].t]) bridge.push_back(make_pair(min(v, G[v][i].t), max(v, G[v][i].t))); } else if (G[v][i].t != p) { low[v] = min(low[v], ord[G[v][i].t]); } } if (p == -1 && ct > 1) isArticulation = true; if (isArticulation) arti.push_back(v); }; int k = 0; rep(i, G.n) { if (!vis[i]) dfs(i, -1, k); } sort(arti.begin(), arti.end()); return arti; } // 閉路パスを一つ返す vi close_path(digraph<> &g) { int n = g.n; vi state(n); vi path; rep(i, n) if (!state[i]) { if (fix([&](auto dfs, int v) -> bool { if (state[v]) { if (state[v] == 1) { path.erase(path.begin(), find(path.begin(), path.end(), v)); return true; } return false; } path.push_back(v); state[v] = 1; forg(gi, g[v]) { if (dfs(t)) return true; } state[v] = -1; path.pop_back(); return false; })(i)) { return path; } } return vi(); } #ifdef type_id_graph /*@formatter:on*/ vi close_path_id(digraph<> &g) { auto D = close_path(g); if (sz(D) == 0) return vi(); D += D[0]; vi ret; int cur = D[0]; rep(i, sz(D) - 1) { forg(gi, g[D[i]]) { if (t == D[i + 1]) { ret += id; break; } } } return ret; // int n = g.n; // vi state(sz(g.edges)); // vi path; // rep(i, n) if (!state[i]) { // if (fix([&](auto dfs, int v) -> bool { // forg(gi, g[v]) { // if (state[id]) { // if (state[id] == 1) { // path.erase(path.begin(), find(path.begin(), // path.end(), id)); return true; // } // return false; // } // path.push_back(id); // state[id] = 1; // if (dfs(t)) { // return true; // } // state[v] = -1; // path.pop_back(); // } // return false; // })(i)) { return path; } // } // return vi(); } /*@formatter:off*/ #endif vi close_path_min(digraph<> &g) { int n = g.n; vvi(dis, n); rep(i, n) dis[i] = dijkstra(g, i, linf); int mind = linf; int f = 0, t = 0; rep(i, n) { rep(j, n) { if (i == j) continue; if (chmi(mind, dis[i][j] + dis[j][i])) { f = i; t = j; } } } vi path; auto add = [&](int f, int t) { int now = f; while (now != t) { rep(i, n) { if (dis[now][i] == 1 && dis[f][i] + dis[i][t] == dis[f][t]) { path.push_back(i); now = i; break; } } } }; add(f, t); add(t, f); return path; } // iを含む最短閉路 https://atcoder.jp/contests/abc022/tasks/abc022_c /*閉路が1つしかない場合、その閉路に含まれる頂点を1としたvectorを返す*/; template <class T> vi get_close1(digraph<T> &g) { int n = g.n; queue<int> q; vi d = outdegree(g); vi res(n, 1); rep(i, n) { if (d[i] == 0) { q += i; res[i] = 0; } } auto rg = rev(g); while (q.size()) { auto now = q.front(); q.pop(); forg(gi, rg[now]) { if (--d[t] == 0) { q += t; res[t] = 0; } } } return res; } //@アルゴリズム template <class T> int krus(undigraph<T> &g) { int res = 0; unionfind uf(g.n); if (sz(g.edges) == 0) g.set_edges(); int i = 0; auto E = g.edges; sort(E); fora(e, E) { if (uf.unite(e.f, e.t)) { res += e.c; } } return res; } template <class T> vector<edge_<T>> krus_ed(undigraph<T> &g) { unionfind uf(g.n); if (sz(g.edges) == 0) g.set_edges(); int i = 0; auto E = g.edges; sort(E); vector<edge_<T>> res; fora(e, E) { if (uf.unite(e.f, e.t)) { res.push_back(e); } } return res; } template <class T> tree<T> krus_tr(undigraph<T> &g) { tree<T> res(g.n); unionfind uf(g.n); if (sz(g.edges) == 0) g.set_edges(); int i = 0; auto E = g.edges; sort(E); fora(e, E) { if (uf.unite(e.f, e.t)) { res.add(e.f, e.t); } } return res; } template <class T> int krus(vector<edge_<T>> &g) { int n = 0; fora(e, g) { chma(n, max(e.f, e.t) + 1); }; int res = 0; unionfind uf(n); auto E = g; sort(E); fora(e, E) { if (uf.unite(e.f, e.t)) { res += e.c; } } return res; } template <class T> vector<edge_<T>> krus_ed(vector<edge_<T>> &g) { int n = 0; fora(e, g) { chma(n, max(e.f, e.t) + 1); }; vector<edge_<T>> res; unionfind uf(n); auto E = g; sort(E); fora(e, E) { if (uf.unite(e.f, e.t)) { res += e; } } return res; }; template <class T> vi krus_i(vector<edge_<T>> &g) { int n = 0; fora(e, g) { chma(n, max(e.f, e.t) + 1); }; vi res; unionfind uf(n); auto E = g; sort(E); int i = 0; fora(e, E) { if (uf.unite(e.f, e.t)) { res += i; } i++; } return res; } //@実験 digraph<> rang_di(int n, int m, bool zibun = 0, bool taju = 0) { umapp was; digraph<> g(n); was[mp(-1, -2)] = 1; while (m) { int f = -1, t = -2; while (f < 0 || (!taju && was[mp(f, t)])) { f = rand(0, n - 1); t = rand(0, n - 1); if (!zibun && f == t) f = -1; } g.add(f, t); was[mp(f, t)] = 1; m--; } return g; } digraph<> perfect_di(int n, bool zibun = 0) { digraph<> g(n); rep(i, n) { rep(j, n) { if (!zibun && i == j) con; g.add(i, j); } } return g; } undigraph<> rang_un(int n, int m, bool zibun = 0, bool taju = 0) { umapp was; undigraph<> g(n); was[mp(-1, -2)] = 1; while (m) { int f = -1, t = -2; while (f < 0 || (!taju && was[mp(min(f, t), max(f, t))])) { f = rand(0, n - 1); t = rand(0, n - 1); if (!zibun && f == t) f = -1; } g.add(f, t); was[mp(min(f, t), max(f, t))] = 1; m--; } return g; } undigraph<> perfect_un(int n, bool zibun = 0) { undigraph<> g(n); rep(i, n) { rep(j, i, n) { if (!zibun && i == j) con; g.add(i, j); } } return g; } /*頂点数がkの木を一つ返す サイズが0の木が帰ったら終了*/ tree<int> next_tree(int k) { assert(2 <= k && k < 11); static str name; static ifstream ina; static int rem; static vp edges; static int pk = -1; /*前回見たk*/ if (pk != k) { if (~pk) ina.close(); edges.clear(); pk = k; name = (k == 6) ? "C:\\Users\\kaout\\Desktop\\trees_sizek\\nazeka6.txt" : "C:\\Users\\kaout\\Desktop\\trees_sizek\\tree_size" + tos(k) + ".txt"; ina = ifstream(name); rem = pow(k, k - 2); /*Cayleyの定理*/ rep(i, k) rep(j, i + 1, k) edges.emplace_back(i, j); pk = k; } tree<int> g(k); if (rem == 0) { g.resize(0); return g; } int m; ina >> m; while (m) { int lb = lbit(m); int id = log2(lb); g.add(edges[id].first, edges[id].second); m ^= lb; } rem--; return g; } undigraph<int> next_undi(int k) { assert(2 <= k && k < 9); static str name; static ifstream ina; static int rem; static vp edges; static vi lims = {-1, -1, 1, 4, 38, 728, 26704, 1866256}; static int pk = -1; /*前回見たk*/ if (pk != k) { if (~pk) ina.close(); edges.clear(); pk = k; name = (k == 6) ? "C:\\Users\\kaout\\Desktop\\undi_sizek\\roku.txt" : "C:\\Users\\kaout\\Desktop\\undi_sizek\\undi_size" + tos(k) + ".txt"; ina = ifstream(name); rem = lims[k]; rep(i, k) rep(j, i + 1, k) edges.emplace_back(i, j); pk = k; } undigraph<int> g(k); if (rem == 0) { g.resize(0); return g; } int m; ina >> m; while (m) { int lb = lbit(m); int id = log2(lb); g.add(edges[id].first, edges[id].second); m ^= lb; } rem--; return g; } vector<tree<int>> trees(int k) { vector<tree<int>> res; while (1) { tree<int> g = next_tree(k); if (sz(g) == 0) break; res.push_back(g); } return res; } vector<undigraph<int>> undis(int k) { vector<undigraph<int>> res; while (1) { undigraph<int> g = next_undi(k); if (sz(g) == 0) break; res.push_back(g); } return res; } template <class T> vector<vector<int>> table(graph<T> &g, int init_value) { vvi(res, g.n, g.n, init_value); rep(i, g.n) { forg(gi, g[i]) { res[i][t] = c; } } return res; } #ifdef type_id_graph template <class T> vector<vector<edge_<T>>> type_list(digraph<T> &g) { vector<vector<edge_<T>>> res; rep(i, g.n) { forg(gi, g[i]) { res[ty].push_back(g[i][gi]); } } return res; } template <class T> vector<vector<edge_<T>>> type_list(undigraph<T> &g, int types = -1) { int tn = types; if (types == -1) tn = g.n; rep(i, g.n) { forg(gi, g[i]) { chma(tn, ty); } } vector<vector<edge_<T>>> res(tn + 1); vi was(g.n); rep(i, g.n) { forg(gi, g[i]) { if (f < t) res[ty].push_back(g[i][gi]); else if (f == t && !was[f]) { res[ty].push_back(g[i][gi]); was[f] = 1; } } } return res; } // idは 00 11 22のようにedgesに持たれている template <class T> vi krus_id(undigraph<T> &g) { unionfind uf(g.n); if (sz(g.edges) == 0) g.set_edges(); int i = 0; auto E = g.edges; sort(E); vi res; fora(e, E) { if (uf.unite(e.f, e.t)) { res.push_back(e.id); } } return res; } // graの方が早い #endif // type,idが使いたい場合はgraty /*@formatter:on*/ void solve() { in(N, M); digraph<> g(2 * k5); ; g.ing(N, M); if (M == 0) { fin(-1); } auto E = close_path_id(g); if (sz(E) == 0) { fin(-1); } auto cut = [&]() { vb ee = bool_(E, M); vi ev(N); vi V; fora(e, E) { /*@formatter:off*/ fora_init(e, E); /*@formatter:on*/ int f = g.edges[e].f, t = g.edges[e].t; ev[f] = 1; ev[t] = 1; V += f; } fore(gi, g.edges) { if (ev[f] && ev[t] && !ee[id]) { int a = find(V, f); int b = find(V, t); if (a < b) { erase(E, a, b); insert(E, a, id); } else { entry(E, b, a); E += id; } return true; } } return false; }; while (cut()) ; vi ev(N); fora(e, E) { /*@formatter:off*/ fora_init(e, E); /*@formatter:on*/ int f = g.edges[e].f, t = g.edges[e].t; ev[f] = 1; ev[t] = 1; } rep(i, N) { if (ev[i] == 0) con; int cou = 0; forg(gi, g[i]) { cou += ev[f] && ev[t]; } if (cou != 1) fin(-1); } out(sz(E)); fora(e, E) { /*@formatter:off*/ fora_init(e, E); /*@formatter:on*/ out(g.edges[e].f + 1); } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } if (sz(message)) { cerr << "****************************" << endl; cerr << message << endl; cerr << "****************************" << endl; } #endif return 0; };
insert
7,760
7,760
7,760
7,762
0
p02902
C++
Time Limit Exceeded
#define _USE_MATH_DEFINES #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef pair<long long int, long long int> P; typedef tuple<int, int, int> T; long long int INF = 1e18; long long int MOD = 1e9 + 7; #define MAX_V 11000 int V; vector<int> G[MAX_V]; // グラフの隣接リスト表現 vector<int> rG[MAX_V]; // 辺の向きを逆にしたグラフ vector<int> vs; // 帰りがけ順の並び bool used[MAX_V]; // すでに調べたか int cmp[MAX_V]; // 属する強連結成分のトポロジカル順序 // from から to への辺を張る関数 void add_edge(int from, int to) { G[from].push_back(to); rG[to].push_back(from); } void dfs(int v) { used[v] = true; for (int i = 0; i < G[v].size(); i++) { if (!used[G[v][i]]) { dfs(G[v][i]); } } vs.push_back(v); } void rdfs(int v, int k) { used[v] = true; cmp[v] = k; for (int i = 0; i < rG[v].size(); i++) { if (!used[rG[v][i]]) { rdfs(rG[v][i], k); } } } // 強連結成分分解を行う関数、返り値は強連結成分の個数 int scc() { memset(used, 0, sizeof(used)); vs.clear(); for (int v = 0; v < V; v++) { if (!used[v]) { dfs(v); } } memset(used, 0, sizeof(used)); int k = 0; for (int i = vs.size() - 1; i >= 0; i--) { if (!used[vs[i]]) { rdfs(vs[i], k++); } } return k; } vector<int> vert[1100]; vector<int> ans; bool inUse[MAX_V]; bool func(bool flag, int fst, int pos) { if (flag && fst == pos) { ans.push_back(pos); return true; } if (used[pos]) { return false; } used[pos] = true; for (int to : G[pos]) { if (inUse[to]) { bool ret = func(true, fst, to); if (ret) { ans.push_back(pos); return true; } } } return false; } int main() { int M; cin >> V >> M; for (int i = 0; i < M; i++) { int A, B; cin >> A >> B; add_edge(A - 1, B - 1); } int cnt = scc(); if (cnt == V) { cout << -1 << endl; return 0; } for (int i = 0; i < V; i++) { vert[cmp[i]].push_back(i); } for (int loop = 0; loop < cnt; loop++) { if (vert[loop].size() >= 2) { while (true) { bool flag = true; memset(used, 0, sizeof(used)); memset(inUse, 0, sizeof(inUse)); for (int pos : vert[loop]) { inUse[pos] = true; } ans.clear(); func(false, vert[loop][0], vert[loop][0]); reverse(ans.begin(), ans.end()); /* for(int i = 0; i < ans.size(); i++){ cout << ans[i] << " "; }cout << endl; */ for (int i = 0; i < ans.size() - 1; i++) { int pos = ans[i]; for (int to : G[pos]) { if (inUse[to] && to != ans[i + 1]) { flag = false; vert[loop].clear(); vert[loop].push_back(pos); ans.pop_back(); int sz = ans.size(); int j = i; while (ans[j % sz] != to) { j += 1; } while (ans[j % sz] != pos) { vert[loop].push_back(ans[j % sz]); j += 1; } break; } } if (!flag) { break; } } if (flag) { break; } } break; } } ans.pop_back(); cout << ans.size() << endl; for (int pos : ans) { cout << pos + 1 << endl; } return 0; }
#define _USE_MATH_DEFINES #include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef pair<long long int, long long int> P; typedef tuple<int, int, int> T; long long int INF = 1e18; long long int MOD = 1e9 + 7; #define MAX_V 11000 int V; vector<int> G[MAX_V]; // グラフの隣接リスト表現 vector<int> rG[MAX_V]; // 辺の向きを逆にしたグラフ vector<int> vs; // 帰りがけ順の並び bool used[MAX_V]; // すでに調べたか int cmp[MAX_V]; // 属する強連結成分のトポロジカル順序 // from から to への辺を張る関数 void add_edge(int from, int to) { G[from].push_back(to); rG[to].push_back(from); } void dfs(int v) { used[v] = true; for (int i = 0; i < G[v].size(); i++) { if (!used[G[v][i]]) { dfs(G[v][i]); } } vs.push_back(v); } void rdfs(int v, int k) { used[v] = true; cmp[v] = k; for (int i = 0; i < rG[v].size(); i++) { if (!used[rG[v][i]]) { rdfs(rG[v][i], k); } } } // 強連結成分分解を行う関数、返り値は強連結成分の個数 int scc() { memset(used, 0, sizeof(used)); vs.clear(); for (int v = 0; v < V; v++) { if (!used[v]) { dfs(v); } } memset(used, 0, sizeof(used)); int k = 0; for (int i = vs.size() - 1; i >= 0; i--) { if (!used[vs[i]]) { rdfs(vs[i], k++); } } return k; } vector<int> vert[1100]; vector<int> ans; bool inUse[MAX_V]; bool func(bool flag, int fst, int pos) { if (flag && fst == pos) { ans.push_back(pos); return true; } if (used[pos]) { return false; } used[pos] = true; for (int to : G[pos]) { if (inUse[to]) { bool ret = func(true, fst, to); if (ret) { ans.push_back(pos); return true; } } } return false; } int main() { int M; cin >> V >> M; for (int i = 0; i < M; i++) { int A, B; cin >> A >> B; add_edge(A - 1, B - 1); } int cnt = scc(); if (cnt == V) { cout << -1 << endl; return 0; } for (int i = 0; i < V; i++) { vert[cmp[i]].push_back(i); } for (int loop = 0; loop < cnt; loop++) { if (vert[loop].size() >= 2) { while (true) { bool flag = true; memset(used, 0, sizeof(used)); memset(inUse, 0, sizeof(inUse)); for (int pos : vert[loop]) { inUse[pos] = true; } ans.clear(); func(false, vert[loop][0], vert[loop][0]); reverse(ans.begin(), ans.end()); memset(inUse, 0, sizeof(inUse)); for (int pos : ans) { inUse[pos] = true; } /* for(int i = 0; i < ans.size(); i++){ cout << ans[i] << " "; }cout << endl; */ for (int i = 0; i < ans.size() - 1; i++) { int pos = ans[i]; for (int to : G[pos]) { if (inUse[to] && to != ans[i + 1]) { flag = false; vert[loop].clear(); vert[loop].push_back(pos); ans.pop_back(); int sz = ans.size(); int j = i; while (ans[j % sz] != to) { j += 1; } while (ans[j % sz] != pos) { vert[loop].push_back(ans[j % sz]); j += 1; } break; } } if (!flag) { break; } } if (flag) { break; } } break; } } ans.pop_back(); cout << ans.size() << endl; for (int pos : ans) { cout << pos + 1 << endl; } return 0; }
insert
132
132
132
136
TLE
p02902
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <queue> #include <stack> #include <vector> using namespace std; using P = pair<int, int>; vector<vector<int>> edge; stack<int> hist; vector<bool> seen; bool dfs(int p, int t) { for (auto nt : edge[t]) { if (nt == p) { return true; } else if (seen[nt]) continue; hist.push(nt); seen[nt] = true; bool ans = dfs(p, nt); if (ans) return true; hist.pop(); } return false; } int main() { int n, m; cin >> n >> m; edge.resize(n + 1); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; edge[a].push_back(b); } vector<int> cp; for (int p = 1; p <= n; p++) { seen.assign(n + 1, false); while (!hist.empty()) hist.pop(); hist.push(p); seen[p] = true; bool ok = dfs(p, p); if (ok) { while (!hist.empty()) { cp.push_back(hist.top()); hist.pop(); } reverse(cp.begin(), cp.end()); break; } } if (cp.empty()) { cout << -1 << endl; return 0; } vector<int> path(n + 1, -1); for (int i = 0; i < cp.size(); i++) { path[cp[i]] = i; } while (true) { bool cut = false; for (int i = 0; i < cp.size(); i++) { for (auto nt : edge[cp[i]]) { if (path[nt] != (i + 1) % cp.size() && path[nt] > 0) { cut = true; int from = i; int to = path[nt]; vector<int> ncp; if (from < to) { for (int j = to; j < cp.size(); j++) { ncp.push_back(cp[j]); } for (int j = 0; j <= from; j++) { ncp.push_back(cp[j]); } } else { for (int j = to; j <= from; j++) { ncp.push_back(cp[j]); } } cp = ncp; break; } } if (cut) break; } if (!cut) break; } cout << cp.size() << endl; for (auto p : cp) cout << p << endl; return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <stack> #include <vector> using namespace std; using P = pair<int, int>; vector<vector<int>> edge; stack<int> hist; vector<bool> seen; bool dfs(int p, int t) { for (auto nt : edge[t]) { if (nt == p) { return true; } else if (seen[nt]) continue; hist.push(nt); seen[nt] = true; bool ans = dfs(p, nt); if (ans) return true; hist.pop(); } return false; } int main() { int n, m; cin >> n >> m; edge.resize(n + 1); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; edge[a].push_back(b); } vector<int> cp; for (int p = 1; p <= n; p++) { seen.assign(n + 1, false); while (!hist.empty()) hist.pop(); hist.push(p); seen[p] = true; bool ok = dfs(p, p); if (ok) { while (!hist.empty()) { cp.push_back(hist.top()); hist.pop(); } reverse(cp.begin(), cp.end()); break; } } if (cp.empty()) { cout << -1 << endl; return 0; } vector<int> path(n + 1, -1); for (int i = 0; i < cp.size(); i++) { path[cp[i]] = i; } while (true) { bool cut = false; path.assign(n + 1, -1); for (int i = 0; i < cp.size(); i++) { path[cp[i]] = i; } for (int i = 0; i < cp.size(); i++) { for (auto nt : edge[cp[i]]) { if (path[nt] != (i + 1) % cp.size() && path[nt] > 0) { cut = true; int from = i; int to = path[nt]; vector<int> ncp; if (from < to) { for (int j = to; j < cp.size(); j++) { ncp.push_back(cp[j]); } for (int j = 0; j <= from; j++) { ncp.push_back(cp[j]); } } else { for (int j = to; j <= from; j++) { ncp.push_back(cp[j]); } } cp = ncp; break; } } if (cut) break; } if (!cut) break; } cout << cp.size() << endl; for (auto p : cp) cout << p << endl; return 0; }
insert
68
68
68
72
TLE
p02902
C++
Runtime Error
#include <cstdio> #include <iostream> const int maxn = 1e3 + 10; struct edge { int to, next; } map[maxn]; int n, m, stk[maxn], top, val, ans[maxn], tp, nxt[maxn]; int head[maxn], cnt; bool vis[maxn], flag, tag[maxn]; inline void add(int x, int y) { map[++cnt] = (edge){y, head[x]}; head[x] = cnt; return; } void dfs(int x) { stk[++top] = x, vis[x] = tag[x] = true; for (register int i = head[x]; i; i = map[i].next) { int y = map[i].to; if (!vis[y]) dfs(y); else if (tag[y]) { val = y, flag = true; } if (flag) return; } top--, vis[x] = false; return; } int main() { scanf("%d%d", &n, &m); for (register int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); add(x, y); } for (register int i = 1; i <= n; i++) { if (!tag[i]) { dfs(i); if (flag) break; } } if (!flag) { printf("-1"); return 0; } int pos; for (register int i = 1; i <= top; i++) if (stk[i] == val) pos = i; nxt[stk[top]] = val; for (register int i = pos; i < top; i++) nxt[stk[i]] = stk[i + 1]; for (register int i = 1; i <= n; i++) { if (!nxt[i]) continue; for (register int j = head[i]; j; j = map[j].next) { int y = map[j].to; if (!nxt[y]) continue; int p = i, q = nxt[i]; while (q != y) { int r = nxt[q]; nxt[p] = r; nxt[q] = 0; q = r; } } } int x; for (register int i = 1; i <= n; i++) if (nxt[i]) x = i; int p = x; ans[++tp] = p; p = nxt[p]; while (p != x) { ans[++tp] = p; p = nxt[p]; } std::cout << tp << std::endl; for (register int i = 1; i <= tp; i++) { std::cout << ans[i] << std::endl; } return 0; }
#include <cstdio> #include <iostream> const int maxn = 1e6 + 10; struct edge { int to, next; } map[maxn]; int n, m, stk[maxn], top, val, ans[maxn], tp, nxt[maxn]; int head[maxn], cnt; bool vis[maxn], flag, tag[maxn]; inline void add(int x, int y) { map[++cnt] = (edge){y, head[x]}; head[x] = cnt; return; } void dfs(int x) { stk[++top] = x, vis[x] = tag[x] = true; for (register int i = head[x]; i; i = map[i].next) { int y = map[i].to; if (!vis[y]) dfs(y); else if (tag[y]) { val = y, flag = true; } if (flag) return; } top--, vis[x] = false; return; } int main() { scanf("%d%d", &n, &m); for (register int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); add(x, y); } for (register int i = 1; i <= n; i++) { if (!tag[i]) { dfs(i); if (flag) break; } } if (!flag) { printf("-1"); return 0; } int pos; for (register int i = 1; i <= top; i++) if (stk[i] == val) pos = i; nxt[stk[top]] = val; for (register int i = pos; i < top; i++) nxt[stk[i]] = stk[i + 1]; for (register int i = 1; i <= n; i++) { if (!nxt[i]) continue; for (register int j = head[i]; j; j = map[j].next) { int y = map[j].to; if (!nxt[y]) continue; int p = i, q = nxt[i]; while (q != y) { int r = nxt[q]; nxt[p] = r; nxt[q] = 0; q = r; } } } int x; for (register int i = 1; i <= n; i++) if (nxt[i]) x = i; int p = x; ans[++tp] = p; p = nxt[p]; while (p != x) { ans[++tp] = p; p = nxt[p]; } std::cout << tp << std::endl; for (register int i = 1; i <= tp; i++) { std::cout << ans[i] << std::endl; } return 0; }
replace
3
4
3
4
0
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> using vt = vector<T>; template <class T> using vvt = vector<vt<T>>; template <class T> using ttt = tuple<T, T>; using tii = tuple<int, int>; using tiii = tuple<int, int, int>; using vi = vector<int>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define pb push_back #define mt make_tuple #define ALL(a) (a).begin(), (a).end() #define FST first #define SEC second #define DEB cerr << "!" << endl #define SHOW(a, b) cerr << (a) << " " << (b) << endl const int INF = (INT_MAX / 2); const ll LLINF = (LLONG_MAX / 2); const double eps = 1e-8; const ll DIV = 1e9 + 7; // const double PI = M_PI; inline ll pow(ll x, ll n, ll m) { ll r = 1; while (n > 0) { if ((n & 1) == 1) r = r * x % m; x = x * x % m; n >>= 1; } return r % m; } inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; } #define chmax(a, b) a = max(a, b) /*Coding Space*/ vector<int> ans; vector<int> G[1001]; int main() { int n, m; cin >> n >> m; rep(i, m) { int a, b; cin >> a >> b; G[a].pb(b); } rep(i, n) { int s = i + 1; bool used[1001] = {}; vt<int> ss; ss.pb(s); queue<vector<int>> q; q.push(ss); while (!q.empty()) { auto now = q.front(); q.pop(); if (now.size() >= 2 && *now.rbegin() == s) { if (ans.size() == 0 || ans.size() > now.size()) ans = now; break; } auto n = *now.rbegin(); for (auto nn : G[n]) { now.pb(nn); q.push(now); now.pop_back(); } } } if (ans.size() == 0) cout << -1 << endl; else { cout << ans.size() - 1 << endl; rep(i, ans.size() - 1) cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> using vt = vector<T>; template <class T> using vvt = vector<vt<T>>; template <class T> using ttt = tuple<T, T>; using tii = tuple<int, int>; using tiii = tuple<int, int, int>; using vi = vector<int>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define pb push_back #define mt make_tuple #define ALL(a) (a).begin(), (a).end() #define FST first #define SEC second #define DEB cerr << "!" << endl #define SHOW(a, b) cerr << (a) << " " << (b) << endl const int INF = (INT_MAX / 2); const ll LLINF = (LLONG_MAX / 2); const double eps = 1e-8; const ll DIV = 1e9 + 7; // const double PI = M_PI; inline ll pow(ll x, ll n, ll m) { ll r = 1; while (n > 0) { if ((n & 1) == 1) r = r * x % m; x = x * x % m; n >>= 1; } return r % m; } inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; } #define chmax(a, b) a = max(a, b) /*Coding Space*/ vector<int> ans; vector<int> G[1001]; int main() { int n, m; cin >> n >> m; rep(i, m) { int a, b; cin >> a >> b; G[a].pb(b); } rep(i, n) { int s = i + 1; bool used[1001] = {}; vt<int> ss; ss.pb(s); queue<vector<int>> q; q.push(ss); while (!q.empty()) { auto now = q.front(); q.pop(); if (now.size() >= 2 && *now.rbegin() == s) { if (ans.size() == 0 || ans.size() > now.size()) ans = now; break; } auto n = *now.rbegin(); for (auto nn : G[n]) { if (used[nn] == 0) { now.pb(nn); q.push(now); now.pop_back(); used[nn] = 1; } } } } if (ans.size() == 0) cout << -1 << endl; else { cout << ans.size() - 1 << endl; rep(i, ans.size() - 1) cout << ans[i] << endl; } }
replace
63
66
63
69
TLE
p02902
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) #define REP(i, k, n) for (long long i = k; i < (long long)(n); i++) #define all(a) a.begin(), a.end() #define eb emplace_back #define pb push_back #define lb(v, k) (lower_bound(all(v), k) - v.begin()) #define chmin(x, y) \ if (x > y) \ x = y #define chmax(x, y) \ if (x < y) \ x = y typedef long long ll; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> PP; typedef priority_queue<ll> PQ; typedef priority_queue<ll, vector<ll>, greater<ll>> SPQ; using vi = vector<ll>; using vvi = vector<vector<ll>>; using vc = vector<char>; using vvc = vector<vector<char>>; const ll inf = 100100100100; const int Inf = 1001001001; const int mod = 1000000007; const double pi = 3.141592653589793; ll to[1005]; int dfs(ll i, vvi v) { for (ll x : v[i]) { to[i] = x; if (to[x] != -1) return i; int k = dfs(x, v); if (k != -1) return k; } to[i] = -1; return -1; } int main() { ll n, m; cin >> n >> m; rep(i, n) to[i] = -1; vvi v(n); rep(i, m) { ll a, b; cin >> a >> b; a--; b--; v[a].pb(b); } rep(i, n) { ll k = dfs(i, v); if (k != -1) { vi ito(n); ll u = k; ito[u] = to[u]; u = to[k]; while (u != k) { ito[u] = to[u]; u = to[u]; } while (true) { vi d; rep(i, n) if (ito[i] != -1) d.pb(i); ll from = inf, go; for (ll j : d) { bool b = false; for (ll x : v[j]) { if (x != ito[j] && ito[x] != -1) { from = j; go = x; b = true; break; } } if (b) break; } if (from == inf) { cout << d.size() << endl; rep(i, d.size()) cout << d[i] + 1 << endl; return 0; } ll j = from; while (true) { ll k = ito[j]; ito[j] = -1; j = k; if (j == go) break; } ito[from] = go; } } } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) #define REP(i, k, n) for (long long i = k; i < (long long)(n); i++) #define all(a) a.begin(), a.end() #define eb emplace_back #define pb push_back #define lb(v, k) (lower_bound(all(v), k) - v.begin()) #define chmin(x, y) \ if (x > y) \ x = y #define chmax(x, y) \ if (x < y) \ x = y typedef long long ll; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> PP; typedef priority_queue<ll> PQ; typedef priority_queue<ll, vector<ll>, greater<ll>> SPQ; using vi = vector<ll>; using vvi = vector<vector<ll>>; using vc = vector<char>; using vvc = vector<vector<char>>; const ll inf = 100100100100; const int Inf = 1001001001; const int mod = 1000000007; const double pi = 3.141592653589793; ll to[1005]; int dfs(ll i, vvi v) { for (ll x : v[i]) { to[i] = x; if (to[x] != -1) return i; int k = dfs(x, v); if (k != -1) return k; } to[i] = -1; return -1; } int main() { ll n, m; cin >> n >> m; rep(i, n) to[i] = -1; vvi v(n); rep(i, m) { ll a, b; cin >> a >> b; a--; b--; v[a].pb(b); } rep(i, n) { ll k = dfs(i, v); if (k != -1) { vi ito(n, -1); ll u = k; ito[u] = to[u]; u = to[k]; while (u != k) { ito[u] = to[u]; u = to[u]; } while (true) { vi d; rep(i, n) if (ito[i] != -1) d.pb(i); ll from = inf, go; for (ll j : d) { bool b = false; for (ll x : v[j]) { if (x != ito[j] && ito[x] != -1) { from = j; go = x; b = true; break; } } if (b) break; } if (from == inf) { cout << d.size() << endl; rep(i, d.size()) cout << d[i] + 1 << endl; return 0; } ll j = from; while (true) { ll k = ito[j]; ito[j] = -1; j = k; if (j == go) break; } ito[from] = go; } } } cout << -1 << endl; }
replace
57
58
57
58
TLE
p02902
C++
Time Limit Exceeded
// 嘘解法です!!!!! #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> using namespace std; using lint = long long; constexpr int MOD = 1000000007, INF = 1010101010; constexpr lint LINF = 1LL << 60; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (const auto &e : vec) os << e << (&e == &vec.back() ? "\n" : " "); return os; } #ifdef _DEBUG template <class T> void dump(const char *str, T &&h) { cerr << str << " = " << h << "\n"; }; template <class Head, class... Tail> void dump(const char *str, Head &&h, Tail &&...t) { while (*str != ',') cerr << *str++; cerr << " = " << h << "\n"; dump(str + (*(str + 1) == ' ' ? 2 : 1), t...); } #define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__) #else #define DMP(...) ((void)0) #endif template <class T> inline bool chmin(T &a, T b) { return a > b && (a = b, true); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector<vector<int>> d(N, vector<int>(N, INF)); for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; a--, b--; d[a][b] = 1; } DMP(d); 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) continue; d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } DMP(d); int mincyc = INF; vector<int> ans; for (int i = 0; i < N; i++) chmin(mincyc, d[i][i]); if (mincyc == INF) cout << -1 << "\n"; else { int st = -1; for (int i = 0; i < N; i++) { if (d[i][i] == mincyc) { st = i; break; } } int now = st; while (ans.size() < mincyc) { for (int i = 0; i < N; i++) { if (d[i][i] == mincyc && d[now][i] == 1 && mincyc == d[i][st] + d[st][i]) { ans.emplace_back(i); now = i; break; } } } cout << ans.size() << "\n"; for (const auto &e : ans) cout << e + 1 << "\n"; } return 0; }
// 嘘解法です!!!!! #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> using namespace std; using lint = long long; constexpr int MOD = 1000000007, INF = 1010101010; constexpr lint LINF = 1LL << 60; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (const auto &e : vec) os << e << (&e == &vec.back() ? "\n" : " "); return os; } #ifdef _DEBUG template <class T> void dump(const char *str, T &&h) { cerr << str << " = " << h << "\n"; }; template <class Head, class... Tail> void dump(const char *str, Head &&h, Tail &&...t) { while (*str != ',') cerr << *str++; cerr << " = " << h << "\n"; dump(str + (*(str + 1) == ' ' ? 2 : 1), t...); } #define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__) #else #define DMP(...) ((void)0) #endif template <class T> inline bool chmin(T &a, T b) { return a > b && (a = b, true); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector<vector<int>> d(N, vector<int>(N, INF)); for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; a--, b--; d[a][b] = 1; } DMP(d); 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) continue; d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } } } DMP(d); int mincyc = INF; vector<int> ans; for (int i = 0; i < N; i++) chmin(mincyc, d[i][i]); if (mincyc == INF) cout << -1 << "\n"; else { int st = -1; for (int i = 0; i < N; i++) { if (d[i][i] == mincyc) { st = i; break; } } int now = st; ans.emplace_back(st); while (ans.size() < mincyc) { for (int i = 0; i < N; i++) { if (d[i][i] == mincyc && d[now][i] == 1 && mincyc == d[i][st] + d[st][i]) { ans.emplace_back(i); now = i; break; } } } cout << ans.size() << "\n"; for (const auto &e : ans) cout << e + 1 << "\n"; } return 0; }
insert
91
91
91
92
TLE
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> // using namespace std; #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define rep(i, j, n) for (ll i = (ll)(j); i < (ll)(n); i++) #define REP(i, j, n) for (ll i = (ll)(j); i <= (ll)(n); i++) #define per(i, j, n) for (ll i = (ll)(j); (ll)(n) <= i; i--) #define ll long long #define ALL(a) (a).begin(), (a).end() #define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (ll)(key))) #define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (ll)(key))) #define pb emplace_back #define mp std::make_pair // #define endl "\n" // using std::cin; using std::cout; using std::endl; using std::lower_bound; using std::string; using std::upper_bound; using std::vector; using vi = vector<ll>; using vii = vector<vi>; using pii = std::pair<ll, ll>; // constexpr ll MOD = 1e9 + 7; // constexpr ll MOD=998244353; // constexpr ll MOD=10000000; constexpr ll MAX = 1e6; constexpr ll INF = (1ll << 62); template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; struct Segment_tree { ll N; vector<pii> node; Segment_tree(ll sz) { N = 1; while (N < sz) N *= 2; node.resize(2 * N - 1, mp(INF, INF)); per(i, N - 2, 0) { node[i] = std::min(node[i * 2 + 1], node[i * 2 + 1]); } } void update(ll X, pii val) { X += N - 1; node[X] = val; while (X > 0) { X = (X - 1) / 2; node[X] = std::min(node[X * 2 + 1], node[X * 2 + 2]); } } pii RMQ(ll a, ll b, ll now, ll l, ll r) { //[a,b),[l,r) if (r < 0) r = N; if (r <= a || b <= l) return mp(INF, INF); if (a <= l && r <= b) return node[now]; auto vl = RMQ(a, b, now * 2 + 1, l, (l + r) / 2), vr = RMQ(a, b, now * 2 + 2, (l + r) / 2, r); return std::min(vl, vr); } }; struct Binary_indexed_tree { int N; vi bit; Binary_indexed_tree(int n) : N(n) { bit.resize(N + 1, 0); } void add(int x, int a) { for (x; x <= N; x += (x & -x)) bit[x] += a; } ll sum(int x) { ll ret = 0; for (x; x > 0; x -= (x & -x)) ret += bit[x]; return ret; } ll lower_bound(ll X) { if (sum(N) < X) return -1; ll ret = 0, memo = 1, sum = 0; while (memo * 2 <= N) memo *= 2; while (memo > 0) { if (memo + ret <= N && sum + bit[memo + ret] < X) { sum += bit[memo + ret]; ret += memo; } memo /= 2; } return ret + 1; } }; struct Union_Find { ll N; vi par; vi siz; Union_Find(int n) : N(n) { par.resize(N); siz.resize(N, 1); rep(i, 0, N) par[i] = i; } ll root(ll X) { if (par[X] == X) return X; return par[X] = root(par[X]); } bool same(ll X, ll Y) { return root(X) == root(Y); } void unite(ll X, ll Y) { X = root(X); Y = root(Y); if (X == Y) return; par[X] = Y; siz[Y] += siz[X]; siz[X] = 0; } ll size(ll X) { return siz[root(X)]; } }; long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } vi fac, finv, inv; void COMinit() { fac.resize(MAX); finv.resize(MAX); inv.resize(MAX); fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll COM(ll n, ll r) { if (n < r || n < 0 || r < 0) return 0; return fac[n] * finv[r] % MOD * finv[n - r] % MOD; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); ll N, M; cin >> N >> M; vii edge(N), memo(N); rep(i, 0, M) { ll X, Y; cin >> X >> Y; edge[X - 1].pb(Y - 1); memo[Y - 1].pb(X - 1); } ll ans = INF, s, g; rep(i, 0, N) { vi dist(N, -1); dist[i] = 0; std::queue<ll> que; que.push(i); while (!que.empty()) { ll X = que.front(); que.pop(); rep(j, 0, edge[X].size()) { ll Y = edge[X][j]; if (dist[Y] == -1) { dist[Y] = dist[X] + 1; que.push(Y); } else if (Y == i && dist[X] + 1 < ans) { ans = dist[X] - dist[Y] + 1; s = i, g = X; } } } } if (ans == INF) cout << -1 << endl; else { cout << ans << endl; vi dist(N, -1); dist[s] = 0; std::queue<ll> que; que.push(s); while (!que.empty()) { ll X = que.front(); que.pop(); rep(j, 0, edge[X].size()) { ll Y = edge[X][j]; if (dist[Y] == -1) { dist[Y] = dist[X] + 1; que.push(Y); } } } ll X = g; cout << g + 1 << endl; while (1) { rep(i, 0, memo[X].size()) { ll Y = memo[X][i]; if (dist[Y] == dist[X] - 1) { cout << Y + 1 << endl; X = Y; } } if (X == s) break; } } }
#include <bits/stdc++.h> // using namespace std; #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define rep(i, j, n) for (ll i = (ll)(j); i < (ll)(n); i++) #define REP(i, j, n) for (ll i = (ll)(j); i <= (ll)(n); i++) #define per(i, j, n) for (ll i = (ll)(j); (ll)(n) <= i; i--) #define ll long long #define ALL(a) (a).begin(), (a).end() #define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (ll)(key))) #define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (ll)(key))) #define pb emplace_back #define mp std::make_pair // #define endl "\n" // using std::cin; using std::cout; using std::endl; using std::lower_bound; using std::string; using std::upper_bound; using std::vector; using vi = vector<ll>; using vii = vector<vi>; using pii = std::pair<ll, ll>; // constexpr ll MOD = 1e9 + 7; // constexpr ll MOD=998244353; // constexpr ll MOD=10000000; constexpr ll MAX = 1e6; constexpr ll INF = (1ll << 62); template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; struct Segment_tree { ll N; vector<pii> node; Segment_tree(ll sz) { N = 1; while (N < sz) N *= 2; node.resize(2 * N - 1, mp(INF, INF)); per(i, N - 2, 0) { node[i] = std::min(node[i * 2 + 1], node[i * 2 + 1]); } } void update(ll X, pii val) { X += N - 1; node[X] = val; while (X > 0) { X = (X - 1) / 2; node[X] = std::min(node[X * 2 + 1], node[X * 2 + 2]); } } pii RMQ(ll a, ll b, ll now, ll l, ll r) { //[a,b),[l,r) if (r < 0) r = N; if (r <= a || b <= l) return mp(INF, INF); if (a <= l && r <= b) return node[now]; auto vl = RMQ(a, b, now * 2 + 1, l, (l + r) / 2), vr = RMQ(a, b, now * 2 + 2, (l + r) / 2, r); return std::min(vl, vr); } }; struct Binary_indexed_tree { int N; vi bit; Binary_indexed_tree(int n) : N(n) { bit.resize(N + 1, 0); } void add(int x, int a) { for (x; x <= N; x += (x & -x)) bit[x] += a; } ll sum(int x) { ll ret = 0; for (x; x > 0; x -= (x & -x)) ret += bit[x]; return ret; } ll lower_bound(ll X) { if (sum(N) < X) return -1; ll ret = 0, memo = 1, sum = 0; while (memo * 2 <= N) memo *= 2; while (memo > 0) { if (memo + ret <= N && sum + bit[memo + ret] < X) { sum += bit[memo + ret]; ret += memo; } memo /= 2; } return ret + 1; } }; struct Union_Find { ll N; vi par; vi siz; Union_Find(int n) : N(n) { par.resize(N); siz.resize(N, 1); rep(i, 0, N) par[i] = i; } ll root(ll X) { if (par[X] == X) return X; return par[X] = root(par[X]); } bool same(ll X, ll Y) { return root(X) == root(Y); } void unite(ll X, ll Y) { X = root(X); Y = root(Y); if (X == Y) return; par[X] = Y; siz[Y] += siz[X]; siz[X] = 0; } ll size(ll X) { return siz[root(X)]; } }; long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } vi fac, finv, inv; void COMinit() { fac.resize(MAX); finv.resize(MAX); inv.resize(MAX); fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll COM(ll n, ll r) { if (n < r || n < 0 || r < 0) return 0; return fac[n] * finv[r] % MOD * finv[n - r] % MOD; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); ll N, M; cin >> N >> M; vii edge(N), memo(N); rep(i, 0, M) { ll X, Y; cin >> X >> Y; edge[X - 1].pb(Y - 1); memo[Y - 1].pb(X - 1); } ll ans = INF, s, g; rep(i, 0, N) { vi dist(N, -1); dist[i] = 0; std::queue<ll> que; que.push(i); while (!que.empty()) { ll X = que.front(); que.pop(); rep(j, 0, edge[X].size()) { ll Y = edge[X][j]; if (dist[Y] == -1) { dist[Y] = dist[X] + 1; que.push(Y); } else if (Y == i && dist[X] + 1 < ans) { ans = dist[X] - dist[Y] + 1; s = i, g = X; } } } } if (ans == INF) cout << -1 << endl; else { cout << ans << endl; vi dist(N, -1); dist[s] = 0; std::queue<ll> que; que.push(s); while (!que.empty()) { ll X = que.front(); que.pop(); rep(j, 0, edge[X].size()) { ll Y = edge[X][j]; if (dist[Y] == -1) { dist[Y] = dist[X] + 1; que.push(Y); } } } ll X = g; cout << g + 1 << endl; while (1) { rep(i, 0, memo[X].size()) { ll Y = memo[X][i]; if (dist[Y] == dist[X] - 1) { cout << Y + 1 << endl; X = Y; break; } } if (X == s) break; } } }
insert
211
211
211
212
TLE
p02902
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const int MAXN = 1024; int vis[MAXN], parent[MAXN]; vector<int> adj[MAXN]; int nxt[MAXN]; void go(vector<int> cycle) { map<int, int> in_cycle; for (int i = 0; i < cycle.size(); i++) { in_cycle[cycle[i]] = i; // cerr << "in cycle: " << cycle[i]+1 << endl; } int u = cycle[0]; // cerr << "adding: " << u + 1 << endl; queue<int> q; q.push(u); while (true) { int cur_pos = in_cycle[u]; int best_pos = cur_pos; int node = -2; // cerr << "searching for nxt after " << u + 1 << endl; // cerr << "cur pos: " << cur_pos << endl; for (int v : adj[u]) { if (in_cycle.count(v) == 0) continue; int pos = in_cycle[v]; // cerr << "trying: " << v+1 << ", pos: " << pos << endl; if (pos > cur_pos) { if (best_pos >= cur_pos && pos > best_pos) { best_pos = pos; node = v; } } else { if (best_pos >= cur_pos || pos > best_pos) { best_pos = pos; node = v; } } } // cerr << "next one is: " << node+1 << endl; if (best_pos > cur_pos) { q.push(node); u = node; } else { while (!q.empty() && in_cycle[q.front()] < best_pos) q.pop(); assert(q.front() == node); break; } } cout << q.size() << endl; while (!q.empty()) { cout << q.front() + 1 << endl; q.pop(); } exit(0); } void dfs(int u) { vis[u] = 1; bool here = false; int w; for (int v : adj[u]) { if (vis[v] == 1) { here = true; w = v; break; } parent[v] = u; dfs(v); } vector<int> cycle; if (here) { while (u != w) { cycle.push_back(u); u = parent[u]; } cycle.push_back(w); reverse(cycle.begin(), cycle.end()); go(cycle); } vis[u] = 2; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); } fill(vis, vis + n, 0); for (int i = 0; i < n; i++) { if (!vis[i]) { dfs(i); } } cout << -1 << endl; return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const int MAXN = 1024; int vis[MAXN], parent[MAXN]; vector<int> adj[MAXN]; int nxt[MAXN]; void go(vector<int> cycle) { map<int, int> in_cycle; for (int i = 0; i < cycle.size(); i++) { in_cycle[cycle[i]] = i; // cerr << "in cycle: " << cycle[i]+1 << endl; } int u = cycle[0]; // cerr << "adding: " << u + 1 << endl; queue<int> q; q.push(u); while (true) { int cur_pos = in_cycle[u]; int best_pos = cur_pos; int node = -2; // cerr << "searching for nxt after " << u + 1 << endl; // cerr << "cur pos: " << cur_pos << endl; for (int v : adj[u]) { if (in_cycle.count(v) == 0) continue; int pos = in_cycle[v]; // cerr << "trying: " << v+1 << ", pos: " << pos << endl; if (pos > cur_pos) { if (best_pos >= cur_pos && pos > best_pos) { best_pos = pos; node = v; } } else { if (best_pos >= cur_pos || pos > best_pos) { best_pos = pos; node = v; } } } // cerr << "next one is: " << node+1 << endl; if (best_pos > cur_pos) { q.push(node); u = node; for (int j = cur_pos + 1; j < best_pos; j++) { int v = cycle[j]; in_cycle.erase(v); } } else { while (!q.empty() && in_cycle[q.front()] < best_pos) q.pop(); assert(q.front() == node); break; } } cout << q.size() << endl; while (!q.empty()) { cout << q.front() + 1 << endl; q.pop(); } exit(0); } void dfs(int u) { vis[u] = 1; bool here = false; int w; for (int v : adj[u]) { if (vis[v] == 1) { here = true; w = v; break; } parent[v] = u; dfs(v); } vector<int> cycle; if (here) { while (u != w) { cycle.push_back(u); u = parent[u]; } cycle.push_back(w); reverse(cycle.begin(), cycle.end()); go(cycle); } vis[u] = 2; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); } fill(vis, vis + n, 0); for (int i = 0; i < n; i++) { if (!vis[i]) { dfs(i); } } cout << -1 << endl; return 0; }
insert
57
57
57
61
0
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long #define fornum(A, B, C) for (A = B; A < C; A++) #define mp make_pair #define pii pair<int, int> #define pll pair<ll, ll> using namespace std; ///////////////////////////////////////////////////// #define INF 1e12 ll N, M; vector<ll> abv[1010]; ll i, j, k; vector<ll> ans; ll ansck; ll mk[1010], mk2[1010]; bool dfs(ll a, ll p) { mk2[a] = 1; ll i = 0, f = 0; fornum(i, 0, abv[a].size()) { ll b = abv[a][i]; if (b < p) continue; if (b == p) { f++; } else if (mk2[b]) { f += 99999; } mk[b]++; } // printf("<%lld>", f); if (f == 1) { ans.push_back(a); return true; } else if (f == 0) { fornum(i, 0, abv[a].size()) { ll b = abv[a][i]; if (b < p) continue; if (mk[b] < 2 && dfs(b, p)) { ans.push_back(a); return true; } } } fornum(i, 0, abv[a].size()) { ll b = abv[a][i]; mk[b]--; } mk2[a] = 0; // printf("\n"); return false; } int main() { scanf("%lld%lld", &N, &M); fornum(i, 0, M) { ll a, b; scanf("%lld%lld", &a, &b); abv[a].push_back(b); } fornum(i, 0, N) { fornum(j, 0, N) { mk[j + 1] = 0; } mk[i + 1] = 1; // printf("%lld\n", i + 1); if (dfs(i + 1, i + 1)) { printf("%d\n", ans.size()); fornum(i, 0, ans.size()) { printf("%lld\n", ans[i]); } return 0; } } printf("-1"); return 0; }
#include <bits/stdc++.h> #define ll long long #define fornum(A, B, C) for (A = B; A < C; A++) #define mp make_pair #define pii pair<int, int> #define pll pair<ll, ll> using namespace std; ///////////////////////////////////////////////////// #define INF 1e12 ll N, M; vector<ll> abv[1010]; ll i, j, k; vector<ll> ans; ll ansck; ll mk[1010], mk2[1010]; bool dfs(ll a, ll p) { mk2[a] = 1; ll i = 0, f = 0; fornum(i, 0, abv[a].size()) { ll b = abv[a][i]; if (b < p) continue; if (b == p) { f++; } else if (mk2[b]) { f += 99999; } mk[b]++; } // printf("<%lld>", f); if (f == 1) { ans.push_back(a); return true; } else if (f == 0) { fornum(i, 0, abv[a].size()) { ll b = abv[a][i]; if (b < p) continue; if (mk[b] < 2 && dfs(b, p)) { ans.push_back(a); return true; } } } fornum(i, 0, abv[a].size()) { ll b = abv[a][i]; if (b < p) mk[b]--; } mk2[a] = 0; // printf("\n"); return false; } int main() { scanf("%lld%lld", &N, &M); fornum(i, 0, M) { ll a, b; scanf("%lld%lld", &a, &b); abv[a].push_back(b); } fornum(i, 0, N) { fornum(j, 0, N) { mk[j + 1] = 0; } mk[i + 1] = 1; // printf("%lld\n", i + 1); if (dfs(i + 1, i + 1)) { printf("%d\n", ans.size()); fornum(i, 0, ans.size()) { printf("%lld\n", ans[i]); } return 0; } } printf("-1"); return 0; }
replace
52
53
52
54
TLE
p02902
C++
Runtime Error
#include "bits/stdc++.h" #define hhh printf("hhh\n") #define see(x) (cerr << (#x) << '=' << (x) << endl) using namespace std; typedef long long ll; typedef pair<int, int> pr; inline int read() { int x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x; } const int maxn = 1e3 + 10; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-7; int N, M; int pre[maxn], dis[maxn], vis[maxn]; int head[maxn], to[maxn], nxt[maxn], tot; int ans[maxn], cnt = inf, res[maxn], cur; inline void add_edge(int u, int v) { ++tot; to[tot] = v; nxt[tot] = head[u]; head[u] = tot; } int main() { // ios::sync_with_stdio(false); cin.tie(0); N = read(), M = read(); for (int i = 1; i <= M; ++i) { int u = read(), v = read(); add_edge(u, v); } for (int i = 1; i <= N; ++i) { memset(vis, 0, sizeof(vis)); int st = i; vis[st] = 1; queue<int> q; q.push(st); while (q.size()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = nxt[i]) { if (vis[to[i]]) continue; int v = to[i]; dis[v] = dis[u] + 1; vis[v] = 1; pre[v] = u; q.push(v); } } for (int i = 1; i <= N; ++i) { if (i == st || !vis[i]) continue; cur = 0; for (int j = head[i]; j; j = nxt[j]) { if (to[j] == st) { int v = i; while (v != st) res[++cur] = v, v = pre[v]; res[++cur] = st; if (cur < cnt) { cnt = cur; for (int i = 1; i <= cur; ++i) ans[i] = res[i]; } break; } } } } if (cnt == inf) printf("-1\n"); else { cout << cnt << endl; for (int i = 1; i <= cnt; ++i) printf("%d\n", ans[i]); } }
#include "bits/stdc++.h" #define hhh printf("hhh\n") #define see(x) (cerr << (#x) << '=' << (x) << endl) using namespace std; typedef long long ll; typedef pair<int, int> pr; inline int read() { int x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x; } const int maxn = 1e3 + 10; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-7; int N, M; int pre[maxn], dis[maxn], vis[maxn]; int head[maxn], to[maxn * 2], nxt[maxn * 2], tot; int ans[maxn], cnt = inf, res[maxn], cur; inline void add_edge(int u, int v) { ++tot; to[tot] = v; nxt[tot] = head[u]; head[u] = tot; } int main() { // ios::sync_with_stdio(false); cin.tie(0); N = read(), M = read(); for (int i = 1; i <= M; ++i) { int u = read(), v = read(); add_edge(u, v); } for (int i = 1; i <= N; ++i) { memset(vis, 0, sizeof(vis)); int st = i; vis[st] = 1; queue<int> q; q.push(st); while (q.size()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = nxt[i]) { if (vis[to[i]]) continue; int v = to[i]; dis[v] = dis[u] + 1; vis[v] = 1; pre[v] = u; q.push(v); } } for (int i = 1; i <= N; ++i) { if (i == st || !vis[i]) continue; cur = 0; for (int j = head[i]; j; j = nxt[j]) { if (to[j] == st) { int v = i; while (v != st) res[++cur] = v, v = pre[v]; res[++cur] = st; if (cur < cnt) { cnt = cur; for (int i = 1; i <= cur; ++i) ans[i] = res[i]; } break; } } } } if (cnt == inf) printf("-1\n"); else { cout << cnt << endl; for (int i = 1; i <= cnt; ++i) printf("%d\n", ans[i]); } }
replace
23
24
23
24
0
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define INF 100000000 #define YJ 1145141919 #define INF_INT_MAX 2147483647 #define INF_LL 9223372036854775 #define INF_LL_MAX 9223372036854775807 #define EPS 1e-10 #define MOD 1000000007 #define MOD9 998244353 #define Pi acos(-1) #define LL long long #define ULL unsigned long long #define LD long double #define int long long using II = pair<int, int>; int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a * b / gcd(a, b); } int extgcd(int a, int b, int &x, int &y) { int g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; } #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(a) begin((a)), end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) const int MAX_N = 1005; const int MAX_M = 2005; int N, M; vector<int> Edge[MAX_N]; int used[MAX_N][2]; vector<int> ans; int dfs(int pos, int depth = 1) { if (used[pos][1]) { return -1; } if (used[pos][0]) { int index = used[pos][0] - 1; return pos; } used[pos][0] = depth; ans.push_back(pos); for (int next : Edge[pos]) { int ret = dfs(next, depth + 1); if (ret != -1) { return ret; } } used[pos][0] = false; ans.pop_back(); used[pos][1] = true; return -1; } bool edgeFlag[MAX_N][MAX_N]; bool used3[MAX_N]; int dist[MAX_N]; int rireki[MAX_N]; signed main() { cin >> N >> M; REP(m, M) { int a, b; cin >> a >> b; Edge[a].push_back(b); edgeFlag[a][b] = true; } REP(n, N + 1) { int ret = dfs(n); if (ret == -1) continue; else { int k = 0; REP(n, ans.size()) { if (ans[k] == ret) { k = n; break; } } FOR(n, k, ans.size()) { used3[ans[n]] = true; } vector<int> ans2; int dd = INF_LL; FOR(n, k, ans.size()) { memset(dist, -1, sizeof(dist)); memset(rireki, -1, sizeof(rireki)); queue<int> que; dist[ans[n]] = 0; que.push(ans[n]); int pre = -1; while (que.size() && pre == -1) { int pos = que.front(); que.pop(); for (int next : Edge[pos]) { if (used3[next] && dist[next] == -1) { dist[next] = dist[pos] + 1; que.push(next); rireki[next] = pos; } if (next == ans[n]) { pre = pos; break; } } } if (dd > dist[pre]) { dd = dist[pre]; ans2.clear(); ans2.push_back(pre); while (pre != ans[n]) { pre = rireki[pre]; ans2.push_back(pre); } } } cout << ans2.size() << endl; for (int a : ans2) { cout << a << endl; } return 0; } } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define INF 100000000 #define YJ 1145141919 #define INF_INT_MAX 2147483647 #define INF_LL 9223372036854775 #define INF_LL_MAX 9223372036854775807 #define EPS 1e-10 #define MOD 1000000007 #define MOD9 998244353 #define Pi acos(-1) #define LL long long #define ULL unsigned long long #define LD long double #define int long long using II = pair<int, int>; int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a * b / gcd(a, b); } int extgcd(int a, int b, int &x, int &y) { int g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; } #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(a) begin((a)), end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) const int MAX_N = 1005; const int MAX_M = 2005; int N, M; vector<int> Edge[MAX_N]; int used[MAX_N][2]; vector<int> ans; int dfs(int pos, int depth = 1) { if (used[pos][1]) { return -1; } if (used[pos][0]) { int index = used[pos][0] - 1; return pos; } used[pos][0] = depth; ans.push_back(pos); for (int next : Edge[pos]) { int ret = dfs(next, depth + 1); if (ret != -1) { return ret; } } used[pos][0] = false; ans.pop_back(); used[pos][1] = true; return -1; } bool edgeFlag[MAX_N][MAX_N]; bool used3[MAX_N]; int dist[MAX_N]; int rireki[MAX_N]; signed main() { cin >> N >> M; REP(m, M) { int a, b; cin >> a >> b; Edge[a].push_back(b); edgeFlag[a][b] = true; } REP(n, N + 1) { int ret = dfs(n); if (ret == -1) continue; else { int k = 0; REP(n, ans.size()) { if (ans[n] == ret) { k = n; break; } } FOR(n, k, ans.size()) { used3[ans[n]] = true; } vector<int> ans2; int dd = INF_LL; FOR(n, k, ans.size()) { memset(dist, -1, sizeof(dist)); memset(rireki, -1, sizeof(rireki)); queue<int> que; dist[ans[n]] = 0; que.push(ans[n]); int pre = -1; while (que.size() && pre == -1) { int pos = que.front(); que.pop(); for (int next : Edge[pos]) { if (used3[next] && dist[next] == -1) { dist[next] = dist[pos] + 1; que.push(next); rireki[next] = pos; } if (next == ans[n]) { pre = pos; break; } } } if (dd > dist[pre]) { dd = dist[pre]; ans2.clear(); ans2.push_back(pre); while (pre != ans[n]) { pre = rireki[pre]; ans2.push_back(pre); } } } cout << ans2.size() << endl; for (int a : ans2) { cout << a << endl; } return 0; } } cout << -1 << endl; return 0; }
replace
94
95
94
95
TLE
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); \ cout.precision(10); \ cout << fixed; #define int long long int #define pb push_back #define endl "\n" #define all(a) a.begin(), a.end() #define N 1009 #define M 1000009 #define mod 1000000007 int powi(int a, int b) { if (b == 0) return 1; int see = powi(a, b / 2); see *= see; see %= mod; if (b % 2) see *= a; see %= mod; return see; } int n, m; int a, b; vector<int> v[N]; bool edge[N][N]; vector<pair<int, int>> ed; int par[N]; bool vis[N]; vector<int> globl; void pre() { memset(vis, 0, sizeof(vis)); } int32_t main() { IOS; pre(); cin >> n >> m; for (int i = 0; i < n; i++) { globl.push_back(-1); } for (int i = 0; i < m; i++) { cin >> a >> b; v[a].push_back(b); edge[a][b] = 1; ed.push_back({a, b}); if (edge[b][a] == 1) { cout << "2\n" << a << endl << b << endl; return 0; } } for (auto i : ed) { // bfs from second pre(); bool cycle = 0; queue<int> q; q.push(i.second); while (!q.empty()) { int tp = q.front(); vis[tp] = 1; for (auto j : v[tp]) { if (vis[j] == 1 && j == i.first) { while (!q.empty()) q.pop(); par[j] = tp; break; } par[j] = tp; q.push(j); } if (q.size() > 0) q.pop(); } if (vis[i.first] == 0) { continue; } vector<int> ans; int temp = i.first; while (temp != i.second) { ans.push_back(temp); temp = par[temp]; } ans.push_back(i.second); if (ans.size() <= globl.size()) { globl = ans; } } if (globl[0] == -1) { cout << "-1\n"; return 0; } cout << globl.size() << endl; for (auto i : globl) { cout << i << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); \ cout.precision(10); \ cout << fixed; #define int long long int #define pb push_back #define endl "\n" #define all(a) a.begin(), a.end() #define N 1009 #define M 1000009 #define mod 1000000007 int powi(int a, int b) { if (b == 0) return 1; int see = powi(a, b / 2); see *= see; see %= mod; if (b % 2) see *= a; see %= mod; return see; } int n, m; int a, b; vector<int> v[N]; bool edge[N][N]; vector<pair<int, int>> ed; int par[N]; bool vis[N]; vector<int> globl; void pre() { memset(vis, 0, sizeof(vis)); } int32_t main() { IOS; pre(); cin >> n >> m; for (int i = 0; i < n; i++) { globl.push_back(-1); } for (int i = 0; i < m; i++) { cin >> a >> b; v[a].push_back(b); edge[a][b] = 1; ed.push_back({a, b}); if (edge[b][a] == 1) { cout << "2\n" << a << endl << b << endl; return 0; } } for (auto i : ed) { // bfs from second pre(); bool cycle = 0; queue<int> q; q.push(i.second); while (!q.empty()) { int tp = q.front(); vis[tp] = 1; for (auto j : v[tp]) { if (vis[j] == 1 && j == i.first) { while (!q.empty()) q.pop(); par[j] = tp; break; } if (vis[j] == 1) continue; par[j] = tp; q.push(j); } if (q.size() > 0) q.pop(); } if (vis[i.first] == 0) { continue; } vector<int> ans; int temp = i.first; while (temp != i.second) { ans.push_back(temp); temp = par[temp]; } ans.push_back(i.second); if (ans.size() <= globl.size()) { globl = ans; } } if (globl[0] == -1) { cout << "-1\n"; return 0; } cout << globl.size() << endl; for (auto i : globl) { cout << i << endl; } return 0; }
insert
72
72
72
74
TLE
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(i, s, e) for (int i = (s); i < (e); i++) #define rep(i, n) REP(i, 0, n) #define rep1(i, n) REP(i, 1, n) #define repe(i, n) for (auto &&i : n) #define all(v) (v).begin(), (v).end() #define decimal fixed << setprecision(20) #define fastcin() \ cin.tie(0); \ ios::sync_with_stdio(false) using namespace std; using LL = long long; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const int INF = 1e9; const LL LLINF = 1e16; using Graph = vector<set<int>>; set<int> bfs(int v, Graph &G) { int n = G.size(); vector<int> f(n, 0); f[v] = 1; queue<int> que; vector<int> prev(n, -1); que.push(v); while (!que.empty()) { int u = que.front(); que.pop(); f[u] = 1; bool cyc = 0; repe(i, G[u]) { if (i == v) { cyc = 1; break; } } if (cyc) { set<int> ans; for (int j = u; j != v;) { ans.insert(j); j = prev[j]; } ans.insert(v); return ans; } repe(i, G[u]) { que.push(i); prev[i] = u; } } return set<int>(); } int main() { fastcin(); int n, m; cin >> n >> m; Graph G(n); rep(i, m) { int a, b; cin >> a >> b; a--; b--; G[a].insert(b); } set<int> ans; int sz = INF; rep(i, n) { auto tmp = bfs(i, G); if (!tmp.empty() && tmp.size() < sz) { sz = tmp.size(); ans = tmp; } } if (sz == INF) cout << -1 << endl; else { cout << sz << endl; repe(i, ans) cout << i + 1 << endl; } }
#include <bits/stdc++.h> #define REP(i, s, e) for (int i = (s); i < (e); i++) #define rep(i, n) REP(i, 0, n) #define rep1(i, n) REP(i, 1, n) #define repe(i, n) for (auto &&i : n) #define all(v) (v).begin(), (v).end() #define decimal fixed << setprecision(20) #define fastcin() \ cin.tie(0); \ ios::sync_with_stdio(false) using namespace std; using LL = long long; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const int INF = 1e9; const LL LLINF = 1e16; using Graph = vector<set<int>>; set<int> bfs(int v, Graph &G) { int n = G.size(); vector<int> f(n, 0); f[v] = 1; queue<int> que; vector<int> prev(n, -1); que.push(v); while (!que.empty()) { int u = que.front(); que.pop(); f[u] = 1; bool cyc = 0; repe(i, G[u]) { if (i == v) { cyc = 1; break; } } if (cyc) { set<int> ans; for (int j = u; j != v;) { ans.insert(j); j = prev[j]; } ans.insert(v); return ans; } repe(i, G[u]) if (!f[i]) { que.push(i); prev[i] = u; } } return set<int>(); } int main() { fastcin(); int n, m; cin >> n >> m; Graph G(n); rep(i, m) { int a, b; cin >> a >> b; a--; b--; G[a].insert(b); } set<int> ans; int sz = INF; rep(i, n) { auto tmp = bfs(i, G); if (!tmp.empty() && tmp.size() < sz) { sz = tmp.size(); ans = tmp; } } if (sz == INF) cout << -1 << endl; else { cout << sz << endl; repe(i, ans) cout << i + 1 << endl; } }
replace
58
59
58
59
TLE
p02902
C++
Runtime Error
#include <bits/stdc++.h> #define mk make_pair #define fs first #define sc second using namespace std; typedef long long ll; typedef long double ld; vector<int> a[1111], b[1111]; bool vis[1111], vis1[1111]; vector<int> v; void dfs(int x) { if (vis[x]) return; vis[x] = 1; for (int i = 0; i < a[x].size(); ++i) { dfs(a[x][i]); } } int tmp2; int dfs1(int x) { if (vis[x]) return 0; if (x == tmp2) { v.push_back(x); return 1; } vis[x] = 1; int tmp; for (int i = 0; i < a[x].size(); ++i) { tmp = dfs1(a[x][i]); if (tmp) { v.push_back(x); return 1; } } return 0; } int main() { int n, m; while (cin >> n >> m) { int x, y; for (int i = 0; i < m; ++i) { scanf("%d%d", &x, &y); a[x].push_back(y); b[y].push_back(x); } int tmp1 = -1; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) vis[j] = 0; dfs(i); for (int j = 0; j < b[i].size(); ++j) { if (vis[b[i][j]]) { tmp1 = i; tmp2 = b[i][j]; memset(vis, 0, sizeof vis); dfs1(i); break; } } if (tmp1 != -1) break; } if (tmp1 == -1) { cout << -1 << endl; continue; } reverse(v.begin(), v.end()); memset(vis, 0, sizeof vis); for (int i = 0; i < v.size(); ++i) { vis[v[i]] = 1; } for (int k = 0; k < v.size(); ++k) { int i = v[k]; for (int j = 0; j < a[i].size(); ++j) { if (vis[a[i][j]]) { while (k + 1 < v.size()) { if (v[k + 1] == a[i][j]) break; vis[v[k + 1]] = 0; v.erase(v.begin() + k + 1); } } } vis[i] = 0; } // memset(vis, 0, sizeof vis); for (int i = 0; i < v.size(); ++i) { vis[v[i]] = 1; } int mx = 0; for (int k = 0; k < v.size(); ++k) { int i = v[k]; for (int j = 0; j < b[i].size(); ++j) { if (vis[b[i][j]]) { while (v.back() != b[i][j]) { v.pop_back(); } mx = max(mx, k); } } vis[i] = 0; } cout << v.size() - mx << endl; for (int i = mx; i < v.size(); ++i) { cout << v[i] << endl; } } return 0; }
#include <bits/stdc++.h> #define mk make_pair #define fs first #define sc second using namespace std; typedef long long ll; typedef long double ld; vector<int> a[1111], b[1111]; bool vis[1111], vis1[1111]; vector<int> v; void dfs(int x) { if (vis[x]) return; vis[x] = 1; for (int i = 0; i < a[x].size(); ++i) { dfs(a[x][i]); } } int tmp2; int dfs1(int x) { if (vis[x]) return 0; if (x == tmp2) { v.push_back(x); return 1; } vis[x] = 1; int tmp; for (int i = 0; i < a[x].size(); ++i) { tmp = dfs1(a[x][i]); if (tmp) { v.push_back(x); return 1; } } return 0; } int main() { int n, m; while (cin >> n >> m) { int x, y; for (int i = 0; i < m; ++i) { scanf("%d%d", &x, &y); a[x].push_back(y); b[y].push_back(x); } int tmp1 = -1; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) vis[j] = 0; dfs(i); for (int j = 0; j < b[i].size(); ++j) { if (vis[b[i][j]]) { tmp1 = i; tmp2 = b[i][j]; memset(vis, 0, sizeof vis); dfs1(i); break; } } if (tmp1 != -1) break; } if (tmp1 == -1) { cout << -1 << endl; continue; } reverse(v.begin(), v.end()); memset(vis, 0, sizeof vis); for (int i = 0; i < v.size(); ++i) { vis[v[i]] = 1; } for (int k = 0; k < v.size(); ++k) { int i = v[k]; for (int j = 0; j < a[i].size(); ++j) { if (vis[a[i][j]]) { while (k + 1 < v.size()) { if (v[k + 1] == a[i][j]) break; vis[v[k + 1]] = 0; v.erase(v.begin() + k + 1); } } } vis[i] = 0; } // memset(vis, 0, sizeof vis); for (int i = 0; i < v.size(); ++i) { vis[v[i]] = 1; } int mx = 0; for (int k = 0; k < v.size(); ++k) { int i = v[k]; for (int j = 0; j < b[i].size(); ++j) { if (vis[b[i][j]]) { while (v.back() != b[i][j]) { vis[v.back()] = 0; v.pop_back(); } mx = max(mx, k); } } vis[i] = 0; } cout << v.size() - mx << endl; for (int i = mx; i < v.size(); ++i) { cout << v[i] << endl; } } return 0; }
insert
96
96
96
97
0
p02902
C++
Runtime Error
/** * 内部により小さなループを持たない、ループを1個探せばよい */ #include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; template <class T> bool contains(unordered_set<T> s, const T &t) { const auto &ite = s.find(t); return ite != s.end(); } // 深さ優先探索で探索済み unordered_set<int> not_check; // 探したループをリストに保存 vector<int> loops; void make_loops(stack<int> &ancestors, int current) { loops.emplace_back(current); while (ancestors.top() != current) { loops.emplace_back(ancestors.top()); ancestors.pop(); } reverse(loops.begin(), loops.end()); } // ループを探す bool check_loop(const vector<vector<int>> &edges, int current, stack<int> &ancestors, unordered_set<int> &ancestors_for_find) { // cout << "current:" << current << endl; if (contains(ancestors_for_find, current)) { make_loops(ancestors, current); return true; // ループ発見 } if (!contains(not_check, current)) { return false; } not_check.erase(current); // 到達を記録 ancestors_for_find.emplace(current); ancestors.emplace(current); for (const auto &next : edges[current]) { // cout << "add next: " << next << endl; const auto &child_result = check_loop(edges, next, ancestors, ancestors_for_find); if (child_result) { return child_result; } } // 戻り際に到達記録を除去 ancestors_for_find.erase(current); ancestors.pop(); return false; } // ループを縮める vector<int> shrink_loop(const vector<vector<int>> &edges) { vector<int> result; unordered_map<int, int> loop_indexes; for (int l = 0; l < loops.size(); l++) { loop_indexes.emplace(loops[l], l); } int i = 0; while (i < loops.size()) { int current = loops[i]; result.emplace_back(current); // cout << "i:" << i << endl; // 点集合の中で、最後の辺を結果に追加する size_t max_next = 0; for (const auto &next : edges[current]) { if (loop_indexes.find(next) != loop_indexes.end()) { // cout << "next:" << next << endl; // cout << "loop_indexes[next]:" << loop_indexes[next] << endl; max_next = max(max_next, (loop_indexes[next] - i + loops.size()) % loops.size()); // cout << "(loop_indexes[next] - i + loops.size()):" << // (loop_indexes[next] - i + loops.size()) << endl; cout << "max_next:" // << max_next << endl; } } max_next = (max_next + i) % loops.size(); // cout << "max_next:" << max_next << endl; if (max_next < i) { // ループした場合、スキップした部分を結果から削除しておしまい result.erase(result.begin(), result.begin() + max_next); break; } for (int j = i + 1; j < max_next; j++) { // スキップした部分を候補から外しておく loop_indexes.erase(loops[j]); } i = max_next; } return result; } int main() { int n, m, a, b; cin >> n >> m; vector<vector<int>> edges(n + 1); for (int i = 0; i < m; i++) { cin >> a >> b; edges[a].emplace_back(b); } for (int i = 1; i <= n; i++) { not_check.emplace(i); } bool loop_found = false; while ( !not_check .empty()) { // 一度の探索で終わらない場合、別の箇所から探索しなおし const auto &root = not_check.begin(); stack<int> s; unordered_set<int> us; loop_found = check_loop(edges, *root, s, us); if (loop_found) { break; } } if (!loop_found) { cout << -1 << endl; return 0; } /* cout << "loop:" << endl; for (const auto& l : loops) { cout << l << endl; } */ const auto &shrinked_loops = shrink_loop(edges); cout << shrinked_loops.size() << endl; for (const auto &l : shrinked_loops) { cout << l << endl; } }
/** * 内部により小さなループを持たない、ループを1個探せばよい */ #include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; template <class T> bool contains(unordered_set<T> s, const T &t) { const auto &ite = s.find(t); return ite != s.end(); } // 深さ優先探索で探索済み unordered_set<int> not_check; // 探したループをリストに保存 vector<int> loops; void make_loops(stack<int> &ancestors, int current) { loops.emplace_back(current); while (ancestors.top() != current) { loops.emplace_back(ancestors.top()); ancestors.pop(); } reverse(loops.begin(), loops.end()); } // ループを探す bool check_loop(const vector<vector<int>> &edges, int current, stack<int> &ancestors, unordered_set<int> &ancestors_for_find) { // cout << "current:" << current << endl; if (contains(ancestors_for_find, current)) { make_loops(ancestors, current); return true; // ループ発見 } if (!contains(not_check, current)) { return false; } not_check.erase(current); // 到達を記録 ancestors_for_find.emplace(current); ancestors.emplace(current); for (const auto &next : edges[current]) { // cout << "add next: " << next << endl; const auto &child_result = check_loop(edges, next, ancestors, ancestors_for_find); if (child_result) { return child_result; } } // 戻り際に到達記録を除去 ancestors_for_find.erase(current); ancestors.pop(); return false; } // ループを縮める vector<int> shrink_loop(const vector<vector<int>> &edges) { vector<int> result; unordered_map<int, int> loop_indexes; for (int l = 0; l < loops.size(); l++) { loop_indexes.emplace(loops[l], l); } int i = 0; while (i < loops.size()) { int current = loops[i]; result.emplace_back(current); // cout << "i:" << i << endl; // 点集合の中で、最後の辺を結果に追加する size_t max_next = 0; for (const auto &next : edges[current]) { if (loop_indexes.find(next) != loop_indexes.end()) { // cout << "next:" << next << endl; // cout << "loop_indexes[next]:" << loop_indexes[next] << endl; max_next = max(max_next, (loop_indexes[next] - i + loops.size()) % loops.size()); // cout << "(loop_indexes[next] - i + loops.size()):" << // (loop_indexes[next] - i + loops.size()) << endl; cout << "max_next:" // << max_next << endl; } } max_next = (max_next + i) % loops.size(); // cout << "max_next:" << max_next << endl; if (max_next < i) { // ループした場合、スキップした部分を結果から削除しておしまい result.erase(result.begin(), find(result.begin(), result.end(), loops[max_next])); break; } for (int j = i + 1; j < max_next; j++) { // スキップした部分を候補から外しておく loop_indexes.erase(loops[j]); } i = max_next; } return result; } int main() { int n, m, a, b; cin >> n >> m; vector<vector<int>> edges(n + 1); for (int i = 0; i < m; i++) { cin >> a >> b; edges[a].emplace_back(b); } for (int i = 1; i <= n; i++) { not_check.emplace(i); } bool loop_found = false; while ( !not_check .empty()) { // 一度の探索で終わらない場合、別の箇所から探索しなおし const auto &root = not_check.begin(); stack<int> s; unordered_set<int> us; loop_found = check_loop(edges, *root, s, us); if (loop_found) { break; } } if (!loop_found) { cout << -1 << endl; return 0; } /* cout << "loop:" << endl; for (const auto& l : loops) { cout << l << endl; } */ const auto &shrinked_loops = shrink_loop(edges); cout << shrinked_loops.size() << endl; for (const auto &l : shrinked_loops) { cout << l << endl; } }
replace
102
103
102
104
0
p02902
C++
Runtime Error
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> // namespace mp = boost::multiprecision; using namespace std; const double PI = 3.14159265358979323846; typedef long long ll; const double EPS = 1e-9; #define rep(i, n) for (int i = 0; i < (n); ++i) // #define rep(i, n) for (ll i = 0; i < (n); ++i) typedef pair<ll, ll> P; const ll INF = 10e17; #define cmin(x, y) x = min(x, y) #define cmax(x, y) x = max(x, y) #define ret() return 0; double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; } std::istream &operator>>(std::istream &in, set<int> &o) { int a; in >> a; o.insert(a); return in; } std::istream &operator>>(std::istream &in, queue<int> &o) { ll a; in >> a; o.push(a); return in; } bool contain(set<int> &s, int a) { return s.find(a) != s.end(); } // ofstream outfile("log.txt"); // outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') // << rank << endl; // std::cout << std::bitset<8>(9); // const ll mod = 1e10; typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK; int min_cycle(int start, int n, vector<vector<int>> &edges) { vector<int> costs(n, INT_MAX); queue<P> q; q.push(P(start, 0)); while (!q.empty()) { P now = q.front(); q.pop(); for (int next : edges[now.first]) { if (costs[next] != INT_MAX) continue; costs[next] = now.second + 1; q.push(P(next, now.second + 1)); } } return costs[start]; } bool dfs(int goal, vector<vector<int>> &edges, vector<int> &route, int now, int depth) { if (now == goal && route.size() == depth) { return true; } if (depth == route.size()) { route.pop_back(); return false; } route.push_back(now); for (int next : edges[now]) { bool b = dfs(goal, edges, route, next, depth); if (b) return true; } route.pop_back(); return false; } int main() { int n, m; cin >> n >> m; vector<vector<int>> edges(n); rep(_, m) { int a, b; cin >> a >> b; a--; b--; edges[a].push_back(b); } P mic(INT_MAX, 0); rep(i, n) cmin(mic, P(min_cycle(i, n, edges), i)); if (mic.first == INT_MAX) { cout << -1 << endl; ret(); } vector<int> route; bool b = dfs(mic.second, edges, route, mic.second, mic.first); assert(b); assert(route.size() == mic.first); cout << mic.first << endl; for (int i : route) cout << i + 1 << endl; }
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> // namespace mp = boost::multiprecision; using namespace std; const double PI = 3.14159265358979323846; typedef long long ll; const double EPS = 1e-9; #define rep(i, n) for (int i = 0; i < (n); ++i) // #define rep(i, n) for (ll i = 0; i < (n); ++i) typedef pair<ll, ll> P; const ll INF = 10e17; #define cmin(x, y) x = min(x, y) #define cmax(x, y) x = max(x, y) #define ret() return 0; double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; } std::istream &operator>>(std::istream &in, set<int> &o) { int a; in >> a; o.insert(a); return in; } std::istream &operator>>(std::istream &in, queue<int> &o) { ll a; in >> a; o.push(a); return in; } bool contain(set<int> &s, int a) { return s.find(a) != s.end(); } // ofstream outfile("log.txt"); // outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') // << rank << endl; // std::cout << std::bitset<8>(9); // const ll mod = 1e10; typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK; int min_cycle(int start, int n, vector<vector<int>> &edges) { vector<int> costs(n, INT_MAX); queue<P> q; q.push(P(start, 0)); while (!q.empty()) { P now = q.front(); q.pop(); for (int next : edges[now.first]) { if (costs[next] != INT_MAX) continue; costs[next] = now.second + 1; q.push(P(next, now.second + 1)); } } return costs[start]; } bool dfs(int goal, vector<vector<int>> &edges, vector<int> &route, int now, int depth) { if (now == goal && route.size() == depth) { return true; } if (depth == route.size()) { return false; } route.push_back(now); for (int next : edges[now]) { bool b = dfs(goal, edges, route, next, depth); if (b) return true; } route.pop_back(); return false; } int main() { int n, m; cin >> n >> m; vector<vector<int>> edges(n); rep(_, m) { int a, b; cin >> a >> b; a--; b--; edges[a].push_back(b); } P mic(INT_MAX, 0); rep(i, n) cmin(mic, P(min_cycle(i, n, edges), i)); if (mic.first == INT_MAX) { cout << -1 << endl; ret(); } vector<int> route; bool b = dfs(mic.second, edges, route, mic.second, mic.first); assert(b); assert(route.size() == mic.first); cout << mic.first << endl; for (int i : route) cout << i + 1 << endl; }
delete
68
69
68
68
0
p02902
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct Edge { int to; }; using Graph = vector<vector<Edge>>; const int INF = 1e8; // 幅優先探索 vector<int> dist; // 始点からの距離を記録 vector<int> pre; void bfs(const Graph &G, int s) { dist.assign((int)G.size(), INF); // 初期化 pre.assign((int)G.size(), -1); queue<int> que; que.emplace(s); // sから探索する dist[s] = 0; while (que.size() != 0) { // 幅優先探索 int now = que.front(); // 現在の状態 que.pop(); for (auto e : G[now]) { if (dist[e.to] == INF) { // 未探索の時のみ行う dist[e.to] = dist[now] + 1; pre[e.to] = now; que.emplace(e.to); } } } } /* get_path(prev, t) 入力:dijkstra で得た prev, ゴール t 出力: t への最短路のパス */ vector<int> get_path(const vector<int> &prev, int t) { vector<int> path; for (int cur = t; cur != -1; cur = prev[cur]) { path.push_back(cur); } reverse(path.begin(), path.end()); return path; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { // cin.tie(0); // ios::sync_with_stdio(false); int V, E; cin >> V >> E; Graph G(V); for (int i = 0; i < E; i++) { int a, b; cin >> a >> b; a--, b--; G[a].push_back({b}); // G[b].push_back({a}); } int ans = INF; int ans_v = 0; int ans_i = 0; for (int v = 0; v < V; v++) { for (int i = 0; i < (int)G[v].size(); i++) { // cout << "ans: " << ans << endl; Edge e = G[v][i]; G[v].erase(G[v].begin() + i); bfs(G, e.to); if (chmin(ans, dist[v] + 1)) { ans_v = v; ans_i = i; } G[v].insert((G[v].begin() + i), e); } } Edge e = G[ans_v][ans_i]; G[ans_v].erase(G[ans_v].begin() + ans_i); bfs(G, e.to); if (dist[ans_v] == INF) { cout << -1 << endl; return 0; } cout << dist[ans_v] + 1 << "\n"; for (auto i : get_path(pre, ans_v)) { cout << i + 1 << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Edge { int to; }; using Graph = vector<vector<Edge>>; const int INF = 1e8; // 幅優先探索 vector<int> dist; // 始点からの距離を記録 vector<int> pre; void bfs(const Graph &G, int s) { dist.assign((int)G.size(), INF); // 初期化 pre.assign((int)G.size(), -1); queue<int> que; que.emplace(s); // sから探索する dist[s] = 0; while (que.size() != 0) { // 幅優先探索 int now = que.front(); // 現在の状態 que.pop(); for (auto e : G[now]) { if (dist[e.to] == INF) { // 未探索の時のみ行う dist[e.to] = dist[now] + 1; pre[e.to] = now; que.emplace(e.to); } } } } /* get_path(prev, t) 入力:dijkstra で得た prev, ゴール t 出力: t への最短路のパス */ vector<int> get_path(const vector<int> &prev, int t) { vector<int> path; for (int cur = t; cur != -1; cur = prev[cur]) { path.push_back(cur); } reverse(path.begin(), path.end()); return path; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { // cin.tie(0); // ios::sync_with_stdio(false); int V, E; cin >> V >> E; Graph G(V); for (int i = 0; i < E; i++) { int a, b; cin >> a >> b; a--, b--; G[a].push_back({b}); // G[b].push_back({a}); } if (E == 0) { cout << -1 << endl; return 0; } int ans = INF; int ans_v = 0; int ans_i = 0; for (int v = 0; v < V; v++) { for (int i = 0; i < (int)G[v].size(); i++) { // cout << "ans: " << ans << endl; Edge e = G[v][i]; G[v].erase(G[v].begin() + i); bfs(G, e.to); if (chmin(ans, dist[v] + 1)) { ans_v = v; ans_i = i; } G[v].insert((G[v].begin() + i), e); } } Edge e = G[ans_v][ans_i]; G[ans_v].erase(G[ans_v].begin() + ans_i); bfs(G, e.to); if (dist[ans_v] == INF) { cout << -1 << endl; return 0; } cout << dist[ans_v] + 1 << "\n"; for (auto i : get_path(pre, ans_v)) { cout << i + 1 << "\n"; } return 0; }
insert
67
67
67
71
0
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; #define int long long #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrep(i, n) for (int(i) = ((n)-1); (i) >= 0; (i)--) #define itn int #define all(x) (x).begin(), (x).end() #define F first #define S second const long long INF = 1LL << 60; const int MOD = 1000000007; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } signed main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; int cnt = 0; int ans = 1005; vector<int> as; vector<vector<int>> g(1005, vector<int>()); rep(i, m) { int a, b; cin >> a >> b; g[a - 1].push_back(b - 1); } bool v[1005]; memset(v, 0, 1005); vector<int> mu; rep(i, n) { queue<pair<int, vector<int>>> q; q.push({i, mu}); while (!q.empty()) { cnt++; if (cnt == 5000000) { cnt = 0; break; } auto p = q.front(); q.pop(); if (p.S.size() >= ans) continue; if (v[p.F]) { if (p.F == i) { if (ans > p.S.size()) { ans = p.S.size(); as = p.S; } } continue; } v[i] = 1; p.S.push_back(p.F); for (int j = 0; j < g[p.F].size(); j++) { q.push({g[p.F][j], p.S}); } } memset(v, 0, 1005); } if (ans != 1005) { cout << ans << '\n'; rep(i, ans) cout << as[i] + 1 << ' '; } else cout << -1 << endl; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; #define int long long #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define rrep(i, n) for (int(i) = ((n)-1); (i) >= 0; (i)--) #define itn int #define all(x) (x).begin(), (x).end() #define F first #define S second const long long INF = 1LL << 60; const int MOD = 1000000007; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } signed main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; int cnt = 0; int ans = 1005; vector<int> as; vector<vector<int>> g(1005, vector<int>()); rep(i, m) { int a, b; cin >> a >> b; g[a - 1].push_back(b - 1); } bool v[1005]; memset(v, 0, 1005); vector<int> mu; rep(i, n) { queue<pair<int, vector<int>>> q; q.push({i, mu}); while (!q.empty()) { cnt++; if (cnt == 100000) { cnt = 0; break; } auto p = q.front(); q.pop(); if (p.S.size() >= ans) continue; if (v[p.F]) { if (p.F == i) { if (ans > p.S.size()) { ans = p.S.size(); as = p.S; } } continue; } v[i] = 1; p.S.push_back(p.F); for (int j = 0; j < g[p.F].size(); j++) { q.push({g[p.F][j], p.S}); } } memset(v, 0, 1005); } if (ans != 1005) { cout << ans << '\n'; rep(i, ans) cout << as[i] + 1 << ' '; } else cout << -1 << endl; }
replace
48
49
48
49
TLE
p02902
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int N = 2009; int n, m; vector<int> g[N]; int dfn[N], low[N], num, vis[N], co[N], col; stack<int> S; int siz[N]; void tarjan(int u) { // cout<<u<<endl; dfn[u] = low[u] = ++num; vis[u] = 1; S.push(u); for (auto v : g[u]) { if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]); else if (vis[v]) low[u] = min(low[u], dfn[v]); } if (dfn[u] == low[u]) { co[u] = ++col; siz[col]++; while (S.top() != u) { co[S.top()] = col; vis[S.top()] = 0; siz[col]++; S.pop(); } vis[u] = 0; S.pop(); } } // bool vv[N]; vector<int> ans; vector<int> uu; int pre[N], vv[N]; void print(int u, int fa) { ans.clear(); while (1) { ans.push_back(u); u = pre[u]; if (u == fa) break; } ans.push_back(fa); if (uu.size() == 0 || uu.size() > ans.size()) uu = ans; // cout<<uu.size()<<endl; // cout<<ans.size()<<endl; // for(auto v:ans)cout<<v<<endl; // exit(0); } void bfs(int u, int fa) { queue<int> q; q.push(u); vv[u] = 1; while (!q.empty()) { int now = q.front(); q.pop(); for (auto v : g[now]) if (vv[v] == 1) { vv[v] = 2; print(now, v); } else { if (vv[v] == 2) continue; if (co[v] != fa) continue; // if(vv[v])memset(vv,0,sizeof(vv)),bfs(now,fa); vv[v] = 1; q.push(v); pre[v] = now; } } } int main() { // freopen("tt.in","r",stdin),freopen("tt.out","w",stdout); cin >> n >> m; for (int i = 1, u, v; i <= m; i++) cin >> u >> v, g[u].push_back(v); for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i); for (int i = 1; i <= col; i++) if (siz[i] > 1) { // cout<<siz[i]<<endl; for (int j = 1; j <= n; j++) if (co[j] == i) { bfs(j, i); break; } break; // return 0; } if (uu.empty()) { cout << -1 << endl; return 0; } cout << uu.size() << endl; for (auto i : uu) cout << i << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2009; int n, m; vector<int> g[N]; int dfn[N], low[N], num, vis[N], co[N], col; stack<int> S; int siz[N]; void tarjan(int u) { // cout<<u<<endl; dfn[u] = low[u] = ++num; vis[u] = 1; S.push(u); for (auto v : g[u]) { if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]); else if (vis[v]) low[u] = min(low[u], dfn[v]); } if (dfn[u] == low[u]) { co[u] = ++col; siz[col]++; while (S.top() != u) { co[S.top()] = col; vis[S.top()] = 0; siz[col]++; S.pop(); } vis[u] = 0; S.pop(); } } // bool vv[N]; vector<int> ans; vector<int> uu; int pre[N], vv[N]; void print(int u, int fa) { ans.clear(); while (1) { ans.push_back(u); u = pre[u]; if (u == fa) break; if (ans.size() > 1000) break; } ans.push_back(fa); if (uu.size() == 0 || uu.size() > ans.size()) uu = ans; // cout<<uu.size()<<endl; // cout<<ans.size()<<endl; // for(auto v:ans)cout<<v<<endl; // exit(0); } void bfs(int u, int fa) { queue<int> q; q.push(u); vv[u] = 1; while (!q.empty()) { int now = q.front(); q.pop(); for (auto v : g[now]) if (vv[v] == 1) { vv[v] = 2; print(now, v); } else { if (vv[v] == 2) continue; if (co[v] != fa) continue; // if(vv[v])memset(vv,0,sizeof(vv)),bfs(now,fa); vv[v] = 1; q.push(v); pre[v] = now; } } } int main() { // freopen("tt.in","r",stdin),freopen("tt.out","w",stdout); cin >> n >> m; for (int i = 1, u, v; i <= m; i++) cin >> u >> v, g[u].push_back(v); for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i); for (int i = 1; i <= col; i++) if (siz[i] > 1) { // cout<<siz[i]<<endl; for (int j = 1; j <= n; j++) if (co[j] == i) { bfs(j, i); break; } break; // return 0; } if (uu.empty()) { cout << -1 << endl; return 0; } cout << uu.size() << endl; for (auto i : uu) cout << i << endl; return 0; }
insert
42
42
42
44
TLE
p02903
C++
Runtime Error
#include <bits/extc++.h> #include <bits/stdc++.h> using namespace __gnu_pbds; using namespace std; const long long llINF = 9223372036854775807; const int INF = 2147483647; const int maxn = 3e5 + 7; const int maxm = 1e7 + 7; const int ALP = 26; const long long mod = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-8; int H, W, A, B; int cnt[2]; int ans[107][107]; bool check() { for (int i = 1; i <= H; i++) { memset(cnt, 0, sizeof(cnt)); for (int j = 1; j <= W; j++) cnt[ans[i][j]]++; if (min(cnt[0], cnt[1]) != A) { return false; } } for (int j = 1; j <= W; j++) { memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= H; i++) cnt[ans[i][j]]++; if (min(cnt[0], cnt[1]) != B) return false; } return true; } int main() { // freopen("1.in","r",stdin); scanf("%d%d%d%d", &H, &W, &A, &B); for (int i = 1; i <= B; i++) { for (int j = 1; j <= W; j++) ans[i][j] = (j > A); } for (int i = B + 1; i <= H; i++) { for (int j = 1; j <= W; j++) ans[i][j] = (j <= A); } if (check()) { for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) printf("%d", ans[i][j]); printf("\n"); } } else { printf("-1\n"); } return 0; }
#include <bits/extc++.h> #include <bits/stdc++.h> using namespace __gnu_pbds; using namespace std; const long long llINF = 9223372036854775807; const int INF = 2147483647; const int maxn = 3e5 + 7; const int maxm = 1e7 + 7; const int ALP = 26; const long long mod = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-8; int H, W, A, B; int cnt[2]; int ans[1007][1007]; bool check() { for (int i = 1; i <= H; i++) { memset(cnt, 0, sizeof(cnt)); for (int j = 1; j <= W; j++) cnt[ans[i][j]]++; if (min(cnt[0], cnt[1]) != A) { return false; } } for (int j = 1; j <= W; j++) { memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= H; i++) cnt[ans[i][j]]++; if (min(cnt[0], cnt[1]) != B) return false; } return true; } int main() { // freopen("1.in","r",stdin); scanf("%d%d%d%d", &H, &W, &A, &B); for (int i = 1; i <= B; i++) { for (int j = 1; j <= W; j++) ans[i][j] = (j > A); } for (int i = B + 1; i <= H; i++) { for (int j = 1; j <= W; j++) ans[i][j] = (j <= A); } if (check()) { for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) printf("%d", ans[i][j]); printf("\n"); } } else { printf("-1\n"); } return 0; }
replace
16
17
16
17
0
p02903
Python
Runtime Error
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines sys.setrecursionlimit(10**7) h, w, a, b = map(int, readline().split()) x_cnt = [0] * h y_cnt = [0] * w ans = [""] * h if h == 1: print("1" * a + "0" * (w - a)) elif w == 1: for i in range(b): print("1") for i in range(h - b): print("0") else: for i in range(h): for j in range(w): print(ans) if y_cnt[j] < b and x_cnt[i] < a: y_cnt[j] += 1 x_cnt[i] += 1 ans[i] += "1" else: ans[i] += "0" if a != 0: if x_cnt[i] != a: print("No") exit() if b != 0: for i in range(w): if y_cnt[i] != b: print("No") exit() for i in range(h): print(ans[i])
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines sys.setrecursionlimit(10**7) h, w, a, b = map(int, readline().split()) ans_1 = "1" * a + "0" * (w - a) ans_2 = "0" * a + "1" * (w - a) for i in range(b): print(ans_1) for i in range(h - b): print(ans_2)
replace
8
39
8
14
0
p02903
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i) static const ll INFTY = 1L << 62L; ll solver(ll h, ll w, ll a, ll b) { rep(i, 0, b) { rep(j, 0, a) cout << '0'; rep(j, a, w) cout << '1'; cout << '\n'; } rep(i, b, h) { rep(j, 0, a) cout << '1'; rep(j, a, w) cout << '0'; cout << '\n'; } } int main() { cin.tie(0); ios::sync_with_stdio(false); ll h, w, a, b; cin >> h >> w >> a >> b; solver(h, w, a, b); }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i) static const ll INFTY = 1L << 62L; void solver(ll h, ll w, ll a, ll b) { rep(i, 0, b) { rep(j, 0, a) cout << '0'; rep(j, a, w) cout << '1'; cout << '\n'; } rep(i, b, h) { rep(j, 0, a) cout << '1'; rep(j, a, w) cout << '0'; cout << '\n'; } } int main() { cin.tie(0); ios::sync_with_stdio(false); ll h, w, a, b; cin >> h >> w >> a >> b; solver(h, w, a, b); }
replace
5
6
5
6
TLE
p02903
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main() { int H, W, A, B; cin >> H >> W >> A >> B; vector<vector<int>> ans(H, vector<int>(W)); for (int i = 0; i < A; i++) { for (int j = 0; j < B; j++) ans[j][i] = 1; } for (int i = A; i < H; i++) { for (int j = B; j < W; j++) ans[j][i] = 1; } for (vector<int> &i : ans) { for (int &j : i) cout << j; cout << endl; } }
#include <iostream> #include <vector> using namespace std; int main() { int H, W, A, B; cin >> H >> W >> A >> B; vector<vector<int>> ans(H, vector<int>(W)); for (int i = 0; i < A; i++) { for (int j = 0; j < B; j++) ans[j][i] = 1; } for (int i = A; i < W; i++) { for (int j = B; j < H; j++) ans[j][i] = 1; } for (vector<int> &i : ans) { for (int &j : i) cout << j; cout << endl; } }
replace
12
14
12
14
0
p02903
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int a[1000][1000]; int sumb[1000]; int main() { int h, w, A, b; cin >> h >> w >> A >> b; for (int j = 1; j <= A; j++) for (int i = 1; i <= b; i++) a[i][j] = 1; for (int j = A + 1; j <= w; j++) { for (int i = 1; i <= b; i++) a[i][j] = 0; } for (int i = 1; i <= A; i++) for (int j = b + 1; j <= h; j++) a[j][i] = 0; for (int i = A + 1; i <= w; i++) { for (int j = b + 1; j <= h; j++) a[j][i] = 1; } for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) cout << a[i][j]; cout << endl; } }
#include <bits/stdc++.h> using namespace std; int a[1006][1006]; int sumb[1006]; int main() { int h, w, A, b; cin >> h >> w >> A >> b; for (int j = 1; j <= A; j++) for (int i = 1; i <= b; i++) a[i][j] = 1; for (int j = A + 1; j <= w; j++) { for (int i = 1; i <= b; i++) a[i][j] = 0; } for (int i = 1; i <= A; i++) for (int j = b + 1; j <= h; j++) a[j][i] = 0; for (int i = A + 1; i <= w; i++) { for (int j = b + 1; j <= h; j++) a[j][i] = 1; } for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) cout << a[i][j]; cout << endl; } }
replace
2
4
2
4
0
p02903
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<int, int> Pi; typedef vector<ll> Vec; typedef vector<int> Vi; typedef vector<string> Vs; typedef vector<P> VP; typedef vector<vector<ll>> VV; typedef vector<vector<int>> VVi; typedef vector<vector<vector<ll>>> VVV; typedef vector<vector<vector<vector<ll>>>> VVVV; #define REP(i, a, b) for (ll i = (a); i < (b); i++) #define PER(i, a, b) for (ll i = (a); i >= (b); i--) #define rep(i, n) REP(i, 0, n) #define per(i, n) PER(i, n, 0) const ll INF = 1e18 + 18; const ll MAX = 100005; const ll MOD = 1000000007; #define Yes(n) cout << ((n) ? "Yes" : "No") << endl; #define YES(n) cout << ((n) ? "YES" : "NO") << endl; #define ALL(v) v.begin(), v.end() #define rALL(v) v.rbegin(), v.rend() #define pb(x) push_back(x) #define mp(a, b) make_pair(a, b) #define Each(a, b) for (auto &a : b) #define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i) #ifdef LOCAL #define dbg(x_) cerr << #x_ << ":" << x_ << endl; #define dbgmap(mp) \ cerr << #mp << ":" << endl; \ for (auto i = mp.begin(); i != mp.end(); ++i) { \ cerr << i->first << ":" << i->second << endl; \ } #define dbgarr(n, m, arr) \ rep(i, n) { \ rep(j, m) { cerr << arr[i][j] << " "; } \ cerr << endl; \ } #define dbgdp(n, arr) \ rep(i, n) { cerr << arr[i] << " "; } \ cerr << endl; #define dbgmint(n, arr) \ rep(i, n) { cerr << arr[i].x << " "; } \ cerr << endl; #define dbgarrmint(n, m, arr) \ rep(i, n) { \ rep(j, m) { cerr << arr[i][j].x << " "; } \ cerr << endl; \ } #else #define dbg(...) #define dbgmap(...) #define dbgarr(...) #define dbgdp(...) #define dbgmint(...) #define dbgarrmint(...) #endif #define out(a) cout << a << endl #define out2(a, b) cout << a << " " << b << endl #define vout(v) \ rep(i, v.size()) { cout << v[i] << " "; } \ cout << endl #define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define fi first #define se second template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s << "(" << p.first << ", " << p.second << ")"; } template <typename T> istream &operator>>(istream &i, vector<T> &v) { rep(j, v.size()) i >> v[j]; return i; } // vector template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) { int len = v.size(); for (int i = 0; i < len; ++i) { s << v[i]; if (i < len - 1) s << " "; } return s; } // 2 dimentional vector template <typename T> ostream &operator<<(ostream &s, const vector<vector<T>> &vv) { int len = vv.size(); for (int i = 0; i < len; ++i) { s << vv[i] << endl; } return s; } // mint struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint &operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; struct combination { vector<mint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { assert(n < MOD); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << std::setprecision(10); ll h, w, a, b; cin >> h >> w >> a >> b; VV g(h, Vec(w, 0)); rep(i, a) { rep(j, b) { g[i][j] = 1; } } dbg(g) REP(i, h - b - 1, h) { REP(j, w - a - 1, w) { g[i][j] = 1; } } rep(i, h) { rep(j, w) { cout << g[i][j]; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<int, int> Pi; typedef vector<ll> Vec; typedef vector<int> Vi; typedef vector<string> Vs; typedef vector<P> VP; typedef vector<vector<ll>> VV; typedef vector<vector<int>> VVi; typedef vector<vector<vector<ll>>> VVV; typedef vector<vector<vector<vector<ll>>>> VVVV; #define REP(i, a, b) for (ll i = (a); i < (b); i++) #define PER(i, a, b) for (ll i = (a); i >= (b); i--) #define rep(i, n) REP(i, 0, n) #define per(i, n) PER(i, n, 0) const ll INF = 1e18 + 18; const ll MAX = 100005; const ll MOD = 1000000007; #define Yes(n) cout << ((n) ? "Yes" : "No") << endl; #define YES(n) cout << ((n) ? "YES" : "NO") << endl; #define ALL(v) v.begin(), v.end() #define rALL(v) v.rbegin(), v.rend() #define pb(x) push_back(x) #define mp(a, b) make_pair(a, b) #define Each(a, b) for (auto &a : b) #define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i) #ifdef LOCAL #define dbg(x_) cerr << #x_ << ":" << x_ << endl; #define dbgmap(mp) \ cerr << #mp << ":" << endl; \ for (auto i = mp.begin(); i != mp.end(); ++i) { \ cerr << i->first << ":" << i->second << endl; \ } #define dbgarr(n, m, arr) \ rep(i, n) { \ rep(j, m) { cerr << arr[i][j] << " "; } \ cerr << endl; \ } #define dbgdp(n, arr) \ rep(i, n) { cerr << arr[i] << " "; } \ cerr << endl; #define dbgmint(n, arr) \ rep(i, n) { cerr << arr[i].x << " "; } \ cerr << endl; #define dbgarrmint(n, m, arr) \ rep(i, n) { \ rep(j, m) { cerr << arr[i][j].x << " "; } \ cerr << endl; \ } #else #define dbg(...) #define dbgmap(...) #define dbgarr(...) #define dbgdp(...) #define dbgmint(...) #define dbgarrmint(...) #endif #define out(a) cout << a << endl #define out2(a, b) cout << a << " " << b << endl #define vout(v) \ rep(i, v.size()) { cout << v[i] << " "; } \ cout << endl #define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end()) #define fi first #define se second template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s << "(" << p.first << ", " << p.second << ")"; } template <typename T> istream &operator>>(istream &i, vector<T> &v) { rep(j, v.size()) i >> v[j]; return i; } // vector template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) { int len = v.size(); for (int i = 0; i < len; ++i) { s << v[i]; if (i < len - 1) s << " "; } return s; } // 2 dimentional vector template <typename T> ostream &operator<<(ostream &s, const vector<vector<T>> &vv) { int len = vv.size(); for (int i = 0; i < len; ++i) { s << vv[i] << endl; } return s; } // mint struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint &operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint &operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; struct combination { vector<mint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { assert(n < MOD); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << std::setprecision(10); ll h, w, a, b; cin >> h >> w >> a >> b; VV g(h, Vec(w, 0)); rep(i, b) { rep(j, a) { g[i][j] = 1; } } dbg(g) REP(i, b, h) { REP(j, a, w) { g[i][j] = 1; } } rep(i, h) { rep(j, w) { cout << g[i][j]; } cout << endl; } return 0; }
replace
204
209
204
209
0
p02903
C++
Runtime Error
#define MOD 1000000007 #if 1 //------------------------------------------------------------ #define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; template <class T> struct Entry { static void Run() { T().Run(); } }; struct MyMain; #if defined(TEST) #include "test.hpp" #else signed main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(16); Entry<MyMain>::Run(); return 0; } #endif //------------------------------------------------------------ using ll = long long; #define int ll #define FOR(i, s, e) for (ll i = ll(s); i < ll(e); ++i) #define RFOR(i, s, e) for (ll i = ll(e) - 1; i >= ll(s); --i) #define REP(i, n) for (ll i = 0, i##_size = ll(n); i < i##_size; ++i) #define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i) #define INF INT64_MAX //------------------------------------------------------------ template <class T> struct ContainerHolder { bool flag = true; T &var; ContainerHolder(T &v) : var(v) {} operator bool() const { return flag; } T *operator->() { return &var; } }; template <class T> ContainerHolder<T> ContainerHold(T &v) { return ContainerHolder<T>(v); } #define CFOR(it, container) \ for (auto holder = ContainerHold(container); holder; holder.flag = false) \ for (auto it = holder->begin(), endIt = holder->end(); it != endIt; ++it) //------------------------------------------------------------ template <class T> struct arr : public vector<T> { arr() {} arr(initializer_list<T> il) : vector<T>(il) {} explicit arr(ll n, T v = T()) : vector<T>(n, v) {} T &operator()(int i) { return (*this)[i]; } T const &operator()(int i) const { return (*this)[i]; } void init(ll n, T v = T()) { this->clear(); this->resize(n, v); } ll sz() const { return (ll)this->size(); } void pb(T v) { this->push_back(v); } void sort() { std::sort(this->begin(), this->end()); } void sort(function<bool(T, T)> p) { std::sort(this->begin(), this->end(), p); } void rsort() { std::sort(this->begin(), this->end(), greater<T>()); } void unique_erase() { this->erase(std::unique(this->begin(), this->end()), this->end()); } bool next_permutation() { return std::next_permutation(this->begin(), this->end()); } // これ以下はソート済み前提 int lower_bound(T const &v, function<bool(T, T)> p) { return std::lower_bound(this->begin(), this->end(), v, p) - this->begin(); } int lower_bound(T const &v) { return std::lower_bound(this->begin(), this->end(), v) - this->begin(); } int upper_bound(T const &v, function<bool(T, T)> p) { return std::upper_bound(this->begin(), this->end(), v, p) - this->begin(); } int upper_bound(T const &v) { return std::upper_bound(this->begin(), this->end(), v) - this->begin(); } int find_nearest(T const &v) { int i = this->lower_bound(v); if (i >= sz()) { --i; } else if ((*this)[i] != v) { int p = i - 1; if (p >= 0) { int id = abs((*this)[i] - v); int pd = abs((*this)[p] - v); if (pd < id) { i = p; } } } return i; } // 見つからなければ-1 int find(T const &v) { int i = this->lower_bound(v); if (i >= sz()) { return -1; } if ((*this)[i] != v) { return -1; } return i; } }; using ints = arr<ll>; template <class T> struct que : public queue<T> { ll sz() const { return (ll)this->size(); } T popfront() { T v = this->front(); this->pop(); return v; } }; template <class A, class B> struct pr { union { A a; A key; A first; A x; }; union { B b; B value; B second; B y; }; pr() : a(A()), b(B()){}; pr(A a_, B b_) : a(a_), b(b_) {} pr(pr const &r) : a(r.a), b(r.b){}; pr(pair<A, B> const &r) : a(r.first), b(r.second){}; bool operator==(pr const &r) const { return a == r.a && b == r.b; } bool operator!=(pr const &r) const { return !((*this) == r); } bool operator<(pr const &r) const { if (a == r.a) { return b < r.b; } return a < r.a; } pr operator+(pr v) const { return pr(x, y) += v; } pr operator-(pr v) const { return pr(x, y) -= v; } pr &operator+=(pr v) { x += v.x; y += v.y; return *this; } pr &operator-=(pr v) { x -= v.x; y -= v.y; return *this; } void flip() { swap(x, y); } friend istream &operator>>(istream &is, pr &p) { is >> p.a >> p.b; return is; } friend ostream &operator<<(ostream &os, pr const &p) { os << p.a << " " << p.b; return os; } }; using pint = pr<ll, ll>; using pints = arr<pint>; template <class K, class V> struct dic : public map<K, V> { bool get(K const &k, V *v) { auto it = this->find(k); if (it != this->end()) { *v = it->second; return true; } return false; } }; template <class T> struct arr2 { vector<T> m_vec; int m_width; int m_height; arr2() : m_width(0), m_height(0) {} arr2(int w, int h, T const &value = T()) : m_width(w), m_height(h) { m_vec.resize(w * h, value); } arr2(arr2 const &r) { m_vec = r.m_vec; m_width = r.m_width; m_height = r.m_height; } arr2(arr2 &&r) { m_vec = move(r.m_vec); m_width = r.m_width; m_height = r.m_height; } arr2 &operator=(arr2 const &r) { m_vec = r.m_vec; m_width = r.m_width; m_height = r.m_height; return *this; } arr2 &operator=(arr2 &&r) { m_vec = move(r.m_vec); m_width = r.m_width; m_height = r.m_height; return *this; } bool operator==(arr2 const &r) const { return m_vec = r.m_vec; } bool operator<(arr2 const &r) const { if (m_width != r.m_width) { return m_width < r.m_width; } if (m_height != r.m_height) { return m_height < r.m_height; } REP(i, m_vec.size()) { if (m_vec[i] != r.m_vec[i]) { return m_vec[i] < r.m_vec[i]; } } return false; } pint size() const { return pint(m_width, m_height); } int width() const { return m_width; } int height() const { return m_height; } void init(int w, int h, T const &value = T()) { m_vec.clear(); m_vec.resize(w * h, value); m_width = w; m_height = h; } void init(pint size, T const &value = T()) { init(size.x, size.y, value); } T &operator()(int x, int y) { return m_vec[y * m_width + x]; } T const &operator()(int x, int y) const { return m_vec[y * m_width + x]; } T &operator()(pint p) { return m_vec[p.y * m_width + p.x]; } T const &operator()(pint p) const { return m_vec[p.y * m_width + p.x]; } T &operator[](pint p) { return m_vec[p.y * m_width + p.x]; } T const &operator[](pint p) const { return m_vec[p.y * m_width + p.x]; } bool isIn(int x, int y) const { return x >= 0 && x < m_width && y >= 0 && y < m_height; } bool isIn(pint p) const { return isIn(p.x, p.y); } bool isOut(int x, int y) const { return x < 0 || x >= m_width || y < 0 || y >= m_height; } bool isOut(pint p) const { return isOut(p.x, p.y); } struct iterator { private: arr2<T> *owner; public: pint pt; iterator(arr2<T> *owner_, pint pt_) : owner(owner_), pt(pt_) {} bool operator==(iterator const &r) const { return pt == r.pt; } bool operator!=(iterator const &r) const { return !((*this) == r); } void operator++() { ++pt.x; if (pt.x >= owner->width()) { ++pt.y; pt.x = 0; } } T &operator*() { return (*owner)(pt); } }; iterator begin() { return iterator(this, pint(0, 0)); } iterator end() { return iterator(this, pint(0, height())); } void disp(ostream &os) { REP(y, m_height) { REP(x, m_width) { os << setw(2) << (*this)(x, y) << " "; } os << endl; } os << endl; } }; const pints around4 = {pint(-1, 0), pint(0, -1), pint(1, 0), pint(0, 1)}; // 6角形グリッド用 const pints around6[2] = { {pint(0, -1), pint(1, -1), pint(-1, 0), pint(1, 0), pint(0, 1), pint(1, 1)}, {pint(-1, -1), pint(0, -1), pint(-1, 0), pint(1, 0), pint(-1, 1), pint(0, 1)}}; //------------------------------------------------------------ // ms int CurrentTime() { return clock() * 1000 / CLOCKS_PER_SEC; } //------------------------------------------------------------ template <class T> void chmin(T &a, T b) { if (b < a) { a = b; } } template <class T> void chmax(T &a, T b) { if (b > a) { a = b; } } constexpr int gcd(int a, int b) { if (a < 0) { a = -a; } if (b < 0) { b = -b; } if (a == 0) { return b; } if (b == 0) { return a; } while (int c = a % b) { a = b; b = c; } return b; } constexpr int lcm(int a, int b) { return a * b / gcd(a, b); } struct bfs { ints froms; ints steps; bfs(int N, int start, function<ints(int)> getAround) { froms.init(N, -1); steps.init(N, -1); que<ll> queue; queue.push(start); froms[start] = start; steps[start] = 0; while (queue.empty() == false) { ll p = queue.popfront(); for (ll n : getAround(p)) { if (steps[n] != -1) { continue; } froms[n] = p; steps[n] = steps[p] + 1; queue.push(n); } } } }; struct BfsResult { arr2<pint> froms; arr2<int> steps; }; template <class T> BfsResult bfs2(arr2<T> const &field, pints const &starts, function<bool(T)> canMove) { BfsResult result; result.froms.init(field.size(), pint(-1, -1)); result.steps.init(field.size(), -1); que<pint> queue; for (auto const &start : starts) { queue.push(start); result.froms(start) = start; result.steps(start) = 0; } while (queue.empty() == false) { auto p = queue.popfront(); for (auto a : around4) { pint n = p + a; if (field.isOut(n)) { continue; } if (result.steps(n) != -1) { continue; } if (!canMove(field(n))) { continue; } result.froms(n) = p; result.steps(n) = result.steps(p) + 1; queue.push(n); } } return result; } // getAround(count) -> around // getCost(from, to) -> cost int d9str(int N, int a, int b, function<ints(int)> getAround, function<int(int, int)> getCost) { struct Node { int point; int totalCost; bool operator<(Node const &n) const { return totalCost > n.totalCost; } }; ints minCosts(N, INT64_MAX); priority_queue<Node> que; minCosts(a) = 0; que.push(Node{a, 0}); while (!que.empty()) { Node node = que.top(); que.pop(); for (int next : getAround(node.point)) { int cost = getCost(node.point, next); if (cost < 0) { continue; } Node newNode; newNode.point = next; newNode.totalCost = node.totalCost + cost; if (newNode.totalCost < minCosts[newNode.point]) { minCosts[newNode.point] = newNode.totalCost; que.push(newNode); } } } if (minCosts[b] == INT64_MAX) { return -1; } return minCosts[b]; } // graph : 隣接行列。i=jは0、未接続はinfで初期化済みであること // return false なら負の閉路あり bool WarshallFloyd(arr2<int> &graph, int inf) { int N = graph.width(); REP(k, N) { REP(i, N) { REP(j, N) { if (graph(i, k) == inf || graph(k, j) == inf) { continue; } chmin(graph(i, j), graph(i, k) + graph(k, j)); } } } REP(i, N) { if (graph(i, i) < 0) { return false; } } return true; } //------------------------------------------------------------ // l <= x < r の間を探索 // 条件を満たすものが存在しなければrが返る int binsearch(int l, int r, function<bool(int)> predicate) { --l; while (r - l > 1) { int c = (l + r) / 2; if (predicate(c)) { r = c; } else { l = c; } } return r; } // func(x)の極値となるxを返す double num_analyze(function<double(double)> func, double l, double r, int step = 10000, double delta = 0.000001) { auto diff = [&](double x) { double a = func(x); double b = func(x + delta); return (b * b - a * a); }; REP(i, step) { double c = (l + r) / 2.0; double d = diff(c); if (d < 0) { l = c; } else { r = c; } } return l; } //------------------------------------------------------------ template <int M> struct modint { int raw; modint() { raw = 0; } modint(int v) { if (v < 0) { raw = (v % M) + M; } else if (v >= M) { raw = v % M; } else { raw = v; } } modint operator+(modint v) const { return modint(raw) += v; } modint operator-(modint v) const { return modint(raw) -= v; } modint operator*(modint v) const { return modint(raw) *= v; } modint &operator+=(modint v) { raw += v.raw; if (raw >= M) { raw -= M; } return *this; } modint &operator-=(modint v) { raw -= v.raw; if (raw < 0) { raw += M; } return *this; } modint &operator*=(modint v) { raw = (raw * v.raw) % M; return *this; } modint pow(int n) const { return modint::pow(raw, n); } static modint pow(int a, int n) { if (n < 0) { // not support abort(); } int r = 1; while (n) { if (n & 1) { r = (r * a) % M; } a = (a * a) % M; n >>= 1; } return modint(r); } modint inv() const { int a = raw; int b = M; int u = 1; int v = 0; while (b) { int t = a / b; a -= t * b; u -= t * v; swap(a, b); swap(u, v); } u %= M; if (u < 0) { u += M; } return u; } friend istream &operator>>(istream &is, modint &m) { int v; is >> v; m = modint(v); return is; } friend ostream &operator<<(ostream &os, modint const &m) { return os << m.raw; } }; using mint = modint<MOD>; using mints = arr<mint>; //------------------------------------------------------------ struct LoopIndex { int const N; LoopIndex(int N_) : N(N_) {} int left(int i) { return (i - 1 + N) % N; } int right(int i) { return (i + 1) % N; } }; //------------------------------------------------------------ struct UnionFind { struct Node { Node *parent = nullptr; int count = 1; Node *Root() { if (parent == nullptr) { return this; } return parent = parent->Root(); } }; vector<Node> nodes; UnionFind(int count) { nodes.resize(count); } void Join(int a, int b) { Node *rootA = nodes[a].Root(); Node *rootB = nodes[b].Root(); if (rootA == rootB) { return; } if (rootA->count > rootB->count) { swap(rootA, rootB); } rootB->count += rootA->count; rootA->parent = rootB; rootA->count = 0; } bool IsReachable(int a, int b) { Node *rootA = nodes[a].Root(); Node *rootB = nodes[b].Root(); return rootA == rootB; } int Count(int a) { return nodes[a].Root()->count; } }; //------------------------------------------------------------ struct Kruskal { struct Edge { int a; int b; int cost; bool operator<(Edge const &r) const { return cost < r.cost; } }; UnionFind uf; int totalCost; arr<dic<int, int>> G; Kruskal(int N, arr<Edge> edges) : uf(N) { edges.sort(); totalCost = 0; G.init(N); REP(i, edges.sz()) { Edge const &edge = edges[i]; if (uf.IsReachable(edge.a, edge.b)) { continue; } G[edge.a][edge.b] = edge.cost; G[edge.b][edge.a] = edge.cost; uf.Join(edge.a, edge.b); totalCost += edge.cost; } } }; //------------------------------------------------------------ arr<char> Eratosthenes(int N) { arr<char> result; result.init(N, 1); result(0) = result(1) = 0; FOR(i, 2, N) { if (result(i) == 0) { continue; } for (int v = i + i; v < N; v += i) { result(v) = 0; } } return result; } bool IsSo(int v) { if (v <= 1) { return false; } FOR(i, 2, sqrt(v) + 1) { if (v % i == 0) { return false; } } return true; }; //------------------------------------------------------------ template <class T> struct SumsImpl { private: int N; arr<T> S; public: SumsImpl(int N_) : N(N_), S(N + 1, 0) {} void Set(int i, T const &value) { S(i + 1) = S(i) + value; } // l <= x < r T Get(int l, int r) const { return S(r) - S(l); } }; using Sums = SumsImpl<int>; template <class T> struct ImosImpl { private: int N; arr<T> S; public: ImosImpl() {} ImosImpl(int N_) : N(N_), S(N + 1, 0) {} void init(int N_) { N = N_; S.init(N + 1, 0); } void Add(int i, T const &value) { S(i + 1) += value; } void Calc() { REP(i, N) { S(i + 1) += S(i); } } // l <= x < r T Get(int l, int r) const { return S(r) - S(l); } T Get(int i) const { return S(i); } }; using Imos = ImosImpl<int>; template <class T> struct Sums2Impl { private: arr2<T> sums; public: Sums2Impl(int W, int H) : sums(W + 1, H + 1) {} void Set(int x, int y, T const &value) { T s = value; s += sums(x + 1, y); s += sums(x, y + 1); s -= sums(x, y); sums(x + 1, y + 1) = s; } // l <= x < r // t <= y < b T Get(int l, int t, int r, int b) const { int s = sums(r, b); s -= sums(r, t); s -= sums(l, b); s += sums(l, t); return s; } }; using Sums2 = Sums2Impl<int>; template <class T> struct Imos2Impl { private: arr2<T> sums; public: Imos2Impl(int W, int H) : sums(W + 1, H + 1) {} void Add(int x, int y, T const &value) { sums(x + 1, y + 1) += value; } void Calc() { REP(y, sums.height() - 1) { REP(x, sums.width() - 1) { T s = 0; s += sums(x + 1, y); s += sums(x, y + 1); s -= sums(x, y); sums(x + 1, y + 1) += s; } } } // l <= x < r // t <= y < b T Get(int l, int t, int r, int b) const { int s = sums(r, b); s -= sums(r, t); s -= sums(l, b); s += sums(l, t); return s; } }; using Imos2 = Imos2Impl<int>; //------------------------------------------------------------ struct CombiTbl { mints fac; mints ifac; // 重複組み合わせを使う場合は N*2 を指定しないといけない CombiTbl(int N) : fac(N), ifac(N) { fac[0] = 1; ifac[0] = 1; FOR(i, 1, N) { fac[i] = fac[i - 1] * i; ifac[i] = fac[i].inv(); } } // a! mint Fac(int a) const { return fac(a); } // aPb mint Perm(int a, int b) const { return fac(a) * ifac(a - b); } // aCb mint Combi(int a, int b) const { return fac(a) * ifac(b) * ifac(a - b); } // aHb(重複組み合わせ) mint RepCombi(int a, int b) const { return Combi(a + b - 1, b); } }; //------------------------------------------------------------ template <class T> struct CompressImpl { private: arr<T> const &origins; arr<int> comp2orgIndex; arr<int> orgIndex2cmp; dic<T, int> orgValue2cmp; public: CompressImpl(arr<T> const &orgins_) : origins(orgins_) { int N = origins.sz(); comp2orgIndex.init(N); REP(i, N) { comp2orgIndex[i] = i; } comp2orgIndex.sort([&](int a, int b) { return origins[a] < origins[b]; }); orgIndex2cmp.init(N); REP(i, N) { orgIndex2cmp[comp2orgIndex[i]] = i; orgValue2cmp[origins[comp2orgIndex[i]]] = i; } } T const &Comp2OrgValue(int comp) const { return origins[comp2orgIndex[comp]]; } int Comp2OrgIndex(int comp) const { return comp2orgIndex[comp]; } int OrgValue2Comp(T const &orgValue) const { return orgValue2cmp.at(orgValue); } int OrgIndex2Comp(int orgIndex) const { return orgIndex2cmp[orgIndex]; } }; using Compress = CompressImpl<int>; //------------------------------------------------------------ template <class T, class COMPARERE> struct PriorityQueue : public priority_queue<T, vector<T>, COMPARERE> { void Cut(int size, function<void(T &)> deleter) { if ((int)this->c.size() > size) { int orgSize = (int)this->c.size(); for (int i = size; i < orgSize; ++i) { deleter(this->c[i]); } this->c.resize(size); } } }; template <class STATE, class COMPARE> struct BeamStackSearch { arr<STATE> statePool; arr<STATE *> stateStack; virtual ~BeamStackSearch() {} void Search(int poolSize, int thinkTime, int maxTurn, int maxTurnNode, arr<STATE *> *topStates) { int const startTime = CurrentTime(); int const endTime = startTime + thinkTime; statePool.init(poolSize); stateStack.init(poolSize); REP(i, poolSize) { stateStack[i] = &statePool[i]; } arr<PriorityQueue<STATE *, COMPARE>> queue(maxTurn + 1); queue[0].push(InitialState()); int expandCount = 0; try { arr<STATE *> nexts; int left = 0; while (CurrentTime() < endTime) { for (int z = 0; z < 100; ++z) { FOR(d, 0, maxTurn) { if (queue[d].empty()) { continue; } STATE *cur = queue[d].top(); queue[d].pop(); nexts.clear(); NextState(d, cur, &nexts); auto &nextQueue = queue[d + 1]; cur->refCount += (int32_t)nexts.sz(); REP(i, nexts.sz()) { nexts[i]->parent = cur; nextQueue.push(nexts[i]); } nextQueue.Cut(maxTurnNode, [this](STATE *state) { DeleteState(state); }); DeleteState(cur); ++expandCount; } } } } catch (...) { cerr << "Over" << endl; } cerr << "expand " << expandCount << endl; cerr << "time " << CurrentTime() - startTime << endl; topStates->clear(); topStates->reserve(maxTurn); STATE *state = queue[maxTurn].top(); while (state->parent) { topStates->push_back(state); state = state->parent; } reverse(topStates->begin(), topStates->end()); } STATE *NewState() { if (stateStack.empty()) { throw 0; } STATE *s = stateStack.back(); stateStack.pop_back(); s->refCount = 1; return s; } virtual void DeleteState(STATE *state) { --state->refCount; if (state->refCount > 0) { return; } if (state->parent) { DeleteState(state->parent); } stateStack.push_back(state); } virtual STATE *InitialState() = 0; virtual void NextState(int turn, STATE *currentState, arr<STATE *> *nextStates) = 0; }; template <class STATE> struct BeamStackStateBase { STATE *parent; int32_t refCount; }; //------------------------------------------------------------ struct XorShift { uint32_t x; uint32_t y; uint32_t z; uint32_t w; XorShift(uint32_t seed = 0) { x = 123456789; y = 362436069; z = 521288629; w = 88675123 + seed; } uint32_t operator()() { uint32_t t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } // l <= x < r uint32_t operator()(uint32_t l, uint32_t r) { return ((*this)() % (r - l)) + l; } }; struct Random { static XorShift &Instnce() { static XorShift x; return x; } static int Get() { return Instnce()(); } static int Get(uint32_t l, uint32_t r) { return Instnce()(l, r); } }; struct SimulatedAnnealing { using JudgeFixStateFunc = function<bool(int, bool *)>; virtual ~SimulatedAnnealing() {} void Simulate(int startTime, int timeLimit, double startTemp, double endTemp, int currentScore) { int loopCount = 0; int maxScore = currentScore; int nowTime = 0; int endTime = startTime + timeLimit; for (;; ++loopCount) { nowTime = CurrentTime(); if (nowTime >= endTime) { break; } double temp = startTemp + (endTemp - startTemp) * (nowTime - startTime) / double(timeLimit); for (int rp = 0; rp < 100; rp++) { CalcNextState([&](int newScore, bool *updateMaxState) { double prob = exp((newScore - currentScore) / temp); if (prob > (Random::Get() % 100000) / (double)100000) { if (newScore > maxScore) { maxScore = newScore; *updateMaxState = true; } currentScore = newScore; return true; } else { return false; } }); } } #if defined(TEST) cerr << "loopCount " << loopCount << endl; #endif } virtual void CalcNextState( function<bool(int newScore, bool *isMaxScore)> judgeFixState) = 0; }; //------------------------------------------------------------ #if defined(TEST) extern istream &mis; extern ostream &mos; #else istream &mis = cin; ostream &mos = cout; #endif //------------------------------------------------------------ struct OutputStream { template <class T> friend OutputStream &operator<<(OutputStream &s, T const &v) { mos << v << '\n'; return s; } } out; struct PrintStream { stringstream ss; template <class T> friend PrintStream &operator<<(PrintStream &s, T &&v) { s.ss << v << ' '; return s; } PrintStream &operator<<(PrintStream &(*manip)(PrintStream &)) { return (*manip)(*this); } } prn; PrintStream &en(PrintStream &s) { string str = s.ss.str(); s.ss = stringstream(); if (str.empty() == false) { str.pop_back(); } mos << str << '\n'; return s; } //------------------------------------------------------------ template <class T> struct in_base { T value; in_base() { mis >> value; } operator T() { return value; } }; struct in_int { int value; in_int() { mis >> value; } in_int(int add) { mis >> value; value += add; } operator int() { return value; } }; template <class A, class B> struct in_pr { pr<A, B> value; in_pr() { mis >> value.a >> value.b; } in_pr(int addA, int addB) { mis >> value.a >> value.b; value.a += addA; value.b += addB; } in_pr(bool flip) { mis >> value.b >> value.a; } in_pr(int addA, int addB, bool flip) { mis >> value.b >> value.a; value.a += addB; value.b += addA; } operator pr<A, B>() { return value; } }; template <class T> struct in_arr { arr<T> value; in_arr(int N) { value.init(N); REP(i, N) { mis >> value[i]; } } in_arr(int N, int add) { value.init(N); REP(i, N) { mis >> value[i]; value[i] += add; } } operator arr<T>() { return value; } }; template <> struct in_arr<pint> { arr<pint> value; in_arr(int N) { value.init(N); REP(i, N) { mis >> value[i]; } } in_arr(int N, int addA, int addB) { value.init(N); REP(i, N) { mis >> value[i].a >> value[i].b; value[i].a += addA; value[i].b += addB; } } in_arr(int N, bool flip) { value.init(N); REP(i, N) { mis >> value[i].b >> value[i].a; } } in_arr(int N, int addA, int addB, bool flip) { value.init(N); REP(i, N) { mis >> value[i].b >> value[i].a; value[i].a += addB; value[i].b += addA; } } operator arr<pint>() { return value; } }; template <class A, class B> struct in_dic { dic<A, B> value; in_dic(int N) { REP(i, N) { pair<A, B> pr; mis >> pr.first >> pr.second; value.insert(pr); } } in_dic(int N, int addA, int addB) { REP(i, N) { pair<A, B> pr; mis >> pr.first >> pr.second; pr.first += addA; pr.second += addB; value.insert(pr); } } in_dic(int N, bool flip) { REP(i, N) { pair<A, B> pr; mis >> pr.second >> pr.first; value.insert(pr); } } in_dic(int N, int addA, int addB, bool flip) { REP(i, N) { pair<A, B> pr; mis >> pr.second >> pr.first; pr.first += addB; pr.second += addA; value.insert(pr); } } operator dic<A, B>() { return value; } }; template <class T> struct in_arr2 { arr2<T> value; in_arr2(int H, int W) { value.init(W, H); REP(y, H) { REP(x, W) { mis >> value(x, y); } } } in_arr2(int H, int W, int add) { value.init(W, H); REP(y, H) { REP(x, W) { mis >> value(x, y); value(x, y) += add; } } } operator arr2<T>() { return value; } }; template <class T, class Tuple, size_t... Index> T MakeFromTupleImpl(Tuple &&t, index_sequence<Index...>) { return T(get<Index>(forward<Tuple>(t))...); } template <class T, class Tuple> T MakeFromTuple(Tuple &&t) { return MakeFromTupleImpl<T>( forward<Tuple>(t), make_index_sequence<tuple_size<remove_reference_t<Tuple>>::value>{}); } template <class... ARGS> struct inputWithParam { tuple<ARGS...> param; inputWithParam() {} explicit inputWithParam(tuple<ARGS...> &&param_) : param(param_) {} operator int() { return MakeFromTuple<in_int>(param); } operator double() { return MakeFromTuple<in_base<double>>(param); } operator string() { return MakeFromTuple<in_base<string>>(param); } operator char() { return MakeFromTuple<in_base<char>>(param); } template <class A, class B> operator pr<A, B>() { return MakeFromTuple<in_pr<A, B>>(param); } template <class T> operator arr<T>() { return MakeFromTuple<in_arr<T>>(param); } template <class A, class B> operator dic<A, B>() { return MakeFromTuple<in_dic<A, B>>(param); } template <class T> operator arr2<T>() { return MakeFromTuple<in_arr2<T>>(param); } }; struct input { operator int() { return inputWithParam<>(); } operator double() { return inputWithParam<>(); } operator string() { return inputWithParam<>(); } operator char() { return inputWithParam<>(); } template <class A, class B> operator pr<A, B>() { return inputWithParam<>(); } template <class... ARGS> inputWithParam<ARGS...> operator()(ARGS &&...args) { inputWithParam<ARGS...> withParam(forward_as_tuple(args...)); return withParam; } } in; //------------------------------------------------------------ #endif struct MyMain { int H = in; int W = in; int A = in; int B = in; void Run() { arr2<int> F(W, H); int x = 0; REP(y, H) { REP(offset, A) { F(x + offset, y) = 1; } x += A; x %= W; } REP(x, W) { int zero = 0; int one = 0; REP(y, H) { if (F(x, y) == 0) { zero++; } else { one++; } } int m = min(zero, one); if (m != B) { out << "No"; return; } } REP(y, H) { string l; REP(x, W) { l += F(x, y) == 0 ? '0' : '1'; } out << l; } } };
#define MOD 1000000007 #if 1 //------------------------------------------------------------ #define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; template <class T> struct Entry { static void Run() { T().Run(); } }; struct MyMain; #if defined(TEST) #include "test.hpp" #else signed main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(16); Entry<MyMain>::Run(); return 0; } #endif //------------------------------------------------------------ using ll = long long; #define int ll #define FOR(i, s, e) for (ll i = ll(s); i < ll(e); ++i) #define RFOR(i, s, e) for (ll i = ll(e) - 1; i >= ll(s); --i) #define REP(i, n) for (ll i = 0, i##_size = ll(n); i < i##_size; ++i) #define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i) #define INF INT64_MAX //------------------------------------------------------------ template <class T> struct ContainerHolder { bool flag = true; T &var; ContainerHolder(T &v) : var(v) {} operator bool() const { return flag; } T *operator->() { return &var; } }; template <class T> ContainerHolder<T> ContainerHold(T &v) { return ContainerHolder<T>(v); } #define CFOR(it, container) \ for (auto holder = ContainerHold(container); holder; holder.flag = false) \ for (auto it = holder->begin(), endIt = holder->end(); it != endIt; ++it) //------------------------------------------------------------ template <class T> struct arr : public vector<T> { arr() {} arr(initializer_list<T> il) : vector<T>(il) {} explicit arr(ll n, T v = T()) : vector<T>(n, v) {} T &operator()(int i) { return (*this)[i]; } T const &operator()(int i) const { return (*this)[i]; } void init(ll n, T v = T()) { this->clear(); this->resize(n, v); } ll sz() const { return (ll)this->size(); } void pb(T v) { this->push_back(v); } void sort() { std::sort(this->begin(), this->end()); } void sort(function<bool(T, T)> p) { std::sort(this->begin(), this->end(), p); } void rsort() { std::sort(this->begin(), this->end(), greater<T>()); } void unique_erase() { this->erase(std::unique(this->begin(), this->end()), this->end()); } bool next_permutation() { return std::next_permutation(this->begin(), this->end()); } // これ以下はソート済み前提 int lower_bound(T const &v, function<bool(T, T)> p) { return std::lower_bound(this->begin(), this->end(), v, p) - this->begin(); } int lower_bound(T const &v) { return std::lower_bound(this->begin(), this->end(), v) - this->begin(); } int upper_bound(T const &v, function<bool(T, T)> p) { return std::upper_bound(this->begin(), this->end(), v, p) - this->begin(); } int upper_bound(T const &v) { return std::upper_bound(this->begin(), this->end(), v) - this->begin(); } int find_nearest(T const &v) { int i = this->lower_bound(v); if (i >= sz()) { --i; } else if ((*this)[i] != v) { int p = i - 1; if (p >= 0) { int id = abs((*this)[i] - v); int pd = abs((*this)[p] - v); if (pd < id) { i = p; } } } return i; } // 見つからなければ-1 int find(T const &v) { int i = this->lower_bound(v); if (i >= sz()) { return -1; } if ((*this)[i] != v) { return -1; } return i; } }; using ints = arr<ll>; template <class T> struct que : public queue<T> { ll sz() const { return (ll)this->size(); } T popfront() { T v = this->front(); this->pop(); return v; } }; template <class A, class B> struct pr { union { A a; A key; A first; A x; }; union { B b; B value; B second; B y; }; pr() : a(A()), b(B()){}; pr(A a_, B b_) : a(a_), b(b_) {} pr(pr const &r) : a(r.a), b(r.b){}; pr(pair<A, B> const &r) : a(r.first), b(r.second){}; bool operator==(pr const &r) const { return a == r.a && b == r.b; } bool operator!=(pr const &r) const { return !((*this) == r); } bool operator<(pr const &r) const { if (a == r.a) { return b < r.b; } return a < r.a; } pr operator+(pr v) const { return pr(x, y) += v; } pr operator-(pr v) const { return pr(x, y) -= v; } pr &operator+=(pr v) { x += v.x; y += v.y; return *this; } pr &operator-=(pr v) { x -= v.x; y -= v.y; return *this; } void flip() { swap(x, y); } friend istream &operator>>(istream &is, pr &p) { is >> p.a >> p.b; return is; } friend ostream &operator<<(ostream &os, pr const &p) { os << p.a << " " << p.b; return os; } }; using pint = pr<ll, ll>; using pints = arr<pint>; template <class K, class V> struct dic : public map<K, V> { bool get(K const &k, V *v) { auto it = this->find(k); if (it != this->end()) { *v = it->second; return true; } return false; } }; template <class T> struct arr2 { vector<T> m_vec; int m_width; int m_height; arr2() : m_width(0), m_height(0) {} arr2(int w, int h, T const &value = T()) : m_width(w), m_height(h) { m_vec.resize(w * h, value); } arr2(arr2 const &r) { m_vec = r.m_vec; m_width = r.m_width; m_height = r.m_height; } arr2(arr2 &&r) { m_vec = move(r.m_vec); m_width = r.m_width; m_height = r.m_height; } arr2 &operator=(arr2 const &r) { m_vec = r.m_vec; m_width = r.m_width; m_height = r.m_height; return *this; } arr2 &operator=(arr2 &&r) { m_vec = move(r.m_vec); m_width = r.m_width; m_height = r.m_height; return *this; } bool operator==(arr2 const &r) const { return m_vec = r.m_vec; } bool operator<(arr2 const &r) const { if (m_width != r.m_width) { return m_width < r.m_width; } if (m_height != r.m_height) { return m_height < r.m_height; } REP(i, m_vec.size()) { if (m_vec[i] != r.m_vec[i]) { return m_vec[i] < r.m_vec[i]; } } return false; } pint size() const { return pint(m_width, m_height); } int width() const { return m_width; } int height() const { return m_height; } void init(int w, int h, T const &value = T()) { m_vec.clear(); m_vec.resize(w * h, value); m_width = w; m_height = h; } void init(pint size, T const &value = T()) { init(size.x, size.y, value); } T &operator()(int x, int y) { return m_vec[y * m_width + x]; } T const &operator()(int x, int y) const { return m_vec[y * m_width + x]; } T &operator()(pint p) { return m_vec[p.y * m_width + p.x]; } T const &operator()(pint p) const { return m_vec[p.y * m_width + p.x]; } T &operator[](pint p) { return m_vec[p.y * m_width + p.x]; } T const &operator[](pint p) const { return m_vec[p.y * m_width + p.x]; } bool isIn(int x, int y) const { return x >= 0 && x < m_width && y >= 0 && y < m_height; } bool isIn(pint p) const { return isIn(p.x, p.y); } bool isOut(int x, int y) const { return x < 0 || x >= m_width || y < 0 || y >= m_height; } bool isOut(pint p) const { return isOut(p.x, p.y); } struct iterator { private: arr2<T> *owner; public: pint pt; iterator(arr2<T> *owner_, pint pt_) : owner(owner_), pt(pt_) {} bool operator==(iterator const &r) const { return pt == r.pt; } bool operator!=(iterator const &r) const { return !((*this) == r); } void operator++() { ++pt.x; if (pt.x >= owner->width()) { ++pt.y; pt.x = 0; } } T &operator*() { return (*owner)(pt); } }; iterator begin() { return iterator(this, pint(0, 0)); } iterator end() { return iterator(this, pint(0, height())); } void disp(ostream &os) { REP(y, m_height) { REP(x, m_width) { os << setw(2) << (*this)(x, y) << " "; } os << endl; } os << endl; } }; const pints around4 = {pint(-1, 0), pint(0, -1), pint(1, 0), pint(0, 1)}; // 6角形グリッド用 const pints around6[2] = { {pint(0, -1), pint(1, -1), pint(-1, 0), pint(1, 0), pint(0, 1), pint(1, 1)}, {pint(-1, -1), pint(0, -1), pint(-1, 0), pint(1, 0), pint(-1, 1), pint(0, 1)}}; //------------------------------------------------------------ // ms int CurrentTime() { return clock() * 1000 / CLOCKS_PER_SEC; } //------------------------------------------------------------ template <class T> void chmin(T &a, T b) { if (b < a) { a = b; } } template <class T> void chmax(T &a, T b) { if (b > a) { a = b; } } constexpr int gcd(int a, int b) { if (a < 0) { a = -a; } if (b < 0) { b = -b; } if (a == 0) { return b; } if (b == 0) { return a; } while (int c = a % b) { a = b; b = c; } return b; } constexpr int lcm(int a, int b) { return a * b / gcd(a, b); } struct bfs { ints froms; ints steps; bfs(int N, int start, function<ints(int)> getAround) { froms.init(N, -1); steps.init(N, -1); que<ll> queue; queue.push(start); froms[start] = start; steps[start] = 0; while (queue.empty() == false) { ll p = queue.popfront(); for (ll n : getAround(p)) { if (steps[n] != -1) { continue; } froms[n] = p; steps[n] = steps[p] + 1; queue.push(n); } } } }; struct BfsResult { arr2<pint> froms; arr2<int> steps; }; template <class T> BfsResult bfs2(arr2<T> const &field, pints const &starts, function<bool(T)> canMove) { BfsResult result; result.froms.init(field.size(), pint(-1, -1)); result.steps.init(field.size(), -1); que<pint> queue; for (auto const &start : starts) { queue.push(start); result.froms(start) = start; result.steps(start) = 0; } while (queue.empty() == false) { auto p = queue.popfront(); for (auto a : around4) { pint n = p + a; if (field.isOut(n)) { continue; } if (result.steps(n) != -1) { continue; } if (!canMove(field(n))) { continue; } result.froms(n) = p; result.steps(n) = result.steps(p) + 1; queue.push(n); } } return result; } // getAround(count) -> around // getCost(from, to) -> cost int d9str(int N, int a, int b, function<ints(int)> getAround, function<int(int, int)> getCost) { struct Node { int point; int totalCost; bool operator<(Node const &n) const { return totalCost > n.totalCost; } }; ints minCosts(N, INT64_MAX); priority_queue<Node> que; minCosts(a) = 0; que.push(Node{a, 0}); while (!que.empty()) { Node node = que.top(); que.pop(); for (int next : getAround(node.point)) { int cost = getCost(node.point, next); if (cost < 0) { continue; } Node newNode; newNode.point = next; newNode.totalCost = node.totalCost + cost; if (newNode.totalCost < minCosts[newNode.point]) { minCosts[newNode.point] = newNode.totalCost; que.push(newNode); } } } if (minCosts[b] == INT64_MAX) { return -1; } return minCosts[b]; } // graph : 隣接行列。i=jは0、未接続はinfで初期化済みであること // return false なら負の閉路あり bool WarshallFloyd(arr2<int> &graph, int inf) { int N = graph.width(); REP(k, N) { REP(i, N) { REP(j, N) { if (graph(i, k) == inf || graph(k, j) == inf) { continue; } chmin(graph(i, j), graph(i, k) + graph(k, j)); } } } REP(i, N) { if (graph(i, i) < 0) { return false; } } return true; } //------------------------------------------------------------ // l <= x < r の間を探索 // 条件を満たすものが存在しなければrが返る int binsearch(int l, int r, function<bool(int)> predicate) { --l; while (r - l > 1) { int c = (l + r) / 2; if (predicate(c)) { r = c; } else { l = c; } } return r; } // func(x)の極値となるxを返す double num_analyze(function<double(double)> func, double l, double r, int step = 10000, double delta = 0.000001) { auto diff = [&](double x) { double a = func(x); double b = func(x + delta); return (b * b - a * a); }; REP(i, step) { double c = (l + r) / 2.0; double d = diff(c); if (d < 0) { l = c; } else { r = c; } } return l; } //------------------------------------------------------------ template <int M> struct modint { int raw; modint() { raw = 0; } modint(int v) { if (v < 0) { raw = (v % M) + M; } else if (v >= M) { raw = v % M; } else { raw = v; } } modint operator+(modint v) const { return modint(raw) += v; } modint operator-(modint v) const { return modint(raw) -= v; } modint operator*(modint v) const { return modint(raw) *= v; } modint &operator+=(modint v) { raw += v.raw; if (raw >= M) { raw -= M; } return *this; } modint &operator-=(modint v) { raw -= v.raw; if (raw < 0) { raw += M; } return *this; } modint &operator*=(modint v) { raw = (raw * v.raw) % M; return *this; } modint pow(int n) const { return modint::pow(raw, n); } static modint pow(int a, int n) { if (n < 0) { // not support abort(); } int r = 1; while (n) { if (n & 1) { r = (r * a) % M; } a = (a * a) % M; n >>= 1; } return modint(r); } modint inv() const { int a = raw; int b = M; int u = 1; int v = 0; while (b) { int t = a / b; a -= t * b; u -= t * v; swap(a, b); swap(u, v); } u %= M; if (u < 0) { u += M; } return u; } friend istream &operator>>(istream &is, modint &m) { int v; is >> v; m = modint(v); return is; } friend ostream &operator<<(ostream &os, modint const &m) { return os << m.raw; } }; using mint = modint<MOD>; using mints = arr<mint>; //------------------------------------------------------------ struct LoopIndex { int const N; LoopIndex(int N_) : N(N_) {} int left(int i) { return (i - 1 + N) % N; } int right(int i) { return (i + 1) % N; } }; //------------------------------------------------------------ struct UnionFind { struct Node { Node *parent = nullptr; int count = 1; Node *Root() { if (parent == nullptr) { return this; } return parent = parent->Root(); } }; vector<Node> nodes; UnionFind(int count) { nodes.resize(count); } void Join(int a, int b) { Node *rootA = nodes[a].Root(); Node *rootB = nodes[b].Root(); if (rootA == rootB) { return; } if (rootA->count > rootB->count) { swap(rootA, rootB); } rootB->count += rootA->count; rootA->parent = rootB; rootA->count = 0; } bool IsReachable(int a, int b) { Node *rootA = nodes[a].Root(); Node *rootB = nodes[b].Root(); return rootA == rootB; } int Count(int a) { return nodes[a].Root()->count; } }; //------------------------------------------------------------ struct Kruskal { struct Edge { int a; int b; int cost; bool operator<(Edge const &r) const { return cost < r.cost; } }; UnionFind uf; int totalCost; arr<dic<int, int>> G; Kruskal(int N, arr<Edge> edges) : uf(N) { edges.sort(); totalCost = 0; G.init(N); REP(i, edges.sz()) { Edge const &edge = edges[i]; if (uf.IsReachable(edge.a, edge.b)) { continue; } G[edge.a][edge.b] = edge.cost; G[edge.b][edge.a] = edge.cost; uf.Join(edge.a, edge.b); totalCost += edge.cost; } } }; //------------------------------------------------------------ arr<char> Eratosthenes(int N) { arr<char> result; result.init(N, 1); result(0) = result(1) = 0; FOR(i, 2, N) { if (result(i) == 0) { continue; } for (int v = i + i; v < N; v += i) { result(v) = 0; } } return result; } bool IsSo(int v) { if (v <= 1) { return false; } FOR(i, 2, sqrt(v) + 1) { if (v % i == 0) { return false; } } return true; }; //------------------------------------------------------------ template <class T> struct SumsImpl { private: int N; arr<T> S; public: SumsImpl(int N_) : N(N_), S(N + 1, 0) {} void Set(int i, T const &value) { S(i + 1) = S(i) + value; } // l <= x < r T Get(int l, int r) const { return S(r) - S(l); } }; using Sums = SumsImpl<int>; template <class T> struct ImosImpl { private: int N; arr<T> S; public: ImosImpl() {} ImosImpl(int N_) : N(N_), S(N + 1, 0) {} void init(int N_) { N = N_; S.init(N + 1, 0); } void Add(int i, T const &value) { S(i + 1) += value; } void Calc() { REP(i, N) { S(i + 1) += S(i); } } // l <= x < r T Get(int l, int r) const { return S(r) - S(l); } T Get(int i) const { return S(i); } }; using Imos = ImosImpl<int>; template <class T> struct Sums2Impl { private: arr2<T> sums; public: Sums2Impl(int W, int H) : sums(W + 1, H + 1) {} void Set(int x, int y, T const &value) { T s = value; s += sums(x + 1, y); s += sums(x, y + 1); s -= sums(x, y); sums(x + 1, y + 1) = s; } // l <= x < r // t <= y < b T Get(int l, int t, int r, int b) const { int s = sums(r, b); s -= sums(r, t); s -= sums(l, b); s += sums(l, t); return s; } }; using Sums2 = Sums2Impl<int>; template <class T> struct Imos2Impl { private: arr2<T> sums; public: Imos2Impl(int W, int H) : sums(W + 1, H + 1) {} void Add(int x, int y, T const &value) { sums(x + 1, y + 1) += value; } void Calc() { REP(y, sums.height() - 1) { REP(x, sums.width() - 1) { T s = 0; s += sums(x + 1, y); s += sums(x, y + 1); s -= sums(x, y); sums(x + 1, y + 1) += s; } } } // l <= x < r // t <= y < b T Get(int l, int t, int r, int b) const { int s = sums(r, b); s -= sums(r, t); s -= sums(l, b); s += sums(l, t); return s; } }; using Imos2 = Imos2Impl<int>; //------------------------------------------------------------ struct CombiTbl { mints fac; mints ifac; // 重複組み合わせを使う場合は N*2 を指定しないといけない CombiTbl(int N) : fac(N), ifac(N) { fac[0] = 1; ifac[0] = 1; FOR(i, 1, N) { fac[i] = fac[i - 1] * i; ifac[i] = fac[i].inv(); } } // a! mint Fac(int a) const { return fac(a); } // aPb mint Perm(int a, int b) const { return fac(a) * ifac(a - b); } // aCb mint Combi(int a, int b) const { return fac(a) * ifac(b) * ifac(a - b); } // aHb(重複組み合わせ) mint RepCombi(int a, int b) const { return Combi(a + b - 1, b); } }; //------------------------------------------------------------ template <class T> struct CompressImpl { private: arr<T> const &origins; arr<int> comp2orgIndex; arr<int> orgIndex2cmp; dic<T, int> orgValue2cmp; public: CompressImpl(arr<T> const &orgins_) : origins(orgins_) { int N = origins.sz(); comp2orgIndex.init(N); REP(i, N) { comp2orgIndex[i] = i; } comp2orgIndex.sort([&](int a, int b) { return origins[a] < origins[b]; }); orgIndex2cmp.init(N); REP(i, N) { orgIndex2cmp[comp2orgIndex[i]] = i; orgValue2cmp[origins[comp2orgIndex[i]]] = i; } } T const &Comp2OrgValue(int comp) const { return origins[comp2orgIndex[comp]]; } int Comp2OrgIndex(int comp) const { return comp2orgIndex[comp]; } int OrgValue2Comp(T const &orgValue) const { return orgValue2cmp.at(orgValue); } int OrgIndex2Comp(int orgIndex) const { return orgIndex2cmp[orgIndex]; } }; using Compress = CompressImpl<int>; //------------------------------------------------------------ template <class T, class COMPARERE> struct PriorityQueue : public priority_queue<T, vector<T>, COMPARERE> { void Cut(int size, function<void(T &)> deleter) { if ((int)this->c.size() > size) { int orgSize = (int)this->c.size(); for (int i = size; i < orgSize; ++i) { deleter(this->c[i]); } this->c.resize(size); } } }; template <class STATE, class COMPARE> struct BeamStackSearch { arr<STATE> statePool; arr<STATE *> stateStack; virtual ~BeamStackSearch() {} void Search(int poolSize, int thinkTime, int maxTurn, int maxTurnNode, arr<STATE *> *topStates) { int const startTime = CurrentTime(); int const endTime = startTime + thinkTime; statePool.init(poolSize); stateStack.init(poolSize); REP(i, poolSize) { stateStack[i] = &statePool[i]; } arr<PriorityQueue<STATE *, COMPARE>> queue(maxTurn + 1); queue[0].push(InitialState()); int expandCount = 0; try { arr<STATE *> nexts; int left = 0; while (CurrentTime() < endTime) { for (int z = 0; z < 100; ++z) { FOR(d, 0, maxTurn) { if (queue[d].empty()) { continue; } STATE *cur = queue[d].top(); queue[d].pop(); nexts.clear(); NextState(d, cur, &nexts); auto &nextQueue = queue[d + 1]; cur->refCount += (int32_t)nexts.sz(); REP(i, nexts.sz()) { nexts[i]->parent = cur; nextQueue.push(nexts[i]); } nextQueue.Cut(maxTurnNode, [this](STATE *state) { DeleteState(state); }); DeleteState(cur); ++expandCount; } } } } catch (...) { cerr << "Over" << endl; } cerr << "expand " << expandCount << endl; cerr << "time " << CurrentTime() - startTime << endl; topStates->clear(); topStates->reserve(maxTurn); STATE *state = queue[maxTurn].top(); while (state->parent) { topStates->push_back(state); state = state->parent; } reverse(topStates->begin(), topStates->end()); } STATE *NewState() { if (stateStack.empty()) { throw 0; } STATE *s = stateStack.back(); stateStack.pop_back(); s->refCount = 1; return s; } virtual void DeleteState(STATE *state) { --state->refCount; if (state->refCount > 0) { return; } if (state->parent) { DeleteState(state->parent); } stateStack.push_back(state); } virtual STATE *InitialState() = 0; virtual void NextState(int turn, STATE *currentState, arr<STATE *> *nextStates) = 0; }; template <class STATE> struct BeamStackStateBase { STATE *parent; int32_t refCount; }; //------------------------------------------------------------ struct XorShift { uint32_t x; uint32_t y; uint32_t z; uint32_t w; XorShift(uint32_t seed = 0) { x = 123456789; y = 362436069; z = 521288629; w = 88675123 + seed; } uint32_t operator()() { uint32_t t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } // l <= x < r uint32_t operator()(uint32_t l, uint32_t r) { return ((*this)() % (r - l)) + l; } }; struct Random { static XorShift &Instnce() { static XorShift x; return x; } static int Get() { return Instnce()(); } static int Get(uint32_t l, uint32_t r) { return Instnce()(l, r); } }; struct SimulatedAnnealing { using JudgeFixStateFunc = function<bool(int, bool *)>; virtual ~SimulatedAnnealing() {} void Simulate(int startTime, int timeLimit, double startTemp, double endTemp, int currentScore) { int loopCount = 0; int maxScore = currentScore; int nowTime = 0; int endTime = startTime + timeLimit; for (;; ++loopCount) { nowTime = CurrentTime(); if (nowTime >= endTime) { break; } double temp = startTemp + (endTemp - startTemp) * (nowTime - startTime) / double(timeLimit); for (int rp = 0; rp < 100; rp++) { CalcNextState([&](int newScore, bool *updateMaxState) { double prob = exp((newScore - currentScore) / temp); if (prob > (Random::Get() % 100000) / (double)100000) { if (newScore > maxScore) { maxScore = newScore; *updateMaxState = true; } currentScore = newScore; return true; } else { return false; } }); } } #if defined(TEST) cerr << "loopCount " << loopCount << endl; #endif } virtual void CalcNextState( function<bool(int newScore, bool *isMaxScore)> judgeFixState) = 0; }; //------------------------------------------------------------ #if defined(TEST) extern istream &mis; extern ostream &mos; #else istream &mis = cin; ostream &mos = cout; #endif //------------------------------------------------------------ struct OutputStream { template <class T> friend OutputStream &operator<<(OutputStream &s, T const &v) { mos << v << '\n'; return s; } } out; struct PrintStream { stringstream ss; template <class T> friend PrintStream &operator<<(PrintStream &s, T &&v) { s.ss << v << ' '; return s; } PrintStream &operator<<(PrintStream &(*manip)(PrintStream &)) { return (*manip)(*this); } } prn; PrintStream &en(PrintStream &s) { string str = s.ss.str(); s.ss = stringstream(); if (str.empty() == false) { str.pop_back(); } mos << str << '\n'; return s; } //------------------------------------------------------------ template <class T> struct in_base { T value; in_base() { mis >> value; } operator T() { return value; } }; struct in_int { int value; in_int() { mis >> value; } in_int(int add) { mis >> value; value += add; } operator int() { return value; } }; template <class A, class B> struct in_pr { pr<A, B> value; in_pr() { mis >> value.a >> value.b; } in_pr(int addA, int addB) { mis >> value.a >> value.b; value.a += addA; value.b += addB; } in_pr(bool flip) { mis >> value.b >> value.a; } in_pr(int addA, int addB, bool flip) { mis >> value.b >> value.a; value.a += addB; value.b += addA; } operator pr<A, B>() { return value; } }; template <class T> struct in_arr { arr<T> value; in_arr(int N) { value.init(N); REP(i, N) { mis >> value[i]; } } in_arr(int N, int add) { value.init(N); REP(i, N) { mis >> value[i]; value[i] += add; } } operator arr<T>() { return value; } }; template <> struct in_arr<pint> { arr<pint> value; in_arr(int N) { value.init(N); REP(i, N) { mis >> value[i]; } } in_arr(int N, int addA, int addB) { value.init(N); REP(i, N) { mis >> value[i].a >> value[i].b; value[i].a += addA; value[i].b += addB; } } in_arr(int N, bool flip) { value.init(N); REP(i, N) { mis >> value[i].b >> value[i].a; } } in_arr(int N, int addA, int addB, bool flip) { value.init(N); REP(i, N) { mis >> value[i].b >> value[i].a; value[i].a += addB; value[i].b += addA; } } operator arr<pint>() { return value; } }; template <class A, class B> struct in_dic { dic<A, B> value; in_dic(int N) { REP(i, N) { pair<A, B> pr; mis >> pr.first >> pr.second; value.insert(pr); } } in_dic(int N, int addA, int addB) { REP(i, N) { pair<A, B> pr; mis >> pr.first >> pr.second; pr.first += addA; pr.second += addB; value.insert(pr); } } in_dic(int N, bool flip) { REP(i, N) { pair<A, B> pr; mis >> pr.second >> pr.first; value.insert(pr); } } in_dic(int N, int addA, int addB, bool flip) { REP(i, N) { pair<A, B> pr; mis >> pr.second >> pr.first; pr.first += addB; pr.second += addA; value.insert(pr); } } operator dic<A, B>() { return value; } }; template <class T> struct in_arr2 { arr2<T> value; in_arr2(int H, int W) { value.init(W, H); REP(y, H) { REP(x, W) { mis >> value(x, y); } } } in_arr2(int H, int W, int add) { value.init(W, H); REP(y, H) { REP(x, W) { mis >> value(x, y); value(x, y) += add; } } } operator arr2<T>() { return value; } }; template <class T, class Tuple, size_t... Index> T MakeFromTupleImpl(Tuple &&t, index_sequence<Index...>) { return T(get<Index>(forward<Tuple>(t))...); } template <class T, class Tuple> T MakeFromTuple(Tuple &&t) { return MakeFromTupleImpl<T>( forward<Tuple>(t), make_index_sequence<tuple_size<remove_reference_t<Tuple>>::value>{}); } template <class... ARGS> struct inputWithParam { tuple<ARGS...> param; inputWithParam() {} explicit inputWithParam(tuple<ARGS...> &&param_) : param(param_) {} operator int() { return MakeFromTuple<in_int>(param); } operator double() { return MakeFromTuple<in_base<double>>(param); } operator string() { return MakeFromTuple<in_base<string>>(param); } operator char() { return MakeFromTuple<in_base<char>>(param); } template <class A, class B> operator pr<A, B>() { return MakeFromTuple<in_pr<A, B>>(param); } template <class T> operator arr<T>() { return MakeFromTuple<in_arr<T>>(param); } template <class A, class B> operator dic<A, B>() { return MakeFromTuple<in_dic<A, B>>(param); } template <class T> operator arr2<T>() { return MakeFromTuple<in_arr2<T>>(param); } }; struct input { operator int() { return inputWithParam<>(); } operator double() { return inputWithParam<>(); } operator string() { return inputWithParam<>(); } operator char() { return inputWithParam<>(); } template <class A, class B> operator pr<A, B>() { return inputWithParam<>(); } template <class... ARGS> inputWithParam<ARGS...> operator()(ARGS &&...args) { inputWithParam<ARGS...> withParam(forward_as_tuple(args...)); return withParam; } } in; //------------------------------------------------------------ #endif struct MyMain { int H = in; int W = in; int A = in; int B = in; void Run() { FOR(y, 0, B) { string s; FOR(x, 0, A) { s += '0'; } FOR(x, A, W) { s += '1'; } out << s; } FOR(y, B, H) { string s; FOR(x, 0, A) { s += '1'; } FOR(x, A, W) { s += '0'; } out << s; } } };
replace
1,364
1,398
1,364
1,378
0
p02903
C++
Runtime Error
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define ll long long #define rep(i, n) for (int i = 0; i < int(n); i++) #define vi vector<int> #define INF (1 << 30) - 1 using namespace std; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { cin.tie(0), ios::sync_with_stdio(false); int h, w, a, b; cin >> h >> w >> a >> b; vector<vi> ans(h, vi(w)); if (b == 0) { rep(i, h) rep(j, a) ans[i][j] = 1; } else { int start = -a; rep(i, h) { if (i % b == 0) start += a; rep(j, a) { ans[i][j + start] = 1; } } } bool res = true; rep(i, w) { int cnt = 0; rep(j, h) cnt += ans[j][i]; if (b != min(cnt, h - cnt)) res = false; } rep(i, h) { int cnt = 0; rep(j, w) cnt += ans[i][j]; if (a != min(cnt, w - cnt)) res = false; } if (res) { rep(i, h) { rep(j, w) cout << ans[i][j]; cout << endl; } } else cout << "No\n"; // cout << "\n"; return 0; }
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define ll long long #define rep(i, n) for (int i = 0; i < int(n); i++) #define vi vector<int> #define INF (1 << 30) - 1 using namespace std; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { cin.tie(0), ios::sync_with_stdio(false); int h, w, a, b; cin >> h >> w >> a >> b; vector<vi> ans(h, vi(w)); rep(i, b) rep(j, w - a) ans[i][j + a] = 1; rep(i, h - b) rep(j, a) ans[i + b][j] = 1; bool res = true; rep(i, w) { int cnt = 0; rep(j, h) cnt += ans[j][i]; if (b != min(cnt, h - cnt)) res = false; } rep(i, h) { int cnt = 0; rep(j, w) cnt += ans[i][j]; if (a != min(cnt, w - cnt)) res = false; } if (res) { rep(i, h) { rep(j, w) cout << ans[i][j]; cout << endl; } } else cout << "No\n"; // cout << "\n"; return 0; }
replace
30
40
30
32
0
p02903
C++
Runtime Error
#include "bits/stdc++.h" // Begin Header {{{ using namespace std; #ifndef DEBUG #define dump(...) #endif #define all(x) x.begin(), x.end() #define rep(i, b, e) for (intmax_t i = (b), i##_limit = (e); i < i##_limit; ++i) #define reps(i, b, e) \ for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i) #define repr(i, b, e) \ for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i) #define var(Type, ...) \ Type __VA_ARGS__; \ input(__VA_ARGS__) constexpr size_t operator""_zu(unsigned long long value) { return value; }; constexpr intmax_t operator""_jd(unsigned long long value) { return value; }; constexpr uintmax_t operator""_ju(unsigned long long value) { return value; }; constexpr int INF = 0x3f3f3f3f; constexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd; template <class T, class Compare = less<>> using MaxHeap = priority_queue<T, vector<T>, Compare>; template <class T, class Compare = greater<>> using MinHeap = priority_queue<T, vector<T>, Compare>; inline void input() {} template <class Head, class... Tail> inline void input(Head &&head, Tail &&...tail) { cin >> head; input(forward<Tail>(tail)...); } template <class T> inline istream &operator>>(istream &is, vector<T> &vec) { for (auto &e : vec) { is >> e; } return is; } inline void output() { cout << "\n"; } template <class Head, class... Tail> inline void output(Head &&head, Tail &&...tail) { cout << head; if (sizeof...(tail)) { cout << " "; } output(forward<Tail>(tail)...); } template <class T> inline ostream &operator<<(ostream &os, const vector<T> &vec) { static constexpr const char *delim[] = {" ", ""}; for (const auto &e : vec) { os << e << delim[&e == &vec.back()]; } return os; } template <class T> inline vector<T> makeVector(const T &initValue, size_t sz) { return vector<T>(sz, initValue); } template <class T, class... Args> inline auto makeVector(const T &initValue, size_t sz, Args... args) { return vector<decltype(makeVector<T>(initValue, args...))>( sz, makeVector<T>(initValue, args...)); } template <class Func> class FixPoint : Func { public: explicit constexpr FixPoint(Func &&f) noexcept : Func(forward<Func>(f)) {} template <class... Args> constexpr decltype(auto) operator()(Args &&...args) const { return Func::operator()(*this, std::forward<Args>(args)...); } }; template <class Func> static inline constexpr decltype(auto) makeFixPoint(Func &&f) noexcept { return FixPoint<Func>{forward<Func>(f)}; } template <class Container> struct reverse_t { Container &c; reverse_t(Container &c) : c(c) {} auto begin() { return c.rbegin(); } auto end() { return c.rend(); } }; template <class Container> auto reversed(Container &c) { return reverse_t<Container>(c); } template <class T> inline bool chmax(T &a, const T &b) noexcept { return b > a && (a = b, true); } template <class T> inline bool chmin(T &a, const T &b) noexcept { return b < a && (a = b, true); } template <class T> inline T diff(const T &a, const T &b) noexcept { return a < b ? b - a : a - b; } // End Header }}} signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); var(size_t, H, W, A, B); vector<string> mat(H, string(W, '1')); rep(i, 0, B) rep(j, 0, B) mat[i][j] = '0'; for (const auto e : mat) output(e); return 0; }
#include "bits/stdc++.h" // Begin Header {{{ using namespace std; #ifndef DEBUG #define dump(...) #endif #define all(x) x.begin(), x.end() #define rep(i, b, e) for (intmax_t i = (b), i##_limit = (e); i < i##_limit; ++i) #define reps(i, b, e) \ for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i) #define repr(i, b, e) \ for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i) #define var(Type, ...) \ Type __VA_ARGS__; \ input(__VA_ARGS__) constexpr size_t operator""_zu(unsigned long long value) { return value; }; constexpr intmax_t operator""_jd(unsigned long long value) { return value; }; constexpr uintmax_t operator""_ju(unsigned long long value) { return value; }; constexpr int INF = 0x3f3f3f3f; constexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd; template <class T, class Compare = less<>> using MaxHeap = priority_queue<T, vector<T>, Compare>; template <class T, class Compare = greater<>> using MinHeap = priority_queue<T, vector<T>, Compare>; inline void input() {} template <class Head, class... Tail> inline void input(Head &&head, Tail &&...tail) { cin >> head; input(forward<Tail>(tail)...); } template <class T> inline istream &operator>>(istream &is, vector<T> &vec) { for (auto &e : vec) { is >> e; } return is; } inline void output() { cout << "\n"; } template <class Head, class... Tail> inline void output(Head &&head, Tail &&...tail) { cout << head; if (sizeof...(tail)) { cout << " "; } output(forward<Tail>(tail)...); } template <class T> inline ostream &operator<<(ostream &os, const vector<T> &vec) { static constexpr const char *delim[] = {" ", ""}; for (const auto &e : vec) { os << e << delim[&e == &vec.back()]; } return os; } template <class T> inline vector<T> makeVector(const T &initValue, size_t sz) { return vector<T>(sz, initValue); } template <class T, class... Args> inline auto makeVector(const T &initValue, size_t sz, Args... args) { return vector<decltype(makeVector<T>(initValue, args...))>( sz, makeVector<T>(initValue, args...)); } template <class Func> class FixPoint : Func { public: explicit constexpr FixPoint(Func &&f) noexcept : Func(forward<Func>(f)) {} template <class... Args> constexpr decltype(auto) operator()(Args &&...args) const { return Func::operator()(*this, std::forward<Args>(args)...); } }; template <class Func> static inline constexpr decltype(auto) makeFixPoint(Func &&f) noexcept { return FixPoint<Func>{forward<Func>(f)}; } template <class Container> struct reverse_t { Container &c; reverse_t(Container &c) : c(c) {} auto begin() { return c.rbegin(); } auto end() { return c.rend(); } }; template <class Container> auto reversed(Container &c) { return reverse_t<Container>(c); } template <class T> inline bool chmax(T &a, const T &b) noexcept { return b > a && (a = b, true); } template <class T> inline bool chmin(T &a, const T &b) noexcept { return b < a && (a = b, true); } template <class T> inline T diff(const T &a, const T &b) noexcept { return a < b ? b - a : a - b; } // End Header }}} signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); var(size_t, H, W, A, B); rep(i, 0, H) rep(j, 0, W) { cout << ((j < A) ^ (i < B)); if (j == W - 1) output(); } return 0; }
replace
119
124
119
124
0