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
p03053
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++) #define all(v) v.begin(), v.end() // #define MOD 1000000007 const int INF = 1LL << 30; char fi[1010][1010]; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int dist[1010][1010]; int main() { int h, w; cin >> h >> w; rep(i, h) rep(j, w) dist[i][j] = -1; queue<pair<int, int>> que; rep(i, h) { rep(j, w) { cin >> fi[i][j]; if (fi[i][j] == '#') { que.push({i, j}); dist[i][j] = 0; } } } while (!que.empty()) { auto v = que.front(); // キューから先頭頂点を取り出す que.pop(); int x = v.first; int y = v.second; rep(i, 4) { int nx = x + dx[i]; int ny = y + dy[i]; if (dist[nx][ny] != -1) continue; dist[nx][ny] = dist[x][y] + 1; que.push({nx, ny}); } } int ans = 0; rep(i, h) { rep(j, w) { ans = max(ans, dist[i][j]); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) #define all(v) v.begin(), v.end() // #define MOD 1000000007 const int INF = 1LL << 30; char fi[1010][1010]; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int dist[1010][1010]; int main() { int h, w; cin >> h >> w; rep(i, h) rep(j, w) dist[i][j] = -1; queue<pair<int, int>> que; rep(i, h) { rep(j, w) { cin >> fi[i][j]; if (fi[i][j] == '#') { que.push({i, j}); dist[i][j] = 0; } } } while (!que.empty()) { auto v = que.front(); // キューから先頭頂点を取り出す que.pop(); int x = v.first; int y = v.second; rep(i, 4) { int nx = x + dx[i]; int ny = y + dy[i]; if (dist[nx][ny] != -1) continue; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; dist[nx][ny] = dist[x][y] + 1; que.push({nx, ny}); } } int ans = 0; rep(i, h) { rep(j, w) { ans = max(ans, dist[i][j]); } } cout << ans << endl; }
insert
41
41
41
43
0
p03053
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int H, W; cin >> H >> W; vector<vector<bool>> field(H, vector<bool>(W, false)); string A; int count = 0; queue<pair<int, int>> q_new; queue<pair<int, int>> q_old; for (int i = 0; i < H; i++) { cin >> A; for (int j = 0; j < W; j++) { if (A[j] == '#') { field[i][j] = true; q_new.push(make_pair(i, j)); count++; } else { field[i][j] = false; } } } int ans = 0; int diff[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, }; while (count < H * W) { ans++; q_old = q_new; while (!q_old.empty()) { int x_old = q_old.front().first; int y_old = q_old.front().second; q_old.pop(); for (int i = 0; i < 4; i++) { int x_new = x_old + diff[i][0]; int y_new = y_old + diff[i][1]; if (x_new < 0 || H <= x_new) { continue; } if (y_new < 0 || W <= y_new) { continue; } if (field[x_new][y_new]) { continue; } field[x_new][y_new] = true; count++; q_new.push(make_pair(x_new, y_new)); } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int H, W; cin >> H >> W; vector<vector<bool>> field(H, vector<bool>(W, false)); string A; int count = 0; queue<pair<int, int>> q_new; queue<pair<int, int>> q_old; for (int i = 0; i < H; i++) { cin >> A; for (int j = 0; j < W; j++) { if (A[j] == '#') { field[i][j] = true; q_new.push(make_pair(i, j)); count++; } else { field[i][j] = false; } } } int ans = 0; int diff[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, }; while (count < H * W) { ans++; q_old = q_new; q_new = queue<pair<int, int>>(); while (!q_old.empty()) { int x_old = q_old.front().first; int y_old = q_old.front().second; q_old.pop(); for (int i = 0; i < 4; i++) { int x_new = x_old + diff[i][0]; int y_new = y_old + diff[i][1]; if (x_new < 0 || H <= x_new) { continue; } if (y_new < 0 || W <= y_new) { continue; } if (field[x_new][y_new]) { continue; } field[x_new][y_new] = true; count++; q_new.push(make_pair(x_new, y_new)); } } } cout << ans << endl; }
insert
36
36
36
37
TLE
p03053
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define lli long long int #define uli unsigned long long int #define INF 99999999999 #define rep(i, m, n) for (lli i = m; i < n; i++) #define rrep(i, m, n) for (lli i = m; i > n; i--) #define pb(n) push_back(n) #define UE(N) N.erase(unique(N.begin(), N.end()), N.end()); #define Sort(n) sort(n.begin(), n.end()) #define Rev(n) reverse(n.begin(), n.end()) #define Out(S) cout << S << endl #define NeOut(S) cout << S #define HpOut(S) cout << setprecision(20) << S << endl #define Vecpr(K, L1, L2, N) vector<pair<L1, L2>> K(N) #define Vec(K, L, N, S) vector<L> K(N, S) #define DV(K, L, N, M, R) vector<vector<L>> K(N, vector<L>(M, R)) #define Lower(v, X) lower_bound(v.begin(), v.end(), X) - v.begin(); #define mod 1000000007 #define MAX 5100000 #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { lli A, B, C, L, R, N, M, K, X = 0, Y = 0, W, H = INF, sum = 0, num = 0, flag = 0; string S, T, O; cin >> H >> W; Vec(Graph, string, H, ""); rep(i, 0, H) cin >> Graph[i]; DV(dist, lli, H, W, -1); queue<pair<int, int>> que; rep(i, 0, H) rep(j, 0, W) { if (Graph[i][j] == '#') { dist[i][j] = 0; que.push(pair<int, int>(i, j)); } } while (!que.empty()) { auto Xq = que.front(); que.pop(); rep(i, 0, 4) { auto nx = Xq.first + dx[i]; auto ny = Xq.second + dy[i]; if (nx < 0 || nx > W - 1 || ny < 0 || ny > H - 1) continue; if (dist[nx][ny] == -1) { dist[nx][ny] = dist[Xq.first][Xq.second] + 1; que.push(pair<int, int>(nx, ny)); } } } rep(i, 0, H) rep(j, 0, W) sum = max(sum, dist[i][j]); Out(sum); }
#include <bits/stdc++.h> using namespace std; #define lli long long int #define uli unsigned long long int #define INF 99999999999 #define rep(i, m, n) for (lli i = m; i < n; i++) #define rrep(i, m, n) for (lli i = m; i > n; i--) #define pb(n) push_back(n) #define UE(N) N.erase(unique(N.begin(), N.end()), N.end()); #define Sort(n) sort(n.begin(), n.end()) #define Rev(n) reverse(n.begin(), n.end()) #define Out(S) cout << S << endl #define NeOut(S) cout << S #define HpOut(S) cout << setprecision(20) << S << endl #define Vecpr(K, L1, L2, N) vector<pair<L1, L2>> K(N) #define Vec(K, L, N, S) vector<L> K(N, S) #define DV(K, L, N, M, R) vector<vector<L>> K(N, vector<L>(M, R)) #define Lower(v, X) lower_bound(v.begin(), v.end(), X) - v.begin(); #define mod 1000000007 #define MAX 5100000 #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { lli A, B, C, L, R, N, M, K, X = 0, Y = 0, W, H = INF, sum = 0, num = 0, flag = 0; string S, T, O; cin >> H >> W; Vec(Graph, string, H, ""); rep(i, 0, H) cin >> Graph[i]; DV(dist, lli, H, W, -1); queue<pair<int, int>> que; rep(i, 0, H) rep(j, 0, W) { if (Graph[i][j] == '#') { dist[i][j] = 0; que.push(pair<int, int>(i, j)); } } while (!que.empty()) { auto Xq = que.front(); que.pop(); rep(i, 0, 4) { auto nx = Xq.first + dx[i]; auto ny = Xq.second + dy[i]; if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue; if (dist[nx][ny] == -1) { dist[nx][ny] = dist[Xq.first][Xq.second] + 1; que.push(pair<int, int>(nx, ny)); } } } rep(i, 0, H) rep(j, 0, W) sum = max(sum, dist[i][j]); Out(sum); }
replace
45
46
45
46
0
p03053
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define INF 1000000000 #define MOD 1000000007 #define ll long long int using namespace std; int main() { int H, W; cin >> H >> W; int time[1009][1009]; for (int i = 0; i < 1009; i++) { for (int j = 0; j < 1009; j++) { time[i][j] = INF; } } queue<int> qx; queue<int> qy; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { char c; cin >> c; if (c == '#') { time[i][j] = 0; qx.push(i); qy.push(j); } } } while (!qx.empty()) { int nowx = qx.front(); qx.pop(); int nowy = qy.front(); qy.pop(); vector<int> dx = {1, 0, -1, 0}; vector<int> dy = {0, 1, 0, -1}; for (int i = 0; i < 4; i++) { int x = nowx + dx[i]; int y = nowy + dy[i]; if (x > H or x == 0 or y == W + 1 or y == 0) continue; time[x][y] = min(time[x][y], time[nowx][nowy] + 1); if (time[x][y] == time[nowx][nowy] + 1) { qx.push(x); qy.push(y); } } } int ans = -1; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { ans = max(ans, time[i][j]); } } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define INF 1000000000 #define MOD 1000000007 #define ll long long int using namespace std; int main() { int H, W; cin >> H >> W; int time[1009][1009]; for (int i = 0; i < 1009; i++) { for (int j = 0; j < 1009; j++) { time[i][j] = INF; } } queue<int> qx; queue<int> qy; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { char c; cin >> c; if (c == '#') { time[i][j] = 0; qx.push(i); qy.push(j); } } } while (!qx.empty()) { int nowx = qx.front(); qx.pop(); int nowy = qy.front(); qy.pop(); vector<int> dx = {1, 0, -1, 0}; vector<int> dy = {0, 1, 0, -1}; for (int i = 0; i < 4; i++) { int x = nowx + dx[i]; int y = nowy + dy[i]; if (x > H or x == 0 or y == W + 1 or y == 0) continue; if (time[x][y] <= time[nowx][nowy] + 1) continue; time[x][y] = time[nowx][nowy] + 1; // time[x][y] = min(time[x][y],time[nowx][nowy] + 1); // if(time[x][y] == time[nowx][nowy] + 1) { qx.push(x); qy.push(y); //} } } int ans = -1; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { ans = max(ans, time[i][j]); } } cout << ans << endl; return 0; }
replace
54
59
54
63
TLE
p03053
C++
Runtime Error
#pragma region header #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define rev(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define rev1(i, n) for (int i = (int)(n); i > 0; i--) #define pb push_back #define all(v) (v).begin(), (v).end() #define resort(v) sort((v).rbegin(), (v).rend()) #define vi vector<int> #define vvi vector<vector<int>> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vector<bool>> using ll = long long; using P = pair<int, int>; /* ----------------よく使う数字や配列----------------- */ int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; constexpr ll mod = 1e9 + 7; constexpr ll inf = INT32_MAX / 2; constexpr ll INF = LLONG_MAX / 2; constexpr long double eps = DBL_EPSILON; constexpr long double pi = 3.141592653589793238462643383279; /* ----------------------end----------------------- */ /* --------------------テンプレート------------------ */ 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; } /* ----------------------end----------------------- */ /* --------------------ライブラリ-------------------- */ ll fact(int i) { // 階乗 if (i == 0) return 1; return (fact(i - 1)) * i % mod; } ll gcd(ll a, ll b) { // 最大公約数 if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { // 最小公倍数 return a * b / gcd(a, b); } int keta(ll n) { // 桁数を求める if (n == 0) return 1; int count = 0; while (n != 0) { n /= 10; count++; } return count; } ll ketasum(ll n) { // 各桁の和 ll sum = 0; while (n != 0) { sum += n % 10; n /= 10; } return sum; } /* ----------------------end----------------------- */ #pragma endregion signed main() { int h, w; cin >> h >> w; vvi bo(h, vi(w, -1)); queue<P> que; rep(i, h) { rep(j, w) { char c; cin >> c; if (c == '#') { que.push({i, j}); bo[i][j] = 0; } } } vi ans; while (!que.empty()) { P p = que.front(); que.pop(); rep(i, 4) { int x = p.first + dx[i], y = p.second + dy[i]; if (x < 0 || x >= h || y < 0 || y >= w || bo[x][y] != -1) continue; bo[x][y] = bo[p.first][p.second] + 1; ans.pb(bo[x][y]); que.push({x, y}); } } cout << ans.back() << endl; return 0; }
#pragma region header #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define rev(i, n) for (int i = (int)(n - 1); i >= 0; i--) #define rev1(i, n) for (int i = (int)(n); i > 0; i--) #define pb push_back #define all(v) (v).begin(), (v).end() #define resort(v) sort((v).rbegin(), (v).rend()) #define vi vector<int> #define vvi vector<vector<int>> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vector<bool>> using ll = long long; using P = pair<int, int>; /* ----------------よく使う数字や配列----------------- */ int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; constexpr ll mod = 1e9 + 7; constexpr ll inf = INT32_MAX / 2; constexpr ll INF = LLONG_MAX / 2; constexpr long double eps = DBL_EPSILON; constexpr long double pi = 3.141592653589793238462643383279; /* ----------------------end----------------------- */ /* --------------------テンプレート------------------ */ 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; } /* ----------------------end----------------------- */ /* --------------------ライブラリ-------------------- */ ll fact(int i) { // 階乗 if (i == 0) return 1; return (fact(i - 1)) * i % mod; } ll gcd(ll a, ll b) { // 最大公約数 if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { // 最小公倍数 return a * b / gcd(a, b); } int keta(ll n) { // 桁数を求める if (n == 0) return 1; int count = 0; while (n != 0) { n /= 10; count++; } return count; } ll ketasum(ll n) { // 各桁の和 ll sum = 0; while (n != 0) { sum += n % 10; n /= 10; } return sum; } /* ----------------------end----------------------- */ #pragma endregion signed main() { int h, w; cin >> h >> w; vvi bo(h, vi(w, -1)); queue<P> que; rep(i, h) { rep(j, w) { char c; cin >> c; if (c == '#') { que.push({i, j}); bo[i][j] = 0; } } } vi ans; ans.pb(0); while (!que.empty()) { P p = que.front(); que.pop(); rep(i, 4) { int x = p.first + dx[i], y = p.second + dy[i]; if (x < 0 || x >= h || y < 0 || y >= w || bo[x][y] != -1) continue; bo[x][y] = bo[p.first][p.second] + 1; ans.pb(bo[x][y]); que.push({x, y}); } } cout << ans.back() << endl; return 0; }
insert
96
96
96
97
0
p03053
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define countof(array) (sizeof(array) / sizeof(array[0])) #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = (n)-1; i >= 0; --i) #define srep(i, n, s) for (int i = s; i < (n); ++i) #define rsrep(i, n, s) for (int i = (n)-1; i >= s; --i) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define aall(a) (a), (a) + countof(a) // for array sorting #define raall(a) (a), (a) + countof(a), greater<>() #define show(x) cout << #x << " = " << x << endl; #define vfind(v, a) find(all(v), a) != v.end() #define yn(f) \ { \ if (f) \ puts("YES"); \ else \ puts("NO"); \ } #define yns(f) \ { \ if (f) \ puts("Yes"); \ else \ puts("No"); \ } #define show_ary(...) \ { \ cout << #__VA_ARGS__ << " = "; \ for (const auto &x : (__VA_ARGS__)) { \ cout << x << " "; \ } \ cout << endl; \ } #define show_pair(...) \ cout << #__VA_ARGS__ << " = " << endl; \ for (const auto &x : (__VA_ARGS__)) { \ cout << " " << x.fi << " : " << x.se << endl; \ } #define out_ary(...) \ { \ for (const auto &x : (__VA_ARGS__)) { \ cout << x << " "; \ } \ cout << endl; \ } #define argmax(v) distance((v).begin(), max_element(all(v))) #define argmin(v) distance((v).begin(), min_element(all(v))) #define vmax(v) *max_element(all(v)) #define vmin(v) *min_element(all(v)) typedef long long int ll; typedef pair<int, int> P; typedef vector<P> vpair; typedef vector<int> vint; typedef vector<ll> vll; typedef vector<double> vdouble; typedef vector<string> vstr; typedef vector<bool> vbool; typedef vector<vint> vvint; typedef vector<vll> vvll; typedef vector<vstr> vvstr; typedef vector<vbool> vvbool; const ll LINF = 2000000000000000000ll; const int INF = 1000000100; const ll MOD = 1e9 + 7; int h, w; vstr a; vint dx = {1, 0, -1, 0}; vint dy = {0, 1, 0, -1}; vvint bfs(vvint d, int x, int y) { queue<P> que; que.push(P(x, y)); d[x][y] = 0; while (que.size()) { P p = que.front(); que.pop(); if (a[p.fi][p.se] == '#') d[p.fi][p.se] = 0; rep(i, 4) { int nx = p.fi + dx[i], ny = p.se + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && d[nx][ny] > d[p.fi][p.se] + 1) { que.push(P(nx, ny)); d[nx][ny] = d[p.fi][p.se] + 1; } } } return d; } int main() { cin >> h >> w; a.resize(h); rep(i, h) { cin >> a[i]; } int mx = 0; vvint d(h, vint(w, INF)); rep(i, h) { rep(j, w) { if (a[i][j] == '.') continue; else { d = bfs(d, i, j); } } } rep(i, h) mx = max(mx, vmax(d[i])); ; int ans = mx; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define countof(array) (sizeof(array) / sizeof(array[0])) #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = (n)-1; i >= 0; --i) #define srep(i, n, s) for (int i = s; i < (n); ++i) #define rsrep(i, n, s) for (int i = (n)-1; i >= s; --i) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define aall(a) (a), (a) + countof(a) // for array sorting #define raall(a) (a), (a) + countof(a), greater<>() #define show(x) cout << #x << " = " << x << endl; #define vfind(v, a) find(all(v), a) != v.end() #define yn(f) \ { \ if (f) \ puts("YES"); \ else \ puts("NO"); \ } #define yns(f) \ { \ if (f) \ puts("Yes"); \ else \ puts("No"); \ } #define show_ary(...) \ { \ cout << #__VA_ARGS__ << " = "; \ for (const auto &x : (__VA_ARGS__)) { \ cout << x << " "; \ } \ cout << endl; \ } #define show_pair(...) \ cout << #__VA_ARGS__ << " = " << endl; \ for (const auto &x : (__VA_ARGS__)) { \ cout << " " << x.fi << " : " << x.se << endl; \ } #define out_ary(...) \ { \ for (const auto &x : (__VA_ARGS__)) { \ cout << x << " "; \ } \ cout << endl; \ } #define argmax(v) distance((v).begin(), max_element(all(v))) #define argmin(v) distance((v).begin(), min_element(all(v))) #define vmax(v) *max_element(all(v)) #define vmin(v) *min_element(all(v)) typedef long long int ll; typedef pair<int, int> P; typedef vector<P> vpair; typedef vector<int> vint; typedef vector<ll> vll; typedef vector<double> vdouble; typedef vector<string> vstr; typedef vector<bool> vbool; typedef vector<vint> vvint; typedef vector<vll> vvll; typedef vector<vstr> vvstr; typedef vector<vbool> vvbool; const ll LINF = 2000000000000000000ll; const int INF = 1000000100; const ll MOD = 1e9 + 7; int h, w; vstr a; vint dx = {1, 0, -1, 0}; vint dy = {0, 1, 0, -1}; vvint bfs(vvint d, int x, int y) { queue<P> que; que.push(P(x, y)); d[x][y] = 0; while (que.size()) { P p = que.front(); que.pop(); if (a[p.fi][p.se] == '#') d[p.fi][p.se] = 0; rep(i, 4) { int nx = p.fi + dx[i], ny = p.se + dy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && d[nx][ny] > d[p.fi][p.se] + 1) { que.push(P(nx, ny)); d[nx][ny] = d[p.fi][p.se] + 1; } } } return d; } int main() { cin >> h >> w; a.resize(h); rep(i, h) { cin >> a[i]; } int mx = 0; vvint d(h, vint(w, INF)); rep(i, h) { rep(j, w) { if (a[i][j] == '.') continue; else { d = bfs(d, i, j); break; } } } rep(i, h) mx = max(mx, vmax(d[i])); ; int ans = mx; cout << ans << endl; return 0; }
insert
110
110
110
111
TLE
p03053
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; /**** Type Define ****/ typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> Q; /**** Const List ****/ const ll INF = 1LL << 60; const ll mod = 1000000007; const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, -1, 0, 1}; /**** General Functions ****/ ll ketawa(ll n) { ll a = 0; while (n != 0) { a += n % 10; n /= 10; } return a; } ll RepeatSquaring(ll N, ll P, ll M) { if (P == 0) return 1; if (P % 2 == 0) { ll t = RepeatSquaring(N, P / 2, M); return (t % M) * (t % M) % M; } return (N * RepeatSquaring(N, P - 1, M)) % M; } bool IsPrime(ll a) { // order root a if (a == 1) return false; for (int i = 2; i * i <= a; i++) { if (a % i == 0 && a != i) { return false; } } return true; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll q = a / b, g = extgcd(b, a - q * b, x, y); ll z = x - q * y; x = y; y = z; return g; } ll invmod(ll a, ll m) { // a^-1 mod m ll x, y; extgcd(a, m, x, y); x %= m; if (x < 0) x += m; return x; } ll nCk(ll n, ll k, ll mod) { ll ans = 1; for (ll i = n, j = 1; j <= k; i--, j++) ans = (((ans * i) % mod) * invmod(j, mod)) % mod; return ans; } ll lmin(ll a, ll b) { return a > b ? b : a; }; ll lmax(ll a, ll b) { return a > b ? a : b; }; ll lsum(ll a, ll b) { return a + b; }; /**** Zip ****/ template <typename T> class Zip { vector<T> d; bool flag; public: Zip() { flag = false; } void add(T x) { d.push_back(x); flag = true; } ll getNum(T x) { // T need to have operator < !! if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return lower_bound(d.begin(), d.end(), x) - d.begin(); } ll size() { if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return (ll)d.size(); } }; /**** Union Find ****/ class UnionFind { vector<ll> par, rank; // par > 0: number, par < 0: -par public: void init(ll n) { par.resize(n, 1); rank.resize(n, 0); } ll getSize(ll x) { return par[find(x)]; } ll find(ll x) { if (par[x] > 0) return x; return -(par[x] = -find(-par[x])); } void merge(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = -y; } else { par[x] += par[y]; par[y] = -x; if (rank[x] == rank[y]) rank[x]++; } } bool isSame(ll x, ll y) { return find(x) == find(y); } }; /**** Segment Tree ****/ class SegmentTree { public: vector<pair<double, double>> node; // node[0]は使用しない ll n; // データの個数(nodeの最下層には何個並んでいるか) pair<double, double> initial_value; // 初期値 public: void Init(ll n_, pair<double, double> initial_value_) { n = 1; while (n < n_) n *= 2; node.resize(2 * n); for (ll i = 0; i < 2 * n; i++) { node[i] = initial_value_; } initial_value = initial_value_; } void Update(ll k, pair<double, double> a) { // node[k]をaにする // それに従って先祖も変わっていく k += n; node[k] = a; while (k > 1) { k = k / 2; node[k] = pair<double, double>( node[k * 2].first * node[k * 2 + 1].first, node[k * 2].second * node[k * 2 + 1].first + node[k * 2 + 1].second); } } /*void Watch(){ for(ll i=0;i<2*n;i++){ cout<<node[i]<<endl; } }*/ double Query() { //[a,b) return node[1].first + node[1].second; } }; /**** LIS ****/ ll lis(ll *a, ll n, ll *dp) { fill(dp, dp + n, INF); // INFを代入 for (ll i = 0; i < n; i++) *lower_bound(dp, dp + n, a[i]) = a[i]; return (ll)(lower_bound(dp, dp + n, INF) - dp); } /**** main function ****/ ll h, w; string a[1001]; queue<P> kouho; ll kita[1001][1001]; ll ans[1001][1001]; int main() { cin >> h >> w; for (ll i = 0; i < h; i++) { cin >> a[i]; } for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { if (a[i][j] == '#') { kouho.push(P(i, j)); } else { ans[i][j] = 999999999; } } } while (!kouho.empty()) { ll x = kouho.front().first; ll y = kouho.front().second; kouho.pop(); kita[x][y] = 1; for (ll i = 0; i < 4; i++) { if (x + dx[i] < 0 || x + dx[i] >= h || y + dy[i] < 0 || y + dy[i] >= w) continue; ans[x + dx[i]][y + dy[i]] = lmin(ans[x + dx[i]][y + dy[i]], ans[x][y] + 1); if (kita[x + dx[i]][y + dy[i]] == 0) { kouho.push(P(x + dx[i], y + dy[i])); } } } ll ans2 = 0; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { ans2 = lmax(ans2, ans[i][j]); } } cout << ans2 << endl; }
#include <algorithm> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> using namespace std; /**** Type Define ****/ typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll, P> Q; /**** Const List ****/ const ll INF = 1LL << 60; const ll mod = 1000000007; const ll dx[4] = {1, 0, -1, 0}; const ll dy[4] = {0, -1, 0, 1}; /**** General Functions ****/ ll ketawa(ll n) { ll a = 0; while (n != 0) { a += n % 10; n /= 10; } return a; } ll RepeatSquaring(ll N, ll P, ll M) { if (P == 0) return 1; if (P % 2 == 0) { ll t = RepeatSquaring(N, P / 2, M); return (t % M) * (t % M) % M; } return (N * RepeatSquaring(N, P - 1, M)) % M; } bool IsPrime(ll a) { // order root a if (a == 1) return false; for (int i = 2; i * i <= a; i++) { if (a % i == 0 && a != i) { return false; } } return true; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1, y = 0; return a; } ll q = a / b, g = extgcd(b, a - q * b, x, y); ll z = x - q * y; x = y; y = z; return g; } ll invmod(ll a, ll m) { // a^-1 mod m ll x, y; extgcd(a, m, x, y); x %= m; if (x < 0) x += m; return x; } ll nCk(ll n, ll k, ll mod) { ll ans = 1; for (ll i = n, j = 1; j <= k; i--, j++) ans = (((ans * i) % mod) * invmod(j, mod)) % mod; return ans; } ll lmin(ll a, ll b) { return a > b ? b : a; }; ll lmax(ll a, ll b) { return a > b ? a : b; }; ll lsum(ll a, ll b) { return a + b; }; /**** Zip ****/ template <typename T> class Zip { vector<T> d; bool flag; public: Zip() { flag = false; } void add(T x) { d.push_back(x); flag = true; } ll getNum(T x) { // T need to have operator < !! if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return lower_bound(d.begin(), d.end(), x) - d.begin(); } ll size() { if (flag) { sort(d.begin(), d.end()); d.erase(unique(d.begin(), d.end()), d.end()); flag = false; } return (ll)d.size(); } }; /**** Union Find ****/ class UnionFind { vector<ll> par, rank; // par > 0: number, par < 0: -par public: void init(ll n) { par.resize(n, 1); rank.resize(n, 0); } ll getSize(ll x) { return par[find(x)]; } ll find(ll x) { if (par[x] > 0) return x; return -(par[x] = -find(-par[x])); } void merge(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = -y; } else { par[x] += par[y]; par[y] = -x; if (rank[x] == rank[y]) rank[x]++; } } bool isSame(ll x, ll y) { return find(x) == find(y); } }; /**** Segment Tree ****/ class SegmentTree { public: vector<pair<double, double>> node; // node[0]は使用しない ll n; // データの個数(nodeの最下層には何個並んでいるか) pair<double, double> initial_value; // 初期値 public: void Init(ll n_, pair<double, double> initial_value_) { n = 1; while (n < n_) n *= 2; node.resize(2 * n); for (ll i = 0; i < 2 * n; i++) { node[i] = initial_value_; } initial_value = initial_value_; } void Update(ll k, pair<double, double> a) { // node[k]をaにする // それに従って先祖も変わっていく k += n; node[k] = a; while (k > 1) { k = k / 2; node[k] = pair<double, double>( node[k * 2].first * node[k * 2 + 1].first, node[k * 2].second * node[k * 2 + 1].first + node[k * 2 + 1].second); } } /*void Watch(){ for(ll i=0;i<2*n;i++){ cout<<node[i]<<endl; } }*/ double Query() { //[a,b) return node[1].first + node[1].second; } }; /**** LIS ****/ ll lis(ll *a, ll n, ll *dp) { fill(dp, dp + n, INF); // INFを代入 for (ll i = 0; i < n; i++) *lower_bound(dp, dp + n, a[i]) = a[i]; return (ll)(lower_bound(dp, dp + n, INF) - dp); } /**** main function ****/ ll h, w; string a[1001]; queue<P> kouho; ll kita[1001][1001]; ll ans[1001][1001]; int main() { cin >> h >> w; for (ll i = 0; i < h; i++) { cin >> a[i]; } for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { if (a[i][j] == '#') { kouho.push(P(i, j)); } else { ans[i][j] = 999999999; } } } while (!kouho.empty()) { ll x = kouho.front().first; ll y = kouho.front().second; kouho.pop(); kita[x][y] = 1; for (ll i = 0; i < 4; i++) { if (x + dx[i] < 0 || x + dx[i] >= h || y + dy[i] < 0 || y + dy[i] >= w) continue; ans[x + dx[i]][y + dy[i]] = lmin(ans[x + dx[i]][y + dy[i]], ans[x][y] + 1); if (kita[x + dx[i]][y + dy[i]] == 0) { kouho.push(P(x + dx[i], y + dy[i])); kita[x + dx[i]][y + dy[i]] = 1; } } } ll ans2 = 0; for (ll i = 0; i < h; i++) { for (ll j = 0; j < w; j++) { ans2 = lmax(ans2, ans[i][j]); } } cout << ans2 << endl; }
insert
240
240
240
241
TLE
p03053
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<int>>; typedef long long ll; typedef long double ld; #define REP(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++) #define rep(i, n) REP(i, 0, n) #define vec vector const ll large_P = 1e9 + 7; int main() { int H, W; cin >> H >> W; vector<string> A(H); rep(i, H) cin >> A.at(i); vector<vector<int>> dist(H, vector<int>(W, -1)); queue<pair<int, int>> que; rep(i, H) { rep(j, W) { if (A.at(i).at(j) == '#') { dist.at(i).at(j) = 0; que.push(make_pair(i, j)); } else { continue; } } } while (!que.empty()) { pair<int, int> v = que.front(); que.pop(); for (int k = -1; k <= 1; k++) { for (int l = -1; l <= 1; l++) { if (k * l != 0) continue; if (v.first + k < 0 || H <= v.first + k) continue; if (v.second + l < 0 || W <= v.second + l) continue; // 場外 if (dist.at(v.first + k).at(v.second + l) < dist.at(v.first).at(v.second) + 1 && dist.at(v.first + k).at(v.second + l) != -1) continue; dist.at(v.first + k).at(v.second + l) = dist.at(v.first).at(v.second) + 1; que.push(make_pair(v.first + k, v.second + l)); } } } int res = 0; rep(i, H) { rep(j, W) { if (res < dist.at(i).at(j)) res = dist.at(i).at(j); } } cout << res; }
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<int>>; typedef long long ll; typedef long double ld; #define REP(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++) #define rep(i, n) REP(i, 0, n) #define vec vector const ll large_P = 1e9 + 7; int main() { int H, W; cin >> H >> W; vector<string> A(H); rep(i, H) cin >> A.at(i); vector<vector<int>> dist(H, vector<int>(W, -1)); queue<pair<int, int>> que; rep(i, H) { rep(j, W) { if (A.at(i).at(j) == '#') { dist.at(i).at(j) = 0; que.push(make_pair(i, j)); } else { continue; } } } while (!que.empty()) { pair<int, int> v = que.front(); que.pop(); for (int k = -1; k <= 1; k++) { for (int l = -1; l <= 1; l++) { if (k * l != 0) continue; if (v.first + k < 0 || H <= v.first + k) continue; if (v.second + l < 0 || W <= v.second + l) continue; // 場外 if (dist.at(v.first + k).at(v.second + l) != -1) continue; dist.at(v.first + k).at(v.second + l) = dist.at(v.first).at(v.second) + 1; que.push(make_pair(v.first + k, v.second + l)); } } } int res = 0; rep(i, H) { rep(j, W) { if (res < dist.at(i).at(j)) res = dist.at(i).at(j); } } cout << res; }
replace
46
49
46
47
TLE
p03053
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define pb push_back #define mp make_pair #define ii pair<int, int> #define ll long long #define forn(i, a, b) for (int i = (int)a; i < (int)b; i++) using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int h, w; cin >> h >> w; vector<vector<int>> score(h, vector<int>(w)); queue<ii> q; forn(i, 0, h) { forn(j, 0, w) { char x; cin >> x; if (x == '#') { q.push(mp(i, j)); score[i][j] = 0; } else { score[i][j] = -1; } } } int depth = 0; int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; while (!q.empty()) { ii top = q.front(); int s = score[top.first][top.second]; q.pop(); forn(i, 0, 4) { int nx = top.first + dir[i][0]; int ny = top.second + dir[i][1]; ii ntop = mp(nx, ny); if (nx > 0 && ny > 0 && nx < w - 1 && ny < h - 1 && score[nx][ny] < 0) { q.push(ntop); score[nx][ny] = s + 1; depth = max(depth, s + 1); } } } cout << depth << endl; }
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <queue> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define pb push_back #define mp make_pair #define ii pair<int, int> #define ll long long #define forn(i, a, b) for (int i = (int)a; i < (int)b; i++) using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int h, w; cin >> h >> w; vector<vector<int>> score(h, vector<int>(w)); queue<ii> q; forn(i, 0, h) { forn(j, 0, w) { char x; cin >> x; if (x == '#') { q.push(mp(i, j)); score[i][j] = 0; } else { score[i][j] = -1; } } } int depth = 0; int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; while (!q.empty()) { ii top = q.front(); int s = score[top.first][top.second]; q.pop(); forn(i, 0, 4) { int nx = top.first + dir[i][0]; int ny = top.second + dir[i][1]; ii ntop = mp(nx, ny); if (nx >= 0 && ny >= 0 && nx < h && ny < w && score[nx][ny] < 0) { q.push(ntop); score[nx][ny] = s + 1; depth = max(depth, s + 1); } } } cout << depth << endl; }
replace
50
51
50
51
0
p03053
C++
Runtime Error
#include <algorithm> #include <bitset> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdint.h> #include <string> #include <vector> using namespace std; #define PI (3.14159265358979323846) #define INF (2147483647) #define INF_LL (9223372036854775807) #define DIV_NUM (1000000007) // BFSによる塗りつぶし探索(多地点スタート形式) #define MAX_H (50) #define MAX_W (50) typedef char Graph[MAX_H][MAX_W]; Graph graph; typedef int Dist[MAX_H][MAX_W]; Dist dist; bool seen[MAX_H][MAX_W]; int H, W; // グラフサイズ typedef pair<int, int> P; // 指定されたマスが次のdfsを行う対象かどうかをチェックする関数 // 0 <= h < H, 0 <= w < W を満たすこと // その他追加条件を満たすこと を確認 bool willCheck(int h, int w) { // グラフサイズ内であることの確認 if (h < 0) return false; if (w < 0) return false; if (H <= h) return false; if (W <= w) return false; // 既に探索済みでないことの確認 if (seen[h][w]) return false; // 問題ごとの条件を満たす確認 if (graph[h][w] == '.') return true; return false; } void bfs(vector<P> start) { // キューを用意して、初期探索点をセット queue<P> que; for (int i = 0; i < start.size(); i++) { que.push(P(start[i].first, start[i].second)); // 初期点の距離を0 dist[start[i].first][start[i].second] = 0; seen[start[i].first][start[i].second] = true; } while (!que.empty()) { // BFSループ // ★★ この中で迂闊に break しないこと // キューの先頭のセルを取得 P v = que.front(); que.pop(); int h = v.first; int w = v.second; // 取得したセルに対する処理 // そのセルからつながるセルに処理を移行 if (willCheck(h + 1, w)) { dist[h + 1][w] = dist[h][w] + 1; seen[h + 1][w] = true; // ここでseenをセットしないと、queに同一アイテムが入る que.push(P(h + 1, w)); } if (willCheck(h - 1, w)) { dist[h - 1][w] = dist[h][w] + 1; seen[h - 1][w] = true; que.push(P(h - 1, w)); } if (willCheck(h, w + 1)) { dist[h][w + 1] = dist[h][w] + 1; seen[h][w + 1] = true; que.push(P(h, w + 1)); } if (willCheck(h, w - 1)) { dist[h][w - 1] = dist[h][w] + 1; seen[h][w - 1] = true; que.push(P(h, w - 1)); } } } int main() { cin >> H >> W; vector<P> start; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char tmp; cin >> tmp; graph[i][j] = tmp; if (tmp == '#') start.push_back(P(i, j)); } } bfs(start); int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ans = max(ans, dist[i][j]); } } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdint.h> #include <string> #include <vector> using namespace std; #define PI (3.14159265358979323846) #define INF (2147483647) #define INF_LL (9223372036854775807) #define DIV_NUM (1000000007) // BFSによる塗りつぶし探索(多地点スタート形式) #define MAX_H (1000) #define MAX_W (1000) typedef char Graph[MAX_H][MAX_W]; Graph graph; typedef int Dist[MAX_H][MAX_W]; Dist dist; bool seen[MAX_H][MAX_W]; int H, W; // グラフサイズ typedef pair<int, int> P; // 指定されたマスが次のdfsを行う対象かどうかをチェックする関数 // 0 <= h < H, 0 <= w < W を満たすこと // その他追加条件を満たすこと を確認 bool willCheck(int h, int w) { // グラフサイズ内であることの確認 if (h < 0) return false; if (w < 0) return false; if (H <= h) return false; if (W <= w) return false; // 既に探索済みでないことの確認 if (seen[h][w]) return false; // 問題ごとの条件を満たす確認 if (graph[h][w] == '.') return true; return false; } void bfs(vector<P> start) { // キューを用意して、初期探索点をセット queue<P> que; for (int i = 0; i < start.size(); i++) { que.push(P(start[i].first, start[i].second)); // 初期点の距離を0 dist[start[i].first][start[i].second] = 0; seen[start[i].first][start[i].second] = true; } while (!que.empty()) { // BFSループ // ★★ この中で迂闊に break しないこと // キューの先頭のセルを取得 P v = que.front(); que.pop(); int h = v.first; int w = v.second; // 取得したセルに対する処理 // そのセルからつながるセルに処理を移行 if (willCheck(h + 1, w)) { dist[h + 1][w] = dist[h][w] + 1; seen[h + 1][w] = true; // ここでseenをセットしないと、queに同一アイテムが入る que.push(P(h + 1, w)); } if (willCheck(h - 1, w)) { dist[h - 1][w] = dist[h][w] + 1; seen[h - 1][w] = true; que.push(P(h - 1, w)); } if (willCheck(h, w + 1)) { dist[h][w + 1] = dist[h][w] + 1; seen[h][w + 1] = true; que.push(P(h, w + 1)); } if (willCheck(h, w - 1)) { dist[h][w - 1] = dist[h][w] + 1; seen[h][w - 1] = true; que.push(P(h, w - 1)); } } } int main() { cin >> H >> W; vector<P> start; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char tmp; cin >> tmp; graph[i][j] = tmp; if (tmp == '#') start.push_back(P(i, j)); } } bfs(start); int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ans = max(ans, dist[i][j]); } } cout << ans << endl; return 0; }
replace
20
22
20
22
0
p03053
C++
Runtime Error
/** * Author: "Robin Singh" !! * rob_in_1 **/ #include <bits/stdc++.h> using namespace std; #define dbg(args...) \ { \ vector<string> _v = split(#args, ','); \ err(_v.begin(), args); \ cerr << '\n'; \ } vector<string> split(const string &s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << '\t'; err(++it, args...); } #define int long long #define pb push_back #define eb emplace_back #define all(x) (x).begin(), (x).end() #define ff first #define ss second #define MP make_pair #define szz(v) ((int)(v).size()) #define fr(i, j, k) for (int i = j; i < k; i++) #define bk(i, j, k) for (int i = j - 1; i >= k; i--) #define mem(ptr, val) memset(ptr, val, sizeof ptr) #define sbit(n) __builtin_popcount(n) #define nl "\n" typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vpii; const long double eps = 1e-9; const int mod = 1e9 + 7; pii dir[8] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {-1, -1}, {-1, 1}, {1, -1}}; //*********************************!Template Ends //Here!**********************************// const int nn = 1e3 + 5; int dist[nn][nn]; string s[nn]; int h, w; queue<pii> q; void bfs() { while (!q.empty()) { auto p = q.front(); q.pop(); int i = p.ff; int j = p.ss; for (int ii = 0; ii < 4; ii++) { int x = i + dir[ii].ff, y = j + dir[ii].ss; if (x >= 0 and y >= 0 and x < h and y < w and dist[x][y] == -1) { dist[x][y] = 1 + dist[i][j]; q.push({x, y}); } } } } void robin() { cin >> h >> w; fr(i, 0, h) cin >> s[i]; int ans = 0; memset(dist, -1, sizeof dist); fr(i, 0, h) { fr(j, 0, w) { if (s[i][j] == '#') { dist[i][j] = 0; q.push({i, j}); } } } bfs(); fr(i, 0, h) fr(j, 0, w) ans = max(ans, dist[i][j]); cout << ans; } int32_t main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; // cin >> T; for (int tc = 1; tc <= T; tc++) { // cout << "Case #" << tc << ": "; robin(); } return 0; }
/** * Author: "Robin Singh" !! * rob_in_1 **/ #include <bits/stdc++.h> using namespace std; #define dbg(args...) \ { \ vector<string> _v = split(#args, ','); \ err(_v.begin(), args); \ cerr << '\n'; \ } vector<string> split(const string &s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << '\t'; err(++it, args...); } #define int long long #define pb push_back #define eb emplace_back #define all(x) (x).begin(), (x).end() #define ff first #define ss second #define MP make_pair #define szz(v) ((int)(v).size()) #define fr(i, j, k) for (int i = j; i < k; i++) #define bk(i, j, k) for (int i = j - 1; i >= k; i--) #define mem(ptr, val) memset(ptr, val, sizeof ptr) #define sbit(n) __builtin_popcount(n) #define nl "\n" typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<pair<int, int>> vpii; const long double eps = 1e-9; const int mod = 1e9 + 7; pii dir[8] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {-1, -1}, {-1, 1}, {1, -1}}; //*********************************!Template Ends //Here!**********************************// const int nn = 1e3 + 5; int dist[nn][nn]; string s[nn]; int h, w; queue<pii> q; void bfs() { while (!q.empty()) { auto p = q.front(); q.pop(); int i = p.ff; int j = p.ss; for (int ii = 0; ii < 4; ii++) { int x = i + dir[ii].ff, y = j + dir[ii].ss; if (x >= 0 and y >= 0 and x < h and y < w and dist[x][y] == -1) { dist[x][y] = 1 + dist[i][j]; q.push({x, y}); } } } } void robin() { cin >> h >> w; fr(i, 0, h) cin >> s[i]; int ans = 0; memset(dist, -1, sizeof dist); fr(i, 0, h) { fr(j, 0, w) { if (s[i][j] == '#') { dist[i][j] = 0; q.push({i, j}); } } } bfs(); fr(i, 0, h) fr(j, 0, w) ans = max(ans, dist[i][j]); cout << ans; } int32_t main() { // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; // cin >> T; for (int tc = 1; tc <= T; tc++) { // cout << "Case #" << tc << ": "; robin(); } return 0; }
replace
100
102
100
101
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> typedef long long LL; using namespace std; int main() { int h, w; cin >> h >> w; vector<vector<char>> g(h, vector<char>(w)); vector<vector<int>> d(h, vector<int>(w, -1)); queue<pair<int, int>> Q; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> g[i][j]; if (g[i][j] == '#') { d[i][j] = 0; Q.push(make_pair(i, j)); } } } int ans = 0; int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; pair<int, int> p; while (!Q.empty()) { p = Q.front(); Q.pop(); int x = p.first; int y = p.second; ans = d[x][y]; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (d[nx][ny] == -1 && nx >= 0 && nx < h && ny >= 0 && ny < w) { d[nx][ny] = d[x][y] + 1; Q.push(make_pair(nx, ny)); } } } cout << ans << endl; }
#include <bits/stdc++.h> typedef long long LL; using namespace std; int main() { int h, w; cin >> h >> w; vector<vector<char>> g(h, vector<char>(w)); vector<vector<int>> d(h, vector<int>(w, -1)); queue<pair<int, int>> Q; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> g[i][j]; if (g[i][j] == '#') { d[i][j] = 0; Q.push(make_pair(i, j)); } } } int ans = 0; int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; pair<int, int> p; while (!Q.empty()) { p = Q.front(); Q.pop(); int x = p.first; int y = p.second; ans = d[x][y]; for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < h && ny >= 0 && ny < w && d[nx][ny] == -1) { d[nx][ny] = d[x][y] + 1; Q.push(make_pair(nx, ny)); } } } cout << ans << endl; }
replace
33
34
33
34
-11
p03053
C++
Runtime Error
#include <algorithm> #include <deque> #include <iostream> #include <math.h> #include <vector> using namespace std; int main() { typedef pair<char, char> pair; int H, W; cin >> H >> W; vector<string> maze(H); vector<vector<int>> dist(H, vector<int>(W, -1)); for (int i = 0; i < H; i++) cin >> maze[i]; deque<pair> q; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (maze[i][j] == '.') continue; q.push_back(make_pair(i, j)); dist[i][j] = 0; } } while (!q.empty()) { auto curr = q.front(); int h = curr.first; int w = curr.second; q.pop_front(); if (h > 0 && dist[h - 1][w] == -1) { q.push_back(make_pair(h - 1, w)); dist[h - 1][w] = dist[h][w] + 1; } if (h < H - 1 && dist[h + 1][w] == -1) { q.push_back(make_pair(h + 1, w)); dist[h + 1][w] = dist[h][w] + 1; } if (w > 0 && dist[h][w - 1] == -1) { q.push_back(make_pair(h, w - 1)); dist[h][w - 1] = dist[h][w] + 1; } if (w < W - 1 && dist[h][w + 1] == -1) { q.push_back(make_pair(h, w + 1)); dist[h][w + 1] = dist[h][w] + 1; } } int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ans = max(dist[i][j], ans); } } cout << ans << endl; }
#include <algorithm> #include <deque> #include <iostream> #include <math.h> #include <vector> using namespace std; int main() { typedef pair<int, int> pair; int H, W; cin >> H >> W; vector<string> maze(H); vector<vector<int>> dist(H, vector<int>(W, -1)); for (int i = 0; i < H; i++) cin >> maze[i]; deque<pair> q; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (maze[i][j] == '.') continue; q.push_back(make_pair(i, j)); dist[i][j] = 0; } } while (!q.empty()) { auto curr = q.front(); int h = curr.first; int w = curr.second; q.pop_front(); if (h > 0 && dist[h - 1][w] == -1) { q.push_back(make_pair(h - 1, w)); dist[h - 1][w] = dist[h][w] + 1; } if (h < H - 1 && dist[h + 1][w] == -1) { q.push_back(make_pair(h + 1, w)); dist[h + 1][w] = dist[h][w] + 1; } if (w > 0 && dist[h][w - 1] == -1) { q.push_back(make_pair(h, w - 1)); dist[h][w - 1] = dist[h][w] + 1; } if (w < W - 1 && dist[h][w + 1] == -1) { q.push_back(make_pair(h, w + 1)); dist[h][w + 1] = dist[h][w] + 1; } } int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ans = max(dist[i][j], ans); } } cout << ans << endl; }
replace
8
9
8
9
0
p03053
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iostream> #include <limits> #include <string> #include <vector> struct Point { int x; int y; Point(int x, int y) : x(x), y(y) {} }; struct Cell { char c; int count; Cell() : c{' '}, count{std::numeric_limits<decltype(count)>::max()} {} bool update_count(int count) { if (count < this->count) { this->count = count; return true; } else { return false; } } }; using Points = std::vector<Point>; using Row = std::vector<Cell>; using Board = std::vector<Row>; bool update(Board &board, const Point &black, int r) { const int y_max = board.size(); const int x_max = board.front().size(); bool is_updated = false; const auto x_first = std::max(black.x - r, 0); const auto x_last = std::min(black.x + r + 1, x_max); if (black.y - r >= 0) { for (int x = x_first; x < x_last; ++x) { if (board[black.y - r][x].update_count(r + std::abs(black.x - x))) { is_updated = true; } } } if (black.y + r < y_max) { for (int x = x_first; x < x_last; ++x) { if (board[black.y + r][x].update_count(r + std::abs(black.x - x))) { is_updated = true; } } } const auto y_first = std::max(black.y - r + 1, 0); const auto y_last = std::min(black.y + r, y_max); if (black.x - r >= 0) { for (int y = y_first; y < y_last; ++y) { if (board[y][black.x - r].update_count(r + std::abs(black.y - y))) { is_updated = true; } } } if (black.x + r < x_max) { for (int y = y_first; y < y_last; ++y) { if (board[y][black.x + r].update_count(r + std::abs(black.y - y))) { is_updated = true; } } } return is_updated; } int solve(Board &board, Points blacks) { for (int i = 1; !blacks.empty(); ++i) { for (auto it = blacks.rbegin(); it != blacks.rend(); ++it) { if (!update(board, *it, i)) { blacks.erase((it + 1).base()); } } } int result = 0; for (const auto &v : board) { const auto it = std::max_element( v.begin(), v.end(), [](const Cell &lhs, const Cell &rhs) { return lhs.count < rhs.count; }); result = std::max(result, it->count); } return result; } int main() { int h, w; std::cin >> h >> w; Board board(h, Row(w)); Points blacks; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { std::cin >> board[i][j].c; if (board[i][j].c == '#') { board[i][j].count = 0; blacks.emplace_back(j, i); } } } std::cout << solve(board, blacks) << std::endl; }
#include <algorithm> #include <cmath> #include <iostream> #include <limits> #include <string> #include <vector> struct Point { int x; int y; Point(int x, int y) : x(x), y(y) {} }; struct Cell { char c; int count; Cell() : c{' '}, count{std::numeric_limits<decltype(count)>::max()} {} bool update_count(int count) { if (count < this->count) { this->count = count; return true; } else { return false; } } }; using Points = std::vector<Point>; using Row = std::vector<Cell>; using Board = std::vector<Row>; bool update(Board &board, const Point &black, int r) { const int y_max = board.size(); const int x_max = board.front().size(); bool is_updated = false; const auto x_first = std::max(black.x - r, 0); const auto x_last = std::min(black.x + r + 1, x_max); if (black.y - r >= 0) { for (int x = x_first; x < x_last; ++x) { if (board[black.y - r][x].update_count(r + std::abs(black.x - x))) { is_updated = true; } } } if (black.y + r < y_max) { for (int x = x_first; x < x_last; ++x) { if (board[black.y + r][x].update_count(r + std::abs(black.x - x))) { is_updated = true; } } } const auto y_first = std::max(black.y - r + 1, 0); const auto y_last = std::min(black.y + r, y_max); if (black.x - r >= 0) { for (int y = y_first; y < y_last; ++y) { if (board[y][black.x - r].update_count(r + std::abs(black.y - y))) { is_updated = true; } } } if (black.x + r < x_max) { for (int y = y_first; y < y_last; ++y) { if (board[y][black.x + r].update_count(r + std::abs(black.y - y))) { is_updated = true; } } } return is_updated; } int solve(Board &board, Points blacks) { const auto first = blacks.begin(); auto last = blacks.end(); for (int i = 1; first != last; ++i) { last = std::remove_if(first, last, [&board, i](const Point &p) { return !update(board, p, i); }); } int result = 0; for (const auto &v : board) { const auto it = std::max_element( v.begin(), v.end(), [](const Cell &lhs, const Cell &rhs) { return lhs.count < rhs.count; }); result = std::max(result, it->count); } return result; } int main() { int h, w; std::cin >> h >> w; Board board(h, Row(w)); Points blacks; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { std::cin >> board[i][j].c; if (board[i][j].c == '#') { board[i][j].count = 0; blacks.emplace_back(j, i); } } } std::cout << solve(board, blacks) << std::endl; }
replace
77
83
77
83
TLE
p03053
C++
Time Limit Exceeded
/* author : s@if */ #include <bits/stdc++.h> using namespace std; #define NIL -1 #define fi first #define sec second #define MAX INT_MAX #define INF 99999999 #define ll long long #define PI acos(-1.0) #define MOD 1000000007 #define PLL pair<ll, ll> #define PII pair<int, int> #define ull unsigned long long #define triplell pair<pair<ll, ll>, ll> #define triple pair<pair<int, int>, int> #define For(i, n) for (int i = 0; i < (int)n; i++) #define Forn(i, a, b) for (int i = (int)a; i <= (int)b; i++) #define forba(i, a, b) for (int i = (int)b; i >= (int)a; i--) bool Check(int N, int pos) { return (bool)(N & (1 << pos)); } int Set(int N, int pos) { return N = N | (1 << pos); } int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1}; int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; int hr[] = {-2, -2, -1, +1, +2, +2, -1, +1}; int hc[] = {+1, -1, +2, +2, -1, +1, -2, -2}; int dx[] = {+1, -1, +0, +0}; int dy[] = {+0, +0, +1, -1}; const int MAXN = (int)1e5 + 9; char ch[1009][1009]; int n, m, cnt; bool chekh(int x, int y) { if (x >= 0 && x < n && y >= 0 && y < m) return true; return false; } void solve(vector<PII> v1) { int i, j; while (1) { /* for(int i = 0;i<v1.size();i++) cout<<v1[i].fi<<" "<<v1[i].sec<<endl; cout<<endl<<endl; */ vector<PII> v2; bool flag = false; for (int i = 0; i < v1.size(); i++) { PII u = v1[i]; for (int j = 0; j < 4; j++) { int nx = u.fi + dx[j]; int ny = u.sec + dy[j]; if (chekh(nx, ny) && ch[nx][ny] == '.') { if (!flag) { cnt++; flag = true; } ch[nx][ny] = '#'; v2.push_back(PII(nx, ny)); } } } for (int i = 0; i < v2.size(); i++) v1.push_back(v2[i]); if (v2.size() == 0) return; } return; } int main() { /* freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); */ int i, j, k, l, p, q, x, y, u, v, r, tc, t; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) scanf("%s", ch[i]); cnt = 0; vector<PII> v1; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (ch[i][j] == '#') { v1.push_back(PII(i, j)); } } } // memset(vis,0,sizeof(vis)); solve(v1); printf("%d\n", cnt); return 0; }
/* author : s@if */ #include <bits/stdc++.h> using namespace std; #define NIL -1 #define fi first #define sec second #define MAX INT_MAX #define INF 99999999 #define ll long long #define PI acos(-1.0) #define MOD 1000000007 #define PLL pair<ll, ll> #define PII pair<int, int> #define ull unsigned long long #define triplell pair<pair<ll, ll>, ll> #define triple pair<pair<int, int>, int> #define For(i, n) for (int i = 0; i < (int)n; i++) #define Forn(i, a, b) for (int i = (int)a; i <= (int)b; i++) #define forba(i, a, b) for (int i = (int)b; i >= (int)a; i--) bool Check(int N, int pos) { return (bool)(N & (1 << pos)); } int Set(int N, int pos) { return N = N | (1 << pos); } int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1}; int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1}; int hr[] = {-2, -2, -1, +1, +2, +2, -1, +1}; int hc[] = {+1, -1, +2, +2, -1, +1, -2, -2}; int dx[] = {+1, -1, +0, +0}; int dy[] = {+0, +0, +1, -1}; const int MAXN = (int)1e5 + 9; char ch[1009][1009]; int n, m, cnt; bool chekh(int x, int y) { if (x >= 0 && x < n && y >= 0 && y < m) return true; return false; } void solve(vector<PII> v1) { int i, j; while (1) { /* for(int i = 0;i<v1.size();i++) cout<<v1[i].fi<<" "<<v1[i].sec<<endl; cout<<endl<<endl; */ vector<PII> v2; bool flag = false; for (int i = 0; i < v1.size(); i++) { PII u = v1[i]; for (int j = 0; j < 4; j++) { int nx = u.fi + dx[j]; int ny = u.sec + dy[j]; if (chekh(nx, ny) && ch[nx][ny] == '.') { if (!flag) { cnt++; flag = true; } ch[nx][ny] = '#'; v2.push_back(PII(nx, ny)); } } } v1 = v2; if (v2.size() == 0) return; } return; } int main() { /* freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); */ int i, j, k, l, p, q, x, y, u, v, r, tc, t; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) scanf("%s", ch[i]); cnt = 0; vector<PII> v1; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (ch[i][j] == '#') { v1.push_back(PII(i, j)); } } } // memset(vis,0,sizeof(vis)); solve(v1); printf("%d\n", cnt); return 0; }
replace
77
79
77
78
TLE
p03053
C++
Time Limit Exceeded
#include <iostream> #include <queue> #include <set> using namespace std; typedef long long ll; struct cell { ll i, j; cell(ll _i, ll _j) { i = _i; j = _j; } }; int main(void) { ll H, W; ll A[1000][1000]; cin >> H >> W; for (ll i = 0; i < H; i++) { for (ll j = 0; j < W; j++) { char c; cin >> c; if (c == '.') { A[i][j] = 0; } else if (c == '#') { A[i][j] = 1; } else break; } } ll d[H][W]; queue<cell> que; for (ll i = 0; i < H; i++) { for (ll j = 0; j < W; j++) { d[i][j] = 2000; if (A[i][j] == 1) { d[i][j] = 0; if (i > 0) que.push(cell(i - 1, j)); if (i < H - 1) que.push(cell(i + 1, j)); if (j > 0) que.push(cell(i, j - 1)); if (j < W - 1) que.push(cell(i, j + 1)); } } } while (!que.empty()) { cell c = que.front(); ll i = c.i, j = c.j; if (i > 0) { if (d[i][j] > d[i - 1][j] + 1) { d[i][j] = d[i - 1][j] + 1; } } if (i < H - 1) { if (d[i][j] > d[i + 1][j] + 1) { d[i][j] = d[i + 1][j] + 1; } } if (j > 0) { if (d[i][j] > d[i][j - 1] + 1) { d[i][j] = d[i][j - 1] + 1; } } if (j < W - 1) { if (d[i][j] > d[i][j + 1] + 1) { d[i][j] = d[i][j + 1] + 1; } } if (i > 0) { if (d[i][j] + 1 < d[i - 1][j]) { que.push(cell(i - 1, j)); } } if (i < H - 1) { if (d[i][j] + 1 < d[i + 1][j]) { que.push(cell(i + 1, j)); } } if (j > 0) { if (d[i][j] + 1 < d[i][j - 1]) { que.push(cell(i, j - 1)); } } if (j < W - 1) { if (d[i][j] + 1 < d[i][j + 1]) { que.push(cell(i, j + 1)); } } que.pop(); } ll ans = 0; for (ll i = 0; i < H; i++) { for (ll j = 0; j < W; j++) { ans = max(ans, d[i][j]); } } cout << ans << endl; }
#include <iostream> #include <queue> #include <set> using namespace std; typedef long long ll; struct cell { ll i, j; cell(ll _i, ll _j) { i = _i; j = _j; } }; int main(void) { ll H, W; ll A[1000][1000]; cin >> H >> W; for (ll i = 0; i < H; i++) { for (ll j = 0; j < W; j++) { char c; cin >> c; if (c == '.') { A[i][j] = 0; } else if (c == '#') { A[i][j] = 1; } else break; } } ll d[H][W]; queue<cell> que; for (ll i = 0; i < H; i++) { for (ll j = 0; j < W; j++) { d[i][j] = 2000; if (A[i][j] == 1) { d[i][j] = 0; if (i > 0) que.push(cell(i - 1, j)); if (i < H - 1) que.push(cell(i + 1, j)); if (j > 0) que.push(cell(i, j - 1)); if (j < W - 1) que.push(cell(i, j + 1)); } } } while (!que.empty()) { cell c = que.front(); ll i = c.i, j = c.j; if (d[i][j] < 2000) { que.pop(); continue; } if (i > 0) { if (d[i][j] > d[i - 1][j] + 1) { d[i][j] = d[i - 1][j] + 1; } } if (i < H - 1) { if (d[i][j] > d[i + 1][j] + 1) { d[i][j] = d[i + 1][j] + 1; } } if (j > 0) { if (d[i][j] > d[i][j - 1] + 1) { d[i][j] = d[i][j - 1] + 1; } } if (j < W - 1) { if (d[i][j] > d[i][j + 1] + 1) { d[i][j] = d[i][j + 1] + 1; } } if (i > 0) { if (d[i][j] + 1 < d[i - 1][j]) { que.push(cell(i - 1, j)); } } if (i < H - 1) { if (d[i][j] + 1 < d[i + 1][j]) { que.push(cell(i + 1, j)); } } if (j > 0) { if (d[i][j] + 1 < d[i][j - 1]) { que.push(cell(i, j - 1)); } } if (j < W - 1) { if (d[i][j] + 1 < d[i][j + 1]) { que.push(cell(i, j + 1)); } } que.pop(); } ll ans = 0; for (ll i = 0; i < H; i++) { for (ll j = 0; j < W; j++) { ans = max(ans, d[i][j]); } } cout << ans << endl; }
insert
55
55
55
59
TLE
p03053
C++
Runtime Error
#include <algorithm> #include <array> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <string> #include <vector> using namespace std; #define Rep(i, a, b) for (int i = a; i < b; i++) #define rep(i, b) Rep(i, 0, b) #define rrep(i, a) for (int i = a; i >= 0; i--) #define allof(a) (a).begin(), (a).end() typedef long long ll; const int inf = 1e9 + 7; const ll infll = 1ll << 60ll; const ll mod = 1e9 + 7; // 0~3までは右左下上 4~7までは斜め constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1}; /* // 最大公約数 ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // 最小公倍数 ll lcm(ll a, ll b) { return a * b / gcd(a, b); } */ typedef pair<int, int> P; int main() { cin.tie(0); ios::sync_with_stdio(false); int h, w; cin >> h >> w; vector<vector<char>> a(h, vector<char>(w)); queue<P> que; rep(i, h) { rep(j, w) { cin >> a[i][j]; if (a[i][j] == '#') { que.push({i, j}); } } } int ans = 0; while (1) { if (que.empty()) { cout << ans - 1 << endl; return 0; } vector<pair<int, int>> bpos; ans++; while (!que.empty()) { P p = que.front(); que.pop(); // 移動4方向をループ for (int k = 0; k < 4; k++) { int nx = p.first + dx[k], ny = p.second + dy[k]; if (nx >= 0 && nx < w && ny >= 0 && ny < h) { if (a[nx][ny] == '.') { bpos.push_back({nx, ny}); a[nx][ny] = '#'; } } } } rep(i, (int)bpos.size()) { que.push({bpos[i].first, bpos[i].second}); } } return 0; }
#include <algorithm> #include <array> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <string> #include <vector> using namespace std; #define Rep(i, a, b) for (int i = a; i < b; i++) #define rep(i, b) Rep(i, 0, b) #define rrep(i, a) for (int i = a; i >= 0; i--) #define allof(a) (a).begin(), (a).end() typedef long long ll; const int inf = 1e9 + 7; const ll infll = 1ll << 60ll; const ll mod = 1e9 + 7; // 0~3までは右左下上 4~7までは斜め constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, -1, 0, 1, 1, -1, -1, 1}; /* // 最大公約数 ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // 最小公倍数 ll lcm(ll a, ll b) { return a * b / gcd(a, b); } */ typedef pair<int, int> P; int main() { cin.tie(0); ios::sync_with_stdio(false); int h, w; cin >> h >> w; vector<vector<char>> a(h, vector<char>(w)); queue<P> que; rep(i, h) { rep(j, w) { cin >> a[i][j]; if (a[i][j] == '#') { que.push({i, j}); } } } int ans = 0; while (1) { if (que.empty()) { cout << ans - 1 << endl; return 0; } vector<pair<int, int>> bpos; ans++; while (!que.empty()) { P p = que.front(); que.pop(); // 移動4方向をループ for (int k = 0; k < 4; k++) { int nx = p.first + dx[k], ny = p.second + dy[k]; if (nx >= 0 && nx < h && ny >= 0 && ny < w) { if (a[nx][ny] == '.') { bpos.push_back({nx, ny}); a[nx][ny] = '#'; } } } } rep(i, (int)bpos.size()) { que.push({bpos[i].first, bpos[i].second}); } } return 0; }
replace
81
82
81
82
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { long H, W; cin >> H >> W; vector<vector<long>> data(H, vector<long>(W, INT_MAX)); char a; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> a; if (a == '#') data.at(i).at(j) = 0; } } for (int i = 0; i < H; i++) { for (int j = 1; j < W; j++) { data.at(i).at(j) = min(data.at(i).at(j - 1) + 1, data.at(i).at(j)); } for (int j = W - 2; j >= 0; j--) { data.at(i).at(j) = min(data.at(i).at(j + 1) + 1, data.at(i).at(j)); } } for (int i = 0; i < W; i++) { for (int j = 1; j < W; j++) { data.at(j).at(i) = min(data.at(j - 1).at(i) + 1, data.at(j).at(i)); } for (int j = H - 2; j >= 0; j--) { data.at(j).at(i) = min(data.at(j + 1).at(i) + 1, data.at(j).at(i)); } } long ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ans = max(ans, data.at(i).at(j)); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long H, W; cin >> H >> W; vector<vector<long>> data(H, vector<long>(W, INT_MAX)); char a; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> a; if (a == '#') data.at(i).at(j) = 0; } } for (int i = 0; i < H; i++) { for (int j = 1; j < W; j++) { data.at(i).at(j) = min(data.at(i).at(j - 1) + 1, data.at(i).at(j)); } for (int j = W - 2; j >= 0; j--) { data.at(i).at(j) = min(data.at(i).at(j + 1) + 1, data.at(i).at(j)); } } for (int i = 0; i < W; i++) { for (int j = 1; j < H; j++) { data.at(j).at(i) = min(data.at(j - 1).at(i) + 1, data.at(j).at(i)); } for (int j = H - 2; j >= 0; j--) { data.at(j).at(i) = min(data.at(j + 1).at(i) + 1, data.at(j).at(i)); } } long ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ans = max(ans, data.at(i).at(j)); } } cout << ans << endl; return 0; }
replace
23
24
23
24
0
p03053
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define INF 3000 #define INFF 9223372036854775807 #define ll long long #define REP(i, n) for (int i = 0; i < n; i++) #define REPP(i, m, n) for (int i = m; i < n; i++) #define ALL(N) (N.begin(), N.end()) #define de cout << "debug" << endl; #define push_back pb 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; } int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; char A[H][W]; for (int i = 1; i <= H; i++) { // 後々のために1-indexにする for (int j = 1; j <= W; j++) { cin >> A[i][j]; } } int D[1010][1010] = {}; int ans = 0; for (int i = 0; i < 1002; i++) { // 盤外にINFを代入して、範囲外の参照に対処する D[0][i] = D[i][0] = D[H + 1][i] = D[i][W + 1] = INF; } for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (A[i][j] == '#') { // #スタートだから0を代入 D[i][j] = 0; } else { // 最小の値+1がそのマスの最短の値となる D[i][j] = min(D[i - 1][j], D[i][j - 1]) + 1; } } } for (int i = H; i > 0; i--) { for (int j = W; j > 0; j--) { // 上のfor文で左上から#の最短距離を数えたのに対し // ここのfor文では右下から#の最短距離を数える(天才の発想) // 対極から二度数えることでそれぞれの撃墜ケースをなくす // また二度目のfor文では一度目のfor文との比較も行う。 D[i][j] = min({D[i][j], D[i + 1][j] + 1, D[i][j + 1] + 1}); } } for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { ans = max(ans, D[i][j]); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define INF 3000 #define INFF 9223372036854775807 #define ll long long #define REP(i, n) for (int i = 0; i < n; i++) #define REPP(i, m, n) for (int i = m; i < n; i++) #define ALL(N) (N.begin(), N.end()) #define de cout << "debug" << endl; #define push_back pb 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; } int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; char A[1010][1010]; for (int i = 1; i <= H; i++) { // 後々のために1-indexにする for (int j = 1; j <= W; j++) { cin >> A[i][j]; } } int D[1010][1010] = {}; int ans = 0; for (int i = 0; i < 1002; i++) { // 盤外にINFを代入して、範囲外の参照に対処する D[0][i] = D[i][0] = D[H + 1][i] = D[i][W + 1] = INF; } for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (A[i][j] == '#') { // #スタートだから0を代入 D[i][j] = 0; } else { // 最小の値+1がそのマスの最短の値となる D[i][j] = min(D[i - 1][j], D[i][j - 1]) + 1; } } } for (int i = H; i > 0; i--) { for (int j = W; j > 0; j--) { // 上のfor文で左上から#の最短距離を数えたのに対し // ここのfor文では右下から#の最短距離を数える(天才の発想) // 対極から二度数えることでそれぞれの撃墜ケースをなくす // また二度目のfor文では一度目のfor文との比較も行う。 D[i][j] = min({D[i][j], D[i + 1][j] + 1, D[i][j + 1] + 1}); } } for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { ans = max(ans, D[i][j]); } } cout << ans << endl; }
replace
30
31
30
31
TLE
p03053
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; string mp[1111]; int h, w; int num[1111][1111]; int vx[4] = {1, 0, -1, 0}; int vy[4] = {0, -1, 0, 1}; bool check(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w && num[x][y] == (1e9); } int main() { cin >> h >> w; for (int i = 0; i < 1111; i++) for (int j = 0; j < 1111; j++) num[i][j] = 1e9; queue<pair<pair<int, int>, int>> que; for (int i = 0; i < h; i++) { cin >> mp[i]; for (int j = 0; j < w; j++) { if (mp[i][j] == '#') { num[i][j] = 0; que.push(make_pair(make_pair(i, j), 0)); } } } while (!que.empty()) { pair<int, int> now = que.front().first; int cnt = que.front().second; for (int i = 0; i < 4; i++) { int x = vx[i] + now.first, y = vy[i] + now.second; if (check(x, y)) { num[x][y] = cnt + 1; que.push(make_pair(make_pair(x, y), cnt + 1)); } } } int ans = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) ans = max(ans, num[i][j]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string mp[1111]; int h, w; int num[1111][1111]; int vx[4] = {1, 0, -1, 0}; int vy[4] = {0, -1, 0, 1}; bool check(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w && num[x][y] == (1e9); } int main() { cin >> h >> w; for (int i = 0; i < 1111; i++) for (int j = 0; j < 1111; j++) num[i][j] = 1e9; queue<pair<pair<int, int>, int>> que; for (int i = 0; i < h; i++) { cin >> mp[i]; for (int j = 0; j < w; j++) { if (mp[i][j] == '#') { num[i][j] = 0; que.push(make_pair(make_pair(i, j), 0)); } } } while (!que.empty()) { pair<int, int> now = que.front().first; int cnt = que.front().second; que.pop(); for (int i = 0; i < 4; i++) { int x = vx[i] + now.first, y = vy[i] + now.second; if (check(x, y)) { num[x][y] = cnt + 1; que.push(make_pair(make_pair(x, y), cnt + 1)); } } } int ans = 0; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) ans = max(ans, num[i][j]); cout << ans << endl; return 0; }
insert
28
28
28
29
TLE
p03053
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define reps(i, n) for (int i = 1; i <= int(n); i++) #define rreps(i, n) for (int i = int(n); i >= 1; i--) #define repi(i, a, b) for (int i = (a); i < int(b); i++) #define all(a) (a).begin(), (a).end() #define bit(b) (1ll << (b)) #define uniq(v) (v).erase(unique(all(v)), (v).end()) #define rsort(v) \ sort(all(v)); \ reverse(all(v)) using namespace std; using i32 = int; using i64 = long long; using f80 = long double; using vi32 = vector<i32>; using vi64 = vector<i64>; using vf80 = vector<f80>; using vstr = vector<string>; inline void yes() { cout << "Yes" << '\n'; exit(0); } inline void no() { cout << "No" << '\n'; exit(0); } inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); } inline i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; } inline bool isprime(i32 n) { if (n < 2) return 0; for (i32 i = 2; i * i <= n; i++) if (n % i == 0) return 0; return 1; } inline vi32 plist(i32 n) { vi32 f(n, 1); f[0] = f[1] = 0; repi(i, 2, n) for (int j = i * 2; j < n; j += i) f[j] = 0; return f; } inline void ioinit() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); } template <class T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {}; template <class T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {}; template <class T> inline bool amax(T &x, T y) { if (x >= y) return 0; x = y; return 1; } template <class T> inline bool amin(T &x, T y) { if (x <= y) return 0; x = y; return 1; } template <class T> inline T power(T x, i64 n) { T r = 1; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <class T> vector<T> make_v(int n, T v) { return vector<T>(n, v); } template <class... Ts> auto make_v(int n, Ts... ts) { return vector<decltype(make_v(ts...))>(n, make_v(ts...)); } int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int main() { ioinit(); int h, w; cin >> h >> w; vstr a(h); rep(i, h) cin >> a[i]; using P = pair<int, int>; queue<P> q; auto board = make_v(h, w, (int)1e9); rep(i, h) rep(j, w) if (a[i][j] == '#') { q.emplace(i, j); board[i][j] = 0; } while (!q.empty()) { int i, j; tie(i, j) = q.front(), q.pop(); rep(k, 4) { int fx = i + dx[k], fy = j + dy[k]; if (fx < 0 || fx >= h || fy < 0 || fy >= w) continue; if (board[fx][fy] < board[i][j] + 1) continue; board[fx][fy] = board[i][j] + 1; q.emplace(fx, fy); } } int ans = 0; rep(i, h) rep(j, w) amax(ans, board[i][j]); cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define reps(i, n) for (int i = 1; i <= int(n); i++) #define rreps(i, n) for (int i = int(n); i >= 1; i--) #define repi(i, a, b) for (int i = (a); i < int(b); i++) #define all(a) (a).begin(), (a).end() #define bit(b) (1ll << (b)) #define uniq(v) (v).erase(unique(all(v)), (v).end()) #define rsort(v) \ sort(all(v)); \ reverse(all(v)) using namespace std; using i32 = int; using i64 = long long; using f80 = long double; using vi32 = vector<i32>; using vi64 = vector<i64>; using vf80 = vector<f80>; using vstr = vector<string>; inline void yes() { cout << "Yes" << '\n'; exit(0); } inline void no() { cout << "No" << '\n'; exit(0); } inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); } inline i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; } inline bool isprime(i32 n) { if (n < 2) return 0; for (i32 i = 2; i * i <= n; i++) if (n % i == 0) return 0; return 1; } inline vi32 plist(i32 n) { vi32 f(n, 1); f[0] = f[1] = 0; repi(i, 2, n) for (int j = i * 2; j < n; j += i) f[j] = 0; return f; } inline void ioinit() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); } template <class T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {}; template <class T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {}; template <class T> inline bool amax(T &x, T y) { if (x >= y) return 0; x = y; return 1; } template <class T> inline bool amin(T &x, T y) { if (x <= y) return 0; x = y; return 1; } template <class T> inline T power(T x, i64 n) { T r = 1; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <class T> vector<T> make_v(int n, T v) { return vector<T>(n, v); } template <class... Ts> auto make_v(int n, Ts... ts) { return vector<decltype(make_v(ts...))>(n, make_v(ts...)); } int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int main() { ioinit(); int h, w; cin >> h >> w; vstr a(h); rep(i, h) cin >> a[i]; using P = pair<int, int>; queue<P> q; auto board = make_v(h, w, (int)1e9); rep(i, h) rep(j, w) if (a[i][j] == '#') { q.emplace(i, j); board[i][j] = 0; } while (!q.empty()) { int i, j; tie(i, j) = q.front(), q.pop(); rep(k, 4) { int fx = i + dx[k], fy = j + dy[k]; if (fx < 0 || fx >= h || fy < 0 || fy >= w) continue; if (board[fx][fy] <= board[i][j] + 1) continue; board[fx][fy] = board[i][j] + 1; q.emplace(fx, fy); } } int ans = 0; rep(i, h) rep(j, w) amax(ans, board[i][j]); cout << ans << '\n'; return 0; }
replace
112
113
112
113
TLE
p03053
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define reps(i, n) for (int i = 1; i <= int(n); i++) #define rreps(i, n) for (int i = int(n); i >= 1; i--) #define repc(i, n) for (int i = 0; i <= int(n); i++) #define rrepc(i, n) for (int i = int(n); i >= 0; i--) #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define repic(i, a, b) for (int i = int(a); i <= int(b); i++) #define each(x, y) for (auto &x : y) #define all(a) (a).begin(), (a).end() #define bit32(x) (1 << (x)) #define bit64(x) (1ll << (x)) #define sz(v) ((int)v.size()) using namespace std; using i64 = long long; using f80 = long double; using vi32 = vector<int>; using vi64 = vector<i64>; using vf80 = vector<f80>; using vstr = vector<string>; inline void yes() { cout << "Yes" << endl; exit(0); } inline void no() { cout << "No" << endl; exit(0); } inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); } inline i64 lcm(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); return a / gcd(a, b) * b; } template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {}; template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {}; template <typename T> inline void amax(T &x, T y) { x = max(x, y); } template <typename T> inline void amin(T &x, T y) { x = min(x, y); } template <typename T> inline T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { each(x, v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; void solve() { int h, w; cin >> h >> w; vstr s(h); cin >> s; using P = tuple<int, int, int>; queue<P> q; vector<vi32> dp(h, vi32(w, 1e9)); rep(i, h) rep(j, w) { if (s[i][j] == '#') { dp[i][j] = 0; q.emplace(i, j, 0); } } while (q.size()) { int x, y, c; tie(x, y, c) = q.front(); q.pop(); if (dp[x][y] < c) { continue; } rep(i, 4) { int wx = x + dx[i]; int wy = y + dy[i]; if (wx < 0 || wx >= h || wy < 0 || wy >= w) continue; if (dp[wx][wy] <= c) continue; dp[wx][wy] = c + 1; q.emplace(wx, wy, c + 1); } } int ans = 0; rep(i, h) rep(j, w) { amax(ans, dp[i][j]); } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < int(n); i++) #define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--) #define reps(i, n) for (int i = 1; i <= int(n); i++) #define rreps(i, n) for (int i = int(n); i >= 1; i--) #define repc(i, n) for (int i = 0; i <= int(n); i++) #define rrepc(i, n) for (int i = int(n); i >= 0; i--) #define repi(i, a, b) for (int i = int(a); i < int(b); i++) #define repic(i, a, b) for (int i = int(a); i <= int(b); i++) #define each(x, y) for (auto &x : y) #define all(a) (a).begin(), (a).end() #define bit32(x) (1 << (x)) #define bit64(x) (1ll << (x)) #define sz(v) ((int)v.size()) using namespace std; using i64 = long long; using f80 = long double; using vi32 = vector<int>; using vi64 = vector<i64>; using vf80 = vector<f80>; using vstr = vector<string>; inline void yes() { cout << "Yes" << endl; exit(0); } inline void no() { cout << "No" << endl; exit(0); } inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); } inline i64 lcm(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); return a / gcd(a, b) * b; } template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {}; template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {}; template <typename T> inline void amax(T &x, T y) { x = max(x, y); } template <typename T> inline void amin(T &x, T y) { x = min(x, y); } template <typename T> inline T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { each(x, v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; void solve() { int h, w; cin >> h >> w; vstr s(h); cin >> s; using P = tuple<int, int, int>; queue<P> q; vector<vi32> dp(h, vi32(w, 1e9)); rep(i, h) rep(j, w) { if (s[i][j] == '#') { dp[i][j] = 0; q.emplace(i, j, 0); } } while (q.size()) { int x, y, c; tie(x, y, c) = q.front(); q.pop(); if (dp[x][y] < c) { continue; } rep(i, 4) { int wx = x + dx[i]; int wy = y + dy[i]; if (wx < 0 || wx >= h || wy < 0 || wy >= w) continue; if (dp[wx][wy] <= c + 1) continue; dp[wx][wy] = c + 1; q.emplace(wx, wy, c + 1); } } int ans = 0; rep(i, h) rep(j, w) { amax(ans, dp[i][j]); } cout << ans << endl; }
replace
111
112
111
112
TLE
p03053
C++
Runtime Error
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define SORT(c) sort((c).begin(), (c).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) #define ANS(ans) cout << (ans) << endl; #define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end()); typedef vector<int> VI; typedef pair<int, int> P; // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) for (int i = 0; i < (n); ++i) int main() { int H, W; cin >> H >> W; const int MAX_N = 10; bool a[MAX_N][MAX_N]; int memo[MAX_N][MAX_N]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; string str; REP(i, H) { cin >> str; REP(j, W) { if (str[j] == '.') a[i][j] = 0; else a[i][j] = 1; } } queue<P> q; REP(i, H) { REP(j, W) { if (a[i][j] == 1) { q.push(make_pair(i, j)); memo[i][j] = 0; } } } int ans = 0; while (q.size() > 0) { int y = q.front().first; int x = q.front().second; REP(i, 4) { if (x + dx[i] >= 0 && x + dx[i] < W && y + dy[i] >= 0 && y + dy[i] < H) { if (a[y + dy[i]][x + dx[i]] == 0) { a[y + dy[i]][x + dx[i]] = 1; q.push(make_pair(y + dy[i], x + dx[i])); memo[y + dy[i]][x + dx[i]] = memo[y][x] + 1; } } } q.pop(); } REP(i, H) { REP(j, W) { ans = max(ans, memo[i][j]); } } ANS(ans); return 0; }
// include //------------------------------------------ #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define SORT(c) sort((c).begin(), (c).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) #define ANS(ans) cout << (ans) << endl; #define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end()); typedef vector<int> VI; typedef pair<int, int> P; // repetition //------------------------------------------ #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) for (int i = 0; i < (n); ++i) int main() { int H, W; cin >> H >> W; const int MAX_N = 1010; bool a[MAX_N][MAX_N]; int memo[MAX_N][MAX_N]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; string str; REP(i, H) { cin >> str; REP(j, W) { if (str[j] == '.') a[i][j] = 0; else a[i][j] = 1; } } queue<P> q; REP(i, H) { REP(j, W) { if (a[i][j] == 1) { q.push(make_pair(i, j)); memo[i][j] = 0; } } } int ans = 0; while (q.size() > 0) { int y = q.front().first; int x = q.front().second; REP(i, 4) { if (x + dx[i] >= 0 && x + dx[i] < W && y + dy[i] >= 0 && y + dy[i] < H) { if (a[y + dy[i]][x + dx[i]] == 0) { a[y + dy[i]][x + dx[i]] = 1; q.push(make_pair(y + dy[i], x + dx[i])); memo[y + dy[i]][x + dx[i]] = memo[y][x] + 1; } } } q.pop(); } REP(i, H) { REP(j, W) { ans = max(ans, memo[i][j]); } } ANS(ans); return 0; }
replace
45
46
45
46
0
p03053
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <bits/stdc++.h> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <ios> #include <iostream> #include <list> #include <map> #include <math.h> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdlib.h> #include <time.h> #include <utility> #include <vector> using namespace std; bool vis[1003][1003]; int ans = 0; int dist[1003][1003], dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; int main() { int n, m; cin >> n >> m; queue<int> qu, qv; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { char c; cin >> c; dist[i][j] = 1e9; if (c == '#') { vis[i][j] = 1; qu.push(i); qv.push(j); dist[i][j] = 0; } } while (!qu.empty()) { int x = qu.front(), y = qv.front(); qu.pop(), qv.pop(); for (int i = 0; i < 4; i++) { int u = x + dir[i][0], v = y + dir[i][1]; if (dist[u][v] > dist[x][y] + 1) { dist[u][v] = dist[x][y] + 1; qu.push(u), qv.push(v); } } ans = max(ans, dist[x][y]); } cout << ans; }
#include <algorithm> #include <assert.h> #include <bits/stdc++.h> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <ios> #include <iostream> #include <list> #include <map> #include <math.h> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdlib.h> #include <time.h> #include <utility> #include <vector> using namespace std; bool vis[1003][1003]; int ans = 0; int dist[1003][1003], dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; int main() { int n, m; cin >> n >> m; queue<int> qu, qv; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { char c; cin >> c; dist[i][j] = 1e9; if (c == '#') { vis[i][j] = 1; qu.push(i); qv.push(j); dist[i][j] = 0; } } while (!qu.empty()) { int x = qu.front(), y = qv.front(); qu.pop(), qv.pop(); for (int i = 0; i < 4; i++) { int u = x + dir[i][0], v = y + dir[i][1]; if (u < 0 || v < 0 || u >= n || v >= m) continue; if (dist[u][v] > dist[x][y] + 1) { dist[u][v] = dist[x][y] + 1; qu.push(u), qv.push(v); } } ans = max(ans, dist[x][y]); } cout << ans; }
insert
53
53
53
55
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> #define PB push_back #define MP make_pair #define F first #define S second #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #ifdef _DEBUG_ #define debug(...) printf(__VA_ARGS__) #else #define debug(...) (void)0 #endif using namespace std; typedef long long ll; typedef pair<int, int> PII; typedef vector<int> VI; const int MAX = 1000 + 10; char in[MAX][MAX]; int dis[MAX][MAX]; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; int main() { int H, W; scanf("%d%d", &H, &W); for (int i = 0; i < H; i++) scanf("%s", in[i + 1] + 1); memset(dis, -1, sizeof(dis)); queue<PII> que; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (in[i][j] == '#') { dis[i][j] = 0; que.push(MP(i, j)); } } } while (!que.empty()) { PII t = que.front(); que.pop(); for (int k = 0; k < 4; k++) { int nx = t.F + dx[k]; int ny = t.S + dy[k]; if (dis[nx][ny] == -1) { dis[nx][ny] = dis[t.F][t.S] + 1; que.push(MP(nx, ny)); } } } int ans = 0; for (int i = 1; i <= H; i++) for (int j = 1; j <= W; j++) ans = max(ans, dis[i][j]); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> #define PB push_back #define MP make_pair #define F first #define S second #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #ifdef _DEBUG_ #define debug(...) printf(__VA_ARGS__) #else #define debug(...) (void)0 #endif using namespace std; typedef long long ll; typedef pair<int, int> PII; typedef vector<int> VI; const int MAX = 1000 + 10; char in[MAX][MAX]; int dis[MAX][MAX]; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; int main() { int H, W; scanf("%d%d", &H, &W); for (int i = 0; i < H; i++) scanf("%s", in[i + 1] + 1); memset(dis, -1, sizeof(dis)); queue<PII> que; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (in[i][j] == '#') { dis[i][j] = 0; que.push(MP(i, j)); } } } while (!que.empty()) { PII t = que.front(); que.pop(); for (int k = 0; k < 4; k++) { int nx = t.F + dx[k]; int ny = t.S + dy[k]; if (in[nx][ny] != '.' && in[nx][ny] != '#') continue; if (dis[nx][ny] == -1) { dis[nx][ny] = dis[t.F][t.S] + 1; que.push(MP(nx, ny)); } } } int ans = 0; for (int i = 1; i <= H; i++) for (int j = 1; j <= W; j++) ans = max(ans, dis[i][j]); printf("%d\n", ans); return 0; }
insert
48
48
48
50
-11
p03053
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; vector<vector<int>> d(h + 2, vector<int>(w + 2)); queue<int> q; for (int i = 0; i < h; i++) { string s; cin >> s; for (int j = 0; j < w; j++) { if (s[j] == '#') q.push((w + 2) * (i + 1) + j + 1); else d[i + 1][j + 1] = 1e9; } } int res = 0; while (q.size()) { int p = q.front(); int r = p / (w + 2); int c = p % (w + 2); int dr[] = {-1, 0, 1, 0}; int dc[] = {0, 1, 0, -1}; int i = 4; while (i--) { int nr = r + dr[i]; int nc = c + dc[i]; if (d[nr][nc] == 1e9) { res = d[nr][nc] = d[r][c] + 1; q.push((w + 2) * nr + nc); } } } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; vector<vector<int>> d(h + 2, vector<int>(w + 2)); queue<int> q; for (int i = 0; i < h; i++) { string s; cin >> s; for (int j = 0; j < w; j++) { if (s[j] == '#') q.push((w + 2) * (i + 1) + j + 1); else d[i + 1][j + 1] = 1e9; } } int res = 0; while (q.size()) { int p = q.front(); q.pop(); int r = p / (w + 2); int c = p % (w + 2); int dr[] = {-1, 0, 1, 0}; int dc[] = {0, 1, 0, -1}; int i = 4; while (i--) { int nr = r + dr[i]; int nc = c + dc[i]; if (d[nr][nc] == 1e9) { res = d[nr][nc] = d[r][c] + 1; q.push((w + 2) * nr + nc); } } } cout << res << endl; }
insert
20
20
20
21
TLE
p03053
C++
Runtime Error
#include <algorithm> #include <iostream> #include <queue> #include <string> #include <tuple> using namespace std; int main(void) { int h, w; cin >> h >> w; string s[1000]; for (int i = 0; i < h; i++) { cin >> s[i]; } queue<tuple<int, int, int>> q; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == '#') { q.push(make_tuple(i, j, 0)); } } } int a[1000][1000]; while (!q.empty()) { tuple<int, int, int> t = q.front(); q.pop(); a[get<0>(t)][get<1>(t)] = get<2>(t); int dx[] = {0, 0, -1, 1}; int dy[] = {-1, 1, 0, 0}; for (int i = 0; i <= 4; i++) { int nx = get<0>(t) + dx[i]; int ny = get<1>(t) + dy[i]; if (nx < 0) continue; if (nx >= h) continue; if (ny < 0) continue; if (ny >= w) continue; if (s[nx][ny] == '#') continue; s[nx][ny] = '#'; q.push(make_tuple(nx, ny, get<2>(t) + 1)); } } int mx = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { mx = max(mx, a[i][j]); } } cout << mx << endl; return 0; }
#include <algorithm> #include <iostream> #include <queue> #include <string> #include <tuple> using namespace std; int main(void) { int h, w; cin >> h >> w; string s[1000]; for (int i = 0; i < h; i++) { cin >> s[i]; } queue<tuple<int, int, int>> q; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == '#') { q.push(make_tuple(i, j, 0)); } } } int a[1000][1000]; while (!q.empty()) { tuple<int, int, int> t = q.front(); q.pop(); a[get<0>(t)][get<1>(t)] = get<2>(t); int dx[] = {0, 0, -1, 1}; int dy[] = {-1, 1, 0, 0}; for (int i = 0; i < 4; i++) { int nx = get<0>(t) + dx[i]; int ny = get<1>(t) + dy[i]; if (nx < 0) continue; if (nx >= h) continue; if (ny < 0) continue; if (ny >= w) continue; if (s[nx][ny] == '#') continue; s[nx][ny] = '#'; q.push(make_tuple(nx, ny, get<2>(t) + 1)); } } int mx = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { mx = max(mx, a[i][j]); } } cout << mx << endl; return 0; }
replace
33
34
33
34
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define all(a) (a).begin(), (a).end() typedef long long ll; ll mod = 1000000007; int main() { int h, w; cin >> h >> w; string grid[w][h]; queue<pair<int, int>> q; int cnt_b = 0; for (int i = 0; i < h; ++i) { string s; cin >> s; for (int j = 0; j < w; ++j) { auto sub = s.substr(j, 1); grid[j][i] = sub; if (sub == "#") { ++cnt_b; q.push(make_pair(i, j)); } } } int out = 0; while (cnt_b < h * w) { vector<pair<int, int>> vp; while (!q.empty()) { auto p = q.front(); q.pop(); int x = p.first; int y = p.second; if (x - 1 >= 0) { if (grid[x - 1][y] == ".") { grid[x - 1][y] = "#"; ++cnt_b; vp.push_back(make_pair(x - 1, y)); } } if (x + 1 < w) { if (grid[x + 1][y] == ".") { ++cnt_b; grid[x + 1][y] = "#"; vp.push_back(make_pair(x + 1, y)); } } if (y - 1 >= 0) { if (grid[x][y - 1] == ".") { ++cnt_b; grid[x][y - 1] = "#"; vp.push_back(make_pair(x, y - 1)); } } if (y + 1 < h) { if (grid[x][y + 1] == ".") { ++cnt_b; grid[x][y + 1] = "#"; vp.push_back(make_pair(x, y + 1)); } } } for (auto &&e : vp) q.push(e); ++out; } cout << out << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define all(a) (a).begin(), (a).end() typedef long long ll; ll mod = 1000000007; int main() { int h, w; cin >> h >> w; string grid[w][h]; queue<pair<int, int>> q; int cnt_b = 0; for (int i = 0; i < h; ++i) { string s; cin >> s; for (int j = 0; j < w; ++j) { auto sub = s.substr(j, 1); grid[j][i] = sub; if (sub == "#") { ++cnt_b; q.push(make_pair(j, i)); } } } int out = 0; while (cnt_b < h * w) { vector<pair<int, int>> vp; while (!q.empty()) { auto p = q.front(); q.pop(); int x = p.first; int y = p.second; if (x - 1 >= 0) { if (grid[x - 1][y] == ".") { grid[x - 1][y] = "#"; ++cnt_b; vp.push_back(make_pair(x - 1, y)); } } if (x + 1 < w) { if (grid[x + 1][y] == ".") { ++cnt_b; grid[x + 1][y] = "#"; vp.push_back(make_pair(x + 1, y)); } } if (y - 1 >= 0) { if (grid[x][y - 1] == ".") { ++cnt_b; grid[x][y - 1] = "#"; vp.push_back(make_pair(x, y - 1)); } } if (y + 1 < h) { if (grid[x][y + 1] == ".") { ++cnt_b; grid[x][y + 1] = "#"; vp.push_back(make_pair(x, y + 1)); } } } for (auto &&e : vp) q.push(e); ++out; } cout << out << endl; return 0; }
replace
20
21
20
21
0
p03053
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; const int inf = 2147483647, dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; // 上 左 下 右 const int N = 100005, M = 1000005, mod = 1000000007; const long long llinf = 9223372036854775807ll; int n, m, t; char a[1000][1000]; queue<pair<int, int>> q; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> a[i][j]; if (a[i][j] == '#') q.push(make_pair(i, j)); } while (!q.empty()) { int len = q.size(); t++; for (int i = 1; i <= len; i++) { pair<int, int> p = q.front(); q.pop(); int x = p.first, y = p.second; for (int d = 0; d < 4; d++) { int xx = x + dx[d], yy = y + dy[d]; if (a[xx][yy] == '.') q.push(make_pair(xx, yy)); a[xx][yy] = '#'; } } } cout << t - 1; return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; const int inf = 2147483647, dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; // 上 左 下 右 const int N = 100005, M = 1000005, mod = 1000000007; const long long llinf = 9223372036854775807ll; int n, m, t; char a[1005][1005]; queue<pair<int, int>> q; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> a[i][j]; if (a[i][j] == '#') q.push(make_pair(i, j)); } while (!q.empty()) { int len = q.size(); t++; for (int i = 1; i <= len; i++) { pair<int, int> p = q.front(); q.pop(); int x = p.first, y = p.second; for (int d = 0; d < 4; d++) { int xx = x + dx[d], yy = y + dy[d]; if (a[xx][yy] == '.') q.push(make_pair(xx, yy)); a[xx][yy] = '#'; } } } cout << t - 1; return 0; }
replace
18
19
18
19
0
p03053
C++
Runtime Error
#pragma region _head #include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <stack> #include <string> #include <utility> #include <vector> void Init() { std::cin.tie(0); std::ios::sync_with_stdio(false); struct Init_caller { Init_caller() { Init(); } } caller; } #pragma region _define #define int LL #define rep(i, n) for (LL(i) = 0; (i) < (n); (i)++) #define all(a) (a).begin(), (a).end() #define size(s) ((LL)s.size()) #define F first #define S second #define check() cout << "! ! !" #define endl "\n" #define _y() cout << "Yes" << endl #define _Y() cout << "YES" << endl #define _n() cout << "No" << endl #define _N() cout << "NO" << endl #define INT_INF INT_MAX #define INF LLONG_MAX #define MOD ((int)pow(10, 9) + 7) #pragma endregion #pragma region _using using namespace std; using LL = long long; using st = string; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; using vc = vector<char>; using vvc = vector<vc>; using vs = vector<st>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using qi = queue<int>; using qc = queue<char>; using qs = queue<st>; using si = stack<int>; using sc = stack<char>; using ss = stack<st>; using pi = pair<int, int>; using ppi = pair<pi, int>; using mii = map<int, int>; using mpii = map<pi, int>; using mib = map<int, bool>; using mci = map<char, int>; using msb = map<st, bool>; using vpi = vector<pi>; using vppi = vector<ppi>; using spi = stack<pi>; using qpi = queue<pi>; #pragma endregion // 4,2,8,6,1,7,3,9 int dx[] = {-1, 0, 0, 1, -1, -1, 1, 1}; int dy[] = {0, 1, -1, 0, 1, -1, 1, -1}; void y_n(bool p) { p ? _y() : _n(); } void Y_N(bool p) { p ? _Y() : _N(); } LL vmax(vi v) { int n = size(v); int MAX = 0; rep(i, n) { MAX = max(MAX, v[i]); } return MAX; } LL vmin(vi v) { int n = size(v); int MIN = INF; rep(i, n) { MIN = min(MIN, v[i]); } return MIN; } LL vsum(vi v) { int n = size(v); int sum = 0; rep(i, n) { sum += v[i]; } return sum; } LL gcd(LL a, LL b) { if (b == 0) { swap(a, b); } LL r; while ((r = a % b) != 0) { a = b; b = r; } return b; } LL lcm(LL a, LL b) { return (a / gcd(a, b) * b); } bool is_square(int n) { if ((int)sqrt(n) * (int)sqrt(n) == n) { return true; } else { return false; } } bool is_prime(int n) { if (n == 1) { return false; } else { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } } return true; } void dec_num(int n, vi &v) { int a = 2; v.push_back(1); v.push_back(n); while (a * a <= n) { if (n % a == 0) { v.push_back(a); v.push_back(n / a); } a++; } sort(all(v)); } void dec_prime(int n, vi &v) { int a = 2; while (a * a <= n) { if (n % a == 0) { v.push_back(a); n /= a; } else { a++; } } v.push_back(n); } int sieve_prime(LL a, LL b) { if (a > b) swap(a, b); vb s(b + 1, true); int cnt_a = 0, cnt_b = 0; for (int i = 2; i <= b; i++) { for (int j = 2; i * j <= b; j++) { s[i * j] = false; } } for (int i = 2; i <= b; i++) { if (s[i]) { // cout << i << " "; if (i < a) { cnt_a++; } cnt_b++; } } return cnt_b - cnt_a; } LL factorial(LL n) { LL a = 1, ret = 1; while (a < n) { a++; ret *= a; // ret %= MOD; } return ret; } vvi comb; void init_comb() { comb.resize(4005, vi(4005)); comb[0][0] = 1; rep(i, 4001) { rep(j, i + 1) { comb[i + 1][j] += comb[i][j]; comb[i + 1][j] %= MOD; comb[i + 1][j + 1] += comb[i][j]; comb[i + 1][j + 1] %= MOD; } } } int combination(int n, int r) { if (r < 0 || r > n) return 0; else return comb[n][r]; } #pragma endregion vi par; int root(int x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } void unite(int x, int y) { if (root(x) == root(y)) { return; } else { par[root(x)] = root(y); return; } } bool same(int x, int y) { return root(x) == root(y); } struct edge { int to; int cost; }; using ve = vector<edge>; using vve = vector<ve>; /*****************************************************************************/ signed main() { int h, w; cin >> h >> w; vs a(h); qpi q; vvi d(w, vi(h, INT_INF)); rep(i, h) { cin >> a[i]; rep(j, w) { if (a[i][j] == '#') { q.push({j, i}); d[j][i] = 0; } } } while (!q.empty()) { pi p = q.front(); q.pop(); rep(i, 4) { int nx = p.first + dx[i], ny = p.second + dy[i]; if (nx < 0 || w <= nx || ny < 0 || h <= ny || a[ny][nx] == '#' || d[nx][ny] != INT_INF) continue; q.push({nx, ny}); // 隣のマスへの遷移 d[nx][ny] = d[p.first][p.second] + 1; } } int ans = 0; rep(i, h) { ans = vmax(d[i]); } cout << ans; return 0; }
#pragma region _head #include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <stack> #include <string> #include <utility> #include <vector> void Init() { std::cin.tie(0); std::ios::sync_with_stdio(false); struct Init_caller { Init_caller() { Init(); } } caller; } #pragma region _define #define int LL #define rep(i, n) for (LL(i) = 0; (i) < (n); (i)++) #define all(a) (a).begin(), (a).end() #define size(s) ((LL)s.size()) #define F first #define S second #define check() cout << "! ! !" #define endl "\n" #define _y() cout << "Yes" << endl #define _Y() cout << "YES" << endl #define _n() cout << "No" << endl #define _N() cout << "NO" << endl #define INT_INF INT_MAX #define INF LLONG_MAX #define MOD ((int)pow(10, 9) + 7) #pragma endregion #pragma region _using using namespace std; using LL = long long; using st = string; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; using vc = vector<char>; using vvc = vector<vc>; using vs = vector<st>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using qi = queue<int>; using qc = queue<char>; using qs = queue<st>; using si = stack<int>; using sc = stack<char>; using ss = stack<st>; using pi = pair<int, int>; using ppi = pair<pi, int>; using mii = map<int, int>; using mpii = map<pi, int>; using mib = map<int, bool>; using mci = map<char, int>; using msb = map<st, bool>; using vpi = vector<pi>; using vppi = vector<ppi>; using spi = stack<pi>; using qpi = queue<pi>; #pragma endregion // 4,2,8,6,1,7,3,9 int dx[] = {-1, 0, 0, 1, -1, -1, 1, 1}; int dy[] = {0, 1, -1, 0, 1, -1, 1, -1}; void y_n(bool p) { p ? _y() : _n(); } void Y_N(bool p) { p ? _Y() : _N(); } LL vmax(vi v) { int n = size(v); int MAX = 0; rep(i, n) { MAX = max(MAX, v[i]); } return MAX; } LL vmin(vi v) { int n = size(v); int MIN = INF; rep(i, n) { MIN = min(MIN, v[i]); } return MIN; } LL vsum(vi v) { int n = size(v); int sum = 0; rep(i, n) { sum += v[i]; } return sum; } LL gcd(LL a, LL b) { if (b == 0) { swap(a, b); } LL r; while ((r = a % b) != 0) { a = b; b = r; } return b; } LL lcm(LL a, LL b) { return (a / gcd(a, b) * b); } bool is_square(int n) { if ((int)sqrt(n) * (int)sqrt(n) == n) { return true; } else { return false; } } bool is_prime(int n) { if (n == 1) { return false; } else { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } } return true; } void dec_num(int n, vi &v) { int a = 2; v.push_back(1); v.push_back(n); while (a * a <= n) { if (n % a == 0) { v.push_back(a); v.push_back(n / a); } a++; } sort(all(v)); } void dec_prime(int n, vi &v) { int a = 2; while (a * a <= n) { if (n % a == 0) { v.push_back(a); n /= a; } else { a++; } } v.push_back(n); } int sieve_prime(LL a, LL b) { if (a > b) swap(a, b); vb s(b + 1, true); int cnt_a = 0, cnt_b = 0; for (int i = 2; i <= b; i++) { for (int j = 2; i * j <= b; j++) { s[i * j] = false; } } for (int i = 2; i <= b; i++) { if (s[i]) { // cout << i << " "; if (i < a) { cnt_a++; } cnt_b++; } } return cnt_b - cnt_a; } LL factorial(LL n) { LL a = 1, ret = 1; while (a < n) { a++; ret *= a; // ret %= MOD; } return ret; } vvi comb; void init_comb() { comb.resize(4005, vi(4005)); comb[0][0] = 1; rep(i, 4001) { rep(j, i + 1) { comb[i + 1][j] += comb[i][j]; comb[i + 1][j] %= MOD; comb[i + 1][j + 1] += comb[i][j]; comb[i + 1][j + 1] %= MOD; } } } int combination(int n, int r) { if (r < 0 || r > n) return 0; else return comb[n][r]; } #pragma endregion vi par; int root(int x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } void unite(int x, int y) { if (root(x) == root(y)) { return; } else { par[root(x)] = root(y); return; } } bool same(int x, int y) { return root(x) == root(y); } struct edge { int to; int cost; }; using ve = vector<edge>; using vve = vector<ve>; /*****************************************************************************/ signed main() { int h, w; cin >> h >> w; vs a(h); qpi q; vvi d(w, vi(h, INT_INF)); rep(i, h) { cin >> a[i]; rep(j, w) { if (a[i][j] == '#') { q.push({j, i}); d[j][i] = 0; } } } while (!q.empty()) { pi p = q.front(); q.pop(); rep(i, 4) { int nx = p.first + dx[i], ny = p.second + dy[i]; if (nx < 0 || w <= nx || ny < 0 || h <= ny || a[ny][nx] == '#' || d[nx][ny] != INT_INF) continue; q.push({nx, ny}); // 隣のマスへの遷移 d[nx][ny] = d[p.first][p.second] + 1; } } int ans = 0; rep(i, h) { rep(j, w) { ans = max(ans, d[j][i]); } } cout << ans; return 0; }
replace
290
291
290
293
0
p03053
Python
Runtime Error
h, w = map(int, input().split()) rb = ["#"] * (w + 2) a = [rb + [["#"] + list(input()) + ["#"] for _ in range(h)] + rb] p = [(i, j) for i in range(1, h + 1) for j in range(1, w + 1) if a[i][j] == "#"] d = (-1, 0), (1, 0), (0, -1), (0, 1) r = -1 while p: r += 1 s = [] for i, j in p: for di, dj in d: di += i dj += j if a[di][dj] == ".": a[di][dj] = "#" s.append((di, dj)) p = s print(r)
h, w = map(int, input().split()) rb = [["#"] * (w + 2)] a = rb + [list("#" + input() + "#") for _ in range(h)] + rb p = [(i, j) for i in range(1, h + 1) for j in range(1, w + 1) if a[i][j] == "#"] d = (-1, 0), (1, 0), (0, -1), (0, 1) r = -1 while p: r += 1 s = [] for i, j in p: for di, dj in d: di += i dj += j if a[di][dj] == ".": a[di][dj] = "#" s.append((di, dj)) p = s print(r)
replace
1
3
1
3
IndexError: list index out of range
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03053/Python/s767280437.py", line 4, in <module> p = [(i, j) for i in range(1, h + 1) for j in range(1, w + 1) if a[i][j] == '#'] File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03053/Python/s767280437.py", line 4, in <listcomp> p = [(i, j) for i in range(1, h + 1) for j in range(1, w + 1) if a[i][j] == '#'] IndexError: list index out of range
p03053
Python
Time Limit Exceeded
h, w = map(int, input().split()) rb = [["#"] * (w + 2)] a = rb + [list("#" + input() + "#") for _ in range(h)] + rb p = [(i, j) for i in range(1, h + 1) for j in range(1, w + 1) if a[i][j] == "#"] d = (-1, 0), (1, 0), (0, -1), (0, 1) r = -1 while p: r += 1 s = [] for i, j in p: for di, dj in d: di += i dj += j if a[di][dj] == ".": a[di][dj] = "#" s.append((di, dj)) p = s print(r)
h, w = map(int, input().split()) a = [["#"] * (w + 2)] * 2 a[1:1] = (list("#" + input() + "#") for _ in range(h)) p = [(i, j) for i in range(1, h + 1) for j in range(1, w + 1) if a[i][j] == "#"] d = (-1, 0), (1, 0), (0, -1), (0, 1) r = -1 while p: r += 1 s = [] for i, j in p: for di, dj in d: di += i dj += j if a[di][dj] == ".": a[di][dj] = "#" s.append((di, dj)) p = s print(r)
replace
1
3
1
3
TLE
p03053
Python
Runtime Error
from collections import deque h, w = map(int, input().split()) inf = 10**6 a = [[0] * (w + 2), *[[0] + [inf] * w + [0] for _ in range(h)], [0] * (w + 2)] d = deque() for i in range(h): for j, k in enumerate(input()): if k == "#": a[i + 1][j + 1] = 0 d.append((i + 1, j + 1)) stp = ((0, 1), (0, -1), (1, 0), (-1, 0)) res = 0 while d: x, y = d.popleft() for k, l in stp: nx, ny = x + k, y + l if a[x][y] + 1 < a[nx][ny]: a[nx][ny] = a[x][y] + 1 res = max(res, a[nx][ny]) d.append((nx, ny)) print(res)
from collections import deque h, w = map(int, input().split()) inf = 10**6 a = [[0] * (w + 2)] + [[0] + [inf] * w + [0] for _ in range(h)] + [[0] * (w + 2)] d = deque() for i in range(h): for j, k in enumerate(input()): if k == "#": a[i + 1][j + 1] = 0 d.append((i + 1, j + 1)) stp = ((0, 1), (0, -1), (1, 0), (-1, 0)) res = 0 while d: x, y = d.popleft() for k, l in stp: nx, ny = x + k, y + l if a[x][y] + 1 < a[nx][ny]: a[nx][ny] = a[x][y] + 1 res = max(res, a[nx][ny]) d.append((nx, ny)) print(res)
replace
4
5
4
5
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for (int i = 0; i < (n); i++) #define REP1(i, n) for (int i = 1; i < (n); i++) #define REP2(i, d, n) for (int i = (d); i < (n); i++) #define RREP(i, n) for (int i = (n); i >= 0; i--) #define CLR(a) memset((a), 0, sizeof(a)) #define MCLR(a) memset((a), -1, sizeof(a)) #define RANGE(x, y, maxX, maxY) \ (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY)) typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef vector<LL> VLL; typedef pair<int, int> PII; const int INF = 0x3f3f3f3f; const LL INFL = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-9; const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1}; int memo[1000][1000]; void solve(long long H, long long W, std::vector<std::string> A) { vector<PII> now, next; REP(i, H) REP(j, W) { if (A[i][j] == '#') now.PB(MP(i, j)); } int cnt = -1; while (!now.empty()) { cnt++; REP(i, now.size()) { REP(j, 4) { int mx = now[i].second + DX[j]; int my = now[i].first + DY[j]; if (RANGE(mx, my, W, H) && A[my][mx] == '.') { next.PB(MP(my, mx)); A[my][mx] = '#'; } } } now = next; next.clear(); } cout << cnt << endl; } int main() { long long H; scanf("%lld", &H); long long W; scanf("%lld", &W); std::vector<std::string> A(W); for (int i = 0; i < H; i++) { std::cin >> A[i]; } solve(H, W, std::move(A)); return 0; }
#include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for (int i = 0; i < (n); i++) #define REP1(i, n) for (int i = 1; i < (n); i++) #define REP2(i, d, n) for (int i = (d); i < (n); i++) #define RREP(i, n) for (int i = (n); i >= 0; i--) #define CLR(a) memset((a), 0, sizeof(a)) #define MCLR(a) memset((a), -1, sizeof(a)) #define RANGE(x, y, maxX, maxY) \ (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY)) typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef vector<LL> VLL; typedef pair<int, int> PII; const int INF = 0x3f3f3f3f; const LL INFL = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-9; const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1}; int memo[1000][1000]; void solve(long long H, long long W, std::vector<std::string> A) { vector<PII> now, next; REP(i, H) REP(j, W) { if (A[i][j] == '#') now.PB(MP(i, j)); } int cnt = -1; while (!now.empty()) { cnt++; REP(i, now.size()) { REP(j, 4) { int mx = now[i].second + DX[j]; int my = now[i].first + DY[j]; if (RANGE(mx, my, W, H) && A[my][mx] == '.') { next.PB(MP(my, mx)); A[my][mx] = '#'; } } } now = next; next.clear(); } cout << cnt << endl; } int main() { long long H; scanf("%lld", &H); long long W; scanf("%lld", &W); std::vector<std::string> A(H); for (int i = 0; i < H; i++) { std::cin >> A[i]; } solve(H, W, std::move(A)); return 0; }
replace
64
65
64
65
0
p03053
C++
Runtime Error
#include <iostream> #include <queue> #include <string.h> using namespace std; const int MAXN = 1005; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; int n, m; bool a[MAXN][MAXN]; int vis[MAXN][MAXN]; int main() { memset(vis, -1, sizeof(vis)); queue<pair<int, int>> q; cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char c; cin >> c; a[i][j] = c == '#'; if (a[i][j]) { q.push(make_pair(i, j)); vis[i][j] = 0; } } } while (!q.empty()) { int x = q.front().first, y = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int gx = x + dx[i], gy = y + dy[i]; if (vis[gx][gy] == -1) { vis[gx][gy] = vis[x][y] + 1; q.push(make_pair(gx, gy)); } } } int ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) ans = max(ans, vis[i][j]); cout << ans << endl; return 0; }
#include <iostream> #include <queue> #include <string.h> using namespace std; const int MAXN = 1005; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; int n, m; bool a[MAXN][MAXN]; int vis[MAXN][MAXN]; int main() { memset(vis, -1, sizeof(vis)); queue<pair<int, int>> q; cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char c; cin >> c; a[i][j] = c == '#'; if (a[i][j]) { q.push(make_pair(i, j)); vis[i][j] = 0; } } } while (!q.empty()) { int x = q.front().first, y = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int gx = x + dx[i], gy = y + dy[i]; if (gx <= 0 || gy <= 0 || gx > n || gy > m) continue; if (vis[gx][gy] == -1) { vis[gx][gy] = vis[x][y] + 1; q.push(make_pair(gx, gy)); } } } int ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) ans = max(ans, vis[i][j]); cout << ans << endl; return 0; }
insert
34
34
34
36
-11
p03053
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define INIT(a, b) memset(a, b, sizeof(a)) using namespace std; const int maxn = 1000 + 7; int H, W; char mp[maxn][maxn]; int ans = 0; int stk[maxn << 2][2]; int wat[maxn << 2][2]; int tail, p; void bfs() { while (tail) { p = 0; for (int i = 0; i < tail; ++i) { if (stk[i][0] - 1 >= 1 && mp[stk[i][0] - 1][stk[i][1]] == '.') { mp[stk[i][0] - 1][stk[i][1]] = '#'; wat[p][0] = stk[i][0] - 1; wat[p][1] = stk[i][1]; p++; } if (stk[i][0] - 1 <= H && mp[stk[i][0] + 1][stk[i][1]] == '.') { mp[stk[i][0] + 1][stk[i][1]] = '#'; wat[p][0] = stk[i][0] + 1; wat[p][1] = stk[i][1]; p++; } if (stk[i][1] - 1 >= 1 && mp[stk[i][0]][stk[i][1] - 1] == '.') { mp[stk[i][0]][stk[i][1] - 1] = '#'; wat[p][0] = stk[i][0]; wat[p][1] = stk[i][1] - 1; p++; } if (stk[i][1] - 1 <= W && mp[stk[i][0]][stk[i][1] + 1] == '.') { mp[stk[i][0]][stk[i][1] + 1] = '#'; wat[p][0] = stk[i][0]; wat[p][1] = stk[i][1] + 1; p++; } } tail = p; if (p) { ans++; for (int i = 0; i < p; ++i) { stk[i][0] = wat[i][0]; stk[i][1] = wat[i][1]; } } } } int main() { ios::sync_with_stdio(false); cin >> H >> W; for (int i = 0; i <= H + 1; ++i) { for (int j = 0; j <= W + 1; ++j) { mp[i][j] = '#'; } } tail = 0; for (int i = 1; i <= H; ++i) { for (int j = 1; j <= W; ++j) { cin >> mp[i][j]; if (mp[i][j] == '#') { stk[tail][0] = i; stk[tail][1] = j; tail++; } } } bfs(); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define INIT(a, b) memset(a, b, sizeof(a)) using namespace std; const int maxn = 1000 + 7; int H, W; char mp[maxn][maxn]; int ans = 0; int stk[maxn * maxn][2]; int wat[maxn * maxn][2]; int tail, p; void bfs() { while (tail) { p = 0; for (int i = 0; i < tail; ++i) { if (stk[i][0] - 1 >= 1 && mp[stk[i][0] - 1][stk[i][1]] == '.') { mp[stk[i][0] - 1][stk[i][1]] = '#'; wat[p][0] = stk[i][0] - 1; wat[p][1] = stk[i][1]; p++; } if (stk[i][0] - 1 <= H && mp[stk[i][0] + 1][stk[i][1]] == '.') { mp[stk[i][0] + 1][stk[i][1]] = '#'; wat[p][0] = stk[i][0] + 1; wat[p][1] = stk[i][1]; p++; } if (stk[i][1] - 1 >= 1 && mp[stk[i][0]][stk[i][1] - 1] == '.') { mp[stk[i][0]][stk[i][1] - 1] = '#'; wat[p][0] = stk[i][0]; wat[p][1] = stk[i][1] - 1; p++; } if (stk[i][1] - 1 <= W && mp[stk[i][0]][stk[i][1] + 1] == '.') { mp[stk[i][0]][stk[i][1] + 1] = '#'; wat[p][0] = stk[i][0]; wat[p][1] = stk[i][1] + 1; p++; } } tail = p; if (p) { ans++; for (int i = 0; i < p; ++i) { stk[i][0] = wat[i][0]; stk[i][1] = wat[i][1]; } } } } int main() { ios::sync_with_stdio(false); cin >> H >> W; for (int i = 0; i <= H + 1; ++i) { for (int j = 0; j <= W + 1; ++j) { mp[i][j] = '#'; } } tail = 0; for (int i = 1; i <= H; ++i) { for (int j = 1; j <= W; ++j) { cin >> mp[i][j]; if (mp[i][j] == '#') { stk[tail][0] = i; stk[tail][1] = j; tail++; } } } bfs(); cout << ans << endl; return 0; }
replace
9
11
9
11
0
p03053
C++
Runtime Error
#include <iostream> #include <set> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int h, w; cin >> h >> w; vector<vector<char>> a(h); set<pair<int, int>> p; for (int i = 0; i < h; i++) { a[i].resize(w); for (int j = 0; j < w; j++) { cin >> a[i][j]; if (a[i][j] == '#') p.insert({i, j}); } } int d[][2] = {-1, 0, 1, 0, 0, -1, 0, 1}; int ans = 0; while (!p.empty()) { set<pair<int, int>> s; for (auto const &x : p) { for (auto const y : d) { if (a[x.first + y[0]][x.second + y[1]] == '.') s.insert({x.first + y[0], x.second + y[1]}); } } if (!s.empty()) { ans++; for (auto const &x : s) a[x.first][x.second] = '#'; } p = move(s); } cout << ans << '\n'; return 0; }
#include <iostream> #include <set> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int h, w; cin >> h >> w; vector<vector<char>> a(h); set<pair<int, int>> p; for (int i = 0; i < h; i++) { a[i].resize(w); for (int j = 0; j < w; j++) { cin >> a[i][j]; if (a[i][j] == '#') p.insert({i, j}); } } int d[][2] = {-1, 0, 1, 0, 0, -1, 0, 1}; int ans = 0; while (!p.empty()) { set<pair<int, int>> s; for (auto const &x : p) { for (auto const y : d) { int i = x.first + y[0], j = x.second + y[1]; if (i >= 0 && i < h && j >= 0 && j < w && a[i][j] == '.') s.insert({i, j}); } } if (!s.empty()) { ans++; for (auto const &x : s) a[x.first][x.second] = '#'; } p = move(s); } cout << ans << '\n'; return 0; }
replace
27
29
27
30
-11
p03053
C++
Runtime Error
/*   こんにちは。あたしはカウガール。   redcoderになるためAtCoderを巡る旅をしてます。       __     ヽ|__|ノ    モォ     ||‘‐‘||レ  _)_, ―‐ 、     /(Y (ヽ_ /・ ヽ    ̄ヽ     ∠ _ ゝ  `^ヽ ノ.::::_(ノヽ      _/ヽ     /ヽ ̄ ̄/ヽ */ #include <bits/stdc++.h> using namespace std; typedef long long ll; #define dump(x) cout << #x << " = " << (x) << endl #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl #define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl #define rep(i, n) REP(i, 0, n) // 0, 1, ..., n-1 #define REP(i, x, n) for (int i = x; i < n; i++) // x, x + 1, ..., n-1 #define FOREACH(x, a) for (auto &(x) : (a)) #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define COUT(x) cout << (x) << endl 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; } int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { cin.tie(0); ios::sync_with_stdio(false); int h, w; cin >> h >> w; vector<string> a(h); rep(i, h) cin >> a[i]; vector<vector<int>> dist(h, vector<int>(w, -1)); queue<pair<int, int>> que; rep(i, h) { rep(j, h) { if (a[i][j] == '#') { dist[i][j] = 0; que.push(make_pair(i, j)); } } } while (!que.empty()) { auto cur = que.front(); que.pop(); for (int dir = 0; dir < 4; dir++) { int nx = cur.first + dx[dir]; int ny = cur.second + dy[dir]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (dist[nx][ny] == -1) { dist[nx][ny] = dist[cur.first][cur.second] + 1; que.push(make_pair(nx, ny)); } } } int res = 0; rep(i, h) rep(j, w) { res = max(res, dist[i][j]); } cout << res << endl; }
/*   こんにちは。あたしはカウガール。   redcoderになるためAtCoderを巡る旅をしてます。       __     ヽ|__|ノ    モォ     ||‘‐‘||レ  _)_, ―‐ 、     /(Y (ヽ_ /・ ヽ    ̄ヽ     ∠ _ ゝ  `^ヽ ノ.::::_(ノヽ      _/ヽ     /ヽ ̄ ̄/ヽ */ #include <bits/stdc++.h> using namespace std; typedef long long ll; #define dump(x) cout << #x << " = " << (x) << endl #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE") << endl #define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl #define rep(i, n) REP(i, 0, n) // 0, 1, ..., n-1 #define REP(i, x, n) for (int i = x; i < n; i++) // x, x + 1, ..., n-1 #define FOREACH(x, a) for (auto &(x) : (a)) #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() #define COUT(x) cout << (x) << endl 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; } int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { cin.tie(0); ios::sync_with_stdio(false); int h, w; cin >> h >> w; vector<string> a(h); rep(i, h) cin >> a[i]; vector<vector<int>> dist(h, vector<int>(w, -1)); queue<pair<int, int>> que; rep(i, h) { rep(j, w) { if (a[i][j] == '#') { dist[i][j] = 0; que.push(make_pair(i, j)); } } } while (!que.empty()) { auto cur = que.front(); que.pop(); for (int dir = 0; dir < 4; dir++) { int nx = cur.first + dx[dir]; int ny = cur.second + dy[dir]; if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (dist[nx][ny] == -1) { dist[nx][ny] = dist[cur.first][cur.second] + 1; que.push(make_pair(nx, ny)); } } } int res = 0; rep(i, h) rep(j, w) { res = max(res, dist[i][j]); } cout << res << endl; }
replace
61
62
61
62
0
p03053
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) bool custom1(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); } bool custom2(pair<int, int> a, pair<int, int> b) { return (a.second < b.second); } const int MAX = 200005; const long long MOD = 1000000007; const long long MODMOD = 998244353; const double PI = acos(-1); long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { 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; } } long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } 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; } long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } struct UnionFind { vector<int> p; vector<int> s; void init(int n) { p.resize(n, -1); s.resize(n, 1); } int find(int n) { if (p[n] == -1) return n; else return find(p[n]); } int size(int n) { return s[find(n)]; } void unite(int n, int m) { int pn = find(n); int pm = find(m); if (pn == pm) return; if (s[pn] > s[pm]) { s[pn] = s[pn] + s[pm]; p[pm] = pn; } else { s[pm] = s[pm] + s[pn]; p[pn] = pm; } } }; long long gcd(long long a, long long b) { if (a % b == 0) return b; else return gcd(b, a % b); } int main() { int h, w; cin >> h >> w; vector<P> black; vector<vector<char>> g(h + 2, vector<char>(w + 2)); rep(i, h) rep(j, w) { cin >> g[i + 1][j + 1]; if (g[i + 1][j + 1] == '#') black.emplace_back(make_pair(i + 1, j + 1)); } vector<vector<int>> depth(h + 2, vector<int>(w + 2, 100100100)); rep(i, black.size()) { queue<P> que; que.emplace(black[i]); depth[black[i].first][black[i].second] = 0; while (!que.empty()) { P p = que.front(); que.pop(); int r = p.first, c = p.second; int d = depth[r][c]; if (g[r][c] == '#') depth[r][c] = 0; if (g[r + 1][c] == '.' && depth[r + 1][c] > d + 1) { depth[r + 1][c] = d + 1; que.emplace(make_pair(r + 1, c)); } if (g[r - 1][c] == '.' && depth[r - 1][c] > d + 1) { depth[r - 1][c] = d + 1; que.emplace(make_pair(r - 1, c)); } if (g[r][c + 1] == '.' && depth[r][c + 1] > d + 1) { depth[r][c + 1] = d + 1; que.emplace(make_pair(r, c + 1)); } if (g[r][c - 1] == '.' && depth[r][c - 1] > d + 1) { depth[r][c - 1] = d + 1; que.emplace(make_pair(r, c - 1)); } } } int ans = 0; rep(i, h) rep(j, w) chmax(ans, depth[i + 1][j + 1]); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; #define rep(i, n) for (int(i) = 0; (i) < (n); (i)++) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) bool custom1(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); } bool custom2(pair<int, int> a, pair<int, int> b) { return (a.second < b.second); } const int MAX = 200005; const long long MOD = 1000000007; const long long MODMOD = 998244353; const double PI = acos(-1); long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { 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; } } long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } 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; } long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } struct UnionFind { vector<int> p; vector<int> s; void init(int n) { p.resize(n, -1); s.resize(n, 1); } int find(int n) { if (p[n] == -1) return n; else return find(p[n]); } int size(int n) { return s[find(n)]; } void unite(int n, int m) { int pn = find(n); int pm = find(m); if (pn == pm) return; if (s[pn] > s[pm]) { s[pn] = s[pn] + s[pm]; p[pm] = pn; } else { s[pm] = s[pm] + s[pn]; p[pn] = pm; } } }; long long gcd(long long a, long long b) { if (a % b == 0) return b; else return gcd(b, a % b); } int main() { int h, w; cin >> h >> w; vector<P> black; vector<vector<char>> g(h + 2, vector<char>(w + 2)); rep(i, h) rep(j, w) { cin >> g[i + 1][j + 1]; if (g[i + 1][j + 1] == '#') black.emplace_back(make_pair(i + 1, j + 1)); } vector<vector<int>> depth(h + 2, vector<int>(w + 2, 100100100)); queue<P> que; rep(i, black.size()) que.emplace(black[i]); while (!que.empty()) { P p = que.front(); que.pop(); int r = p.first, c = p.second; if (g[r][c] == '#') depth[r][c] = 0; int d = depth[r][c]; if (g[r + 1][c] == '.' && depth[r + 1][c] > d + 1) { depth[r + 1][c] = d + 1; que.emplace(make_pair(r + 1, c)); } if (g[r - 1][c] == '.' && depth[r - 1][c] > d + 1) { depth[r - 1][c] = d + 1; que.emplace(make_pair(r - 1, c)); } if (g[r][c + 1] == '.' && depth[r][c + 1] > d + 1) { depth[r][c + 1] = d + 1; que.emplace(make_pair(r, c + 1)); } if (g[r][c - 1] == '.' && depth[r][c - 1] > d + 1) { depth[r][c - 1] = d + 1; que.emplace(make_pair(r, c - 1)); } } int ans = 0; rep(i, h) rep(j, w) chmax(ans, depth[i + 1][j + 1]); cout << ans << endl; }
replace
108
135
108
132
TLE
p03053
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> using namespace std; using ll = long long; using ld = long double; const int INF = 1e9; const double EPS = 1e-9; const ll MOD = 1e9 + 7; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int main() { long long H; scanf("%lld", &H); long long W; scanf("%lld", &W); std::vector<std::string> A(W); for (int i = 0; i < W; i++) { std::cin >> A[i]; } queue<pair<int, int>> q; int seen[1001][1001] = {}; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { seen[i][j] = INF; if (A[i][j] == '#') { q.emplace(j, i); seen[i][j] = 0; } } } int ans = 0; while (!q.empty()) { auto p = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { int x = p.first, y = p.second; int nx = x + dx[i], ny = y + dy[i]; if (0 <= nx && nx < W && 0 <= ny && ny < H && seen[ny][nx] == INF) { q.emplace(nx, ny); seen[ny][nx] = seen[y][x] + 1; ans = seen[ny][nx]; } } } cout << ans << endl; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> using namespace std; using ll = long long; using ld = long double; const int INF = 1e9; const double EPS = 1e-9; const ll MOD = 1e9 + 7; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int main() { long long H; scanf("%lld", &H); long long W; scanf("%lld", &W); std::vector<std::string> A(H); for (int i = 0; i < H; i++) { std::cin >> A[i]; } queue<pair<int, int>> q; int seen[1001][1001] = {}; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { seen[i][j] = INF; if (A[i][j] == '#') { q.emplace(j, i); seen[i][j] = 0; } } } int ans = 0; while (!q.empty()) { auto p = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { int x = p.first, y = p.second; int nx = x + dx[i], ny = y + dy[i]; if (0 <= nx && nx < W && 0 <= ny && ny < H && seen[ny][nx] == INF) { q.emplace(nx, ny); seen[ny][nx] = seen[y][x] + 1; ans = seen[ny][nx]; } } } cout << ans << endl; return 0; }
replace
45
47
45
47
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; #define ull unsigned long long #define ld long double template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; #define rep2(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) rep2(i, 0, n) #define rep1(i, n) rep1(i, 1, n + 1) // #define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--) // #define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() // #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define RSORT(x) sort(rall(x)); #define pb push_back #define mp make_pair #define INF (1e9) #define PI (acos(-1)) #define EPS (1e-7) // 総数を1000000007(素数)で割った余り const long long mod = 1e9 + 7; const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}; const int dy[] = {1, 0, -1, 0, -1, 1, 1, -1}; ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; } ull lcm(ull a, ull b) { return a / gcd(a, b) * b; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << std::fixed << std::setprecision(15); // cout << std::setfill('0') << std::setw(2); long long H; std::cin >> H; long long W; std::cin >> W; std::vector<std::string> S(W); for (int i = 0; i < W; i++) { std::cin >> S[i]; } VV<int> r(H, (V<int>(W, INF))); queue<pii> q; rep(i, H) rep(j, W) { if (S[i][j] == '#') { r[i][j] = 0; q.emplace(i, j); } } int ans = 0; while (!q.empty()) { pii p = q.front(); int x = p.second, y = p.first; q.pop(); auto update = [&](int x, int y, int d) { if (r[y][x] != INF) return; r[y][x] = d; ans = max(ans, d); q.push(make_pair(y, x)); }; rep(i, 4) { int xx = x + dx[i]; int yy = y + dy[i]; if (xx < 0 || yy < 0 || xx >= W || yy >= H) continue; if (S[yy][xx] == '#') continue; update(xx, yy, r[y][x] + 1); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; #define ull unsigned long long #define ld long double template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; #define rep2(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) rep2(i, 0, n) #define rep1(i, n) rep1(i, 1, n + 1) // #define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--) // #define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--) #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() // #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define RSORT(x) sort(rall(x)); #define pb push_back #define mp make_pair #define INF (1e9) #define PI (acos(-1)) #define EPS (1e-7) // 総数を1000000007(素数)で割った余り const long long mod = 1e9 + 7; const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}; const int dy[] = {1, 0, -1, 0, -1, 1, 1, -1}; ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; } ull lcm(ull a, ull b) { return a / gcd(a, b) * b; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << std::fixed << std::setprecision(15); // cout << std::setfill('0') << std::setw(2); long long H; std::cin >> H; long long W; std::cin >> W; std::vector<std::string> S(H); for (int i = 0; i < H; i++) { std::cin >> S[i]; } VV<int> r(H, (V<int>(W, INF))); queue<pii> q; rep(i, H) rep(j, W) { if (S[i][j] == '#') { r[i][j] = 0; q.emplace(i, j); } } int ans = 0; while (!q.empty()) { pii p = q.front(); int x = p.second, y = p.first; q.pop(); auto update = [&](int x, int y, int d) { if (r[y][x] != INF) return; r[y][x] = d; ans = max(ans, d); q.push(make_pair(y, x)); }; rep(i, 4) { int xx = x + dx[i]; int yy = y + dy[i]; if (xx < 0 || yy < 0 || xx >= W || yy >= H) continue; if (S[yy][xx] == '#') continue; update(xx, yy, r[y][x] + 1); } } cout << ans << endl; return 0; }
replace
53
55
53
55
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; int dx[4] = {-1, 0, 0, 1}; int dy[4] = {0, 1, -1, 0}; char buf[55][55]; int field[55][55]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> buf[i][j]; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { field[i][j] = 1e5; } } queue<int> v; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (buf[i][j] == '#') { field[i][j] = 0; v.push(j); v.push(i); } } } while (v.size()) { int a = v.front(); v.pop(); int b = v.front(); v.pop(); for (int k = 0; k < 4; k++) { int x = dx[k] + a; int y = dy[k] + b; if (x >= 0 && x < w && y >= 0 && y < h && buf[y][x] == '.' && field[y][x] == 1e5) { field[y][x] = field[b][a] + 1; buf[y][x] = '#'; v.push(x); v.push(y); } } } int max = -1e6; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (max < field[i][j]) { max = field[i][j]; } } } cout << max << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; int dx[4] = {-1, 0, 0, 1}; int dy[4] = {0, 1, -1, 0}; char buf[1005][1005]; int field[1005][1005]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> buf[i][j]; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { field[i][j] = 1e5; } } queue<int> v; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (buf[i][j] == '#') { field[i][j] = 0; v.push(j); v.push(i); } } } while (v.size()) { int a = v.front(); v.pop(); int b = v.front(); v.pop(); for (int k = 0; k < 4; k++) { int x = dx[k] + a; int y = dy[k] + b; if (x >= 0 && x < w && y >= 0 && y < h && buf[y][x] == '.' && field[y][x] == 1e5) { field[y][x] = field[b][a] + 1; buf[y][x] = '#'; v.push(x); v.push(y); } } } int max = -1e6; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (max < field[i][j]) { max = field[i][j]; } } } cout << max << endl; return 0; }
replace
7
9
7
9
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, a) for (int i = 0; i < int(a); ++i) #define REP(i, a, b) for (int i = int(a); i < int(b); ++i) #define pb push_back #define mp make_pair #define F first #define S second using ll = long long; using itn = int; using namespace std; int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int main() { int H, W; cin >> H >> W; string A[H]; rep(i, H) cin >> A[i]; vector<vector<int>> dist(H, vector<int>(W, -1)); queue<pair<int, int>> que; rep(y, H) { rep(x, W) { if (A[x][y] == '#') { dist.at(y).at(x) = 0; que.push(mp(y, x)); } } } while (!que.empty()) { pair<int, int> n = que.front(); que.pop(); rep(i, 4) { int nx = n.S + dx[i], ny = n.F + dy[i]; if (nx < 0 || ny < 0 || nx >= W || ny >= H) continue; if (dist.at(ny).at(nx) >= 0) continue; if (A[ny][nx] == '#') continue; dist.at(ny).at(nx) = dist.at(n.F).at(n.S) + 1; que.push(mp(ny, nx)); } } int ans = 0; rep(i, H) { rep(j, W) { ans = max(ans, dist.at(i).at(j)); } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, a) for (int i = 0; i < int(a); ++i) #define REP(i, a, b) for (int i = int(a); i < int(b); ++i) #define pb push_back #define mp make_pair #define F first #define S second using ll = long long; using itn = int; using namespace std; int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int main() { int H, W; cin >> H >> W; string A[H]; rep(i, H) cin >> A[i]; vector<vector<int>> dist(H, vector<int>(W, -1)); queue<pair<int, int>> que; rep(y, H) { rep(x, W) { if (A[y][x] == '#') { dist.at(y).at(x) = 0; que.push(mp(y, x)); } } } while (!que.empty()) { pair<int, int> n = que.front(); que.pop(); rep(i, 4) { int nx = n.S + dx[i], ny = n.F + dy[i]; if (nx < 0 || ny < 0 || nx >= W || ny >= H) continue; if (dist.at(ny).at(nx) >= 0) continue; if (A[ny][nx] == '#') continue; dist.at(ny).at(nx) = dist.at(n.F).at(n.S) + 1; que.push(mp(ny, nx)); } } int ans = 0; rep(i, H) { rep(j, W) { ans = max(ans, dist.at(i).at(j)); } } cout << ans << endl; }
replace
23
24
23
24
0
p03053
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <string.h> #include <string> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> P; queue<P> q; char c[1010][1010]; int d[1010][1010]; int main() { int H, W; cin >> H >> W; for (int i = 0; i < H; i++) { cin >> c[i]; for (int j = 0; j < W; j++) { d[i][j] = 10000000; if (c[i][j] == '#') { q.push(P(i, j)); d[i][j] = 0; } } } int ans = 0; while (!q.empty()) { P now = q.front(); q.pop(); int nh = now.first; int nw = now.second; int dh[4] = {0, 0, 1, -1}; int dw[4] = {1, -1, 0, 0}; for (int i = 0; i < 4; i++) { int h = nh + dh[i]; int w = nw + dw[i]; if (d[h][w] > d[nh][nw] + 1) { d[h][w] = d[nh][nw] + 1; ans = max(ans, d[h][w]); q.push(P(h, w)); } } } cout << ans; return 0; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <string.h> #include <string> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> P; queue<P> q; char c[1010][1010]; int d[1010][1010]; int main() { int H, W; cin >> H >> W; for (int i = 0; i < H; i++) { cin >> c[i]; for (int j = 0; j < W; j++) { d[i][j] = 10000000; if (c[i][j] == '#') { q.push(P(i, j)); d[i][j] = 0; } } } int ans = 0; while (!q.empty()) { P now = q.front(); q.pop(); int nh = now.first; int nw = now.second; int dh[4] = {0, 0, 1, -1}; int dw[4] = {1, -1, 0, 0}; for (int i = 0; i < 4; i++) { int h = nh + dh[i]; int w = nw + dw[i]; if (0 <= h && h < H && 0 <= w && w < W) { if (d[h][w] > d[nh][nw] + 1) { d[h][w] = d[nh][nw] + 1; ans = max(ans, d[h][w]); q.push(P(h, w)); } } } } cout << ans; return 0; }
replace
40
44
40
46
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef vector<vector<char>> field_t; typedef pair<int, int> point_t; // point_tの足し算 point_t operator+(const point_t &lhs, const point_t &rhs) { point_t res; res.first = lhs.first + rhs.first; res.second = lhs.second + rhs.second; return res; } // 範囲内外判定 bool is_in_field(int col, int row, const point_t &point) { int c = point.first; int r = point.second; return ((c >= 0 && c < col) && (r >= 0 && r < row)); } // 配列要素の最大値 int max_v(vector<vector<int>> &v) { int res = v.at(0).at(0); for (int i = 0; i < v.size(); i++) { for (int j = 0; j < v.at(0).size(); j++) { res = max(res, v.at(i).at(j)); } } return res; } // 多点WFS int solve(int H, int W, queue<point_t> &que, vector<vector<int>> &v) { vector<point_t> points = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; while (!que.empty()) { point_t cur = que.front(); que.pop(); int col_cur = cur.first; int row_cur = cur.second; for (const auto &p : points) { point_t next = cur + p; int col_next = next.first; int row_next = next.second; if (is_in_field(H, W, next)) { if (v.at(col_next).at(row_next) == -1) { v.at(col_next).at(row_next) = v.at(col_cur).at(row_cur) + 1; que.push(next); } } } } return max_v(v); } int main() { // 入力 int H, W; cin >> H >> W; field_t A(H, vector<char>(W)); queue<point_t> que; vector<vector<int>> v(W, vector<int>(H, -1)); // 訪問までに必要な回数 for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A.at(i).at(j); if (A.at(i).at(j) == '#') { que.push({i, j}); // 開始位置の代入 v.at(i).at(j) = 0; // 開始位置の訪問回数を0に } } } // 出力 cout << solve(H, W, que, v) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<vector<char>> field_t; typedef pair<int, int> point_t; // point_tの足し算 point_t operator+(const point_t &lhs, const point_t &rhs) { point_t res; res.first = lhs.first + rhs.first; res.second = lhs.second + rhs.second; return res; } // 範囲内外判定 bool is_in_field(int col, int row, const point_t &point) { int c = point.first; int r = point.second; return ((c >= 0 && c < col) && (r >= 0 && r < row)); } // 配列要素の最大値 int max_v(vector<vector<int>> &v) { int res = v.at(0).at(0); for (int i = 0; i < v.size(); i++) { for (int j = 0; j < v.at(0).size(); j++) { res = max(res, v.at(i).at(j)); } } return res; } // 多点WFS int solve(int H, int W, queue<point_t> &que, vector<vector<int>> &v) { vector<point_t> points = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; while (!que.empty()) { point_t cur = que.front(); que.pop(); int col_cur = cur.first; int row_cur = cur.second; for (const auto &p : points) { point_t next = cur + p; int col_next = next.first; int row_next = next.second; if (is_in_field(H, W, next)) { if (v.at(col_next).at(row_next) == -1) { v.at(col_next).at(row_next) = v.at(col_cur).at(row_cur) + 1; que.push(next); } } } } return max_v(v); } int main() { // 入力 int H, W; cin >> H >> W; field_t A(H, vector<char>(W)); queue<point_t> que; vector<vector<int>> v(H, vector<int>(W, -1)); // 訪問までに必要な回数 for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> A.at(i).at(j); if (A.at(i).at(j) == '#') { que.push({i, j}); // 開始位置の代入 v.at(i).at(j) = 0; // 開始位置の訪問回数を0に } } } // 出力 cout << solve(H, W, que, v) << endl; return 0; }
replace
63
64
63
64
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll H, W; cin >> H >> W; vector<char> vec(H * W); // vecは各頂点の白か黒かを保持 vector<ll> answer(H * W + 1, H + W); // 各黒点からの距離のうち小さいものを保持(随時更新) for (ll i = 0; i < H * W; i++) { cin >> vec.at(i); } using Graph = vector<vector<char>>; Graph G(H * W + 1); // グラフのサイズはマス目のサイズ+最初の仮想の点分の1 for (ll i = 0; i < H * W; ++i) { // グラフの作成 if (vec.at(i) == '#') { // 仮想の点からの辺を定義 G[H * W].push_back(i); } if (i % W != W - 1) { // 左右方向の辺の定義 G[i].push_back(i + 1); G[i + 1].push_back(i); } if (i < W * (H - 1)) { // 上下方向の辺の定義 G[i].push_back(i + W); G[i + W].push_back(i); } } vector<ll> dist(H * W + 1, -2); // 全頂点を「未訪問」に初期化(-2だったら未訪問と判断) queue<ll> que; dist[H * W] = -1; // 仮想の点をスタートにする que.push(H * W); while (!que.empty()) { ll v = que.front(); // キューから先頭頂点を取り出す que.pop(); for (ll nv : G[v]) { // vから辿れる頂点をすべて調べる if (dist[nv] != -2) { continue; } // すでに発見済みの頂点は探索しない dist[nv] = dist[v] + 1; // 新たな白色頂点nv について距離情報distを更新してキューに追加する answer.at(nv) = min(answer.at(nv), dist[nv]); // 各黒点からの距離の最小値を必要に応じて更新 que.push(nv); } } ll rock = 0; // rockは各白点の(各黒点からの距離の最小値の)最大値を保持(随時更新) for (ll i = 0; i < H * W; i++) { if (vec.at(i) == '.') { rock = max(answer.at(i), rock); } } cout << rock << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll H, W; cin >> H >> W; vector<char> vec(H * W); // vecは各頂点の白か黒かを保持 vector<ll> answer(H * W + 1, H + W); // 各黒点からの距離のうち小さいものを保持(随時更新) for (ll i = 0; i < H * W; i++) { cin >> vec.at(i); } using Graph = vector<vector<ll>>; Graph G(H * W + 1); // グラフのサイズはマス目のサイズ+最初の仮想の点分の1 for (ll i = 0; i < H * W; ++i) { // グラフの作成 if (vec.at(i) == '#') { // 仮想の点からの辺を定義 G[H * W].push_back(i); } if (i % W != W - 1) { // 左右方向の辺の定義 G[i].push_back(i + 1); G[i + 1].push_back(i); } if (i < W * (H - 1)) { // 上下方向の辺の定義 G[i].push_back(i + W); G[i + W].push_back(i); } } vector<ll> dist(H * W + 1, -2); // 全頂点を「未訪問」に初期化(-2だったら未訪問と判断) queue<ll> que; dist[H * W] = -1; // 仮想の点をスタートにする que.push(H * W); while (!que.empty()) { ll v = que.front(); // キューから先頭頂点を取り出す que.pop(); for (ll nv : G[v]) { // vから辿れる頂点をすべて調べる if (dist[nv] != -2) { continue; } // すでに発見済みの頂点は探索しない dist[nv] = dist[v] + 1; // 新たな白色頂点nv について距離情報distを更新してキューに追加する answer.at(nv) = min(answer.at(nv), dist[nv]); // 各黒点からの距離の最小値を必要に応じて更新 que.push(nv); } } ll rock = 0; // rockは各白点の(各黒点からの距離の最小値の)最大値を保持(随時更新) for (ll i = 0; i < H * W; i++) { if (vec.at(i) == '.') { rock = max(answer.at(i), rock); } } cout << rock << endl; }
replace
12
13
12
13
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using p_int = pair<int, int>; int H, W; // char field[1000][1000]; char field[6][6]; p_int direction[4] = {make_pair(1, 0), make_pair(0, 1), make_pair(-1, 0), make_pair(0, -1)}; vector<p_int> blacks; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cin >> H >> W; for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { cin >> field[y][x]; if (field[y][x] == '#') { blacks.emplace_back(y, x); } } } int total = 0; while (true) { bool reverse = false; vector<p_int> next_blacks; for (auto b : blacks) { int y = b.first; int x = b.second; for (auto d : direction) { int ny = y + d.first; int nx = x + d.second; if (0 <= ny && ny < H && 0 <= nx && nx < W && field[ny][nx] == '.') { next_blacks.emplace_back(ny, nx); field[ny][nx] = '#'; reverse = true; } } } // for (int y = 0; y < H; ++y) { // for (int x = 0; x < W; ++x) { // if (field[y][x] == '#') { // for (auto d: direction) { // int ny = y + d.first; // int nx = x + d.second; // if (0 <= ny && ny < H && 0 <= nx && nx < W && // field[ny][nx] == '.') { // field[ny][nx] = 'x'; // reverse = true; // } // } // } // } // } if (!reverse) { break; } blacks.clear(); for (auto b : next_blacks) { blacks.push_back(b); } // for (int i = 0; i < H; ++i) { // for (int j = 0; j < W; ++j) { // if (field[i][j] == 'x') { // field[i][j] = '#'; // } // } // } total++; } cout << total << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using p_int = pair<int, int>; int H, W; char field[1000][1000]; p_int direction[4] = {make_pair(1, 0), make_pair(0, 1), make_pair(-1, 0), make_pair(0, -1)}; vector<p_int> blacks; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cin >> H >> W; for (int y = 0; y < H; ++y) { for (int x = 0; x < W; ++x) { cin >> field[y][x]; if (field[y][x] == '#') { blacks.emplace_back(y, x); } } } int total = 0; while (true) { bool reverse = false; vector<p_int> next_blacks; for (auto b : blacks) { int y = b.first; int x = b.second; for (auto d : direction) { int ny = y + d.first; int nx = x + d.second; if (0 <= ny && ny < H && 0 <= nx && nx < W && field[ny][nx] == '.') { next_blacks.emplace_back(ny, nx); field[ny][nx] = '#'; reverse = true; } } } // for (int y = 0; y < H; ++y) { // for (int x = 0; x < W; ++x) { // if (field[y][x] == '#') { // for (auto d: direction) { // int ny = y + d.first; // int nx = x + d.second; // if (0 <= ny && ny < H && 0 <= nx && nx < W && // field[ny][nx] == '.') { // field[ny][nx] = 'x'; // reverse = true; // } // } // } // } // } if (!reverse) { break; } blacks.clear(); for (auto b : next_blacks) { blacks.push_back(b); } // for (int i = 0; i < H; ++i) { // for (int j = 0; j < W; ++j) { // if (field[i][j] == 'x') { // field[i][j] = '#'; // } // } // } total++; } cout << total << '\n'; return 0; }
replace
5
7
5
6
0
p03053
C++
Runtime Error
#include <iostream> #include <queue> #define rep(i, n) for (int i = 0; i < n; i++) #define move(i) for (int i = -1; i <= 1; i += 2) #define WHITE 0 #define GRAY 1 #define BLACK 2 #define INF 1e9 using namespace std; using P = pair<int, int>; int h, w, M[50][50], C[50][50], D[50][50]; queue<P> Q; int main() { cin >> h >> w; rep(i, h) { rep(j, w) { char s; cin >> s; if (s == '.') { M[i][j] = 0; C[i][j] = WHITE; D[i][j] = INF; } else { M[i][j] = 1; C[i][j] = GRAY; D[i][j] = 0; Q.push(make_pair(i, j)); } } } // BFS int last_d = 0; while (!Q.empty()) { P p = Q.front(); Q.pop(); // cout <<"~("<< p.first << "," << p.second << ")~"<<endl; move(x) { int i = p.first + x, j = p.second; // cout <<"("<< i << "," << j << ")"<<endl; if ((i >= 0) && (i < h) && (j >= 0) && (j < w)) { // cout << M[i][j] << "," << C[i][j] << endl; if (M[i][j] == 0 && C[i][j] == WHITE) { C[i][j] = GRAY; last_d = D[i][j] = D[p.first][p.second] + 1; Q.push(make_pair(i, j)); } } } move(y) { int i = p.first, j = p.second + y; // cout <<"("<< i << "," << j << ")"<<endl; if ((i >= 0) && (i < h) && (j >= 0) && (j < w)) { // cout << M[i][j] << "," << C[i][j] << endl; if (M[i][j] == 0 && C[i][j] == WHITE) { C[i][j] = GRAY; last_d = D[i][j] = D[p.first][p.second] + 1; Q.push(make_pair(i, j)); } } } } cout << last_d << endl; return 0; }
#include <iostream> #include <queue> #define rep(i, n) for (int i = 0; i < n; i++) #define move(i) for (int i = -1; i <= 1; i += 2) #define WHITE 0 #define GRAY 1 #define BLACK 2 #define INF 1e9 using namespace std; using P = pair<int, int>; int h, w, M[1010][1010], C[1010][1010], D[1010][1010]; queue<P> Q; int main() { cin >> h >> w; rep(i, h) { rep(j, w) { char s; cin >> s; if (s == '.') { M[i][j] = 0; C[i][j] = WHITE; D[i][j] = INF; } else { M[i][j] = 1; C[i][j] = GRAY; D[i][j] = 0; Q.push(make_pair(i, j)); } } } // BFS int last_d = 0; while (!Q.empty()) { P p = Q.front(); Q.pop(); // cout <<"~("<< p.first << "," << p.second << ")~"<<endl; move(x) { int i = p.first + x, j = p.second; // cout <<"("<< i << "," << j << ")"<<endl; if ((i >= 0) && (i < h) && (j >= 0) && (j < w)) { // cout << M[i][j] << "," << C[i][j] << endl; if (M[i][j] == 0 && C[i][j] == WHITE) { C[i][j] = GRAY; last_d = D[i][j] = D[p.first][p.second] + 1; Q.push(make_pair(i, j)); } } } move(y) { int i = p.first, j = p.second + y; // cout <<"("<< i << "," << j << ")"<<endl; if ((i >= 0) && (i < h) && (j >= 0) && (j < w)) { // cout << M[i][j] << "," << C[i][j] << endl; if (M[i][j] == 0 && C[i][j] == WHITE) { C[i][j] = GRAY; last_d = D[i][j] = D[p.first][p.second] + 1; Q.push(make_pair(i, j)); } } } } cout << last_d << endl; return 0; }
replace
13
14
13
14
0
p03053
C++
Runtime Error
/* #include */ #include "bits/stdc++.h" #include <sstream> /* 名前空間 */ using namespace std; /* #define */ // リピート #define rpt0(i, n) for (ll i = 0; i < (n); i++) // リピート 0からn - 1まで #define rpt1(i, n) for (ll i = 1; i <= (n); i++) // リピート 1からnまで #define rptb0(i, n) \ for (ll i = (n)-1; i >= 0; --i) // バックするリピート n - 1から0まで #define rptb1(i, n) \ for (ll i = (n); i >= 1; --i) // バックするリピート nから1まで // 実用省略 #define pb push_back #define all(v) v.begin(), v.end() // 数学 #define PI (acos(-1)) // 円周率pi /* typedef */ // long系 typedef long long ll; typedef long double ld; // vector typedef vector<int> vint; // intのvector typedef vector<double> vdouble; // doubleのvector typedef vector<ll> vll; // llのvector typedef vector<ld> vld; // ldのvector typedef vector<pair<ll, ll>> vl_l; // l_lのvector typedef vector<pair<int, int>> vi_i; // i_iのvector typedef vector<pair<double, double>> vd_d; // d_dのvector // pair系 typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; typedef pair<double, double> d_d; /* 入出力 read, write */ /* 変数入力 */ // 1変数 template <typename T> void read(T &x) { cin >> x; } // 2変数 template <typename T, typename T0> void read(T &x, T0 &y) { cin >> x >> y; } // 3変数 template <typename T, typename T0, typename T1> void read(T &x, T0 &y, T1 &z) { cin >> x >> y >> z; } // 4変数 template <typename T, typename T0, typename T1, typename T2> void read(T &x, T0 &y, T1 &z, T2 &w) { cin >> x >> y >> z >> w; } /* vector, 配列, pair入力 */ // vector template <typename T> void read(vector<T> &oneD, int n) { ll n1 = (ll)n; rpt0(i, n1) { ll x; read(x); oneD.push_back(x); } } template <typename T> void read(vector<T> &oneD, ll n) { rpt0(i, n) { ll x; read(x); oneD.push_back(x); } } // 配列 template <typename T> void read(T oneD[], int n) { ll n1 = (ll)n; rpt1(i, n1) { read(oneD[i]); } } template <typename T> void read(T oneD[], ll n) { rpt1(i, n) { read(oneD[i]); } } // pair template <typename T, typename T0> void read(pair<T, T0> &p) { cin >> p.first >> p.second; } /* 変数出力 */ // 1変数 template <typename T> void write(T &x) { cout << x << endl; } // 2変数 template <typename T, typename T0> void write(T &x, T0 &y) { cout << x << " " << y << "\n"; } // 3変数 template <typename T, typename T0, typename T1> void write(T &x, T0 &y, T1 &z) { cout << x << " " << y << " " << z << "\n"; } // 4変数 template <typename T, typename T0, typename T1, typename T2> void write(T &x, T0 &y, T1 &z, T2 &w) { cout << x << " " << y << " " << z << " " << w << "\n"; } /* vector, 配列, pair出力 */ // vector template <typename T> void write(vector<T> &oneD, int n) { ll n1 = (ll)n; rpt0(i, n1) { cout << oneD[i] << " "; } cout << endl; } template <typename T> void write(vector<T> &oneD, ll n) { rpt0(i, n) { cout << oneD[i] << " "; } cout << endl; } // 配列 template <typename T> void write(T oneD[], int n) { ll n1 = (ll)n; rpt1(i, n1) { write(oneD[i]); } cout << endl; } template <typename T> void write(T oneD[], ll n) { rpt1(i, n) { write(oneD[i]); } cout << endl; } // pair template <typename T, typename T0> void write(pair<T, T0> &p) { cout << p.first << " " << p.second << " "; cout << endl; } // 八方向への移動ベクトル const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; // 右、上、左、下、右上、右下、左上、左下 // 入力 int fh, fw; // フィールドのfh->縦、fw->横 vector<string> field; // フィールド int main(void) { // 入力受け取り read(fh, fw); field.resize(fh); for (int h = 0; h < fh; ++h) cin >> field[h]; // 探索開始 // BFS のためのデータ構造 int dist[50][50]; memset(dist, -1, sizeof(dist)); // dist 配列全体を -1 に初期化 queue<i_i> que; // 訪問保留中の頂点集合 // 初期条件 (頂点 0 を初期ノードとする) rpt0(h, fh) { rpt0(w, fw) { if (field[h][w] == '#') { dist[h][w] = 0; i_i sp(h, w); // スタートの座標をキューにセット que.push(sp); // スタートを保留にする } } } // BFS 開始 (キューが空になるまで探索を行う) while (!que.empty()) { i_i v = que.front(); // キューから先頭頂点を取り出す que.pop(); // 保留にする // v から辿れる頂点をすべて調べる for (int dir = 0; dir < 4; ++dir) { // 方向選択 // dir = 0 -> 右、1 -> 上、2 -> 左、 3 -> 下 int nh = v.first + dx[dir]; int nw = v.second + dy[dir]; if (nh < 0 || nh >= fh || nw < 0 || nw >= fw) continue; // 場外アウトの場合 else if (field[nh][nw] == '#') continue; // 移動先が壁の場合 else if (dist[nh][nw] != -1) continue; // 移動先が探索済みの場合 // 新たな探索していない頂点 nv について距離情報を更新してキューに追加する dist[nh][nw] = dist[v.first][v.second] + 1; i_i np(nh, nw); que.push(np); } } // 結果出力 (各頂点の頂点 0 からの距離を見る) int max = 0; for (int h = 0; h < fh; h++) { for (int w = 0; w < fw; w++) { if (max < dist[h][w]) { max = dist[h][w]; } } } write(max); return 0; }
/* #include */ #include "bits/stdc++.h" #include <sstream> /* 名前空間 */ using namespace std; /* #define */ // リピート #define rpt0(i, n) for (ll i = 0; i < (n); i++) // リピート 0からn - 1まで #define rpt1(i, n) for (ll i = 1; i <= (n); i++) // リピート 1からnまで #define rptb0(i, n) \ for (ll i = (n)-1; i >= 0; --i) // バックするリピート n - 1から0まで #define rptb1(i, n) \ for (ll i = (n); i >= 1; --i) // バックするリピート nから1まで // 実用省略 #define pb push_back #define all(v) v.begin(), v.end() // 数学 #define PI (acos(-1)) // 円周率pi /* typedef */ // long系 typedef long long ll; typedef long double ld; // vector typedef vector<int> vint; // intのvector typedef vector<double> vdouble; // doubleのvector typedef vector<ll> vll; // llのvector typedef vector<ld> vld; // ldのvector typedef vector<pair<ll, ll>> vl_l; // l_lのvector typedef vector<pair<int, int>> vi_i; // i_iのvector typedef vector<pair<double, double>> vd_d; // d_dのvector // pair系 typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; typedef pair<double, double> d_d; /* 入出力 read, write */ /* 変数入力 */ // 1変数 template <typename T> void read(T &x) { cin >> x; } // 2変数 template <typename T, typename T0> void read(T &x, T0 &y) { cin >> x >> y; } // 3変数 template <typename T, typename T0, typename T1> void read(T &x, T0 &y, T1 &z) { cin >> x >> y >> z; } // 4変数 template <typename T, typename T0, typename T1, typename T2> void read(T &x, T0 &y, T1 &z, T2 &w) { cin >> x >> y >> z >> w; } /* vector, 配列, pair入力 */ // vector template <typename T> void read(vector<T> &oneD, int n) { ll n1 = (ll)n; rpt0(i, n1) { ll x; read(x); oneD.push_back(x); } } template <typename T> void read(vector<T> &oneD, ll n) { rpt0(i, n) { ll x; read(x); oneD.push_back(x); } } // 配列 template <typename T> void read(T oneD[], int n) { ll n1 = (ll)n; rpt1(i, n1) { read(oneD[i]); } } template <typename T> void read(T oneD[], ll n) { rpt1(i, n) { read(oneD[i]); } } // pair template <typename T, typename T0> void read(pair<T, T0> &p) { cin >> p.first >> p.second; } /* 変数出力 */ // 1変数 template <typename T> void write(T &x) { cout << x << endl; } // 2変数 template <typename T, typename T0> void write(T &x, T0 &y) { cout << x << " " << y << "\n"; } // 3変数 template <typename T, typename T0, typename T1> void write(T &x, T0 &y, T1 &z) { cout << x << " " << y << " " << z << "\n"; } // 4変数 template <typename T, typename T0, typename T1, typename T2> void write(T &x, T0 &y, T1 &z, T2 &w) { cout << x << " " << y << " " << z << " " << w << "\n"; } /* vector, 配列, pair出力 */ // vector template <typename T> void write(vector<T> &oneD, int n) { ll n1 = (ll)n; rpt0(i, n1) { cout << oneD[i] << " "; } cout << endl; } template <typename T> void write(vector<T> &oneD, ll n) { rpt0(i, n) { cout << oneD[i] << " "; } cout << endl; } // 配列 template <typename T> void write(T oneD[], int n) { ll n1 = (ll)n; rpt1(i, n1) { write(oneD[i]); } cout << endl; } template <typename T> void write(T oneD[], ll n) { rpt1(i, n) { write(oneD[i]); } cout << endl; } // pair template <typename T, typename T0> void write(pair<T, T0> &p) { cout << p.first << " " << p.second << " "; cout << endl; } // 八方向への移動ベクトル const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; // 右、上、左、下、右上、右下、左上、左下 // 入力 int fh, fw; // フィールドのfh->縦、fw->横 vector<string> field; // フィールド int main(void) { // 入力受け取り read(fh, fw); field.resize(fh); for (int h = 0; h < fh; ++h) cin >> field[h]; // 探索開始 // BFS のためのデータ構造 int dist[1000][1000]; memset(dist, -1, sizeof(dist)); // dist 配列全体を -1 に初期化 queue<i_i> que; // 訪問保留中の頂点集合 // 初期条件 (頂点 0 を初期ノードとする) rpt0(h, fh) { rpt0(w, fw) { if (field[h][w] == '#') { dist[h][w] = 0; i_i sp(h, w); // スタートの座標をキューにセット que.push(sp); // スタートを保留にする } } } // BFS 開始 (キューが空になるまで探索を行う) while (!que.empty()) { i_i v = que.front(); // キューから先頭頂点を取り出す que.pop(); // 保留にする // v から辿れる頂点をすべて調べる for (int dir = 0; dir < 4; ++dir) { // 方向選択 // dir = 0 -> 右、1 -> 上、2 -> 左、 3 -> 下 int nh = v.first + dx[dir]; int nw = v.second + dy[dir]; if (nh < 0 || nh >= fh || nw < 0 || nw >= fw) continue; // 場外アウトの場合 else if (field[nh][nw] == '#') continue; // 移動先が壁の場合 else if (dist[nh][nw] != -1) continue; // 移動先が探索済みの場合 // 新たな探索していない頂点 nv について距離情報を更新してキューに追加する dist[nh][nw] = dist[v.first][v.second] + 1; i_i np(nh, nw); que.push(np); } } // 結果出力 (各頂点の頂点 0 からの距離を見る) int max = 0; for (int h = 0; h < fh; h++) { for (int w = 0; w < fw; w++) { if (max < dist[h][w]) { max = dist[h][w]; } } } write(max); return 0; }
replace
145
146
145
146
0
p03053
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define int long long int const int INF = 1001001001001001LL; const int MOD = 1000000007; signed main() { int h, w; cin >> h >> w; queue<pair<int, int>> q; vector<vector<int>> g(h, vector<int>(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { char in; cin >> in; if (in == '#') { g[i][j] = 1; q.push({i, j}); } else g[i][j] = 0; } } int di[4] = {0, 1, 0, -1}; int dj[4] = {1, 0, -1, 0}; int cnt = 0; while (1) { vector<pair<int, int>> nxt; if (q.empty()) { cout << cnt - 1 << endl; return 0; } while (!q.empty()) { int i = q.front().first; int j = q.front().second; cerr << i << " " << j << endl; q.pop(); for (int l = 0; l < 4; l++) { int ni = i + di[l]; int nj = j + dj[l]; if (ni < 0 || h <= ni || nj < 0 || w <= nj) continue; if (g[ni][nj] == 1) continue; g[ni][nj] = 1; nxt.push_back({ni, nj}); } } for (int i = 0; i < (int)nxt.size(); i++) q.push(nxt[i]); cnt++; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define int long long int const int INF = 1001001001001001LL; const int MOD = 1000000007; signed main() { int h, w; cin >> h >> w; queue<pair<int, int>> q; vector<vector<int>> g(h, vector<int>(w)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { char in; cin >> in; if (in == '#') { g[i][j] = 1; q.push({i, j}); } else g[i][j] = 0; } } int di[4] = {0, 1, 0, -1}; int dj[4] = {1, 0, -1, 0}; int cnt = 0; while (1) { vector<pair<int, int>> nxt; if (q.empty()) { cout << cnt - 1 << endl; return 0; } while (!q.empty()) { int i = q.front().first; int j = q.front().second; q.pop(); for (int l = 0; l < 4; l++) { int ni = i + di[l]; int nj = j + dj[l]; if (ni < 0 || h <= ni || nj < 0 || w <= nj) continue; if (g[ni][nj] == 1) continue; g[ni][nj] = 1; nxt.push_back({ni, nj}); } } for (int i = 0; i < (int)nxt.size(); i++) q.push(nxt[i]); cnt++; } return 0; }
delete
62
63
62
62
TLE
p03053
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using Edge = int; using Graph = vector<vector<Edge>>; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) const ll MOD = 1000000007; const ll nmax = 8; const ll INF = 1e9; bool graph[nmax][nmax]; struct SegmentTree { private: ll n; vector<ll> node; public: SegmentTree(vector<ll> v) { ll sz = v.size(); n = 1; while (n < sz) { n *= 2; } node.resize(2 * n - 1, INF); for (ll i = 0; i < sz; i++) { node[i + n - 1] = v[i]; } for (ll i = n - 2; i >= 0; i--) { node[i] = min(node[2 * i + 1], node[2 * i + 2]); } } void update(ll x, ll val) { x += (n - 1); node[x] = val; while (x > 0) { x = (x - 1) / 2; node[x] = min(node[2 * x + 1], node[2 * x + 2]); } } // findは半開区間で考える ll find(ll a, ll b, ll k = 0, ll l = 0, ll r = -1) { if (r < 0) r = n; if (r <= a || b <= l) return INF; if (a <= l && r <= b) return node[k]; ll vl = find(a, b, 2 * k + 1, l, (l + r) / 2); ll vr = find(a, b, 2 * k + 2, (l + r) / 2, r); return min(vl, vr); } }; class UnionFind { public: vector<ll> Parent; UnionFind(ll N) { Parent = vector<ll>(N, -1); } ll find(ll A) { if (Parent[A] < 0) return A; return Parent[A] = find(Parent[A]); } ll size(ll A) { return -Parent[find(A)]; } bool Union(ll A, ll B) { A = find(A); B = find(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } ll mulMod(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); } ll powMod(ll a, ll p) { if (p == 0) { return 1; } else if (p % 2 == 0) { ll half = powMod(a, p / 2); return mulMod(half, half); } else { return mulMod(powMod(a, p - 1), a); } } ll ceil(ll a, ll b) { return (a + b - 1) / b; } vector<ll> tsort(Graph G) { ll N = G.size(); vector<ll> in(N); for (auto &&edges : G) { for (auto &&edge : edges) { in[edge]++; } } queue<int> que; for (int i = 0; i < N; i++) { if (in[i] == 0) { que.push(i); } } int cnt = 0; vector<ll> ans; while (!que.empty()) { int v = que.front(); que.pop(); ans.push_back(v); for (auto &&next : G[v]) { in[next]--; if (in[next] == 0) { que.push(next); } } } return ans; } Graph G(100); void treeDFS(int from, int current, int dist, int &maxDist, int &maxVertex) { if (dist > maxDist) { maxDist = dist; maxVertex = current; } for (auto to : G[current]) { if (to == from) continue; treeDFS(current, to, dist + 1, maxDist, maxVertex); } } pair<int, int> getTreeDiameter() { int start = 0, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); start = end, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); return make_pair(start, end); } ll dx[4] = {-1, 0, 0, 1}; ll dy[4] = {0, -1, 1, 0}; void solve(long long H, long long W, std::vector<std::string> A) { ll dist[H][W]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { dist[i][j] = INF; } } queue<pair<ll, ll>> que; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (A[i][j] == '#') { que.push(make_pair(i, j)); dist[i][j] = 0; } } } while (!que.empty()) { pair<ll, ll> now = que.front(); que.pop(); for (int k = 0; k < 4; k++) { ll nx = now.first + dx[k]; ll ny = now.second + dy[k]; if (nx < 0 || H <= nx || ny < 0 || W <= ny) continue; if (dist[nx][ny] != INF || A[nx][ny] == '#') continue; dist[nx][ny] = min(dist[nx][ny], dist[now.first][now.second] + 1); que.push(make_pair(nx, ny)); } } ll ans = -1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ans = max(ans, dist[i][j]); } } cout << ans << endl; } int main() { long long H; scanf("%lld", &H); long long W; scanf("%lld", &W); std::vector<std::string> A(W); for (int i = 0; i < W; i++) { std::cin >> A[i]; } solve(H, W, std::move(A)); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using Edge = int; using Graph = vector<vector<Edge>>; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) const ll MOD = 1000000007; const ll nmax = 8; const ll INF = 1e9; bool graph[nmax][nmax]; struct SegmentTree { private: ll n; vector<ll> node; public: SegmentTree(vector<ll> v) { ll sz = v.size(); n = 1; while (n < sz) { n *= 2; } node.resize(2 * n - 1, INF); for (ll i = 0; i < sz; i++) { node[i + n - 1] = v[i]; } for (ll i = n - 2; i >= 0; i--) { node[i] = min(node[2 * i + 1], node[2 * i + 2]); } } void update(ll x, ll val) { x += (n - 1); node[x] = val; while (x > 0) { x = (x - 1) / 2; node[x] = min(node[2 * x + 1], node[2 * x + 2]); } } // findは半開区間で考える ll find(ll a, ll b, ll k = 0, ll l = 0, ll r = -1) { if (r < 0) r = n; if (r <= a || b <= l) return INF; if (a <= l && r <= b) return node[k]; ll vl = find(a, b, 2 * k + 1, l, (l + r) / 2); ll vr = find(a, b, 2 * k + 2, (l + r) / 2, r); return min(vl, vr); } }; class UnionFind { public: vector<ll> Parent; UnionFind(ll N) { Parent = vector<ll>(N, -1); } ll find(ll A) { if (Parent[A] < 0) return A; return Parent[A] = find(Parent[A]); } ll size(ll A) { return -Parent[find(A)]; } bool Union(ll A, ll B) { A = find(A); B = find(B); if (A == B) { return false; } if (size(A) < size(B)) swap(A, B); Parent[A] += Parent[B]; Parent[B] = A; return true; } }; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } ll mulMod(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); } ll powMod(ll a, ll p) { if (p == 0) { return 1; } else if (p % 2 == 0) { ll half = powMod(a, p / 2); return mulMod(half, half); } else { return mulMod(powMod(a, p - 1), a); } } ll ceil(ll a, ll b) { return (a + b - 1) / b; } vector<ll> tsort(Graph G) { ll N = G.size(); vector<ll> in(N); for (auto &&edges : G) { for (auto &&edge : edges) { in[edge]++; } } queue<int> que; for (int i = 0; i < N; i++) { if (in[i] == 0) { que.push(i); } } int cnt = 0; vector<ll> ans; while (!que.empty()) { int v = que.front(); que.pop(); ans.push_back(v); for (auto &&next : G[v]) { in[next]--; if (in[next] == 0) { que.push(next); } } } return ans; } Graph G(100); void treeDFS(int from, int current, int dist, int &maxDist, int &maxVertex) { if (dist > maxDist) { maxDist = dist; maxVertex = current; } for (auto to : G[current]) { if (to == from) continue; treeDFS(current, to, dist + 1, maxDist, maxVertex); } } pair<int, int> getTreeDiameter() { int start = 0, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); start = end, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); return make_pair(start, end); } ll dx[4] = {-1, 0, 0, 1}; ll dy[4] = {0, -1, 1, 0}; void solve(long long H, long long W, std::vector<std::string> A) { ll dist[H][W]; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { dist[i][j] = INF; } } queue<pair<ll, ll>> que; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (A[i][j] == '#') { que.push(make_pair(i, j)); dist[i][j] = 0; } } } while (!que.empty()) { pair<ll, ll> now = que.front(); que.pop(); for (int k = 0; k < 4; k++) { ll nx = now.first + dx[k]; ll ny = now.second + dy[k]; if (nx < 0 || H <= nx || ny < 0 || W <= ny) continue; if (dist[nx][ny] != INF || A[nx][ny] == '#') continue; dist[nx][ny] = min(dist[nx][ny], dist[now.first][now.second] + 1); que.push(make_pair(nx, ny)); } } ll ans = -1; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { ans = max(ans, dist[i][j]); } } cout << ans << endl; } int main() { long long H; scanf("%lld", &H); long long W; scanf("%lld", &W); std::vector<std::string> A(H); for (int i = 0; i < H; i++) { std::cin >> A[i]; } solve(H, W, std::move(A)); return 0; }
replace
221
223
221
223
0
p03053
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; int n, m, ans; char s[1111][1111]; int vis[1111][1111]; struct node { int x, y, step; } nex, now; queue<node> que; void bfs() { while (!que.empty()) { now = que.front(); que.pop(); ans = max(ans, now.step); for (int i = 0; i < 4; i++) { nex.x = now.x + dx[i]; nex.y = now.y + dy[i]; nex.step = now.step + 1; if (!vis[nex.x][nex.y]) { s[nex.x][nex.y] == '#'; vis[nex.x][nex.y] = 1; que.push(nex); } } } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> s[i][j]; now.x = i, now.y = j, now.step = 0; if (s[i][j] == '#') vis[i][j] = 1, que.push(now); } bfs(); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; int n, m, ans; char s[1111][1111]; int vis[1111][1111]; struct node { int x, y, step; } nex, now; queue<node> que; void bfs() { while (!que.empty()) { now = que.front(); que.pop(); ans = max(ans, now.step); for (int i = 0; i < 4; i++) { nex.x = now.x + dx[i]; nex.y = now.y + dy[i]; nex.step = now.step + 1; if (s[nex.x][nex.y] == '.' && !vis[nex.x][nex.y]) { s[nex.x][nex.y] == '#'; vis[nex.x][nex.y] = 1; que.push(nex); } } } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> s[i][j]; now.x = i, now.y = j, now.step = 0; if (s[i][j] == '#') vis[i][j] = 1, que.push(now); } bfs(); printf("%d\n", ans); return 0; }
replace
21
22
21
22
-11
p03053
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <string> #include <vector> #define CHMAX(a, b) a = std::max(a, b) #define CHMIN(a, b) a = std::min(a, b) #define CHABS(a) a = std::abs(a) #define COUT(a) std::cout << a << std::endl #define CERR(a) std::cerr << a << std::endl #define FOR(n) for (lli i = 0; i < n; i++) using namespace std; using lli = long long int; using pll = pair<lli, lli>; using tlll = tuple<lli, lli, lli>; lli mod197 = 1000000007LL; // ax + by = gcd(a,b) 最大公約数 template <typename T> T extgcd(T a, T b, T &x, T &y) { T d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } lli visited[1002][1002] = {}; int main(void) { lli H, W; cin >> H >> W; std::queue<tlll> q; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char c; cin >> c; if (c == '#') { q.push(tlll{i + 1, j + 1, 1}); } } } for (int i = 1; i <= H; i++) { visited[i][W + 1] = 1; visited[i][0] = 1; } for (int i = 1; i <= W; i++) { visited[0][i] = 1; visited[W + 1][i] = 1; } lli ans = 0; lli dir[] = {1, 0, -1, 0, 1}; while (!q.empty()) { tlll t = q.front(); q.pop(); if (visited[get<0>(t)][get<1>(t)] == 0) { visited[get<0>(t)][get<1>(t)] = get<2>(t); ans = get<2>(t); for (int i = 0; i < 4; i++) q.push(tlll{get<0>(t) + dir[i], get<1>(t) + dir[i + 1], get<2>(t) + 1}); } } COUT(ans - 1); return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <numeric> #include <queue> #include <string> #include <vector> #define CHMAX(a, b) a = std::max(a, b) #define CHMIN(a, b) a = std::min(a, b) #define CHABS(a) a = std::abs(a) #define COUT(a) std::cout << a << std::endl #define CERR(a) std::cerr << a << std::endl #define FOR(n) for (lli i = 0; i < n; i++) using namespace std; using lli = long long int; using pll = pair<lli, lli>; using tlll = tuple<lli, lli, lli>; lli mod197 = 1000000007LL; // ax + by = gcd(a,b) 最大公約数 template <typename T> T extgcd(T a, T b, T &x, T &y) { T d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } lli visited[1002][1002] = {}; int main(void) { lli H, W; cin >> H >> W; std::queue<tlll> q; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { char c; cin >> c; if (c == '#') { q.push(tlll{i + 1, j + 1, 1}); } } } for (int i = 1; i <= H; i++) { visited[i][W + 1] = 1; visited[i][0] = 1; } for (int i = 1; i <= W; i++) { visited[0][i] = 1; visited[H + 1][i] = 1; } lli ans = 0; lli dir[] = {1, 0, -1, 0, 1}; while (!q.empty()) { tlll t = q.front(); q.pop(); if (visited[get<0>(t)][get<1>(t)] == 0) { visited[get<0>(t)][get<1>(t)] = get<2>(t); ans = get<2>(t); for (int i = 0; i < 4; i++) q.push(tlll{get<0>(t) + dir[i], get<1>(t) + dir[i + 1], get<2>(t) + 1}); } } COUT(ans - 1); return 0; }
replace
61
62
61
62
0
p03054
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int h, w, n, sr, sc; char s1[N], s2[N]; bool judge(char opt) { int x = sr, y = sc; for (int i = 1; i <= n; i++) { if (s1[i] == opt) { if (opt == 'U') x--; else if (opt == 'D') x++; else if (opt == 'L') y--; else y++; } if (x < 1 || x > h || y < 1 || y > w) return true; if (opt == 'U') { if (s2[i] == 'D' && x + 1 <= h) x++; } else if (opt == 'D') { if (s2[i] == 'U' && x - 1 >= 1) x--; } else if (opt == 'L') { if (s2[i] == 'R' && y + 1 <= w) y++; } else { if (s2[i] == 'L' && y - 1 >= 1) y--; } } return false; } int main() { scanf("%d%d%d", &h, &w, &n); scanf("%d%d", &sr, &sc); scanf("%s%s", s1 + 1, s2 + 1); if (judge('U') || judge('D') || judge('L') || judge('R')) printf("NO\n"); else printf("YES\n"); }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int h, w, n, sr, sc; char s1[N], s2[N]; bool judge(char opt) { int x = sr, y = sc; for (int i = 1; i <= n; i++) { if (s1[i] == opt) { if (opt == 'U') x--; else if (opt == 'D') x++; else if (opt == 'L') y--; else y++; } if (x < 1 || x > h || y < 1 || y > w) return true; if (opt == 'U') { if (s2[i] == 'D' && x + 1 <= h) x++; } else if (opt == 'D') { if (s2[i] == 'U' && x - 1 >= 1) x--; } else if (opt == 'L') { if (s2[i] == 'R' && y + 1 <= w) y++; } else { if (s2[i] == 'L' && y - 1 >= 1) y--; } } return false; } int main() { scanf("%d%d%d", &h, &w, &n); scanf("%d%d", &sr, &sc); scanf("%s%s", s1 + 1, s2 + 1); if (judge('U') || judge('D') || judge('L') || judge('R')) printf("NO\n"); else printf("YES\n"); }
replace
2
3
2
3
0
p03054
C++
Runtime Error
// https://atcoder.jp/contests/agc033/tasks/agc033_b /*<head>*/ // #include "Template.cpp" /*</head>*/ /*<body>*/ /* #region header */ /** * @file template.cpp * @brief 競技プログラミング用テンプレート * @author btk15049 * @date 2019/05/02 */ #include <bits/stdc++.h> using namespace std; /* #region macro */ #ifdef BTK #define DEBUG if (1) #define CIN_ONLY if (0) #else #define DEBUG if (0) #define CIN_ONLY if (1) #endif #define ALL(v) (v).begin(), (v).end() #define REC(ret, ...) std::function<ret(__VA_ARGS__)> /* #endregion */ namespace _Template_ { struct cww { cww() { CIN_ONLY { ios::sync_with_stdio(false); cin.tie(0); } } } star; template <typename T> inline bool chmin(T &l, T r) { bool a = l > r; if (a) l = r; return a; } template <typename T> inline bool chmax(T &l, T r) { bool a = l < r; if (a) l = r; return a; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &it : v) is >> it; return is; } class reverse_range { private: struct I { int x; int operator*() { return x - 1; } bool operator!=(I &lhs) { return x > lhs.x; } void operator++() { --x; } }; I i, n; public: reverse_range(int n) : i({0}), n({n}) {} reverse_range(int i, int n) : i({i}), n({n}) {} I &begin() { return n; } I &end() { return i; } }; class range { private: struct I { int x; int operator*() { return x; } bool operator!=(I &lhs) { return x < lhs.x; } void operator++() { ++x; } }; I i, n; public: range(int n) : i({0}), n({n}) {} range(int i, int n) : i({i}), n({n}) {} I &begin() { return i; } I &end() { return n; } reverse_range operator!() { return reverse_range(*i, *n); } }; template <typename T> inline T &unused_var(T &v) { return v; } using LL = long long; } // namespace _Template_ using namespace _Template_; /* #endregion */ /*</body>*/ int R, C, N; int sr, sc; string S, T; int lb[212345][2]; int ub[212345][2]; bool solve(int width, int pos, char d, char u) { lb[N][1] = 0; ub[N][1] = width; for (int i : !range(N)) { lb[i][0] = lb[i + 1][1]; ub[i][0] = ub[i + 1][1]; if (T[i] == u) lb[i][0] = max(lb[i][0] - 1, 0); if (T[i] == d) ub[i][0] = min(ub[i][0] + 1, width); // cerr << i << "t" << lb << " " << ub << endl; lb[i][1] = lb[i][0]; ub[i][1] = ub[i][0]; if (S[i] == u) ub[i][1]--; if (S[i] == d) lb[i][1]++; // if (lb >= ub) return true; // cerr << i << "s" << lb << " " << ub << endl; } int ll = pos, rr = pos + 1; for (int i : range(N)) { chmin(rr, ub[i][0]); chmax(ll, lb[i][0]); if (ll >= rr) { cerr << i << endl; cerr << ll << " " << rr << endl; return true; } if (T[i] == u) rr++; if (T[i] == d) ll--; chmin(rr, ub[i][1]); chmax(ll, lb[i][1]); if (ll >= rr) { cerr << i << endl; cerr << ll << " " << rr << endl; return true; } // cerr << i << " " << ll << " " << rr << endl; } return false; // return !(lb <= pos || pos < ub); } int main() { cin >> R >> C >> N; cin >> sr >> sc; cin >> S >> T; if ((N % 2 == 0) && (N & (1 << 3)) > 0 && (N & (1 << 5)) == 0) return -1; sr--; sc--; if (solve(C, sc, 'L', 'R') || solve(R, sr, 'U', 'D')) { cout << "NO" << endl; } else { cout << "YES" << endl; } return 0; }
// https://atcoder.jp/contests/agc033/tasks/agc033_b /*<head>*/ // #include "Template.cpp" /*</head>*/ /*<body>*/ /* #region header */ /** * @file template.cpp * @brief 競技プログラミング用テンプレート * @author btk15049 * @date 2019/05/02 */ #include <bits/stdc++.h> using namespace std; /* #region macro */ #ifdef BTK #define DEBUG if (1) #define CIN_ONLY if (0) #else #define DEBUG if (0) #define CIN_ONLY if (1) #endif #define ALL(v) (v).begin(), (v).end() #define REC(ret, ...) std::function<ret(__VA_ARGS__)> /* #endregion */ namespace _Template_ { struct cww { cww() { CIN_ONLY { ios::sync_with_stdio(false); cin.tie(0); } } } star; template <typename T> inline bool chmin(T &l, T r) { bool a = l > r; if (a) l = r; return a; } template <typename T> inline bool chmax(T &l, T r) { bool a = l < r; if (a) l = r; return a; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &it : v) is >> it; return is; } class reverse_range { private: struct I { int x; int operator*() { return x - 1; } bool operator!=(I &lhs) { return x > lhs.x; } void operator++() { --x; } }; I i, n; public: reverse_range(int n) : i({0}), n({n}) {} reverse_range(int i, int n) : i({i}), n({n}) {} I &begin() { return n; } I &end() { return i; } }; class range { private: struct I { int x; int operator*() { return x; } bool operator!=(I &lhs) { return x < lhs.x; } void operator++() { ++x; } }; I i, n; public: range(int n) : i({0}), n({n}) {} range(int i, int n) : i({i}), n({n}) {} I &begin() { return i; } I &end() { return n; } reverse_range operator!() { return reverse_range(*i, *n); } }; template <typename T> inline T &unused_var(T &v) { return v; } using LL = long long; } // namespace _Template_ using namespace _Template_; /* #endregion */ /*</body>*/ int R, C, N; int sr, sc; string S, T; int lb[212345][2]; int ub[212345][2]; bool solve(int width, int pos, char d, char u) { lb[N][1] = 0; ub[N][1] = width; for (int i : !range(N)) { lb[i][0] = lb[i + 1][1]; ub[i][0] = ub[i + 1][1]; if (T[i] == u) lb[i][0] = max(lb[i][0] - 1, 0); if (T[i] == d) ub[i][0] = min(ub[i][0] + 1, width); // cerr << i << "t" << lb << " " << ub << endl; lb[i][1] = lb[i][0]; ub[i][1] = ub[i][0]; if (S[i] == u) ub[i][1]--; if (S[i] == d) lb[i][1]++; // if (lb >= ub) return true; // cerr << i << "s" << lb << " " << ub << endl; } int ll = pos, rr = pos + 1; for (int i : range(N)) { chmin(rr, ub[i][0]); chmax(ll, lb[i][0]); if (ll >= rr) { cerr << i << endl; cerr << ll << " " << rr << endl; return true; } if (T[i] == u) rr++; if (T[i] == d) ll--; chmin(rr, ub[i][1]); chmax(ll, lb[i][1]); if (ll >= rr) { cerr << i << endl; cerr << ll << " " << rr << endl; return true; } // cerr << i << " " << ll << " " << rr << endl; } return false; // return !(lb <= pos || pos < ub); } int main() { cin >> R >> C >> N; cin >> sr >> sc; cin >> S >> T; if ((N % 2 == 0) && (N & (1 << 3)) > 0 && (N & (1 << 5)) == 0 && (N & (1 << 2)) > 0 && ((N & (1 << 4)) == 0)) { cout << "NO" << endl; return 0; } sr--; sc--; if (solve(C, sc, 'L', 'R') || solve(R, sr, 'U', 'D')) { cout << "NO" << endl; } else { cout << "YES" << endl; } return 0; }
replace
152
154
152
157
0
p03054
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; typedef long long LL; const int N = 2e5 + 100; char a[N], b[N], c[N]; int h_max[N], h_min[N], v_max[N], v_min[N]; int h_max_suf[N], h_min_suf[N], v_max_suf[N], v_min_suf[N]; int main() { int n, m, Q; cin >> n >> m >> Q; int x, y; cin >> x >> y; Q *= 2; scanf("%s", c + 1); scanf("%s", b + 1); for (int i = 1; i <= Q; i++) { if (i % 2 == 0) a[i] = b[(i + 1) / 2]; else a[i] = c[(i + 1) / 2]; } h_max[0] = h_min[0] = y; v_max[0] = v_min[0] = x; for (int i = 1; i <= Q; i++) { h_max[i] = h_max[i - 1], h_min[i] = h_min[i - 1]; v_max[i] = v_max[i - 1], v_min[i] = v_min[i - 1]; if (i % 2 == 0) { if (a[i] == 'R') h_min[i]++; if (a[i] == 'L') h_max[i]--; if (a[i] == 'U') v_max[i]--; if (a[i] == 'D') v_min[i]++; } if (i % 2 == 1) { if (a[i] == 'R') h_max[i]++; if (a[i] == 'L') h_min[i]--; if (a[i] == 'D') v_max[i]++; if (a[i] == 'U') v_min[i]--; } } v_max[Q + 1] = h_max[Q + 1] = -0x3f3f3f3f; v_min[Q + 1] = h_min[Q + 1] = 0x3f3f3f3f; for (int i = Q; i >= 1; i--) { h_max[i] = max(h_max[i], h_max[i + 1]); h_min[i] = min(h_min[i], h_min[i + 1]); v_max[i] = max(v_max[i], v_max[i + 1]); v_min[i] = min(v_min[i], v_min[i + 1]); h_max_suf[i] = h_max[i] - h_max[i - 1]; h_min_suf[i] = h_min[i] - h_min[i - 1]; v_max_suf[i] = v_max[i] - v_max[i - 1]; v_min_suf[i] = v_min[i] - v_min[i - 1]; } bool flag = true; for (int i = 1; i <= Q; i++) { h_max[i] = h_max[i - 1], h_min[i] = h_min[i - 1]; v_max[i] = v_max[i - 1], v_min[i] = v_min[i - 1]; if (i % 2 == 0) { if (a[i] == 'R') h_min[i]++; if (a[i] == 'L') h_max[i]--; if (a[i] == 'U') v_max[i]--; if (a[i] == 'D') v_min[i]++; } if (i % 2 == 1) { if (a[i] == 'R') h_max[i]++; if (a[i] == 'L') h_min[i]--; if (a[i] == 'D') v_max[i]++; if (a[i] == 'U') v_min[i]--; } if (i != Q) { h_max[i] = max(h_max[i], 1 - h_min_suf[i + 1]); h_min[i] = min(h_min[i], m - h_max_suf[i + 1]); v_max[i] = max(v_max[i], 1 - v_min_suf[i + 1]); v_min[i] = min(v_min[i], n - v_max_suf[i + 1]); } if (h_max[i] > m || h_min[i] < 1 || v_max[i] > n || v_min[i] < 1) flag = false; } if (flag) puts("YES"); else puts("NO"); }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std; typedef long long LL; const int N = 4e5 + 100; char a[N], b[N], c[N]; int h_max[N], h_min[N], v_max[N], v_min[N]; int h_max_suf[N], h_min_suf[N], v_max_suf[N], v_min_suf[N]; int main() { int n, m, Q; cin >> n >> m >> Q; int x, y; cin >> x >> y; Q *= 2; scanf("%s", c + 1); scanf("%s", b + 1); for (int i = 1; i <= Q; i++) { if (i % 2 == 0) a[i] = b[(i + 1) / 2]; else a[i] = c[(i + 1) / 2]; } h_max[0] = h_min[0] = y; v_max[0] = v_min[0] = x; for (int i = 1; i <= Q; i++) { h_max[i] = h_max[i - 1], h_min[i] = h_min[i - 1]; v_max[i] = v_max[i - 1], v_min[i] = v_min[i - 1]; if (i % 2 == 0) { if (a[i] == 'R') h_min[i]++; if (a[i] == 'L') h_max[i]--; if (a[i] == 'U') v_max[i]--; if (a[i] == 'D') v_min[i]++; } if (i % 2 == 1) { if (a[i] == 'R') h_max[i]++; if (a[i] == 'L') h_min[i]--; if (a[i] == 'D') v_max[i]++; if (a[i] == 'U') v_min[i]--; } } v_max[Q + 1] = h_max[Q + 1] = -0x3f3f3f3f; v_min[Q + 1] = h_min[Q + 1] = 0x3f3f3f3f; for (int i = Q; i >= 1; i--) { h_max[i] = max(h_max[i], h_max[i + 1]); h_min[i] = min(h_min[i], h_min[i + 1]); v_max[i] = max(v_max[i], v_max[i + 1]); v_min[i] = min(v_min[i], v_min[i + 1]); h_max_suf[i] = h_max[i] - h_max[i - 1]; h_min_suf[i] = h_min[i] - h_min[i - 1]; v_max_suf[i] = v_max[i] - v_max[i - 1]; v_min_suf[i] = v_min[i] - v_min[i - 1]; } bool flag = true; for (int i = 1; i <= Q; i++) { h_max[i] = h_max[i - 1], h_min[i] = h_min[i - 1]; v_max[i] = v_max[i - 1], v_min[i] = v_min[i - 1]; if (i % 2 == 0) { if (a[i] == 'R') h_min[i]++; if (a[i] == 'L') h_max[i]--; if (a[i] == 'U') v_max[i]--; if (a[i] == 'D') v_min[i]++; } if (i % 2 == 1) { if (a[i] == 'R') h_max[i]++; if (a[i] == 'L') h_min[i]--; if (a[i] == 'D') v_max[i]++; if (a[i] == 'U') v_min[i]--; } if (i != Q) { h_max[i] = max(h_max[i], 1 - h_min_suf[i + 1]); h_min[i] = min(h_min[i], m - h_max_suf[i + 1]); v_max[i] = max(v_max[i], 1 - v_min_suf[i + 1]); v_min[i] = min(v_min[i], n - v_max_suf[i + 1]); } if (h_max[i] > m || h_min[i] < 1 || v_max[i] > n || v_min[i] < 1) flag = false; } if (flag) puts("YES"); else puts("NO"); }
replace
9
10
9
10
0
p03054
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef pair<P, int> T; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define all(x) (x).begin(), (x).end() #define sq(x) ((x) * (x)) #define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++) #define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++) #define EQ(a, b) (abs((a) - (b)) < eps) template <class T> void chmin(T &a, const T &b) { if (a > b) a = b; } template <class T> void chmax(T &a, const T &b) { if (a < b) a = b; } int H, W, N; int sx, sy; string S[2]; int L[2][100100]; int R[2][100100]; int U[2][100100]; int D[2][100100]; int cntL(int id, int a, int b) { //[a,b] if (a == 0) return L[id][b]; return L[id][b] - L[id][a - 1]; } int cntR(int id, int a, int b) { //[a,b] if (a == 0) return R[id][b]; return R[id][b] - R[id][a - 1]; } int cntU(int id, int a, int b) { //[a,b] if (a == 0) return U[id][b]; return U[id][b] - U[id][a - 1]; } int cntD(int id, int a, int b) { //[a,b] if (a == 0) return D[id][b]; return D[id][b] - D[id][a - 1]; } int main() { cin >> H >> W >> N; cin >> sx >> sy; sx--; sy--; cin >> S[0]; cin >> S[1]; for (int i = 0; i < 2; i++) { for (int j = 0; j < N; j++) { if (S[i][j] == 'L') { L[i][j]++; } if (S[i][j] == 'R') { R[i][j]++; } if (S[i][j] == 'U') { U[i][j]++; } if (S[i][j] == 'D') { D[i][j]++; } } for (int j = 1; j < N; j++) { L[i][j] += L[i][j - 1]; R[i][j] += R[i][j - 1]; U[i][j] += U[i][j - 1]; D[i][j] += D[i][j - 1]; } } int cx, cy; cx = sx; cy = sy; for (int i = 0; i < N; i++) { if (S[0][i] == 'R') cy++; if (cy >= W) { cout << "NO" << endl; return 0; } if (S[1][i] == 'L' && cy > 0) cy--; } cx = sx; cy = sy; for (int i = 0; i < N; i++) { if (S[0][i] == 'L') cy--; if (cy < 0) { cout << "NO" << endl; return 0; } if (S[1][i] == 'R' && cy < W - 1) cy++; } cx = sx; cy = sy; for (int i = 0; i < N; i++) { if (S[0][i] == 'U') cx--; if (cx < 0) { cout << "NO" << endl; return 0; } if (S[1][i] == 'D' && cx < H - 1) cx++; } cx = sx; cy = sy; for (int i = 0; i < N; i++) { if (S[0][i] == 'D') cx++; if (cx >= H) { cout << "NO" << endl; return 0; } if (S[1][i] == 'U' && cx > 0) cx--; } cout << "YES" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; typedef pair<P, int> T; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define all(x) (x).begin(), (x).end() #define sq(x) ((x) * (x)) #define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++) #define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++) #define EQ(a, b) (abs((a) - (b)) < eps) template <class T> void chmin(T &a, const T &b) { if (a > b) a = b; } template <class T> void chmax(T &a, const T &b) { if (a < b) a = b; } int H, W, N; int sx, sy; string S[2]; int L[2][200100]; int R[2][200100]; int U[2][200100]; int D[2][200100]; int cntL(int id, int a, int b) { //[a,b] if (a == 0) return L[id][b]; return L[id][b] - L[id][a - 1]; } int cntR(int id, int a, int b) { //[a,b] if (a == 0) return R[id][b]; return R[id][b] - R[id][a - 1]; } int cntU(int id, int a, int b) { //[a,b] if (a == 0) return U[id][b]; return U[id][b] - U[id][a - 1]; } int cntD(int id, int a, int b) { //[a,b] if (a == 0) return D[id][b]; return D[id][b] - D[id][a - 1]; } int main() { cin >> H >> W >> N; cin >> sx >> sy; sx--; sy--; cin >> S[0]; cin >> S[1]; for (int i = 0; i < 2; i++) { for (int j = 0; j < N; j++) { if (S[i][j] == 'L') { L[i][j]++; } if (S[i][j] == 'R') { R[i][j]++; } if (S[i][j] == 'U') { U[i][j]++; } if (S[i][j] == 'D') { D[i][j]++; } } for (int j = 1; j < N; j++) { L[i][j] += L[i][j - 1]; R[i][j] += R[i][j - 1]; U[i][j] += U[i][j - 1]; D[i][j] += D[i][j - 1]; } } int cx, cy; cx = sx; cy = sy; for (int i = 0; i < N; i++) { if (S[0][i] == 'R') cy++; if (cy >= W) { cout << "NO" << endl; return 0; } if (S[1][i] == 'L' && cy > 0) cy--; } cx = sx; cy = sy; for (int i = 0; i < N; i++) { if (S[0][i] == 'L') cy--; if (cy < 0) { cout << "NO" << endl; return 0; } if (S[1][i] == 'R' && cy < W - 1) cy++; } cx = sx; cy = sy; for (int i = 0; i < N; i++) { if (S[0][i] == 'U') cx--; if (cx < 0) { cout << "NO" << endl; return 0; } if (S[1][i] == 'D' && cx < H - 1) cx++; } cx = sx; cy = sy; for (int i = 0; i < N; i++) { if (S[0][i] == 'D') cx++; if (cx >= H) { cout << "NO" << endl; return 0; } if (S[1][i] == 'U' && cx > 0) cx--; } cout << "YES" << endl; return 0; }
replace
30
34
30
34
0
p03054
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int mx = 200005; int h, w, n, x, y, r, l, u, d; char s[mx], t[mx]; int main() { scanf("%d%d%d%d%d%s%s", &h, &w, &n, &x, &y, s, t); l = 1; r = w; u = 1; d = h; for (int i = n - 1; i >= 0; i--) { if (t[i] == 'L' && r != w) r++; else if (t[i] == 'R' && l != 1) l--; else if (t[i] == 'U' && d != h) d++; else if (t[i] == 'D' && u != 1) u--; if (s[i] == 'L') l++; else if (s[i] == 'R') r--; else if (s[i] == 'U') u++; else d--; if (l > r || u > d) return puts("NO"); } puts(l <= y && y <= r && u <= x && x <= d ? "YES" : "NO"); }
#include <bits/stdc++.h> using namespace std; const int mx = 200005; int h, w, n, x, y, r, l, u, d; char s[mx], t[mx]; int main() { scanf("%d%d%d%d%d%s%s", &h, &w, &n, &x, &y, s, t); l = 1; r = w; u = 1; d = h; for (int i = n - 1; i >= 0; i--) { if (t[i] == 'L' && r != w) r++; else if (t[i] == 'R' && l != 1) l--; else if (t[i] == 'U' && d != h) d++; else if (t[i] == 'D' && u != 1) u--; if (s[i] == 'L') l++; else if (s[i] == 'R') r--; else if (s[i] == 'U') u++; else d--; if (l > r || u > d) return puts("NO") * 0; } puts(l <= y && y <= r && u <= x && x <= d ? "YES" : "NO"); }
replace
30
31
30
31
0
p03054
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; namespace { #define DUMPOUT cerr #ifndef DEBUG_ #define dump(...) #else #define dump(...) \ DUMPOUT << " "; \ DUMPOUT << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" \ << endl; \ DUMPOUT << " "; \ dump_func(__VA_ARGS__) #endif void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...); } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto i = begin(v); i != end(v); ++i) os << *i << (i == end(v) - 1 ? "" : " "); return os; } template <class T> void out2Dvector(vector<T> v) { for (const auto &vv : v) cout << vv << endl; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (auto i = begin(v); i != end(v); ++i) is >> *i; return is; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } } // namespace namespace { using ull = unsigned long long; using ll = long long; #define endl "\n" #define REP(i, n) for (ll i = 0; i < n; i++) #define REPR(i, n) for (ll i = n; i >= 0; i--) #define FOR(i, m, n) for (ll i = m; i < n; i++) #define even(x) (x) % 2 == 0 #define odd(x) (x) % 2 != 0 #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pcnt __builtin_popcount #define buli(x) __builtin_popcountll(x) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define inp(t, x) \ t x; \ cin >> x; #define ithBit(n, i) ((n) >> (i)&1) #define INIT() \ cin.tie(0); \ ios::sync_with_stdio(0); \ cout << fixed << setprecision(20) // these functions return the position of result of Binary Search. #define LB(s, t, x) (int)(lower_bound(s, t, x) - s) #define UB(s, t, x) (int)(upper_bound(s, t, x) - s) #define M_PI 3.14159265358979323846 ll qp(ll a, ll b, int mo) { ll ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { ll temp = gcd(a, b); return temp ? (a / temp * b) : 0; } int mDays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int dx8[] = {1, -1, 0, 0, 1, 1, -1, -1}, dy8[] = {0, 0, -1, 1, -1, 1, -1, 1}; template <typename F> class #if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) [[nodiscard]] #elif defined(__GNUC__) && __GNUC_PREREQ(3, 4) __attribute__((warn_unused_result)) #endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) FixPoint : F { public: explicit constexpr FixPoint(F &&f) noexcept : F(std::forward<F>(f)) {} template <typename... Args> constexpr decltype(auto) operator()(Args &&...args) const { return F::operator()(*this, std::forward<Args>(args)...); } // namespace }; // class FixPoint template <typename F> static inline constexpr decltype(auto) makeFixPoint(F &&f) noexcept { return FixPoint<F>{std::forward<F>(f)}; } 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, size_t b, Ts... ts) { return vector<decltype(make_v<T>(b, ts...))>(a, make_v<T>(b, ts...)); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) { for (auto &e : u) fill_v<T>(e, v...); } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } inline bool rangeCheck2D(int nx, int ny, int Width, int Height) { return nx >= 0 and nx < Width and ny >= 0 and ny < Height; } ll i_query(set<int> x, set<int> y) { cout << x.size() << " " << y.size() << " "; for (const auto &e : x) cout << e << " "; for (const auto &e : y) cout << e << " "; cout << endl; fflush(stdout); ll ret; cin >> ret; return ret; } void i_answer(ll ans, ll d) { cout << "-1 " << ans << endl; fflush(stdout); } } // namespace /* lambda recursion auto func = makeFixPoint([]() -> int {}); int ret = func(); tuple binding auto [x, y] = make_tuple(0, 0); for pair auto [a, b] = pair<int, int>({v1, v2}); bitset<N> bs(ini_val); // N must be constant bs.reset(); // reset all */ int main(void) { INIT(); // comment out for Interective Program inp(ll, H); inp(ll, W); inp(ll, N); int sy, sx; cin >> sy >> sx; string s, t; cin >> s >> t; map<pair<bool, tuple<int, int, int>>, bool> dp; // turn == 1 : takahashi's turn auto rec = makeFixPoint([&](auto rec, int idx, bool turn, int cx, int cy) { pair<bool, tuple<int, int, int>> key = {turn, make_tuple(idx, cx, cy)}; if (dp.count(key)) return dp[key]; if (idx >= N) return dp[key] = true; if (not rangeCheck2D(cx, cy, W, H)) return dp[key] = false; int nidx = idx; if (not turn) nidx++; bool surv = rec(nidx, not turn, cx, cy); if (turn and not surv) return dp[key] = false; if (not turn and surv) return dp[key] = true; int nx = cx, ny = cy; char c = (turn) ? s[idx] : t[idx]; if (c == 'L') ny += 0, nx += -1; if (c == 'R') ny += 0, nx += 1; if (c == 'U') ny += -1, nx += 0; if (c == 'D') ny += 1, nx += 0; bool s2 = rec(nidx, not turn, nx, ny); if (turn) surv &= s2; else surv |= s2; return dp[key] = surv; }); if (rec(0, true, sx - 1, sy - 1)) cout << "YES" << endl; else cout << "NO" << endl; return 0; } //*/
#include <bits/stdc++.h> using namespace std; namespace { #define DUMPOUT cerr #ifndef DEBUG_ #define dump(...) #else #define dump(...) \ DUMPOUT << " "; \ DUMPOUT << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" \ << endl; \ DUMPOUT << " "; \ dump_func(__VA_ARGS__) #endif void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) { DUMPOUT << " "; } else { DUMPOUT << ", "; } dump_func(std::move(tail)...); } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto i = begin(v); i != end(v); ++i) os << *i << (i == end(v) - 1 ? "" : " "); return os; } template <class T> void out2Dvector(vector<T> v) { for (const auto &vv : v) cout << vv << endl; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (auto i = begin(v); i != end(v); ++i) is >> *i; return is; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } } // namespace namespace { using ull = unsigned long long; using ll = long long; #define endl "\n" #define REP(i, n) for (ll i = 0; i < n; i++) #define REPR(i, n) for (ll i = n; i >= 0; i--) #define FOR(i, m, n) for (ll i = m; i < n; i++) #define even(x) (x) % 2 == 0 #define odd(x) (x) % 2 != 0 #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pcnt __builtin_popcount #define buli(x) __builtin_popcountll(x) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define inp(t, x) \ t x; \ cin >> x; #define ithBit(n, i) ((n) >> (i)&1) #define INIT() \ cin.tie(0); \ ios::sync_with_stdio(0); \ cout << fixed << setprecision(20) // these functions return the position of result of Binary Search. #define LB(s, t, x) (int)(lower_bound(s, t, x) - s) #define UB(s, t, x) (int)(upper_bound(s, t, x) - s) #define M_PI 3.14159265358979323846 ll qp(ll a, ll b, int mo) { ll ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { ll temp = gcd(a, b); return temp ? (a / temp * b) : 0; } int mDays[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int dx8[] = {1, -1, 0, 0, 1, 1, -1, -1}, dy8[] = {0, 0, -1, 1, -1, 1, -1, 1}; template <typename F> class #if defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) [[nodiscard]] #elif defined(__GNUC__) && __GNUC_PREREQ(3, 4) __attribute__((warn_unused_result)) #endif // defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) FixPoint : F { public: explicit constexpr FixPoint(F &&f) noexcept : F(std::forward<F>(f)) {} template <typename... Args> constexpr decltype(auto) operator()(Args &&...args) const { return F::operator()(*this, std::forward<Args>(args)...); } // namespace }; // class FixPoint template <typename F> static inline constexpr decltype(auto) makeFixPoint(F &&f) noexcept { return FixPoint<F>{std::forward<F>(f)}; } 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, size_t b, Ts... ts) { return vector<decltype(make_v<T>(b, ts...))>(a, make_v<T>(b, ts...)); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) { for (auto &e : u) fill_v<T>(e, v...); } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } inline bool rangeCheck2D(int nx, int ny, int Width, int Height) { return nx >= 0 and nx < Width and ny >= 0 and ny < Height; } ll i_query(set<int> x, set<int> y) { cout << x.size() << " " << y.size() << " "; for (const auto &e : x) cout << e << " "; for (const auto &e : y) cout << e << " "; cout << endl; fflush(stdout); ll ret; cin >> ret; return ret; } void i_answer(ll ans, ll d) { cout << "-1 " << ans << endl; fflush(stdout); } } // namespace /* lambda recursion auto func = makeFixPoint([]() -> int {}); int ret = func(); tuple binding auto [x, y] = make_tuple(0, 0); for pair auto [a, b] = pair<int, int>({v1, v2}); bitset<N> bs(ini_val); // N must be constant bs.reset(); // reset all */ int main(void) { INIT(); // comment out for Interective Program inp(ll, H); inp(ll, W); inp(ll, N); int sy, sx; cin >> sy >> sx; string s, t; cin >> s >> t; // left int cx = sx, cy = sy; REP(i, N) { if (s[i] == 'L') cx--; if (cx < 1) goto NONO; if (t[i] == 'R') cx++; if (cx > W) cx = W; } // right cx = sx, cy = sy; REP(i, N) { if (s[i] == 'R') cx++; if (cx > W) goto NONO; if (t[i] == 'L') cx--; if (cx < 1) cx = 1; } // up cx = sx, cy = sy; REP(i, N) { if (s[i] == 'U') cy--; if (cy < 1) goto NONO; if (t[i] == 'D') cy++; if (cy > H) cy = H; } // right cx = sx, cy = sy; REP(i, N) { if (s[i] == 'D') cy++; if (cy > H) goto NONO; if (t[i] == 'U') cy--; if (cy < 1) cy = 1; } cout << "YES" << endl; return 0; NONO: cout << "NO" << endl; return 0; } //*/
replace
218
259
218
274
TLE
p03054
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; string dirs = "LRUD"; int pre[2][4][200005]; int di[] = {0, 0, -1, 1}; int dj[] = {-1, 1, 0, 0}; int n, m, l; bool valid(int i, int j) { return i > 0 && j > 0 && i <= n && j <= m; } int main() { #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); #endif ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m >> l; int x, y; cin >> x >> y; string s[2]; cin >> s[0] >> s[1]; bool found = false; for (int d = 0; d < 4; d++) { int nx = x, ny = y; for (int i = 0; i < l; i++) { if (dirs.find(s[0][i]) == d) { nx += di[d], ny += dj[d]; } if (!valid(nx, ny)) { found = true; break; } if (dirs.find(s[1][i]) == (d ^ 1)) { if (valid(nx + di[d ^ 1], ny + dj[d ^ 1])) { nx += di[d ^ 1], ny += dj[d ^ 1]; } } } } cout << (found ? "NO\n" : "YES\n"); return 0; }
#include <bits/stdc++.h> using namespace std; string dirs = "LRUD"; int pre[2][4][200005]; int di[] = {0, 0, -1, 1}; int dj[] = {-1, 1, 0, 0}; int n, m, l; bool valid(int i, int j) { return i > 0 && j > 0 && i <= n && j <= m; } int main() { #ifndef ONLINE_JUDGE // freopen("input.in", "r", stdin); #endif ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m >> l; int x, y; cin >> x >> y; string s[2]; cin >> s[0] >> s[1]; bool found = false; for (int d = 0; d < 4; d++) { int nx = x, ny = y; for (int i = 0; i < l; i++) { if (dirs.find(s[0][i]) == d) { nx += di[d], ny += dj[d]; } if (!valid(nx, ny)) { found = true; break; } if (dirs.find(s[1][i]) == (d ^ 1)) { if (valid(nx + di[d ^ 1], ny + dj[d ^ 1])) { nx += di[d ^ 1], ny += dj[d ^ 1]; } } } } cout << (found ? "NO\n" : "YES\n"); return 0; }
replace
13
14
13
14
0
p03054
C++
Runtime Error
#include <bits/stdc++.h> #define lsb(x) (x & (-x)) #define ll long long #define ull unsigned long long #if 0 const int MOD = ; inline int lgput(int a, int b) { int ans = 1; while(b > 0) { if(b & 1) ans = (1LL * ans * a) % MOD; b >>= 1; a = (1LL * a * a) % MOD; } return ans; } inline void mod(int &x) { if(x >= MOD) x -= MOD; } inline void add(int &x, int y) { x += y; mod(x); } inline void sub(int &x, int y) { x += MOD - y; mod(x); } inline void mul(int &x, int y) { x = (1LL * x * y) % MOD; } inline int inv(int x) { return lgput(x, MOD - 2); } #endif #if 0 int fact[], invfact[]; inline void prec(int n) { fact[0] = 1; for(int i = 1; i <= n; i++) { fact[i] = (1LL * fact[i - 1] * i) % MOD; } invfact[n] = lgput(fact[n], MOD - 2); for(int i = n - 1; i >= 0; i--) { invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD; } } inline int comb(int n, int k) { if(n < k) return 0; return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD; } #endif using namespace std; const int MAXN = (int)2e5 + 5; char s[MAXN], t[MAXN], str[2 * MAXN + 1]; pair<int, int> dpl[MAXN], dpc[MAXN]; int n; inline void inter(int &l, int &r, int a, int b) { if (l > r) { cout << "NO"; exit(0); } l = max(l, a); r = min(r, b); } inline void solve(pair<int, int> *dp, int h, char ch1, char ch2) { for (int i = 2 * n; i >= 1; i--) { int cur = 0; if (ch1 == str[i]) cur--; if (ch2 == str[i]) cur++; int l, r; if (i % 2 == 1) { l = 0, r = h + 1; inter(l, r, dp[i + 1].first, dp[i + 1].second); inter(l, r, dp[i + 1].first - cur, dp[i + 1].second - cur); } else { l = h + 1, r = 0; l = min(l, min(dp[i + 1].first, dp[i + 1].first - cur)); r = max(r, max(dp[i + 1].second, dp[i + 1].second - cur)); } inter(l, r, 1, h); dp[i] = {l, r}; } } int main() { #if 0 ifstream cin("A.in"); ofstream cout("A.out"); #endif int i, h, w, sl, sc; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> h >> w >> n; cin >> sl >> sc; cin >> s + 1 >> t + 1; for (i = 0; i < n; i++) { str[2 * i + 1] = s[i + 1]; str[2 * i + 2] = t[i + 1]; } dpl[2 * n + 1] = {1, h}; dpc[2 * n + 1] = {1, w}; solve(dpl, h, 'U', 'D'); solve(dpc, w, 'L', 'R'); if (dpl[1].first <= sl && dpl[1].second >= sl && dpc[1].first <= sc && dpc[1].second >= sc) { cout << "YES"; return 0; } cout << "NO"; return 0; }
#include <bits/stdc++.h> #define lsb(x) (x & (-x)) #define ll long long #define ull unsigned long long #if 0 const int MOD = ; inline int lgput(int a, int b) { int ans = 1; while(b > 0) { if(b & 1) ans = (1LL * ans * a) % MOD; b >>= 1; a = (1LL * a * a) % MOD; } return ans; } inline void mod(int &x) { if(x >= MOD) x -= MOD; } inline void add(int &x, int y) { x += y; mod(x); } inline void sub(int &x, int y) { x += MOD - y; mod(x); } inline void mul(int &x, int y) { x = (1LL * x * y) % MOD; } inline int inv(int x) { return lgput(x, MOD - 2); } #endif #if 0 int fact[], invfact[]; inline void prec(int n) { fact[0] = 1; for(int i = 1; i <= n; i++) { fact[i] = (1LL * fact[i - 1] * i) % MOD; } invfact[n] = lgput(fact[n], MOD - 2); for(int i = n - 1; i >= 0; i--) { invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD; } } inline int comb(int n, int k) { if(n < k) return 0; return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD; } #endif using namespace std; const int MAXN = (int)2e5 + 5; char s[MAXN], t[MAXN], str[2 * MAXN + 1]; pair<int, int> dpl[2 * MAXN], dpc[2 * MAXN]; int n; inline void inter(int &l, int &r, int a, int b) { if (l > r) { cout << "NO"; exit(0); } l = max(l, a); r = min(r, b); } inline void solve(pair<int, int> *dp, int h, char ch1, char ch2) { for (int i = 2 * n; i >= 1; i--) { int cur = 0; if (ch1 == str[i]) cur--; if (ch2 == str[i]) cur++; int l, r; if (i % 2 == 1) { l = 0, r = h + 1; inter(l, r, dp[i + 1].first, dp[i + 1].second); inter(l, r, dp[i + 1].first - cur, dp[i + 1].second - cur); } else { l = h + 1, r = 0; l = min(l, min(dp[i + 1].first, dp[i + 1].first - cur)); r = max(r, max(dp[i + 1].second, dp[i + 1].second - cur)); } inter(l, r, 1, h); dp[i] = {l, r}; } } int main() { #if 0 ifstream cin("A.in"); ofstream cout("A.out"); #endif int i, h, w, sl, sc; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> h >> w >> n; cin >> sl >> sc; cin >> s + 1 >> t + 1; for (i = 0; i < n; i++) { str[2 * i + 1] = s[i + 1]; str[2 * i + 2] = t[i + 1]; } dpl[2 * n + 1] = {1, h}; dpc[2 * n + 1] = {1, w}; solve(dpl, h, 'U', 'D'); solve(dpc, w, 'L', 'R'); if (dpl[1].first <= sl && dpl[1].second >= sl && dpc[1].first <= sc && dpc[1].second >= sc) { cout << "YES"; return 0; } cout << "NO"; return 0; }
replace
68
69
68
69
0
p03054
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef pair<int, int> pii; const int INF = 1l << 60; #define u_b upper_bound #define l_b lower_bound const int MAX_N = 200000; int H, W, N; int sr, sc; string S, T; int slr[MAX_N], tlr[MAX_N], sud[MAX_N], tud[MAX_N]; template <typename... Args> void debug(Args... args) { for (auto arg : {args...}) cerr << arg << " "; cerr << endl; } signed main() { cin >> H >> W >> N; cin >> sr >> sc; cin >> S >> T; rep(i, N) { if (S[i] == 'L') slr[i] = -1; else if (S[i] == 'R') slr[i] = 1; else if (S[i] == 'U') sud[i] = -1; else if (S[i] == 'D') sud[i] = 1; if (T[i] == 'L') tlr[i] = -1; else if (T[i] == 'R') tlr[i] = 1; else if (T[i] == 'U') tud[i] = -1; else if (T[i] == 'D') tud[i] = 1; } // lrwc { int l = 1, r = W; int k = N - 1; while (k >= 0 && slr[k] == 0) k--; if (k == -1) goto label1; if (slr[k] == 1) r--; else if (slr[k] == -1) l++; else { assert(0); } for (int i = k - 1; i >= 0; --i) { if (tlr[i] == 1) l--; else if (tlr[i] == -1) r++; if (l <= 0) l = 1; if (r > W) r = W; if (slr[i] == 1) r--; else if (slr[i] == -1) l++; if (l > r) { l = INF; break; } // debug(l, r); } if (sc < l || r < sc) { return 1; cout << "NO" << endl; return 0; } // debug(l, r); } label1:; { // udhr int u = 1, d = H; int k = N - 1; while (k >= 0 && sud[k] == 0) k--; if (k == -1) goto label2; if (sud[k] == 1) d--; else if (sud[k] == -1) u++; else { assert(0); } for (int i = k - 1; i >= 0; --i) { if (tud[i] == 1) u--; else if (tud[i] == -1) d++; if (u <= 0) u = 1; if (d > H) d = H; if (sud[i] == 1) d--; else if (sud[i] == -1) u++; if (u > d) { u = INF; break; } // debug(u, d); } if (sr < u || d < sr) { cout << "NO" << endl; return 0; } // debug(u, d); } label2: cout << "YES" << endl; return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef pair<int, int> pii; const int INF = 1l << 60; #define u_b upper_bound #define l_b lower_bound const int MAX_N = 200000; int H, W, N; int sr, sc; string S, T; int slr[MAX_N], tlr[MAX_N], sud[MAX_N], tud[MAX_N]; template <typename... Args> void debug(Args... args) { for (auto arg : {args...}) cerr << arg << " "; cerr << endl; } signed main() { cin >> H >> W >> N; cin >> sr >> sc; cin >> S >> T; rep(i, N) { if (S[i] == 'L') slr[i] = -1; else if (S[i] == 'R') slr[i] = 1; else if (S[i] == 'U') sud[i] = -1; else if (S[i] == 'D') sud[i] = 1; if (T[i] == 'L') tlr[i] = -1; else if (T[i] == 'R') tlr[i] = 1; else if (T[i] == 'U') tud[i] = -1; else if (T[i] == 'D') tud[i] = 1; } // lrwc { int l = 1, r = W; int k = N - 1; while (k >= 0 && slr[k] == 0) k--; if (k == -1) goto label1; if (slr[k] == 1) r--; else if (slr[k] == -1) l++; else { assert(0); } for (int i = k - 1; i >= 0; --i) { if (tlr[i] == 1) l--; else if (tlr[i] == -1) r++; if (l <= 0) l = 1; if (r > W) r = W; if (slr[i] == 1) r--; else if (slr[i] == -1) l++; if (l > r) { l = INF; break; } // debug(l, r); } if (sc < l || r < sc) { cout << "NO" << endl; return 0; } // debug(l, r); } label1:; { // udhr int u = 1, d = H; int k = N - 1; while (k >= 0 && sud[k] == 0) k--; if (k == -1) goto label2; if (sud[k] == 1) d--; else if (sud[k] == -1) u++; else { assert(0); } for (int i = k - 1; i >= 0; --i) { if (tud[i] == 1) u--; else if (tud[i] == -1) d++; if (u <= 0) u = 1; if (d > H) d = H; if (sud[i] == 1) d--; else if (sud[i] == -1) u++; if (u > d) { u = INF; break; } // debug(u, d); } if (sr < u || d < sr) { cout << "NO" << endl; return 0; } // debug(u, d); } label2: cout << "YES" << endl; return 0; }
delete
79
80
79
79
0
p03054
C++
Runtime Error
#include <bits/stdc++.h> const int N = 100010; char str1[N], str2[N]; int n; int main() { int n1, n2, x1, x2; scanf("%d%d%d", &n1, &n2, &n); scanf("%d%d", &x1, &x2); scanf("%s%s", str1 + 1, str2 + 1); bool f = 1; int L = 0, R = n1 + 1; for (int i = n; i >= 1; i--) { if (str2[i] == 'L' || str2[i] == 'R') { } else if (str2[i] == 'D') { L = std::max(L - 1, 0); } else { R = std::min(R + 1, n1 + 1); } if (str1[i] == 'L' || str1[i] == 'R') { } else if (str1[i] == 'D') { R = R - 1; } else { L = L + 1; } if (L + 1 >= R) { f = 0; break; } } if (x1 <= L || R <= x1) { f = 0; } L = 0, R = n2 + 1; for (int i = n; i >= 1 && f; i--) { if (str2[i] == 'U' || str2[i] == 'D') { } else if (str2[i] == 'L') { R = std::min(R + 1, n2 + 1); } else { L = std::max(L - 1, 0); } if (str1[i] == 'U' || str1[i] == 'D') { } else if (str1[i] == 'L') { L = L + 1; } else { R = R - 1; } if (L + 1 >= R) { f = 0; break; } } if (x2 <= L || R <= x2) { f = 0; } if (f) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
#include <bits/stdc++.h> const int N = 200010; char str1[N], str2[N]; int n; int main() { int n1, n2, x1, x2; scanf("%d%d%d", &n1, &n2, &n); scanf("%d%d", &x1, &x2); scanf("%s%s", str1 + 1, str2 + 1); bool f = 1; int L = 0, R = n1 + 1; for (int i = n; i >= 1; i--) { if (str2[i] == 'L' || str2[i] == 'R') { } else if (str2[i] == 'D') { L = std::max(L - 1, 0); } else { R = std::min(R + 1, n1 + 1); } if (str1[i] == 'L' || str1[i] == 'R') { } else if (str1[i] == 'D') { R = R - 1; } else { L = L + 1; } if (L + 1 >= R) { f = 0; break; } } if (x1 <= L || R <= x1) { f = 0; } L = 0, R = n2 + 1; for (int i = n; i >= 1 && f; i--) { if (str2[i] == 'U' || str2[i] == 'D') { } else if (str2[i] == 'L') { R = std::min(R + 1, n2 + 1); } else { L = std::max(L - 1, 0); } if (str1[i] == 'U' || str1[i] == 'D') { } else if (str1[i] == 'L') { L = L + 1; } else { R = R - 1; } if (L + 1 >= R) { f = 0; break; } } if (x2 <= L || R <= x2) { f = 0; } if (f) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
replace
2
3
2
3
0
p03054
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } #define gc getchar inline int rd() { int x = 0, fl = 1; char ch = gc(); for (; ch < 48 || ch > 57; ch = gc()) if (ch == '-') fl = -1; for (; 48 <= ch && ch <= 57; ch = gc()) x = (x << 3) + (x << 1) + (ch ^ 48); return x * fl; } inline void wri(int a) { if (a < 0) a = -a, putchar('-'); if (a >= 10) wri(a / 10); putchar(a % 10 | 48); } inline void wln(int a) { wri(a); puts(""); } const int N = 100002; int h, w, n, sx, sy, x, y, i; char s1[N], s2[N]; int main() { scanf("%d%d%d%d%d%s%s", &h, &w, &n, &sx, &sy, s1 + 1, s2 + 1); x = sx, y = sy; for (i = 1; i <= n; i++) { if (s1[i] == 'L') { y--; if (!y) return puts("NO"), 0; } if (s2[i] == 'R') y++, y = min(y, w); } x = sx, y = sy; for (i = 1; i <= n; i++) { if (s1[i] == 'R') { y++; if (y > w) return puts("NO"), 0; } if (s2[i] == 'L') y--, y = max(y, 1); } x = sx, y = sy; for (i = 1; i <= n; i++) { if (s1[i] == 'U') { x--; if (!x) return puts("NO"), 0; } if (s2[i] == 'D') x++, x = min(x, h); } x = sx, y = sy; for (i = 1; i <= n; i++) { if (s1[i] == 'D') { x++; if (x > h) return puts("NO"), 0; } if (s2[i] == 'U') x--, x = max(x, 1); } puts("YES"); }
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } #define gc getchar inline int rd() { int x = 0, fl = 1; char ch = gc(); for (; ch < 48 || ch > 57; ch = gc()) if (ch == '-') fl = -1; for (; 48 <= ch && ch <= 57; ch = gc()) x = (x << 3) + (x << 1) + (ch ^ 48); return x * fl; } inline void wri(int a) { if (a < 0) a = -a, putchar('-'); if (a >= 10) wri(a / 10); putchar(a % 10 | 48); } inline void wln(int a) { wri(a); puts(""); } const int N = 200002; int h, w, n, sx, sy, x, y, i; char s1[N], s2[N]; int main() { scanf("%d%d%d%d%d%s%s", &h, &w, &n, &sx, &sy, s1 + 1, s2 + 1); x = sx, y = sy; for (i = 1; i <= n; i++) { if (s1[i] == 'L') { y--; if (!y) return puts("NO"), 0; } if (s2[i] == 'R') y++, y = min(y, w); } x = sx, y = sy; for (i = 1; i <= n; i++) { if (s1[i] == 'R') { y++; if (y > w) return puts("NO"), 0; } if (s2[i] == 'L') y--, y = max(y, 1); } x = sx, y = sy; for (i = 1; i <= n; i++) { if (s1[i] == 'U') { x--; if (!x) return puts("NO"), 0; } if (s2[i] == 'D') x++, x = min(x, h); } x = sx, y = sy; for (i = 1; i <= n; i++) { if (s1[i] == 'D') { x++; if (x > h) return puts("NO"), 0; } if (s2[i] == 'U') x--, x = max(x, 1); } puts("YES"); }
replace
30
31
30
31
0
p03054
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 7; typedef pair<int, int> ii; int main() { int h, w, n; scanf("%d %d %d", &h, &w, &n); int sr, sc; scanf("%d %d", &sr, &sc); char t[N]; char a[N]; int r[] = {0, 0}; int l[] = {0, 0}; int u[] = {0, 0}; int d[] = {0, 0}; getchar(); for (int i = 0; i < n; ++i) { scanf("%c", &t[i]); if (t[i] == 'R') ++r[0]; if (t[i] == 'L') ++l[0]; if (t[i] == 'U') ++u[0]; if (t[i] == 'D') ++d[0]; } getchar(); for (int i = 0; i < n; ++i) { scanf("%c", &a[i]); if (a[i] == 'R') ++r[1]; if (a[i] == 'L') ++l[1]; if (a[i] == 'U') ++u[1]; if (a[i] == 'D') ++d[1]; } /* for(int i = 0; i < n; ++i) cout << t[i]; cout<<endl; for(int i = 0; i < n; ++i) cout << a[i]; cout<<endl; */ if (sr - u[0] < 1) { int check = sr; for (int i = 0; i < n; ++i) { if (t[i] == 'U') --check; if (check < 1) { puts("NO"); return 0; } if (check >= 1 && check < h && a[i] == 'D') ++check; } } if (sr + d[0] > h) { int check = sr; for (int i = 0; i < n; ++i) { if (t[i] == 'D') ++check; if (check > h) { puts("NO"); return 0; } if (check > 1 && check <= h && a[i] == 'U') --check; } } if (sc - l[0] < 1) { int check = sc; for (int i = 0; i < n; ++i) { if (t[i] == 'L') --check; if (check < 1) { puts("NO"); return 0; } if (check >= 1 && check < w && a[i] == 'R') ++check; } } if (sc + r[0] > w) { int check = sc; for (int i = 0; i < n; ++i) { if (t[i] == 'R') ++check; if (check > w) { puts("NO"); return 0; } if (check > 1 && check <= w && a[i] == 'L') --check; } } puts("YES"); /* string st = t; string sa = a; int ans = solve(sr, sc, st, sa, pos, 0); */ return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200000 + 7; typedef pair<int, int> ii; int main() { int h, w, n; scanf("%d %d %d", &h, &w, &n); int sr, sc; scanf("%d %d", &sr, &sc); char t[N]; char a[N]; int r[] = {0, 0}; int l[] = {0, 0}; int u[] = {0, 0}; int d[] = {0, 0}; getchar(); for (int i = 0; i < n; ++i) { scanf("%c", &t[i]); if (t[i] == 'R') ++r[0]; if (t[i] == 'L') ++l[0]; if (t[i] == 'U') ++u[0]; if (t[i] == 'D') ++d[0]; } getchar(); for (int i = 0; i < n; ++i) { scanf("%c", &a[i]); if (a[i] == 'R') ++r[1]; if (a[i] == 'L') ++l[1]; if (a[i] == 'U') ++u[1]; if (a[i] == 'D') ++d[1]; } /* for(int i = 0; i < n; ++i) cout << t[i]; cout<<endl; for(int i = 0; i < n; ++i) cout << a[i]; cout<<endl; */ if (sr - u[0] < 1) { int check = sr; for (int i = 0; i < n; ++i) { if (t[i] == 'U') --check; if (check < 1) { puts("NO"); return 0; } if (check >= 1 && check < h && a[i] == 'D') ++check; } } if (sr + d[0] > h) { int check = sr; for (int i = 0; i < n; ++i) { if (t[i] == 'D') ++check; if (check > h) { puts("NO"); return 0; } if (check > 1 && check <= h && a[i] == 'U') --check; } } if (sc - l[0] < 1) { int check = sc; for (int i = 0; i < n; ++i) { if (t[i] == 'L') --check; if (check < 1) { puts("NO"); return 0; } if (check >= 1 && check < w && a[i] == 'R') ++check; } } if (sc + r[0] > w) { int check = sc; for (int i = 0; i < n; ++i) { if (t[i] == 'R') ++check; if (check > w) { puts("NO"); return 0; } if (check > 1 && check <= w && a[i] == 'L') --check; } } puts("YES"); /* string st = t; string sa = a; int ans = solve(sr, sc, st, sa, pos, 0); */ return 0; }
replace
4
5
4
5
0
p03054
C++
Runtime Error
// #pragma GCC optimize(3) #define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> typedef long long LL; using namespace std; #define MAXN 105 char t[MAXN]; char a[MAXN]; int tongt[4]; // LRUD int tonga[4]; bool a1[MAXN]; int main() { // ios::sync_with_stdio(false); // cin.tie(0); int groups; // scanf("%d", &groups); groups = 1; for (int i = 0; i < groups; i++) { int h, w, n, sr, sc; char tempt, tempa; bool flag = false; scanf("%d%d%d", &h, &w, &n); scanf("%d%d", &sc, &sr); scanf("%s", t); scanf("%s", a); for (int j = 0; j < n; j++) { if (t[j] == 'L') tongt[0]++; if (t[j] == 'R') tongt[1]++; if (t[j] == 'U') tongt[2]++; if (t[j] == 'D') tongt[3]++; if (a[j] == 'L') tonga[0]++; if (a[j] == 'R') tonga[1]++; if (a[j] == 'U') tonga[2]++; if (a[j] == 'D') tonga[3]++; if (tonga[0] - tongt[1] >= sr) { tonga[0]--; a1[j] = true; } if (tonga[1] - tongt[0] >= (w - sr + 1)) { tonga[1]--; a1[j] = true; } if (tonga[2] - tongt[3] >= sc) { tonga[2]--; a1[j] = true; } if (tonga[3] - tongt[2] >= (h - sc + 1)) { tonga[3]--; a1[j] = true; } } for (int j = n - 1; j >= 0; j--) { if (a[j] == 'L' && !a1[j]) tonga[0]--; if (a[j] == 'R' && !a1[j]) tonga[1]--; if (a[j] == 'U' && !a1[j]) tonga[2]--; if (a[j] == 'D' && !a1[j]) tonga[3]--; if (tongt[0] - tonga[1] >= sr) flag = true; if (tongt[1] - tonga[0] >= (w - sr + 1)) flag = true; if (tongt[2] - tonga[3] >= sc) flag = true; if (tongt[3] - tonga[2] >= (h - sc + 1)) flag = true; if (flag) break; if (t[j] == 'L') tongt[0]--; if (t[j] == 'R') tongt[1]--; if (t[j] == 'U') tongt[2]--; if (t[j] == 'D') tongt[3]--; } if (flag) printf("NO"); else printf("YES"); } return 0; }
// #pragma GCC optimize(3) #define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> typedef long long LL; using namespace std; #define MAXN 200005 char t[MAXN]; char a[MAXN]; int tongt[4]; // LRUD int tonga[4]; bool a1[MAXN]; int main() { // ios::sync_with_stdio(false); // cin.tie(0); int groups; // scanf("%d", &groups); groups = 1; for (int i = 0; i < groups; i++) { int h, w, n, sr, sc; char tempt, tempa; bool flag = false; scanf("%d%d%d", &h, &w, &n); scanf("%d%d", &sc, &sr); scanf("%s", t); scanf("%s", a); for (int j = 0; j < n; j++) { if (t[j] == 'L') tongt[0]++; if (t[j] == 'R') tongt[1]++; if (t[j] == 'U') tongt[2]++; if (t[j] == 'D') tongt[3]++; if (a[j] == 'L') tonga[0]++; if (a[j] == 'R') tonga[1]++; if (a[j] == 'U') tonga[2]++; if (a[j] == 'D') tonga[3]++; if (tonga[0] - tongt[1] >= sr) { tonga[0]--; a1[j] = true; } if (tonga[1] - tongt[0] >= (w - sr + 1)) { tonga[1]--; a1[j] = true; } if (tonga[2] - tongt[3] >= sc) { tonga[2]--; a1[j] = true; } if (tonga[3] - tongt[2] >= (h - sc + 1)) { tonga[3]--; a1[j] = true; } } for (int j = n - 1; j >= 0; j--) { if (a[j] == 'L' && !a1[j]) tonga[0]--; if (a[j] == 'R' && !a1[j]) tonga[1]--; if (a[j] == 'U' && !a1[j]) tonga[2]--; if (a[j] == 'D' && !a1[j]) tonga[3]--; if (tongt[0] - tonga[1] >= sr) flag = true; if (tongt[1] - tonga[0] >= (w - sr + 1)) flag = true; if (tongt[2] - tonga[3] >= sc) flag = true; if (tongt[3] - tonga[2] >= (h - sc + 1)) flag = true; if (flag) break; if (t[j] == 'L') tongt[0]--; if (t[j] == 'R') tongt[1]--; if (t[j] == 'U') tongt[2]--; if (t[j] == 'D') tongt[3]--; } if (flag) printf("NO"); else printf("YES"); } return 0; }
replace
8
9
8
9
0
p03054
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int H, W, N, Sx, Sy; cin >> H >> W >> N >> Sx >> Sy; string S, T; cin >> S >> T; bool flag = true; // true ならTの勝ち。つまりYes int down[20200]; // 累積和にあたるもの int up[20200]; // 初手で出れる場合があるんだよな int right[20200]; int left[20200]; for (int i = 0; i < N; i++) { down[i] = 0; up[i] = 0; right[i] = 0; left[i] = 0; } // 向きに注意 // 下へ出れるか // SからはDをTからはUを if (S.at(0) == 'D') { down[0] = 1; } for (int i = 0; i < N - 1; i++) { down[i + 1] = down[i]; if (T.at(i) == 'U' && (Sx + down[i] > 1)) down[i + 1]--; if (S.at(i + 1) == 'D') down[i + 1]++; } // for (int i = 0; i < N; i++) { if (down[i] >= H + 1 - Sx) { flag = false; } } // 上へ出れるか if (S.at(0) == 'U') { up[0] = 1; } for (int i = 0; i < N - 1; i++) { up[i + 1] = up[i]; if (T.at(i) == 'D' && (Sx - up[i] < H)) up[i + 1]--; if (S.at(i + 1) == 'U') up[i + 1]++; } // for (int i = 0; i < N; i++) { if (up[i] >= Sx) { flag = false; } } // 右へ出れるか if (S.at(0) == 'R') { right[0] = 1; } for (int i = 0; i < N - 1; i++) { right[i + 1] = right[i]; if (T.at(i) == 'L' && (Sy + right[i] > 1)) right[i + 1]--; if (S.at(i + 1) == 'R') right[i + 1]++; } // for (int i = 0; i < N; i++) { if (right[i] >= W + 1 - Sy) { flag = false; } } // 左へ出れるか if (S.at(0) == 'L') { left[0] = 1; } for (int i = 0; i < N - 1; i++) { left[i + 1] = left[i]; if (T.at(i) == 'R' && (Sy - left[i] < W)) left[i + 1]--; if (S.at(i + 1) == 'L') left[i + 1]++; } // for (int i = 0; i < N; i++) { if (left[i] >= Sy) { flag = false; } } if (flag) cout << "YES" << endl; else cout << "NO" << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int H, W, N, Sx, Sy; cin >> H >> W >> N >> Sx >> Sy; string S, T; cin >> S >> T; bool flag = true; // true ならTの勝ち。つまりYes int down[202000]; // 累積和にあたるもの int up[202000]; // 初手で出れる場合があるんだよな int right[202000]; int left[202000]; for (int i = 0; i < N; i++) { down[i] = 0; up[i] = 0; right[i] = 0; left[i] = 0; } // 向きに注意 // 下へ出れるか // SからはDをTからはUを if (S.at(0) == 'D') { down[0] = 1; } for (int i = 0; i < N - 1; i++) { down[i + 1] = down[i]; if (T.at(i) == 'U' && (Sx + down[i] > 1)) down[i + 1]--; if (S.at(i + 1) == 'D') down[i + 1]++; } // for (int i = 0; i < N; i++) { if (down[i] >= H + 1 - Sx) { flag = false; } } // 上へ出れるか if (S.at(0) == 'U') { up[0] = 1; } for (int i = 0; i < N - 1; i++) { up[i + 1] = up[i]; if (T.at(i) == 'D' && (Sx - up[i] < H)) up[i + 1]--; if (S.at(i + 1) == 'U') up[i + 1]++; } // for (int i = 0; i < N; i++) { if (up[i] >= Sx) { flag = false; } } // 右へ出れるか if (S.at(0) == 'R') { right[0] = 1; } for (int i = 0; i < N - 1; i++) { right[i + 1] = right[i]; if (T.at(i) == 'L' && (Sy + right[i] > 1)) right[i + 1]--; if (S.at(i + 1) == 'R') right[i + 1]++; } // for (int i = 0; i < N; i++) { if (right[i] >= W + 1 - Sy) { flag = false; } } // 左へ出れるか if (S.at(0) == 'L') { left[0] = 1; } for (int i = 0; i < N - 1; i++) { left[i + 1] = left[i]; if (T.at(i) == 'R' && (Sy - left[i] < W)) left[i + 1]--; if (S.at(i + 1) == 'L') left[i + 1]++; } // for (int i = 0; i < N; i++) { if (left[i] >= Sy) { flag = false; } } if (flag) cout << "YES" << endl; else cout << "NO" << endl; }
replace
11
15
11
15
0
p03054
C++
Time Limit Exceeded
#pragma GCC optimize("O3", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #include <bitset> #include <iostream> using namespace std; int h, w, n; int sr, sc; string s, t; bitset<200003> b; template <char c1, char c2> bool solve(int m, int pos) { b.reset(); b[0] = true; b[m + 1] = true; for (int i = n - 1; i >= 0; i--) { if (t[i] == c1) b &= (b << 1); else if (t[i] == c2) b &= (b >> 1); b[0] = true; b[m + 1] = true; if (s[i] == c1) b |= (b << 1); else if (s[i] == c2) b |= (b >> 1); b[0] = true; b[m + 1] = true; } return b[pos + 1]; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> h >> w >> n; cin >> sr >> sc; sr--; sc--; cin >> s >> t; bool ans = false; if (solve<'L', 'R'>(w, sc) || solve<'U', 'D'>(h, sr)) { ans = true; } cout << (ans ? "NO" : "YES") << endl; return 0; }
#pragma GCC optimize("O3", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC optimize("tree-vectorize") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #include <bitset> #include <iostream> using namespace std; int h, w, n; int sr, sc; string s, t; bitset<200003> b; template <char c1, char c2> bool solve(int m, int pos) { b.reset(); b[0] = true; b[m + 1] = true; for (int i = n - 1; i >= 0; i--) { if (t[i] == c1) b &= (b << 1); else if (t[i] == c2) b &= (b >> 1); b[0] = true; b[m + 1] = true; if (s[i] == c1) b |= (b << 1); else if (s[i] == c2) b |= (b >> 1); b[0] = true; b[m + 1] = true; } return b[pos + 1]; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> h >> w >> n; cin >> sr >> sc; sr--; sc--; cin >> s >> t; bool ans = false; if (solve<'L', 'R'>(w, sc) || solve<'U', 'D'>(h, sr)) { ans = true; } cout << (ans ? "NO" : "YES") << endl; return 0; }
insert
1
1
1
2
TLE
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MOD (long long int)(1e9 + 7) #define ll long long int #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i = 1; i <= (int)(n); i++) #define REP(i, n) for (int i = n - 1; i >= 0; i--) #define REPS(i, n) for (int i = n; i > 0; i--) #define FOR(i, a, b) for (int i = a; i < (int)(b); i++) #define ALL(x) (x).begin(), (x).end() #define RALL(a) (a).rbegin(), (a).rend() #define CLR(a) memset((a), 0, sizeof(a)) #define PB push_back #define MP make_pair #define SP << " " << const int INF = 1001001001; const ll LINF = 100100100100100100; const double EPS = 1e-10; const long double PI = acos(-1.0L); typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef pair<double, double> PDD; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VL; #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) __attribute__((constructor)) void initial() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } const int maxV = 111111; vector<int> G[maxV]; // 頂点情報のみのグラフ // treeDFS(親, 現在地, 根から現在地までの距離, 根からの最大の距離, // 根から最大の距離となる頂点 void treeDFS(int from, int current, int dist, int &maxDist, int &maxVertex) { // 距離と終点を更新 if (dist > maxDist) { maxDist = dist; maxVertex = current; } for (auto to : G[current]) { // 逆流を防ぐ if (to == from) continue; treeDFS(current, to, dist + 1, maxDist, maxVertex); } } int getTreeDiameter() { int start = 0, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); start = end, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); // printf("start: %d, end: %d, diameter: %d\n", start, end, maxDist); return maxDist; } signed main() { int n; cin >> n; rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; G[a].PB(b); G[b].PB(a); } int d = getTreeDiameter(); if (d % 3 == 1) cout << "Second" << endl; else cout << "First" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD (long long int)(1e9 + 7) #define ll long long int #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i = 1; i <= (int)(n); i++) #define REP(i, n) for (int i = n - 1; i >= 0; i--) #define REPS(i, n) for (int i = n; i > 0; i--) #define FOR(i, a, b) for (int i = a; i < (int)(b); i++) #define ALL(x) (x).begin(), (x).end() #define RALL(a) (a).rbegin(), (a).rend() #define CLR(a) memset((a), 0, sizeof(a)) #define PB push_back #define MP make_pair #define SP << " " << const int INF = 1001001001; const ll LINF = 100100100100100100; const double EPS = 1e-10; const long double PI = acos(-1.0L); typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef pair<double, double> PDD; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VL; #define chmax(a, b) a = (((a) < (b)) ? (b) : (a)) #define chmin(a, b) a = (((a) > (b)) ? (b) : (a)) __attribute__((constructor)) void initial() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } const int maxV = 311111; vector<int> G[maxV]; // 頂点情報のみのグラフ // treeDFS(親, 現在地, 根から現在地までの距離, 根からの最大の距離, // 根から最大の距離となる頂点 void treeDFS(int from, int current, int dist, int &maxDist, int &maxVertex) { // 距離と終点を更新 if (dist > maxDist) { maxDist = dist; maxVertex = current; } for (auto to : G[current]) { // 逆流を防ぐ if (to == from) continue; treeDFS(current, to, dist + 1, maxDist, maxVertex); } } int getTreeDiameter() { int start = 0, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); start = end, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); // printf("start: %d, end: %d, diameter: %d\n", start, end, maxDist); return maxDist; } signed main() { int n; cin >> n; rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; G[a].PB(b); G[b].PB(a); } int d = getTreeDiameter(); if (d % 3 == 1) cout << "Second" << endl; else cout << "First" << endl; return 0; }
replace
34
35
34
35
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using PII = pair<ll, ll>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() template <typename T> void chmin(T &a, const T &b) { a = min(a, b); } template <typename T> void chmax(T &a, const T &b) { a = max(a, b); } struct FastIO { FastIO() { cin.tie(0); ios::sync_with_stdio(0); } } fastiofastio; #ifdef DEBUG #include "../program_contest_library/memo/dump.hpp" #else #define dump(...) #endif const ll INF = 1LL << 60; int main(void) { ll n; cin >> n; vector<vector<ll>> g(n); REP(i, n - 1) { ll a, b; cin >> a >> b; a--, b--; g[a].push_back(b); g[b].push_back(a); } vector<ll> dist(n, INF); auto dfs = [&](auto &&self, ll v, ll p, ll d) -> void { dist[v] = d; for (auto to : g[v]) if (to != p) { self(self, to, v, d + 1); } }; dfs(dfs, 0, -1, 0); ll ma = 0, idx = -1; REP(i, n) if (ma < dist[i]) ma = dist[i], idx = i; dist.assign(n, INF); dfs(dfs, idx, -1, 0); ma = 0; REP(i, n) chmax(ma, dist[i]); if (ma == 0) cout << "First\n"; else if (ma == 1) cout << "Second\n"; else { ma -= 2; ma %= 3; if (ma <= 1) cout << "First\n"; else cout << "Second\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using PII = pair<ll, ll>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() template <typename T> void chmin(T &a, const T &b) { a = min(a, b); } template <typename T> void chmax(T &a, const T &b) { a = max(a, b); } struct FastIO { FastIO() { cin.tie(0); ios::sync_with_stdio(0); } } fastiofastio; #ifdef DEBUG #include "../program_contest_library/memo/dump.hpp" #else #define dump(...) #endif const ll INF = 1LL << 60; int main(void) { ll n; cin >> n; vector<vector<ll>> g(n); REP(i, n - 1) { ll a, b; cin >> a >> b; a--, b--; g[a].push_back(b); g[b].push_back(a); } vector<ll> dist(n, INF); auto dfs = [&](auto &&self, ll v, ll p, ll d) -> void { dist[v] = d; for (auto to : g[v]) if (to != p) { self(self, to, v, d + 1); } }; dfs(dfs, 0, -1, 0); ll ma = -1, idx = -1; REP(i, n) if (ma < dist[i]) ma = dist[i], idx = i; dist.assign(n, INF); dfs(dfs, idx, -1, 0); ma = 0; REP(i, n) chmax(ma, dist[i]); if (ma == 0) cout << "First\n"; else if (ma == 1) cout << "Second\n"; else { ma -= 2; ma %= 3; if (ma <= 1) cout << "First\n"; else cout << "Second\n"; } return 0; }
replace
43
44
43
44
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int fir[N], snd[N], d[N]; int hed[N], to[N << 1], nxt[N << 1], cnt; int L = 0; inline void dfs(int x, int pre) { d[x] = d[pre] + 1; fir[x] = snd[x] = d[x]; for (int i = hed[x]; i; i = nxt[i]) { int v = to[i]; if (v == pre) continue; dfs(v, x); if (fir[v] >= fir[x]) snd[x] = fir[x], fir[x] = fir[v]; else if (fir[v] >= snd[x]) snd[x] = fir[v]; } L = max(L, fir[x] + snd[x] - 2 * d[x]); } int n; int main() { cin >> n; for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); ++cnt; to[cnt] = v, nxt[cnt] = hed[u]; hed[u] = cnt; ++cnt; to[cnt] = u, nxt[cnt] = hed[v]; hed[v] = cnt; } dfs(1, 0); cout << (L % 3 == 1 ? "Second" : "First") << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int fir[N], snd[N], d[N]; int hed[N], to[N << 1], nxt[N << 1], cnt; int L = 0; inline void dfs(int x, int pre) { d[x] = d[pre] + 1; fir[x] = snd[x] = d[x]; for (int i = hed[x]; i; i = nxt[i]) { int v = to[i]; if (v == pre) continue; dfs(v, x); if (fir[v] >= fir[x]) snd[x] = fir[x], fir[x] = fir[v]; else if (fir[v] >= snd[x]) snd[x] = fir[v]; } L = max(L, fir[x] + snd[x] - 2 * d[x]); } int n; int main() { cin >> n; for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); ++cnt; to[cnt] = v, nxt[cnt] = hed[u]; hed[u] = cnt; ++cnt; to[cnt] = u, nxt[cnt] = hed[v]; hed[v] = cnt; } dfs(1, 0); cout << (L % 3 == 1 ? "Second" : "First") << endl; }
replace
2
3
2
3
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; #define fi first #define se second #define pb push_back vector<vector<int>> E; // Eは頂点数+1をとっておく int diameter(int x) { // 頂点数を入れる int ans = 0, en; queue<int> q; vector<int> dis(x + 1, 0); // 何マスで訪れたか dis.at(x) = -1; q.push(x); while (q.size() != 0) { int s = q.front(); q.pop(); for (int i = 0; i < E.at(s).size(); i++) { if (dis.at(E.at(s).at(i)) == 0) { dis.at(E.at(s).at(i)) += max(dis.at(s) + 1, 1); q.push(E.at(s).at(i)); if (ans < dis.at(E.at(s).at(i))) { en = E.at(s).at(i); } ans = max(ans, dis.at(E.at(s).at(i))); } } } dis.assign(x + 1, 0); dis.at(en) = -1; q.push(en); while (q.size() != 0) { int s = q.front(); q.pop(); for (int i = 0; i < E.at(s).size(); i++) { if (dis.at(E.at(s).at(i)) == 0) { dis.at(E.at(s).at(i)) += max(dis.at(s) + 1, 1); q.push(E.at(s).at(i)); ans = max(ans, dis.at(E.at(s).at(i))); } } } return ans; } int main() { int n, d; cin >> n; E.assign(n + 1, vector<int>()); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; E.at(a).pb(b); E.at(b).pb(a); // Edge } d = diameter(n); d++; if (d % 3 == 2) { cout << "Second" << endl; } else { cout << "First" << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define fi first #define se second #define pb push_back vector<vector<int>> E; // Eは頂点数+1をとっておく int diameter(int x) { // 頂点数を入れる int ans = 0, en = x; queue<int> q; vector<int> dis(x + 1, 0); // 何マスで訪れたか dis.at(x) = -1; q.push(x); while (q.size() != 0) { int s = q.front(); q.pop(); for (int i = 0; i < E.at(s).size(); i++) { if (dis.at(E.at(s).at(i)) == 0) { dis.at(E.at(s).at(i)) += max(dis.at(s) + 1, 1); q.push(E.at(s).at(i)); if (ans < dis.at(E.at(s).at(i))) { en = E.at(s).at(i); } ans = max(ans, dis.at(E.at(s).at(i))); } } } dis.assign(x + 1, 0); dis.at(en) = -1; q.push(en); while (q.size() != 0) { int s = q.front(); q.pop(); for (int i = 0; i < E.at(s).size(); i++) { if (dis.at(E.at(s).at(i)) == 0) { dis.at(E.at(s).at(i)) += max(dis.at(s) + 1, 1); q.push(E.at(s).at(i)); ans = max(ans, dis.at(E.at(s).at(i))); } } } return ans; } int main() { int n, d; cin >> n; E.assign(n + 1, vector<int>()); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; E.at(a).pb(b); E.at(b).pb(a); // Edge } d = diameter(n); d++; if (d % 3 == 2) { cout << "Second" << endl; } else { cout << "First" << endl; } }
replace
11
12
11
12
0
p03055
C++
Runtime Error
// template version 1.15 using namespace std; #include <bits/stdc++.h> // varibable settings #define int long long const int INF = 1e18; // define basic macro {{{ #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define _rrep(i, n) rrepi(i, 0, n) #define rrepi(i, a, b) for (int i = (int)((b)-1); i >= (int)(a); --i) #define rrep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__) #define each(i, a) for (auto &&i : a) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define pb(a) push_back(a) #define mp(a, b) make_pair(a, b) #define mt(a, b, c) make_tuple(a, b, c) #define divceil(a, b) ((a) + (b)-1) / (b) #define is_in(x, a, b) ((a) <= (x) && (x) < (b)) #define uni(x) \ sort(all(x)); \ x.erase(unique(all(x)), x.end()) #define ub upper_bound #define lb lower_bound #define posl(A, x) (lower_bound(all(A), x) - A.begin()) #define posu(A, x) (upper_bound(all(A), x) - A.begin()) template <class T> inline void chmax(T &a, const T &b) { if ((a) < (b)) (a) = (b); } template <class T> inline void chmin(T &a, const T &b) { if ((a) > (b)) (a) = (b); } typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef long double ld; typedef pair<int, int> pii; typedef tuple<int, int, int> iii; template <typename T> using PQ = priority_queue<T, vector<T>, greater<T>>; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; void sputs() {} template <class Head, class... Tail> void sputs(Head &&head, Tail &&...tail) { cout << head; if (sizeof...(Tail) == 0) { cout << endl; } else { cout << " "; } sputs(std::move(tail)...); } #if defined(PCM) || defined(LOCAL) #include "lib/dump.hpp" #else #define dump(...) 42 #define dump_1d(...) 42 #define dump_2d(...) 42 #endif //}}} signed main() { int n; cin >> n; vvi g(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; g[a].pb(b); g[b].pb(a); } int x = -1; int max_d = 0; auto dfs = [&](const auto &dfs, auto &d, int u, int cost) -> void { each(v, g[u]) { if (d[v] < INF) continue; d[v] = cost + 1; if (d[v] > max_d) { chmax(max_d, d[v]); x = v; } dfs(dfs, d, v, cost + 1); } }; vi d0(n, INF); d0[0] = 0; dfs(dfs, d0, 0, 0); vi dx(n, INF); dx[x] = 0; max_d = 0; dfs(dfs, dx, x, 0); dump(max_d); vi dp(n + 1); dp[0] = 0; dp[1] = 1; dp[2] = 0; rep(i, 3, n + 1) { if (dp[i - 1] == 0 || dp[i - 2] == 0) dp[i] = 1; } dump(dp); cout << (dp[max_d + 1] ? "First" : "Second") << endl; return 0; }
// template version 1.15 using namespace std; #include <bits/stdc++.h> // varibable settings #define int long long const int INF = 1e18; // define basic macro {{{ #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define _rrep(i, n) rrepi(i, 0, n) #define rrepi(i, a, b) for (int i = (int)((b)-1); i >= (int)(a); --i) #define rrep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__) #define each(i, a) for (auto &&i : a) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define pb(a) push_back(a) #define mp(a, b) make_pair(a, b) #define mt(a, b, c) make_tuple(a, b, c) #define divceil(a, b) ((a) + (b)-1) / (b) #define is_in(x, a, b) ((a) <= (x) && (x) < (b)) #define uni(x) \ sort(all(x)); \ x.erase(unique(all(x)), x.end()) #define ub upper_bound #define lb lower_bound #define posl(A, x) (lower_bound(all(A), x) - A.begin()) #define posu(A, x) (upper_bound(all(A), x) - A.begin()) template <class T> inline void chmax(T &a, const T &b) { if ((a) < (b)) (a) = (b); } template <class T> inline void chmin(T &a, const T &b) { if ((a) > (b)) (a) = (b); } typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef long double ld; typedef pair<int, int> pii; typedef tuple<int, int, int> iii; template <typename T> using PQ = priority_queue<T, vector<T>, greater<T>>; struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast; void sputs() {} template <class Head, class... Tail> void sputs(Head &&head, Tail &&...tail) { cout << head; if (sizeof...(Tail) == 0) { cout << endl; } else { cout << " "; } sputs(std::move(tail)...); } #if defined(PCM) || defined(LOCAL) #include "lib/dump.hpp" #else #define dump(...) 42 #define dump_1d(...) 42 #define dump_2d(...) 42 #endif //}}} signed main() { int n; cin >> n; if (n == 1) { cout << "First" << endl; return 0; } vvi g(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; g[a].pb(b); g[b].pb(a); } int x = -1; int max_d = 0; auto dfs = [&](const auto &dfs, auto &d, int u, int cost) -> void { each(v, g[u]) { if (d[v] < INF) continue; d[v] = cost + 1; if (d[v] > max_d) { chmax(max_d, d[v]); x = v; } dfs(dfs, d, v, cost + 1); } }; vi d0(n, INF); d0[0] = 0; dfs(dfs, d0, 0, 0); vi dx(n, INF); dx[x] = 0; max_d = 0; dfs(dfs, dx, x, 0); dump(max_d); vi dp(n + 1); dp[0] = 0; dp[1] = 1; dp[2] = 0; rep(i, 3, n + 1) { if (dp[i - 1] == 0 || dp[i - 2] == 0) dp[i] = 1; } dump(dp); cout << (dp[max_d + 1] ? "First" : "Second") << endl; return 0; }
insert
78
78
78
82
0
p03055
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; const int N = 1e5 + 7; int dp[N]; void init() { dp[0] = 1; for (int i = 1; i < N; i++) { int some_lose = 0; for (int j = 1; j <= 2; j++) { if (i - j >= 0 && !dp[i - j]) { some_lose = 1; } } if (some_lose) dp[i] = 1; } } int used[N]; vector<int> g[N]; void dfs(int cur, int ch, pair<int, int> &mx) { used[cur] = 1; mx = max(mx, {ch, cur}); for (auto t : g[cur]) { if (!used[t]) { dfs(t, ch + 1, mx); } } } int solve() { pair<int, int> mx = {-1, -1}; dfs(0, 0, mx); int v = mx.second; fill(used, used + N, 0); mx = {-1, -1}; dfs(v, 0, mx); return mx.first; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); init(); // for (int i = 0; i < 10; i++) { // cerr << dp[i] << ' '; // } // cerr << endl; int n; cin >> n; for (int i = 0; i + 1 < n; i++) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } int d = solve(); if (dp[d]) { cout << "First\n"; } else { cout << "Second\n"; } }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; const int N = 2e5 + 7; int dp[N]; void init() { dp[0] = 1; for (int i = 1; i < N; i++) { int some_lose = 0; for (int j = 1; j <= 2; j++) { if (i - j >= 0 && !dp[i - j]) { some_lose = 1; } } if (some_lose) dp[i] = 1; } } int used[N]; vector<int> g[N]; void dfs(int cur, int ch, pair<int, int> &mx) { used[cur] = 1; mx = max(mx, {ch, cur}); for (auto t : g[cur]) { if (!used[t]) { dfs(t, ch + 1, mx); } } } int solve() { pair<int, int> mx = {-1, -1}; dfs(0, 0, mx); int v = mx.second; fill(used, used + N, 0); mx = {-1, -1}; dfs(v, 0, mx); return mx.first; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); init(); // for (int i = 0; i < 10; i++) { // cerr << dp[i] << ' '; // } // cerr << endl; int n; cin >> n; for (int i = 0; i + 1 < n; i++) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } int d = solve(); if (dp[d]) { cout << "First\n"; } else { cout << "Second\n"; } }
replace
23
24
23
24
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int d[101]; vector<int> gr[101]; void dfs(int cur, int parent) { for (int adj : gr[cur]) { if (adj != parent) { d[adj] = d[cur] + 1; dfs(adj, cur); } } } int main() { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--, v--; gr[u].push_back(v); gr[v].push_back(u); } dfs(0, -1); int edge = max_element(d, d + n) - d; d[edge] = 0; dfs(edge, -1); int ans = *max_element(d, d + n); if (ans % 3 == 1) cout << "Second" << endl; else cout << "First" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int d[N]; vector<int> gr[N]; void dfs(int cur, int parent) { for (int adj : gr[cur]) { if (adj != parent) { d[adj] = d[cur] + 1; dfs(adj, cur); } } } int main() { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--, v--; gr[u].push_back(v); gr[v].push_back(u); } dfs(0, -1); int edge = max_element(d, d + n) - d; d[edge] = 0; dfs(edge, -1); int ans = *max_element(d, d + n); if (ans % 3 == 1) cout << "Second" << endl; else cout << "First" << endl; return 0; }
replace
2
4
2
5
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; int N; bool dp[MAXN]; vector<int> adj[MAXN]; int s, md; void dfs(int cur, int prv, int d) { if (d > md) { s = cur; md = d; } for (int nxt : adj[cur]) { if (nxt == prv) continue; dfs(nxt, cur, d + 1); } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> N; for (int i = 0; i < N - 1; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1, -1, 1); dfs(s, -1, 1); dp[0] = false; dp[1] = true; dp[2] = false; for (int i = 3; i <= md; i++) { dp[i] = (!dp[i - 1] || !dp[i - 2]); } if (dp[md]) { cout << "First" << endl; } else { cout << "Second" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 200010; int N; bool dp[MAXN]; vector<int> adj[MAXN]; int s, md; void dfs(int cur, int prv, int d) { if (d > md) { s = cur; md = d; } for (int nxt : adj[cur]) { if (nxt == prv) continue; dfs(nxt, cur, d + 1); } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> N; for (int i = 0; i < N - 1; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1, -1, 1); dfs(s, -1, 1); dp[0] = false; dp[1] = true; dp[2] = false; for (int i = 3; i <= md; i++) { dp[i] = (!dp[i - 1] || !dp[i - 2]); } if (dp[md]) { cout << "First" << endl; } else { cout << "Second" << endl; } return 0; }
replace
3
4
3
4
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> #define FASTIO #define endl \ "\n" // Since endl forces a buffer flush which slows down large I/O // operations. #define ff first #define ss second #define pb push_back #define input(c, n) \ for (int i = 0; i < n; i++) \ cin >> c[i]; #define exit_message(s) return 0 * printf(s) #define show(c) \ for (auto i : c) \ cout << i << " "; \ cout << "\n"; #define display(c, n) \ for (int i = 0; i < n; i++) \ cout << c[i] << " "; \ cout << "\n"; #define forl(i, a, n) for (int i = a; i < n; i++) #define dugbe(k) cout << "-\t> " << #k << " = " << k << "\n"; /* TIPS FOR DEBUGGING * Check if values are not overflowing (use long long where required) * Check if ranges are inclusive or not. * Check properly if greedy will work or not before using it. * Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being * 2 minutes faster will bite you in the ass if you don't make sure * it works on edge cases. * Keep adding to this list. */ using namespace std; typedef long long ll; typedef pair<int, int> pr; typedef pair<ll, ll> prll; typedef vector<int> vi; typedef vector<ll> vill; typedef vector<pr> vpr; ifstream in("input.txt"); const ll mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long true_rand(long long n) { // Returns a random number between 0 and n - 1 inclusive using mt19937. uniform_int_distribution<long long> uid(0, n - 1); return uid(rng); } ll mod_pow(ll a, ll b) { if (b == 0) return 1LL; ll res = mod_pow(a, b >> 1); res = (res * res) % mod; if (b & 1LL) res = (res * a) % mod; return res; } const int N = 1e5 + 5; int n; vi g[N] = {}; int vis[N] = {}; void dfs(int s) { if (!vis[s]) vis[s] = 1; for (auto e : g[s]) { if (!vis[e]) { vis[e] = vis[s] + 1; dfs(e); } } } int32_t main() { #ifdef FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); #endif // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n; forl(i, 0, n - 1) { int x, y; cin >> x >> y; g[x].pb(y); g[y].pb(x); } dfs(1); int mx = 0; forl(i, 1, n + 1) { if (vis[i] > vis[mx]) mx = i; } memset(vis, 0, sizeof vis); dfs(mx); mx = 0; forl(i, 1, n + 1) { if (vis[i] > vis[mx]) mx = i; } int diam = vis[mx]; if (diam % 3 == 2) cout << "Second\n"; else cout << "First\n"; return 0; }
#include <bits/stdc++.h> #define FASTIO #define endl \ "\n" // Since endl forces a buffer flush which slows down large I/O // operations. #define ff first #define ss second #define pb push_back #define input(c, n) \ for (int i = 0; i < n; i++) \ cin >> c[i]; #define exit_message(s) return 0 * printf(s) #define show(c) \ for (auto i : c) \ cout << i << " "; \ cout << "\n"; #define display(c, n) \ for (int i = 0; i < n; i++) \ cout << c[i] << " "; \ cout << "\n"; #define forl(i, a, n) for (int i = a; i < n; i++) #define dugbe(k) cout << "-\t> " << #k << " = " << k << "\n"; /* TIPS FOR DEBUGGING * Check if values are not overflowing (use long long where required) * Check if ranges are inclusive or not. * Check properly if greedy will work or not before using it. * Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being * 2 minutes faster will bite you in the ass if you don't make sure * it works on edge cases. * Keep adding to this list. */ using namespace std; typedef long long ll; typedef pair<int, int> pr; typedef pair<ll, ll> prll; typedef vector<int> vi; typedef vector<ll> vill; typedef vector<pr> vpr; ifstream in("input.txt"); const ll mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long true_rand(long long n) { // Returns a random number between 0 and n - 1 inclusive using mt19937. uniform_int_distribution<long long> uid(0, n - 1); return uid(rng); } ll mod_pow(ll a, ll b) { if (b == 0) return 1LL; ll res = mod_pow(a, b >> 1); res = (res * res) % mod; if (b & 1LL) res = (res * a) % mod; return res; } const int N = 2e5 + 5; int n; vi g[N] = {}; int vis[N] = {}; void dfs(int s) { if (!vis[s]) vis[s] = 1; for (auto e : g[s]) { if (!vis[e]) { vis[e] = vis[s] + 1; dfs(e); } } } int32_t main() { #ifdef FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); #endif // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n; forl(i, 0, n - 1) { int x, y; cin >> x >> y; g[x].pb(y); g[y].pb(x); } dfs(1); int mx = 0; forl(i, 1, n + 1) { if (vis[i] > vis[mx]) mx = i; } memset(vis, 0, sizeof vis); dfs(mx); mx = 0; forl(i, 1, n + 1) { if (vis[i] > vis[mx]) mx = i; } int diam = vis[mx]; if (diam % 3 == 2) cout << "Second\n"; else cout << "First\n"; return 0; }
replace
63
64
63
64
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < n; i++) #define PER(i, n) for (int i = n - 1; i >= 0; i--) #define FOR(i, l, r) for (int i = l; i <= r; i++) #define ROF(i, l, r) for (int i = r; i >= l; i--) #define DEBUG(x) cout << #x << "=" << x << endl; #define SHOW1(A, n) \ { REP(i, n) cout << A[i] << (i == n - 1 ? '\n' : ' '); } #define SHOW2(A, m, n) \ { REP(j, m) SHOW1(A[j], n) } #define pb push_back #define fi first #define se second #define ALL(x) x.begin(), x.end() #define SZ(x) (int)((x).size()) typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> pii; typedef pair<LL, LL> pll; const int INF = 0x3f3f3f3f, MOD = 998244353; const double PI = acos(-1), EPS = 1e-15; const int MAXN = 2e3 + 9, MAXM = 2e5 + 9; int to[MAXN], f[MAXN], nxt[MAXN], tot; void init() { tot = 0; memset(f, -1, sizeof(f)); } void add(int u, int v) { to[tot] = v; nxt[tot] = f[u]; f[u] = tot++; } int n, vis[MAXN]; pii bfs(int s) { queue<pii> Q; Q.push({s, 1}); memset(vis, 0, sizeof(vis)); vis[s] = 1; pii now; while (!Q.empty()) { now = Q.front(); Q.pop(); int u = now.fi, t = now.se; for (int i = f[u]; ~i; i = nxt[i]) { int v = to[i]; if (!vis[v]) { vis[v] = 1; Q.push({v, t + 1}); } } } return now; } int main() { #ifdef LOCAL // freopen("i.txt", "r", stdin); // freopen("o.txt", "w", stdout); #endif // LOCAL while (scanf("%d", &n) == 1) { if (n == 1) { puts("First"); continue; } init(); REP(i, n - 1) { int x, y; scanf("%d%d", &x, &y); add(x, y); add(y, x); } pii now = bfs(1); now = bfs(now.fi); int len = now.se; if (len % 3 == 2) puts("Second"); else puts("First"); } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < n; i++) #define PER(i, n) for (int i = n - 1; i >= 0; i--) #define FOR(i, l, r) for (int i = l; i <= r; i++) #define ROF(i, l, r) for (int i = r; i >= l; i--) #define DEBUG(x) cout << #x << "=" << x << endl; #define SHOW1(A, n) \ { REP(i, n) cout << A[i] << (i == n - 1 ? '\n' : ' '); } #define SHOW2(A, m, n) \ { REP(j, m) SHOW1(A[j], n) } #define pb push_back #define fi first #define se second #define ALL(x) x.begin(), x.end() #define SZ(x) (int)((x).size()) typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> pii; typedef pair<LL, LL> pll; const int INF = 0x3f3f3f3f, MOD = 998244353; const double PI = acos(-1), EPS = 1e-15; const int MAXN = 4e5 + 9, MAXM = 2e5 + 9; int to[MAXN], f[MAXN], nxt[MAXN], tot; void init() { tot = 0; memset(f, -1, sizeof(f)); } void add(int u, int v) { to[tot] = v; nxt[tot] = f[u]; f[u] = tot++; } int n, vis[MAXN]; pii bfs(int s) { queue<pii> Q; Q.push({s, 1}); memset(vis, 0, sizeof(vis)); vis[s] = 1; pii now; while (!Q.empty()) { now = Q.front(); Q.pop(); int u = now.fi, t = now.se; for (int i = f[u]; ~i; i = nxt[i]) { int v = to[i]; if (!vis[v]) { vis[v] = 1; Q.push({v, t + 1}); } } } return now; } int main() { #ifdef LOCAL // freopen("i.txt", "r", stdin); // freopen("o.txt", "w", stdout); #endif // LOCAL while (scanf("%d", &n) == 1) { if (n == 1) { puts("First"); continue; } init(); REP(i, n - 1) { int x, y; scanf("%d%d", &x, &y); add(x, y); add(y, x); } pii now = bfs(1); now = bfs(now.fi); int len = now.se; if (len % 3 == 2) puts("Second"); else puts("First"); } return 0; }
replace
23
24
23
24
0
p03055
C++
Runtime Error
/* Author: QAQAutomaton Lang: C++ Code: C.cpp Mail: lk@qaq-am.com Blog: https://www.qaq-am.com/ */ #include <bits/stdc++.h> #define debug(...) fprintf(stderr, __VA_ARGS__) #define DEBUG printf("Passing [%s] in LINE %d\n", __FUNCTION__, __LINE__) #define Debug debug("Passing [%s] in LINE %d\n", __FUNCTION__, __LINE__) #define all(x) x.begin(), x.end() #define x first #define y second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef complex<double> cp; typedef pair<int, int> pii; int inf; const double eps = 1e-8; const double pi = acos(-1.0); template <class T, class T2> int chkmin(T &a, T2 b) { return a > b ? a = b, 1 : 0; } template <class T, class T2> int chkmax(T &a, T2 b) { return a < b ? a = b, 1 : 0; } template <class T> T sqr(T a) { return a * a; } template <class T, class T2> T mmin(T a, T2 b) { return a < b ? a : b; } template <class T, class T2> T mmax(T a, T2 b) { return a > b ? a : b; } template <class T> T aabs(T a) { return a < 0 ? -a : a; } template <class T> int dcmp(T a, T b) { return a > b; } template <int *a> int cmp_a(int x, int y) { return a[x] < a[y]; } #define min mmin #define max mmax #define abs aabs struct __INIT__ { __INIT__() { memset(&inf, 0x3f, sizeof(inf)); } } __INIT___; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; // getchar #define gc() \ (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), \ (iS == iT ? EOF : *iS++)) \ : *iS++) // print the remaining part inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } // putchar inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); } template <typename A> inline bool read(A &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1; else if (c == EOF) return 0; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; return 1; } inline bool read(char &x) { while ((x = gc()) == ' ' || x == '\n' || x == '\r') ; return x != EOF; } inline bool read(char *x) { while ((*x = gc()) == '\n' || *x == ' ' || *x == '\r') ; if (*x == EOF) return 0; while (!(*x == '\n' || *x == ' ' || *x == '\r' || *x == EOF)) *(++x) = gc(); *x = 0; return 1; } template <typename A, typename... B> inline bool read(A &x, B &...y) { return read(x) && read(y...); } template <typename A> inline bool write(A x) { if (!x) putc('0'); if (x < 0) putc('-'), x = -x; while (x) qu[++qr] = x % 10 + '0', x /= 10; while (qr) putc(qu[qr--]); return 0; } inline bool write(char x) { putc(x); return 0; } inline bool write(const char *x) { while (*x) { putc(*x); ++x; } return 0; } inline bool write(char *x) { while (*x) { putc(*x); ++x; } return 0; } template <typename A, typename... B> inline bool write(A x, B... y) { return write(x) || write(y...); } // no need to call flush at the end manually! struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io ::putc; using io ::read; using io ::write; vector<int> to[100005]; int q[100005], d[100005], *l, *r; int f[100005]; int calc(int n) { f[1] = 1; f[2] = 0; for (int i = 3; i <= n; ++i) { f[i] = !(f[i - 1] && f[i - 2]); } return f[n]; } signed main() { #ifdef QAQAutoMaton freopen("C.in", "r", stdin); freopen("C.out", "w", stdout); #endif int n, u, v; read(n); for (int i = 1; i < n; ++i) { read(u, v); to[u].push_back(v); to[v].push_back(u); } d[1] = 1; *(l = r = q) = 1; while (l <= r) { for (auto i : to[*l]) if (!d[i]) { d[i] = d[*l] + 1; *(++r) = i; } ++l; } int x = *r; for (int i = 1; i <= n; ++i) d[i] = 0; d[x] = 1; *(l = r = q) = x; while (l <= r) { for (auto i : to[*l]) if (!d[i]) { d[i] = d[*l] + 1; *(++r) = i; } ++l; } write(calc(d[*r]) ? "First\n" : "Second\n"); return 0; }
/* Author: QAQAutomaton Lang: C++ Code: C.cpp Mail: lk@qaq-am.com Blog: https://www.qaq-am.com/ */ #include <bits/stdc++.h> #define debug(...) fprintf(stderr, __VA_ARGS__) #define DEBUG printf("Passing [%s] in LINE %d\n", __FUNCTION__, __LINE__) #define Debug debug("Passing [%s] in LINE %d\n", __FUNCTION__, __LINE__) #define all(x) x.begin(), x.end() #define x first #define y second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef complex<double> cp; typedef pair<int, int> pii; int inf; const double eps = 1e-8; const double pi = acos(-1.0); template <class T, class T2> int chkmin(T &a, T2 b) { return a > b ? a = b, 1 : 0; } template <class T, class T2> int chkmax(T &a, T2 b) { return a < b ? a = b, 1 : 0; } template <class T> T sqr(T a) { return a * a; } template <class T, class T2> T mmin(T a, T2 b) { return a < b ? a : b; } template <class T, class T2> T mmax(T a, T2 b) { return a > b ? a : b; } template <class T> T aabs(T a) { return a < 0 ? -a : a; } template <class T> int dcmp(T a, T b) { return a > b; } template <int *a> int cmp_a(int x, int y) { return a[x] < a[y]; } #define min mmin #define max mmax #define abs aabs struct __INIT__ { __INIT__() { memset(&inf, 0x3f, sizeof(inf)); } } __INIT___; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; // getchar #define gc() \ (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), \ (iS == iT ? EOF : *iS++)) \ : *iS++) // print the remaining part inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } // putchar inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); } template <typename A> inline bool read(A &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1; else if (c == EOF) return 0; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; return 1; } inline bool read(char &x) { while ((x = gc()) == ' ' || x == '\n' || x == '\r') ; return x != EOF; } inline bool read(char *x) { while ((*x = gc()) == '\n' || *x == ' ' || *x == '\r') ; if (*x == EOF) return 0; while (!(*x == '\n' || *x == ' ' || *x == '\r' || *x == EOF)) *(++x) = gc(); *x = 0; return 1; } template <typename A, typename... B> inline bool read(A &x, B &...y) { return read(x) && read(y...); } template <typename A> inline bool write(A x) { if (!x) putc('0'); if (x < 0) putc('-'), x = -x; while (x) qu[++qr] = x % 10 + '0', x /= 10; while (qr) putc(qu[qr--]); return 0; } inline bool write(char x) { putc(x); return 0; } inline bool write(const char *x) { while (*x) { putc(*x); ++x; } return 0; } inline bool write(char *x) { while (*x) { putc(*x); ++x; } return 0; } template <typename A, typename... B> inline bool write(A x, B... y) { return write(x) || write(y...); } // no need to call flush at the end manually! struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io ::putc; using io ::read; using io ::write; vector<int> to[200005]; int q[200005], d[200005], *l, *r; int f[200005]; int calc(int n) { f[1] = 1; f[2] = 0; for (int i = 3; i <= n; ++i) { f[i] = !(f[i - 1] && f[i - 2]); } return f[n]; } signed main() { #ifdef QAQAutoMaton freopen("C.in", "r", stdin); freopen("C.out", "w", stdout); #endif int n, u, v; read(n); for (int i = 1; i < n; ++i) { read(u, v); to[u].push_back(v); to[v].push_back(u); } d[1] = 1; *(l = r = q) = 1; while (l <= r) { for (auto i : to[*l]) if (!d[i]) { d[i] = d[*l] + 1; *(++r) = i; } ++l; } int x = *r; for (int i = 1; i <= n; ++i) d[i] = 0; d[x] = 1; *(l = r = q) = x; while (l <= r) { for (auto i : to[*l]) if (!d[i]) { d[i] = d[*l] + 1; *(++r) = i; } ++l; } write(calc(d[*r]) ? "First\n" : "Second\n"); return 0; }
replace
130
133
130
133
0
p03055
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; vector<vector<int>> edge; void dfs(int idx, int y, int d, vector<int> &vv) { if (vv[idx] >= 0) return; vv[idx] = d; for (int x : edge[idx]) { if (x == y) continue; dfs(x, idx, d + 1, vv); } } int main() { int N; cin >> N; edge = vector<vector<int>>(N); for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; a--, b--; edge[a].push_back(b); edge[b].push_back(a); } vector<int> vv(N, -1); dfs(0, -1, 0, vv); int idx = 0; for (int i = 0; i < N; i++) { if (vv[idx] < vv[i]) { idx = i; } } vv = vector<int>(N, -1); dfs(idx, -1, 0, vv); int mx = 0; for (int i = 0; i < N; i++) { mx = max(mx, vv[i]); } cout << (mx % 3 == 1 ? "Second" : "First") << endl; }
#include <iostream> #include <vector> using namespace std; vector<vector<int>> edge; void dfs(int idx, int y, int d, vector<int> &vv) { if (vv[idx] >= 0) return; vv[idx] = d; for (int x : edge[idx]) { if (x == y) continue; dfs(x, idx, d + 1, vv); } } int main() { int N; cin >> N; edge = vector<vector<int>>(N); for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; a--, b--; edge[a].push_back(b); edge[b].push_back(a); } vector<int> vv(N, -1); dfs(0, -1, 0, vv); int idx = 0; for (int i = 0; i < N; i++) { if (vv[idx] < vv[i]) { idx = i; } } vv = vector<int>(N, -1); dfs(idx, -1, 0, vv); int mx = 0; for (int i = 0; i < N; i++) { mx = max(mx, vv[i]); } cout << (mx % 3 == 1 ? "Second" : "First") << endl; }
replace
21
22
21
22
0
p03055
C++
Runtime Error
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; using ll = long long; using lpair = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define printa(x, n) \ for (ll i = 0; i < n; i++) { \ cout << (x[i]) << " \n"[i == n - 1]; \ }; void print() {} template <class H, class... T> void print(H &&h, T &&...t) { cout << h << " \n"[sizeof...(t) == 0]; print(forward<T>(t)...); } ll dist[200010] = {}; vector<ll> tree[200010]; void dfs(ll cur, ll par, ll d) { dist[cur] = d; for (auto &e : tree[cur]) { if (e == par) continue; dfs(e, cur, d + 1); } } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; ll a[200010], b[200010]; rep(i, 0, N - 1) { cin >> a[i] >> b[i]; a[i]--; b[i]--; tree[a[i]].push_back(b[i]); tree[b[i]].push_back(a[i]); } dfs(0, -1, 0); ll max_val = 0, max_pos = -1; rep(i, 0, N) { if (max_val < dist[i]) { max_val = dist[i]; max_pos = i; } } rep(i, 0, N) dist[i] = 0; dfs(max_pos, -1, 0); max_val = 0; rep(i, 0, N) max_val = max(max_val, dist[i]); if (max_val % 3 == 1) { print("Second"); } else { print("First"); } }
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; using ll = long long; using lpair = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = 1e18; #define rep(i, m, n) for (ll i = (m); i < (n); i++) #define rrep(i, m, n) for (ll i = (m); i >= (n); i--) #define printa(x, n) \ for (ll i = 0; i < n; i++) { \ cout << (x[i]) << " \n"[i == n - 1]; \ }; void print() {} template <class H, class... T> void print(H &&h, T &&...t) { cout << h << " \n"[sizeof...(t) == 0]; print(forward<T>(t)...); } ll dist[200010] = {}; vector<ll> tree[200010]; void dfs(ll cur, ll par, ll d) { dist[cur] = d; for (auto &e : tree[cur]) { if (e == par) continue; dfs(e, cur, d + 1); } } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; if (N == 1) { print("First"); return 0; } ll a[200010], b[200010]; rep(i, 0, N - 1) { cin >> a[i] >> b[i]; a[i]--; b[i]--; tree[a[i]].push_back(b[i]); tree[b[i]].push_back(a[i]); } dfs(0, -1, 0); ll max_val = 0, max_pos = -1; rep(i, 0, N) { if (max_val < dist[i]) { max_val = dist[i]; max_pos = i; } } rep(i, 0, N) dist[i] = 0; dfs(max_pos, -1, 0); max_val = 0; rep(i, 0, N) max_val = max(max_val, dist[i]); if (max_val % 3 == 1) { print("Second"); } else { print("First"); } }
insert
34
34
34
39
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #define int long long // #define double long double #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rep1(i, n) for (int i = 1; i < (int)(n); ++i) #define repeq(i, n) for (int i = 0; i <= (int)(n); ++i) #define rep1eq(i, n) for (int i = 1; i <= (int)(n); ++i) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define rrep1(i, n) for (int i = (int)(n)-1; i > 0; --i) #define rrepeq(i, n) for (int i = (int)(n); i >= 0; --i) #define rrep1eq(i, n) for (int i = (int)(n); i > 0; --i) #define REP(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) #define RREP(i, a, b) for (int i = (int)(a); i >= (int)(b); --i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() using ll = long long; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; template <typename T> using Graph = vector<vector<T>>; template <typename T> using Spacial = vector<vector<vector<T>>>; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1e9 + 7; const int MOD2 = 998244353; // const double EPS = 1e-9; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; string interval[2] = {" ", "\n"}; // {" ", "\n"} template <typename T> struct is_plural : false_type {}; template <typename T1, typename T2> struct is_plural<pair<T1, T2>> : true_type {}; template <typename T> struct is_plural<vector<T>> : true_type {}; template <typename T> struct is_plural<complex<T>> : true_type {}; template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << " " << p.second; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto itr = vec.begin(); itr != vec.end(); ++itr) is >> *itr; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { if (vec.empty()) return os; bool pl = is_plural<T>(); os << vec.front(); for (auto itr = ++vec.begin(); itr != vec.end(); ++itr) os << interval[pl] << *itr; return os; } bool CoutYN(bool a, string y = "Yes", string n = "No") { cout << (a ? y : n) << "\n"; return a; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } long long modpow(int a, long long n, int mod = MOD) { long long ret = 1; do { if (n & 1) ret = ret * a % mod; a = 1LL * a * a % mod; } while (n >>= 1); return ret; } template <typename T> T GCD(T a, T b) { return b ? GCD(b, a % b) : a; } template <typename T> T LCM(T a, T b) { return a / GCD(a, b) * b; } template <typename T1, typename T2> bool CompareBySecond(pair<T1, T2> a, pair<T1, T2> b) { return a.second != b.second ? a.second < b.second : a.first < b.first; } template <typename T1, typename T2> bool CompareByInverse(pair<T1, T2> a, pair<T1, T2> b) { return a.first != b.first ? a.first < b.first : a.second > b.second; } /* -------- <templates end> -------- */ void solve() { int n; cin >> n; Graph<int> g(n); rep1(_, n) { int a, b; cin >> a >> b; --a, --b; g[a].emplace_back(b); g[b].emplace_back(a); } int s, t; int MAX = 0; auto dfs = [&](auto dfs, int v, int p, int d, int &x) -> void { if (chmax(MAX, d)) x = v; for (auto &nv : g[v]) { if (nv != p) { dfs(dfs, nv, v, d + 1, x); } } }; dfs(dfs, 0, -1, 0, s); MAX = 0; dfs(dfs, s, -1, 0, t); CoutYN(MAX % 3 == 1, "Second", "First"); } /* -------- <programs end> -------- */ signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; // #define int long long // #define double long double #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rep1(i, n) for (int i = 1; i < (int)(n); ++i) #define repeq(i, n) for (int i = 0; i <= (int)(n); ++i) #define rep1eq(i, n) for (int i = 1; i <= (int)(n); ++i) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define rrep1(i, n) for (int i = (int)(n)-1; i > 0; --i) #define rrepeq(i, n) for (int i = (int)(n); i >= 0; --i) #define rrep1eq(i, n) for (int i = (int)(n); i > 0; --i) #define REP(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) #define RREP(i, a, b) for (int i = (int)(a); i >= (int)(b); --i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() using ll = long long; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; template <typename T> using Graph = vector<vector<T>>; template <typename T> using Spacial = vector<vector<vector<T>>>; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1e9 + 7; const int MOD2 = 998244353; // const double EPS = 1e-9; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; string interval[2] = {" ", "\n"}; // {" ", "\n"} template <typename T> struct is_plural : false_type {}; template <typename T1, typename T2> struct is_plural<pair<T1, T2>> : true_type {}; template <typename T> struct is_plural<vector<T>> : true_type {}; template <typename T> struct is_plural<complex<T>> : true_type {}; template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << " " << p.second; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto itr = vec.begin(); itr != vec.end(); ++itr) is >> *itr; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { if (vec.empty()) return os; bool pl = is_plural<T>(); os << vec.front(); for (auto itr = ++vec.begin(); itr != vec.end(); ++itr) os << interval[pl] << *itr; return os; } bool CoutYN(bool a, string y = "Yes", string n = "No") { cout << (a ? y : n) << "\n"; return a; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } long long modpow(int a, long long n, int mod = MOD) { long long ret = 1; do { if (n & 1) ret = ret * a % mod; a = 1LL * a * a % mod; } while (n >>= 1); return ret; } template <typename T> T GCD(T a, T b) { return b ? GCD(b, a % b) : a; } template <typename T> T LCM(T a, T b) { return a / GCD(a, b) * b; } template <typename T1, typename T2> bool CompareBySecond(pair<T1, T2> a, pair<T1, T2> b) { return a.second != b.second ? a.second < b.second : a.first < b.first; } template <typename T1, typename T2> bool CompareByInverse(pair<T1, T2> a, pair<T1, T2> b) { return a.first != b.first ? a.first < b.first : a.second > b.second; } /* -------- <templates end> -------- */ void solve() { int n; cin >> n; if (n == 1) { cout << "First\n"; return; } Graph<int> g(n); rep1(_, n) { int a, b; cin >> a >> b; --a, --b; g[a].emplace_back(b); g[b].emplace_back(a); } int s, t; int MAX = 0; auto dfs = [&](auto dfs, int v, int p, int d, int &x) -> void { if (chmax(MAX, d)) x = v; for (auto &nv : g[v]) { if (nv != p) { dfs(dfs, nv, v, d + 1, x); } } }; dfs(dfs, 0, -1, 0, s); MAX = 0; dfs(dfs, s, -1, 0, t); CoutYN(MAX % 3 == 1, "Second", "First"); } /* -------- <programs end> -------- */ signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); solve(); return 0; }
insert
99
99
99
105
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> #define f first #define s second #define MOD 1000000007 using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, ll> plii; typedef pair<int, pii> piii; const int INF = 1e9 + 10; const ll LINF = 1LL * INF * INF; const int MAXN = 1e5 + 10; const int MAXM = 3e3 + 10; priority_queue<int> pq; vector<vector<int>> graph; int A[MAXN]; char S[MAXN]; int depth[MAXN][3]; void dfs(int here, int num) { int there; for (int i = 0; i < graph[here].size(); i++) { there = graph[here][i]; if (depth[there][num] != -1) continue; depth[there][num] = depth[here][num] + 1; dfs(there, num); } return; } int getlength(int n) { for (int i = 1; i <= n; i++) depth[i][0] = depth[i][1] = -1; depth[1][0] = 0; dfs(1, 0); int idx = 1; int mx = 0; for (int i = 1; i <= n; i++) { if (mx < depth[i][0]) { mx = depth[i][0]; idx = i; } } depth[idx][1] = 0; dfs(idx, 1); for (int i = 1; i <= n; i++) mx = max(mx, depth[i][1]); return mx; } int main() { int n, m, k, a, b, x, y; int sum = 0; int cnt = 0; int mx = 0; int mn = INF; scanf("%d", &n); graph = vector<vector<int>>(n + 1); for (int i = 1; i < n; i++) { scanf("%d%d", &x, &y); graph[x].push_back(y); graph[y].push_back(x); } int len = getlength(n); if (len % 3 == 1) printf("Second"); else printf("First"); return 0; }
#include <bits/stdc++.h> #define f first #define s second #define MOD 1000000007 using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, ll> plii; typedef pair<int, pii> piii; const int INF = 1e9 + 10; const ll LINF = 1LL * INF * INF; const int MAXN = 2e5 + 10; const int MAXM = 3e3 + 10; priority_queue<int> pq; vector<vector<int>> graph; int A[MAXN]; char S[MAXN]; int depth[MAXN][3]; void dfs(int here, int num) { int there; for (int i = 0; i < graph[here].size(); i++) { there = graph[here][i]; if (depth[there][num] != -1) continue; depth[there][num] = depth[here][num] + 1; dfs(there, num); } return; } int getlength(int n) { for (int i = 1; i <= n; i++) depth[i][0] = depth[i][1] = -1; depth[1][0] = 0; dfs(1, 0); int idx = 1; int mx = 0; for (int i = 1; i <= n; i++) { if (mx < depth[i][0]) { mx = depth[i][0]; idx = i; } } depth[idx][1] = 0; dfs(idx, 1); for (int i = 1; i <= n; i++) mx = max(mx, depth[i][1]); return mx; } int main() { int n, m, k, a, b, x, y; int sum = 0; int cnt = 0; int mx = 0; int mn = INF; scanf("%d", &n); graph = vector<vector<int>>(n + 1); for (int i = 1; i < n; i++) { scanf("%d%d", &x, &y); graph[x].push_back(y); graph[y].push_back(x); } int len = getlength(n); if (len % 3 == 1) printf("Second"); else printf("First"); return 0; }
replace
12
13
12
13
0
p03055
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; const int N = 1e5 + 5; struct edge { int u, v, next; } e[N * 2]; int head[N], cnt; int dep[N], x, y; int n, m; void add_edge(int u, int v) { e[++cnt] = (edge){u, v, head[u]}; head[u] = cnt; } void dfs(int u, int fa) { dep[u] = dep[fa] + 1; if (dep[u] > dep[x]) x = u; for (int i = head[u]; i; i = e[i].next) if (e[i].v != fa) dfs(e[i].v, u); } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); add_edge(u, v); add_edge(v, u); } dfs(1, 0); memset(dep, 0, sizeof dep); dfs(x, 0); if (dep[x] < 3) puts(dep[x] == 1 ? "First" : "Second"); else puts(dep[x] % 3 == 2 ? "Second" : "First"); }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; const int N = 2e5 + 5; struct edge { int u, v, next; } e[N * 2]; int head[N], cnt; int dep[N], x, y; int n, m; void add_edge(int u, int v) { e[++cnt] = (edge){u, v, head[u]}; head[u] = cnt; } void dfs(int u, int fa) { dep[u] = dep[fa] + 1; if (dep[u] > dep[x]) x = u; for (int i = head[u]; i; i = e[i].next) if (e[i].v != fa) dfs(e[i].v, u); } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); add_edge(u, v); add_edge(v, u); } dfs(1, 0); memset(dep, 0, sizeof dep); dfs(x, 0); if (dep[x] < 3) puts(dep[x] == 1 ? "First" : "Second"); else puts(dep[x] % 3 == 2 ? "Second" : "First"); }
replace
7
8
7
8
0
p03055
C++
Runtime Error
#include <algorithm> // minmax, sort, swap #include <climits> // INT_MIN, LLONG_MIN #include <cmath> // long, trig, pow #include <cstdio> // printf, scanf #include <deque> // deque #include <functional> // std::function<void(int)> #include <iomanip> // cout<<setprecision(n) #include <iostream> // cin, cout, cerr, clog #include <map> // key-value pairs sorted by keys #include <numeric> // iota, accumulate, inner_product #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <string> // string, stoi, to_string #include <unordered_map> // hashed by keys #include <unordered_set> // hashed by keys #include <vector> // vector #define rep(i, n) for (int i = 0; i < (n); i++) #define ENDL '\n' #define print(i) std::cout << (i) << '\n' #define int long long // at least int64 > 9*10^18 #define all(v) (v).begin(), (v).end() /* libraries */ signed main() { int n; std::cin >> n; std::vector<std::vector<int>> t(n, std::vector<int>()); rep(i, n - 1) { int a, b; std::cin >> a >> b; a--; b--; t[a].emplace_back(b); t[b].emplace_back(a); } int v; // maxi int fmaxd = 0; // first max std::vector<int> went(n, 0); std::function<void(int, int)> dfs = [&](int i, int d) { went[i] = true; if (fmaxd < d) { fmaxd = d; v = i; } for (auto to : t[i]) { if (!went[to]) { dfs(to, d + 1); } } }; dfs(0, 0); rep(i, n) went[i] = false; dfs(v, 0); if (fmaxd % 3 == 1) { print("Second"); } else { print("First"); } return 0; }
#include <algorithm> // minmax, sort, swap #include <climits> // INT_MIN, LLONG_MIN #include <cmath> // long, trig, pow #include <cstdio> // printf, scanf #include <deque> // deque #include <functional> // std::function<void(int)> #include <iomanip> // cout<<setprecision(n) #include <iostream> // cin, cout, cerr, clog #include <map> // key-value pairs sorted by keys #include <numeric> // iota, accumulate, inner_product #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <string> // string, stoi, to_string #include <unordered_map> // hashed by keys #include <unordered_set> // hashed by keys #include <vector> // vector #define rep(i, n) for (int i = 0; i < (n); i++) #define ENDL '\n' #define print(i) std::cout << (i) << '\n' #define int long long // at least int64 > 9*10^18 #define all(v) (v).begin(), (v).end() /* libraries */ signed main() { int n; std::cin >> n; std::vector<std::vector<int>> t(n, std::vector<int>()); rep(i, n - 1) { int a, b; std::cin >> a >> b; a--; b--; t[a].emplace_back(b); t[b].emplace_back(a); } int v = 0; // maxi int fmaxd = 0; // first max std::vector<int> went(n, 0); std::function<void(int, int)> dfs = [&](int i, int d) { went[i] = true; if (fmaxd < d) { fmaxd = d; v = i; } for (auto to : t[i]) { if (!went[to]) { dfs(to, d + 1); } } }; dfs(0, 0); rep(i, n) went[i] = false; dfs(v, 0); if (fmaxd % 3 == 1) { print("Second"); } else { print("First"); } return 0; }
replace
39
40
39
40
0
p03055
C++
Runtime Error
#ifdef __LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; template <typename T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define itn int #define fi first #define se second #define intmax numeric_limits<int>::max() #define llmax numeric_limits<ll>::max() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rrep1(i, n) for (int i = (int)(n); i >= 1; i--) #define all(vec) vec.begin(), vec.end() #define sortt(vec) sort((vec).begin(), (vec).end()) #define rsort(vec) sort((vec).rbegin(), (vec).rend()) typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef tuple<ll, ll, ll> tlll; typedef tuple<int, int, int> tiii; const ll mod = 1e9 + 7; const int inf = 1 << 30; const ll lnf = 1ll << 60; int prevv[100010]; // 頂点の最大数 struct edge { int to, cost; }; vector<ll> dijkstra(int n, vector<vector<edge>> G, int sv) { fill(prevv, prevv + n, -1); priority_queue<pll, vector<pll>, greater<pll>> que; vector<ll> dist(n, lnf); dist[sv] = 0; que.push(pll(0, sv)); while (!que.empty()) { ll cost; int v; tie(cost, v) = que.top(); que.pop(); if (dist[v] < cost) continue; for (int i = 0; i < G[v].size(); i++) { edge e = G[v][i]; ll ec = e.cost; int nv = e.to; if (dist[nv] > dist[v] + ec) { dist[nv] = dist[v] + ec; prevv[nv] = v; que.push(pll(dist[nv], nv)); } } } return dist; } // 頂点への最短路 vector<int> get_path(int t) { vector<int> path(0); for (; t != -1; t = prevv[t]) path.push_back(t); reverse(path.begin(), path.end()); return path; } int main() { itn n; cin >> n; if (n == 1) { cout << "First" << endl; return 0; } vector<vector<edge>> g(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; g[a].push_back({b, 1}); g[b].push_back({a, 1}); } vector<ll> dist1 = dijkstra(n, g, 0); itn v = -1; ll mx = -1; rep(i, n) { if (chmax(mx, dist1[i])) v = i; } vector<ll> dist2 = dijkstra(n, g, v); itn u = -1; mx = -1; rep(i, n) { if (chmax(mx, dist2[i])) u = i; } int dia = mx + 1; if ((dia - 2) % 3 == 0) { cout << "Second" << endl; } else cout << "First" << endl; }
#ifdef __LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; template <typename T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define itn int #define fi first #define se second #define intmax numeric_limits<int>::max() #define llmax numeric_limits<ll>::max() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rrep1(i, n) for (int i = (int)(n); i >= 1; i--) #define all(vec) vec.begin(), vec.end() #define sortt(vec) sort((vec).begin(), (vec).end()) #define rsort(vec) sort((vec).rbegin(), (vec).rend()) typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef tuple<ll, ll, ll> tlll; typedef tuple<int, int, int> tiii; const ll mod = 1e9 + 7; const int inf = 1 << 30; const ll lnf = 1ll << 60; int prevv[200010]; // 頂点の最大数 struct edge { int to, cost; }; vector<ll> dijkstra(int n, vector<vector<edge>> G, int sv) { fill(prevv, prevv + n, -1); priority_queue<pll, vector<pll>, greater<pll>> que; vector<ll> dist(n, lnf); dist[sv] = 0; que.push(pll(0, sv)); while (!que.empty()) { ll cost; int v; tie(cost, v) = que.top(); que.pop(); if (dist[v] < cost) continue; for (int i = 0; i < G[v].size(); i++) { edge e = G[v][i]; ll ec = e.cost; int nv = e.to; if (dist[nv] > dist[v] + ec) { dist[nv] = dist[v] + ec; prevv[nv] = v; que.push(pll(dist[nv], nv)); } } } return dist; } // 頂点への最短路 vector<int> get_path(int t) { vector<int> path(0); for (; t != -1; t = prevv[t]) path.push_back(t); reverse(path.begin(), path.end()); return path; } int main() { itn n; cin >> n; if (n == 1) { cout << "First" << endl; return 0; } vector<vector<edge>> g(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; g[a].push_back({b, 1}); g[b].push_back({a, 1}); } vector<ll> dist1 = dijkstra(n, g, 0); itn v = -1; ll mx = -1; rep(i, n) { if (chmax(mx, dist1[i])) v = i; } vector<ll> dist2 = dijkstra(n, g, v); itn u = -1; mx = -1; rep(i, n) { if (chmax(mx, dist2[i])) u = i; } int dia = mx + 1; if ((dia - 2) % 3 == 0) { cout << "Second" << endl; } else cout << "First" << endl; }
replace
41
42
41
42
0
p03055
C++
Runtime Error
/* ЗАПУСКАЕМ ░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░ ▄███▀░◐░░░▌░░░░░░░ ░░░░▌░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▌░░░░░▐▄▄░░░░░ ░░░░▌░░░░▄▀▒▒▀▀▀▀▄ ░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄ ░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄ ░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄ ░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄ ░░░░░░░░░░░▌▌░▌▌░░░░░ ░░░░░░░░░░░▌▌░▌▌░░░░░ ░░░░░░░░░▄▄▌▌▄▌▌░░░░░ */ #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #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> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chkmax(T1 &a, T2 b) { if (a < b) a = b; } #define files(FILENAME) \ read(FILENAME); \ write(FILENAME) #define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin) #define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout) #define all(c) (c).begin(), (c).end() #define sz(c) (int)(c).size() #define left left228 #define right right228 #define y1 y1228 #define mp make_pair #define pb push_back #define y2 y2228 #define rank rank228 const string FILENAME = "input"; const int MAXN = 200228; int n; vector<int> g[MAXN]; bool used[MAXN]; int dist[MAXN]; void dfs(int u) { used[u] = true; for (auto h : g[u]) { if (!used[h]) { dist[h] = dist[u] + 1; dfs(h); } } } int ans = 0; void dfs1(int u) { used[u] = true; chkmax(ans, dist[u]); for (auto h : g[u]) { if (!used[h]) { dist[h] = dist[u] + 1; dfs1(h); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); read(FILENAME); cin >> n; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--, b--; g[a].pb(b); g[b].pb(a); } memset(used, 0, sizeof(used)); dist[0] = 1; dfs(0); int pos = 0; for (int i = 0; i < n; i++) { if (dist[i] > dist[pos]) { pos = i; } } dist[pos] = 1; memset(used, 0, sizeof(used)); dfs1(pos); if ((ans) % 3 != 2) { cout << "First\n"; } else { cout << "Second\n"; } return 0; }
/* ЗАПУСКАЕМ ░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░ ▄███▀░◐░░░▌░░░░░░░ ░░░░▌░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▐░░░░░▐░░░░░░░ ░░░░▌░░░░░▐▄▄░░░░░ ░░░░▌░░░░▄▀▒▒▀▀▀▀▄ ░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄ ░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄ ░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄ ░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄ ░░░░░░░░░░░▌▌░▌▌░░░░░ ░░░░░░░░░░░▌▌░▌▌░░░░░ ░░░░░░░░░▄▄▌▌▄▌▌░░░░░ */ #include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #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> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chkmax(T1 &a, T2 b) { if (a < b) a = b; } #define files(FILENAME) \ read(FILENAME); \ write(FILENAME) #define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin) #define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout) #define all(c) (c).begin(), (c).end() #define sz(c) (int)(c).size() #define left left228 #define right right228 #define y1 y1228 #define mp make_pair #define pb push_back #define y2 y2228 #define rank rank228 const string FILENAME = "input"; const int MAXN = 200228; int n; vector<int> g[MAXN]; bool used[MAXN]; int dist[MAXN]; void dfs(int u) { used[u] = true; for (auto h : g[u]) { if (!used[h]) { dist[h] = dist[u] + 1; dfs(h); } } } int ans = 0; void dfs1(int u) { used[u] = true; chkmax(ans, dist[u]); for (auto h : g[u]) { if (!used[h]) { dist[h] = dist[u] + 1; dfs1(h); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // read(FILENAME); cin >> n; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--, b--; g[a].pb(b); g[b].pb(a); } memset(used, 0, sizeof(used)); dist[0] = 1; dfs(0); int pos = 0; for (int i = 0; i < n; i++) { if (dist[i] > dist[pos]) { pos = i; } } dist[pos] = 1; memset(used, 0, sizeof(used)); dfs1(pos); if ((ans) % 3 != 2) { cout << "First\n"; } else { cout << "Second\n"; } return 0; }
replace
104
105
104
105
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; // const int MOD=998244353; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<vector<int>> tree(n + 1); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; tree[x].push_back(y); tree[y].push_back(x); } vector<int> d1(n + 1, -1), d2(n + 1, -1); int r = 1, dr = 0; queue<int> q; q.push(1); d1[1] = 0; while (q.size()) { int cur = q.front(); q.pop(); for (int i : tree[cur]) { if (d1[i] == -1) { q.push(i); d1[i] = d1[cur] + 1; if (d1[i] > dr) { dr = d1[i]; r = i; } } } } int diam = 0; q.push(r); d2[r] = 0; while (q.size()) { int cur = q.front(); q.pop(); for (int i : tree[cur]) { if (d2[i] == -1) { q.push(i); d2[i] = d2[cur] + 1; diam = max(diam, d2[i]); } } } diam++; cout << (diam % 3 == 2 ? "Second" : "First"); return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; // const int MOD=998244353; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<vector<int>> tree(n + 1); for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; tree[x].push_back(y); tree[y].push_back(x); } vector<int> d1(n + 1, -1), d2(n + 1, -1); int r = 1, dr = 0; queue<int> q; q.push(1); d1[1] = 0; while (q.size()) { int cur = q.front(); q.pop(); for (int i : tree[cur]) { if (d1[i] == -1) { q.push(i); d1[i] = d1[cur] + 1; if (d1[i] > dr) { dr = d1[i]; r = i; } } } } int diam = 0; q.push(r); d2[r] = 0; while (q.size()) { int cur = q.front(); q.pop(); for (int i : tree[cur]) { if (d2[i] == -1) { q.push(i); d2[i] = d2[cur] + 1; diam = max(diam, d2[i]); } } } diam++; cout << (diam % 3 == 2 ? "Second" : "First"); return 0; }
replace
17
18
17
18
0
p03055
C++
Runtime Error
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) #define REP(i, n) for (int i = 1; i <= n; ++i) #define all(x) begin(x), end(x) #define show(obj) \ { \ for (auto x : obj) \ cout << x << ' '; \ cout << endl; \ } #define line "----------" typedef long long ll; typedef pair<int, int> P; typedef pair<ll, ll> LP; const int inf = 1001001000; const ll INF = 1LL << 60; const int MOD = (int)1e9 + 7; vector<vector<int>> G(200100, vector<int>()); // UnweightedTree // argument -> [Vertex_Size, Start_Node] // return -> [Furthest_Node, Diameter] P tree_diameter(int V, int st) { int t = inf; int diameter = -1; queue<int> Q; vector<int> dist(V, inf); dist[st] = 0; Q.push(st); while (!Q.empty()) { int f = Q.front(); Q.pop(); for (auto x : G[f]) { if (dist[x] == inf) { dist[x] = dist[f] + 1; Q.push(x); if (dist[x] > diameter) { diameter = dist[x]; t = x; } } } } return make_pair(t, diameter); } int main() { int N; cin >> N; int a, b; G.resize(N); rep(i, N - 1) { cin >> a >> b; --a; --b; G[a].push_back(b); G[b].push_back(a); } P x = tree_diameter(N, 0); P y = tree_diameter(N, x.first); int furthest_dist = y.second + 1; cout << ((furthest_dist % 3 == 2) ? "Second" : "First") << endl; return 0; }
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) #define REP(i, n) for (int i = 1; i <= n; ++i) #define all(x) begin(x), end(x) #define show(obj) \ { \ for (auto x : obj) \ cout << x << ' '; \ cout << endl; \ } #define line "----------" typedef long long ll; typedef pair<int, int> P; typedef pair<ll, ll> LP; const int inf = 1001001000; const ll INF = 1LL << 60; const int MOD = (int)1e9 + 7; vector<vector<int>> G(200100, vector<int>()); // UnweightedTree // argument -> [Vertex_Size, Start_Node] // return -> [Furthest_Node, Diameter] P tree_diameter(int V, int st) { int t = st; int diameter = 0; queue<int> Q; vector<int> dist(V, inf); dist[st] = 0; Q.push(st); while (!Q.empty()) { int f = Q.front(); Q.pop(); for (auto x : G[f]) { if (dist[x] == inf) { dist[x] = dist[f] + 1; Q.push(x); if (dist[x] > diameter) { diameter = dist[x]; t = x; } } } } return make_pair(t, diameter); } int main() { int N; cin >> N; int a, b; G.resize(N); rep(i, N - 1) { cin >> a >> b; --a; --b; G[a].push_back(b); G[b].push_back(a); } P x = tree_diameter(N, 0); P y = tree_diameter(N, x.first); int furthest_dist = y.second + 1; cout << ((furthest_dist % 3 == 2) ? "Second" : "First") << endl; return 0; }
replace
38
40
38
40
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; pair<uint64_t, uint64_t> farthest(uint64_t N, map<uint64_t, set<uint64_t>> &edges, uint64_t src) { vector<bool> visited(N, false); stack<pair<uint64_t, uint64_t>> s; s.emplace(src, 0); uint64_t max_len = 0, max_len_node = -1; while (!s.empty()) { pair<uint64_t, uint64_t> p = s.top(); s.pop(); if (visited[p.first]) { continue; } visited[p.first] = true; if (max_len < p.second) { max_len_node = p.first; max_len = p.second; } for (uint64_t next : edges[p.first]) { if (!visited[next]) { s.emplace(next, p.second + 1); } } } return make_pair(max_len_node, max_len); } int main() { uint64_t N; cin >> N; map<uint64_t, set<uint64_t>> edges; for (uint64_t n = 0; n < N; ++n) { edges.emplace(n, set<uint64_t>()); } for (uint64_t n = 0; n < N - 1; ++n) { uint64_t a, b; cin >> a >> b; edges[a - 1].insert(b - 1); edges[b - 1].insert(a - 1); } pair<uint64_t, uint64_t> pu = farthest(N, edges, 0); pair<uint64_t, uint64_t> pv = farthest(N, edges, pu.first); uint64_t diameter = pv.second; cout << ((diameter % 3) != 1 ? "First" : "Second") << endl; return 0; }
#include <bits/stdc++.h> using namespace std; pair<uint64_t, uint64_t> farthest(uint64_t N, map<uint64_t, set<uint64_t>> &edges, uint64_t src) { vector<bool> visited(N, false); stack<pair<uint64_t, uint64_t>> s; s.emplace(src, 0); uint64_t max_len = 0, max_len_node = 0; while (!s.empty()) { pair<uint64_t, uint64_t> p = s.top(); s.pop(); if (visited[p.first]) { continue; } visited[p.first] = true; if (max_len < p.second) { max_len_node = p.first; max_len = p.second; } for (uint64_t next : edges[p.first]) { if (!visited[next]) { s.emplace(next, p.second + 1); } } } return make_pair(max_len_node, max_len); } int main() { uint64_t N; cin >> N; map<uint64_t, set<uint64_t>> edges; for (uint64_t n = 0; n < N; ++n) { edges.emplace(n, set<uint64_t>()); } for (uint64_t n = 0; n < N - 1; ++n) { uint64_t a, b; cin >> a >> b; edges[a - 1].insert(b - 1); edges[b - 1].insert(a - 1); } pair<uint64_t, uint64_t> pu = farthest(N, edges, 0); pair<uint64_t, uint64_t> pv = farthest(N, edges, pu.first); uint64_t diameter = pv.second; cout << ((diameter % 3) != 1 ? "First" : "Second") << endl; return 0; }
replace
11
12
11
12
0
p03055
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) #define pb(e) push_back(e) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define mp(a, b) make_pair((a), (b)) #define mt(a, b, c) make_tuple((a), (b), (c)) typedef long long ll; const int maxV = 111111; vector<int> G[maxV]; // 頂点情報のみのグラフ // treeDFS(親, 現在地, 根から現在地までの距離, 根からの最大の距離, // 根から最大の距離となる頂点 void treeDFS(int from, int current, int dist, int &maxDist, int &maxVertex) { // 距離と終点を更新 if (dist > maxDist) { maxDist = dist; maxVertex = current; } for (auto to : G[current]) { // 逆流を防ぐ if (to == from) continue; treeDFS(current, to, dist + 1, maxDist, maxVertex); } } void getTreeDiameter() { int start = 0, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); start = end, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); // printf("start: %d, end: %d, diameter: %d\n", start, end, maxDist); if (maxDist == 0) cout << "First" << endl; else if (maxDist == 1) cout << "Second" << endl; else if (maxDist % 3 == 1) cout << "Second" << endl; else cout << "First" << endl; } int main() { int n; cin >> n; rep(i, n - 1) { int a, b; cin >> a >> b; G[a - 1].pb(b - 1); G[b - 1].pb(a - 1); } getTreeDiameter(); return 0; }
#include <algorithm> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) #define pb(e) push_back(e) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define mp(a, b) make_pair((a), (b)) #define mt(a, b, c) make_tuple((a), (b), (c)) typedef long long ll; const int maxV = 211111; vector<int> G[maxV]; // 頂点情報のみのグラフ // treeDFS(親, 現在地, 根から現在地までの距離, 根からの最大の距離, // 根から最大の距離となる頂点 void treeDFS(int from, int current, int dist, int &maxDist, int &maxVertex) { // 距離と終点を更新 if (dist > maxDist) { maxDist = dist; maxVertex = current; } for (auto to : G[current]) { // 逆流を防ぐ if (to == from) continue; treeDFS(current, to, dist + 1, maxDist, maxVertex); } } void getTreeDiameter() { int start = 0, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); start = end, end = 0, maxDist = 0; treeDFS(-1, start, 0, maxDist, end); // printf("start: %d, end: %d, diameter: %d\n", start, end, maxDist); if (maxDist == 0) cout << "First" << endl; else if (maxDist == 1) cout << "Second" << endl; else if (maxDist % 3 == 1) cout << "Second" << endl; else cout << "First" << endl; } int main() { int n; cin >> n; rep(i, n - 1) { int a, b; cin >> a >> b; G[a - 1].pb(b - 1); G[b - 1].pb(a - 1); } getTreeDiameter(); return 0; }
replace
30
31
30
31
0
p03055
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cstdio> #include <iostream> #include <iterator> #include <map> #include <set> #include <string> #include <thread> #include <unordered_set> #include <vector> using namespace std; const int N = 1e5 + 10; vector<int> G[N]; pair<int, int> dfs(int v, int p = -1) { pair<int, int> lr = {0, v}; for (auto x : G[v]) { if (x == p) continue; auto res = dfs(x, v); res.first++; lr = max(lr, res); } return lr; } bool res(int x) { if (x % 3 == 1) { return false; } return true; } int main() { srand(time(NULL)); ::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; --a, --b; G[a].push_back(b); G[b].push_back(a); } pair<int, int> lr = dfs(0); lr = dfs(lr.second); if (res(lr.first)) { cout << "First\n"; } else { cout << "Second\n"; } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cstdio> #include <iostream> #include <iterator> #include <map> #include <set> #include <string> #include <thread> #include <unordered_set> #include <vector> using namespace std; const int N = 3e5 + 10; vector<int> G[N]; pair<int, int> dfs(int v, int p = -1) { pair<int, int> lr = {0, v}; for (auto x : G[v]) { if (x == p) continue; auto res = dfs(x, v); res.first++; lr = max(lr, res); } return lr; } bool res(int x) { if (x % 3 == 1) { return false; } return true; } int main() { srand(time(NULL)); ::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; --a, --b; G[a].push_back(b); G[b].push_back(a); } pair<int, int> lr = dfs(0); lr = dfs(lr.second); if (res(lr.first)) { cout << "First\n"; } else { cout << "Second\n"; } return 0; }
replace
16
17
16
17
0
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define ALL(v) (v).begin(), (v).end() using ll = long long; using P = pair<int, int>; const int INF = 1e9; const long long LINF = 1e18; const long long MOD = 1e9 + 7; template <typename T> vector<T> Dijkstra(int s, vector<vector<pair<int, T>>> &G, /*vector<int> & prev,*/ const T INF = 1e9) { using P = pair<T, int>; int V = G.size(); vector<T> dist(V, INF); priority_queue<P, vector<P>, greater<P>> que; dist[s] = 0; que.emplace(0, s); /*prev.assign(V, -1);*/ while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (int i = 0; i < G[v].size(); i++) { int to = G[v][i].first; T cost = G[v][i].second; if (dist[to] > dist[v] + cost) { dist[to] = dist[v] + cost; /*prev[to] = v;*/ que.emplace(dist[to], to); } } } return dist; } signed main() { int n; cin >> n; vector<vector<pair<int, int>>> G(n); int a, b; rep(i, n - 1) { cin >> a >> b; a--; b--; G[a].emplace_back(b, 1); G[b].emplace_back(a, 1); } vector<int> dist = Dijkstra(0, G); int v; int mx = 0; rep(i, n) { if (dist[i] > mx) { mx = dist[i]; v = i; } } vector<int> distv = Dijkstra(v, G); int d = 0; rep(i, n) { d = max(d, distv[i]); } cout << (d % 3 == 1 ? "Second" : "First") << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define ALL(v) (v).begin(), (v).end() using ll = long long; using P = pair<int, int>; const int INF = 1e9; const long long LINF = 1e18; const long long MOD = 1e9 + 7; template <typename T> vector<T> Dijkstra(int s, vector<vector<pair<int, T>>> &G, /*vector<int> & prev,*/ const T INF = 1e9) { using P = pair<T, int>; int V = G.size(); vector<T> dist(V, INF); priority_queue<P, vector<P>, greater<P>> que; dist[s] = 0; que.emplace(0, s); /*prev.assign(V, -1);*/ while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (int i = 0; i < G[v].size(); i++) { int to = G[v][i].first; T cost = G[v][i].second; if (dist[to] > dist[v] + cost) { dist[to] = dist[v] + cost; /*prev[to] = v;*/ que.emplace(dist[to], to); } } } return dist; } signed main() { int n; cin >> n; if (n == 1) { cout << "First" << endl; return 0; } vector<vector<pair<int, int>>> G(n); int a, b; rep(i, n - 1) { cin >> a >> b; a--; b--; G[a].emplace_back(b, 1); G[b].emplace_back(a, 1); } vector<int> dist = Dijkstra(0, G); int v; int mx = 0; rep(i, n) { if (dist[i] > mx) { mx = dist[i]; v = i; } } vector<int> distv = Dijkstra(v, G); int d = 0; rep(i, n) { d = max(d, distv[i]); } cout << (d % 3 == 1 ? "Second" : "First") << endl; return 0; }
insert
42
42
42
46
0
p03055
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; vector<int> edge[200000]; int d[2][200000]; int INF = 1e9; int opposite = 0; int max_dist = 0; void f(int index, int x) { int dx = d[index][x]; if (dx > max_dist) { opposite = x; max_dist = dx; } int s = edge[x].size(); for (int i = 0; i < s; i++) { int y = edge[x][i]; if (d[index][y] == INF) { d[index][y] = dx + 1; f(index, y); } } return; } int main() { for (int i = 0; i < 2; i++) { for (int j = 0; j < 2e5; j++) { d[i][j] = INF; } } int N; cin >> N; for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; edge[a - 1].push_back(b - 1); edge[b - 1].push_back(a - 1); } d[0][0] = 0; f(0, 0); d[1][opposite] = 0; f(1, opposite); string answer = (max_dist % 3 != 1 ? "First" : "Second"); cout << answer << endl; }
#include <iostream> #include <vector> using namespace std; vector<int> edge[200000]; int d[2][200000]; int INF = 1e9; int opposite = 0; int max_dist = 0; void f(int index, int x) { int dx = d[index][x]; if (dx > max_dist) { opposite = x; max_dist = dx; } int s = edge[x].size(); for (int i = 0; i < s; i++) { int y = edge[x][i]; if (d[index][y] == INF) { d[index][y] = dx + 1; f(index, y); } } return; } int main() { for (int i = 0; i < 2; i++) { for (int j = 0; j < 2e5; j++) { d[i][j] = INF; } } int N; cin >> N; if (N == 1) { cout << "First" << endl; return 0; } for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; edge[a - 1].push_back(b - 1); edge[b - 1].push_back(a - 1); } d[0][0] = 0; f(0, 0); d[1][opposite] = 0; f(1, opposite); string answer = (max_dist % 3 != 1 ? "First" : "Second"); cout << answer << endl; }
insert
36
36
36
40
0
p03055
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 = 1 << 30; int n, a, b, x, d[100009]; vector<int> e[100009]; int bfs(int s) { rep(i, n + 1) d[i] = inf; d[s] = 0; queue<int> que; que.push(s); int now; while (!que.empty()) { now = que.front(); que.pop(); rep(i, e[now].size()) { if (d[e[now][i]] != inf) continue; d[e[now][i]] = d[now] + 1; que.push(e[now][i]); } } x = now; return d[now]; } int main() { cin >> n; rep(i, n - 1) { cin >> a >> b; e[a].push_back(b); e[b].push_back(a); } bfs(1); cout << (bfs(x) % 3 != 1 ? "First\n" : "Second\n"); }
#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 = 1 << 30; int n, a, b, x, d[200009]; vector<int> e[200009]; int bfs(int s) { rep(i, n + 1) d[i] = inf; d[s] = 0; queue<int> que; que.push(s); int now; while (!que.empty()) { now = que.front(); que.pop(); rep(i, e[now].size()) { if (d[e[now][i]] != inf) continue; d[e[now][i]] = d[now] + 1; que.push(e[now][i]); } } x = now; return d[now]; } int main() { cin >> n; rep(i, n - 1) { cin >> a >> b; e[a].push_back(b); e[b].push_back(a); } bfs(1); cout << (bfs(x) % 3 != 1 ? "First\n" : "Second\n"); }
replace
6
8
6
8
0
p03055
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; int FindFurthest(vector<vector<int>> &adj_nodes, int start, int *p_furthest) { int N = adj_nodes.size(); vector<bool> visited(N, false); int remaining = N; queue<int> q; q.push(start); visited[start] = true; --remaining; queue<int> nq; int dist = 1; bool done = false; while (remaining > 0) { while (not q.empty()) { int x = q.front(); q.pop(); for (auto nx : adj_nodes[x]) { if (not visited[nx]) { nq.push(nx); visited[nx] = true; --remaining; if (remaining == 0) { *p_furthest = nx; done = true; break; } } } if (done) break; } if (done) break; swap(q, nq); ++dist; } return dist; } int main() { int N; cin >> N; vector<vector<int>> adj_nodes(N); for (int i = 0; i < N - 1; ++i) { int a, b; cin >> a >> b; --a; --b; adj_nodes[a].push_back(b); adj_nodes[b].push_back(a); } int dist; int furthest; dist = FindFurthest(adj_nodes, 0, &furthest); int furthest2; dist = FindFurthest(adj_nodes, furthest, &furthest2); if (dist % 3 == 1) cout << "Second" << endl; else cout << "First" << endl; return 0; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; int FindFurthest(vector<vector<int>> &adj_nodes, int start, int *p_furthest) { int N = adj_nodes.size(); vector<bool> visited(N, false); int remaining = N; queue<int> q; q.push(start); visited[start] = true; --remaining; queue<int> nq; int dist = 1; bool done = false; while (remaining > 0) { while (not q.empty()) { int x = q.front(); q.pop(); for (auto nx : adj_nodes[x]) { if (not visited[nx]) { nq.push(nx); visited[nx] = true; --remaining; if (remaining == 0) { *p_furthest = nx; done = true; break; } } } if (done) break; } if (done) break; swap(q, nq); ++dist; } return dist; } int main() { int N; cin >> N; if (N == 1) { cout << "First" << endl; return 0; } vector<vector<int>> adj_nodes(N); for (int i = 0; i < N - 1; ++i) { int a, b; cin >> a >> b; --a; --b; adj_nodes[a].push_back(b); adj_nodes[b].push_back(a); } int dist; int furthest; dist = FindFurthest(adj_nodes, 0, &furthest); int furthest2; dist = FindFurthest(adj_nodes, furthest, &furthest2); if (dist % 3 == 1) cout << "Second" << endl; else cout << "First" << endl; return 0; }
insert
58
58
58
63
0
p03055
C++
Runtime Error
#include <algorithm> #include <iomanip> // std::setw(int), std::setfill(char) #include <ios> // std::left, std::right #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <string> #include <vector> using namespace std; vector<int> G[200000]; int tree[200000] = {}; void dfs1(int x) { for (int j = 0; j < G[x].size(); j++) { if (tree[G[x][j]] < 0) { tree[G[x][j]] = tree[x] + 1; dfs1(G[x][j]); } } return; } int main() { int N; cin >> N; int a, b; for (int i = 0; i < N; ++i) { tree[i] = (-1); cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } tree[0] = 0; dfs1(0); int x, y, xd, yd; x = 0; xd = -1; y = 0; yd = -1; for (int i = 0; i < N; ++i) { if (xd < tree[i]) { xd = tree[i]; x = i; } tree[i] = (-1); } tree[x] = 0; dfs1(x); for (int i = 0; i < N; ++i) { if (yd < tree[i]) { yd = tree[i]; y = i; } } if (yd % 3 == 1) { cout << "Second" << "\n"; } else { cout << "First" << "\n"; } return 0; }
#include <algorithm> #include <iomanip> // std::setw(int), std::setfill(char) #include <ios> // std::left, std::right #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <string> #include <vector> using namespace std; vector<int> G[200000]; int tree[200000] = {}; void dfs1(int x) { for (int j = 0; j < G[x].size(); j++) { if (tree[G[x][j]] < 0) { tree[G[x][j]] = tree[x] + 1; dfs1(G[x][j]); } } return; } int main() { int N; cin >> N; int a, b; for (int i = 0; i < N; ++i) { tree[i] = (-1); if (i < N - 1) { cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } } tree[0] = 0; dfs1(0); int x, y, xd, yd; x = 0; xd = -1; y = 0; yd = -1; for (int i = 0; i < N; ++i) { if (xd < tree[i]) { xd = tree[i]; x = i; } tree[i] = (-1); } tree[x] = 0; dfs1(x); for (int i = 0; i < N; ++i) { if (yd < tree[i]) { yd = tree[i]; y = i; } } if (yd % 3 == 1) { cout << "Second" << "\n"; } else { cout << "First" << "\n"; } return 0; }
replace
33
38
33
40
0
p03055
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define fi first #define se second #define mp make_pair #define rep(i, n) for (int i = 0; i < n; ++i) #define rrep(i, n) for (int i = n; i >= 0; --i) const int inf = 1e9 + 7; const ll mod = 1e9 + 7; const ll mod1 = 998244353; const ll big = 1e18; const double PI = 2 * asin(1); int main() { int N; cin >> N; vector<vector<int>> edge(N); int a, b; for (int i = 0; i < N - 1; ++i) { cin >> a >> b; a--; b--; edge[a].push_back(b); edge[b].push_back(a); } queue<pair<int, int>> que; que.push(mp(0, 0)); pair<int, int> state; map<int, int> amap; for (int i = 0; i < N; ++i) amap[i] = -1; amap[0] = 0; while (!que.empty()) { state = que.front(); que.pop(); for (int i = 0; i < edge[state.fi].size(); ++i) { if (amap[edge[state.fi][i]] > -1) continue; que.push(mp(edge[state.fi][i], state.se + 1)); amap[edge[state.fi][i]] = state.se + 1; } } int ans = 0; int node; for (int i = 0; i < N; ++i) { if (ans < amap[i]) { ans = amap[i]; node = i; } } que.push(mp(node, 0)); for (int i = 0; i < N; ++i) amap[i] = -1; amap[node] = 0; while (!que.empty()) { state = que.front(); que.pop(); for (int i = 0; i < edge[state.fi].size(); ++i) { if (amap[edge[state.fi][i]] > -1) continue; que.push(mp(edge[state.fi][i], state.se + 1)); amap[edge[state.fi][i]] = state.se + 1; } } for (int i = 0; i < N; ++i) { ans = max(ans, amap[i]); } ans++; if (ans % 3 == 2) cout << "Second" << endl; else cout << "First" << endl; }
#include <algorithm> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; #define fi first #define se second #define mp make_pair #define rep(i, n) for (int i = 0; i < n; ++i) #define rrep(i, n) for (int i = n; i >= 0; --i) const int inf = 1e9 + 7; const ll mod = 1e9 + 7; const ll mod1 = 998244353; const ll big = 1e18; const double PI = 2 * asin(1); int main() { int N; cin >> N; if (N == 1) { cout << "First" << endl; return 0; } vector<vector<int>> edge(N); int a, b; for (int i = 0; i < N - 1; ++i) { cin >> a >> b; a--; b--; edge[a].push_back(b); edge[b].push_back(a); } queue<pair<int, int>> que; que.push(mp(0, 0)); pair<int, int> state; map<int, int> amap; for (int i = 0; i < N; ++i) amap[i] = -1; amap[0] = 0; while (!que.empty()) { state = que.front(); que.pop(); for (int i = 0; i < edge[state.fi].size(); ++i) { if (amap[edge[state.fi][i]] > -1) continue; que.push(mp(edge[state.fi][i], state.se + 1)); amap[edge[state.fi][i]] = state.se + 1; } } int ans = 0; int node; for (int i = 0; i < N; ++i) { if (ans < amap[i]) { ans = amap[i]; node = i; } } que.push(mp(node, 0)); for (int i = 0; i < N; ++i) amap[i] = -1; amap[node] = 0; while (!que.empty()) { state = que.front(); que.pop(); for (int i = 0; i < edge[state.fi].size(); ++i) { if (amap[edge[state.fi][i]] > -1) continue; que.push(mp(edge[state.fi][i], state.se + 1)); amap[edge[state.fi][i]] = state.se + 1; } } for (int i = 0; i < N; ++i) { ans = max(ans, amap[i]); } ans++; if (ans % 3 == 2) cout << "Second" << endl; else cout << "First" << endl; }
insert
28
28
28
32
0
p03055
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using VI = vector<int>; using VL = vector<ll>; using VS = vector<string>; template <class T> using PQ = priority_queue<T, vector<T>, greater<T>>; #define FOR(i, a, n) for (int(i) = (a); (i) < (n); ++(i)) #define eFOR(i, a, n) for (int(i) = (a); (i) <= (n); ++(i)) #define rFOR(i, a, n) for (int(i) = (n)-1; (i) >= (a); --(i)) #define erFOR(i, a, n) for (int(i) = (n); (i) >= (a); --(i)) #define each(i, a) for (auto &i : a) #define SORT(i) sort((i).begin(), (i).end()) #define rSORT(i, a) sort((i).begin(), (i).end(), (a)) #define all(i) (i).begin(), (i).end() #define out(y, x) ((y) < 0 || h <= (y) || (x) < 0 || w <= (x)) #define line cout << "-----------------------------\n" #define ENDL(i, n) ((i) == (n)-1 ? "\n" : " ") #define stop system("pause") constexpr ll INF = 1000000000; constexpr ll LLINF = 1LL << 60; constexpr ll mod = 1000000007; constexpr ll MOD = 998244353; constexpr ld eps = 1e-10; constexpr ld pi = 3.1415926535897932; 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; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } template <class T> inline istream &operator>>(istream &is, vector<T> &v) { for (auto &elemnt : v) is >> elemnt; return is; } template <class T> inline istream &operator>>(istream &is, deque<T> &v) { for (auto &elemnt : v) is >> elemnt; return is; } template <class T, class U> inline istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <class T> inline vector<T> vec(size_t a) { return vector<T>(a); } template <class T> inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); } template <class T, class... Ts> inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); } template <class T, class... Ts> inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); } using P = pair<int, int>; P farthest(vector<VI> g, int now, int par) { P ret = {0, now}; each(to, g[now]) { if (to == par) continue; auto a = farthest(g, to, now); ++a.first; chmax(ret, a); } return ret; } VI memo; int solve(int n) { if (n == 0) return 1; if (n == 1) return 0; if (memo[n] != -1) return memo[n]; if (solve(n - 1) == 0 || solve(n - 2) == 0) return memo[n] = 1; else return memo[n] = 0; } int main() { init(); int n; cin >> n; auto g = vec<VI>(n); FOR(i, 0, n - 1) { int a, b; cin >> a >> b; --a, --b; g[a].push_back(b); g[b].push_back(a); } int s = farthest(g, 0, -1).second; int d = farthest(g, s, -1).first; memo.resize(d + 1, -1); cout << (solve(d) == 1 ? "First" : "Second") << "\n"; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using VI = vector<int>; using VL = vector<ll>; using VS = vector<string>; template <class T> using PQ = priority_queue<T, vector<T>, greater<T>>; #define FOR(i, a, n) for (int(i) = (a); (i) < (n); ++(i)) #define eFOR(i, a, n) for (int(i) = (a); (i) <= (n); ++(i)) #define rFOR(i, a, n) for (int(i) = (n)-1; (i) >= (a); --(i)) #define erFOR(i, a, n) for (int(i) = (n); (i) >= (a); --(i)) #define each(i, a) for (auto &i : a) #define SORT(i) sort((i).begin(), (i).end()) #define rSORT(i, a) sort((i).begin(), (i).end(), (a)) #define all(i) (i).begin(), (i).end() #define out(y, x) ((y) < 0 || h <= (y) || (x) < 0 || w <= (x)) #define line cout << "-----------------------------\n" #define ENDL(i, n) ((i) == (n)-1 ? "\n" : " ") #define stop system("pause") constexpr ll INF = 1000000000; constexpr ll LLINF = 1LL << 60; constexpr ll mod = 1000000007; constexpr ll MOD = 998244353; constexpr ld eps = 1e-10; constexpr ld pi = 3.1415926535897932; 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; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } template <class T> inline istream &operator>>(istream &is, vector<T> &v) { for (auto &elemnt : v) is >> elemnt; return is; } template <class T> inline istream &operator>>(istream &is, deque<T> &v) { for (auto &elemnt : v) is >> elemnt; return is; } template <class T, class U> inline istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <class T> inline vector<T> vec(size_t a) { return vector<T>(a); } template <class T> inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); } template <class T, class... Ts> inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); } template <class T, class... Ts> inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); } using P = pair<int, int>; P farthest(vector<VI> &g, int now, int par) { P ret = {0, now}; each(to, g[now]) { if (to == par) continue; auto a = farthest(g, to, now); ++a.first; chmax(ret, a); } return ret; } VI memo; int solve(int n) { if (n == 0) return 1; if (n == 1) return 0; if (memo[n] != -1) return memo[n]; if (solve(n - 1) == 0 || solve(n - 2) == 0) return memo[n] = 1; else return memo[n] = 0; } int main() { init(); int n; cin >> n; auto g = vec<VI>(n); FOR(i, 0, n - 1) { int a, b; cin >> a >> b; --a, --b; g[a].push_back(b); g[b].push_back(a); } int s = farthest(g, 0, -1).second; int d = farthest(g, s, -1).first; memo.resize(d + 1, -1); cout << (solve(d) == 1 ? "First" : "Second") << "\n"; }
replace
73
74
73
74
TLE
p03055
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #include <boost/multiprecision/cpp_int.hpp> // using multiInt = boost::multiprecision::cpp_int; using ll = long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; template <typename Q_temp> using smaller_queue = priority_queue<Q_temp, vector<Q_temp>, greater<Q_temp>>; const int INF = (int)1e9; const ll LINF = (ll)4e18; const ll MOD = (ll)(1e9 + 7); const double PI = acos(-1.0); const int limit = 100010; #define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i) #define rep(i, n) REP(i, 0, n) #define MP make_pair #define MT make_tuple #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl #define all(v) v.begin(), v.end() #define NP(v) next_permutation(all(v)) #define dbg(x_) cerr << #x_ << ":" << x_ << endl; #define dbg2(x_) \ for (auto a_ : x_) \ cerr << a_ << " "; \ cerr << endl; #define dbg3(x_, sx_) \ rep(i, sx_) cerr << x_[i] << " "; \ cerr << endl; vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0}; vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0}; inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; } //------------------------------------------------------ const ll MAX_V = 200010; class dijkstra { public: struct edge { ll to; ll cost; }; vector<edge> E[MAX_V]; ll d[MAX_V]; void calc(ll s) { priority_queue<pll, vector<pll>, greater<pll>> que; rep(i, MAX_V) d[i] = LINF; que.emplace(0LL, s); d[s] = 0LL; while (!que.empty()) { pll p = que.top(); que.pop(); ll v = p.second; if (d[v] < p.first) continue; for (auto &&e : E[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.emplace(d[e.to], e.to); } } } } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; dijkstra ds; rep(i, n) { int a, b; cin >> a >> b; a--, b--; ds.E[a].push_back({b, 1}); ds.E[b].push_back({a, 1}); } ds.calc(0); int v, max_d = -1; rep(i, n) { if (ds.d[i] > max_d) { max_d = ds.d[i]; v = i; } } ds.calc(v); ll diameter = 0; rep(i, n) diameter = max(diameter, ds.d[i]); cout << (diameter % 3 == 1 ? "Second" : "First") << endl; return 0; }
#include <bits/stdc++.h> using namespace std; // #include <boost/multiprecision/cpp_int.hpp> // using multiInt = boost::multiprecision::cpp_int; using ll = long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; template <typename Q_temp> using smaller_queue = priority_queue<Q_temp, vector<Q_temp>, greater<Q_temp>>; const int INF = (int)1e9; const ll LINF = (ll)4e18; const ll MOD = (ll)(1e9 + 7); const double PI = acos(-1.0); const int limit = 100010; #define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i) #define rep(i, n) REP(i, 0, n) #define MP make_pair #define MT make_tuple #define YES(n) cout << ((n) ? "YES" : "NO") << endl #define Yes(n) cout << ((n) ? "Yes" : "No") << endl #define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl #define all(v) v.begin(), v.end() #define NP(v) next_permutation(all(v)) #define dbg(x_) cerr << #x_ << ":" << x_ << endl; #define dbg2(x_) \ for (auto a_ : x_) \ cerr << a_ << " "; \ cerr << endl; #define dbg3(x_, sx_) \ rep(i, sx_) cerr << x_[i] << " "; \ cerr << endl; vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0}; vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0}; inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; } //------------------------------------------------------ const ll MAX_V = 200010; class dijkstra { public: struct edge { ll to; ll cost; }; vector<edge> E[MAX_V]; ll d[MAX_V]; void calc(ll s) { priority_queue<pll, vector<pll>, greater<pll>> que; rep(i, MAX_V) d[i] = LINF; que.emplace(0LL, s); d[s] = 0LL; while (!que.empty()) { pll p = que.top(); que.pop(); ll v = p.second; if (d[v] < p.first) continue; for (auto &&e : E[v]) { if (d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; que.emplace(d[e.to], e.to); } } } } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; dijkstra ds; rep(i, n - 1) { int a, b; cin >> a >> b; a--, b--; ds.E[a].push_back({b, 1}); ds.E[b].push_back({a, 1}); } ds.calc(0); int v, max_d = -1; rep(i, n) { if (ds.d[i] > max_d) { max_d = ds.d[i]; v = i; } } ds.calc(v); ll diameter = 0; rep(i, n) diameter = max(diameter, ds.d[i]); cout << (diameter % 3 == 1 ? "Second" : "First") << endl; return 0; }
replace
82
83
82
83
0