text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; vector<vector<long long> > adj(123456); long long w[123456], a[123456], b[123456]; long long iinf; int add(long long &a, long long &b, long long &c) { long long val; if (iinf / c > abs(b)) { val = b * c; } else { cout << "NO" << endl; exit(0); } a += val; if (abs(a) > iinf) { cout << "NO" << endl; exit(0); } return 0; } int dfs(int cur, int par) { int i, flag = 0; for (i = 0; i < adj[cur].size(); i++) { if (adj[cur][i] != par) { dfs(adj[cur][i], cur); flag = 1; } } b[cur] -= a[cur]; if (par == -1) { if (b[cur] < 0) { cout << "NO" << endl; } else { cout << "YES" << endl; } exit(0); } else { if (b[cur] >= 0) { b[par] += b[cur]; } else { add(b[par], b[cur], w[cur]); } } return 0; } int main() { std::ios::sync_with_stdio(false); int n, dep; cin >> n; iinf = (1000 * 1000 * 1000 + 5); iinf *= (1000 * 1000 * 1000 + 5); int i; for (i = 0; i < n; i++) { cin >> b[i]; } for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 1; i < n; i++) { cin >> dep >> w[i]; dep--; adj[dep].push_back(i); adj[i].push_back(dep); } dfs(0, -1); return 0; }
#include <bits/stdc++.h> using namespace std; void ga(int N, int *A) { for (int i(0); i < N; i++) scanf("%d", A + i); } vector<pair<int, int> > g[100005]; long long A[100005], B[100005]; int N, a, b; long long dfs(int u) { long long S = A[u] - B[u], a; for (auto &h : g[u]) { a = dfs(h.first); if (a >= 0) S += a; else if (-a * 1. * h.second > 2e17) puts("NO"), exit(0); else S += a * h.second; if (S > 2e17) puts("NO"), exit(0); } return S; } int main(void) { scanf("%d", &N); for (int i(0); i < N; i++) scanf("%lld", A + i); for (int i(0); i < N; i++) scanf("%lld", B + i); for (int k(1); k < N; k++) scanf("%d%d", &a, &b), g[--a].push_back({k, b}); puts(dfs(0) >= 0 ? "YES" : "NO"); return 0; }
#include <bits/stdc++.h> using namespace std; inline void file() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); if (0) { freopen(".in", "r", stdin); freopen(".out", "w", stdout); } } const clock_t MAXT = (100 * CLOCKS_PER_SEC) / 1000; const int PX[4] = {0, 0, 1, -1}, PY[4] = {-1, 1, 0, 0}, N = 1e5 + 10, INF = 1e9; const long long INFL = 1e18, MOD = 1e9 + 7; const long double EPS = 1e-6; long long a[N]; int n, p[N]; vector<pair<long long, long long> > g[N]; inline bool check(long long c, long long a, long long b) { return fabs((long double)a * b + c) > 1e18; return a > (INFL - c + b - 1) / b; } void dfs(int v, int pred, long long c = 1) { p[v] = pred; for (pair<long long, long long> i : g[v]) if (i.first != pred) dfs(i.first, v, i.second); if (a[v] > 0) c = 1; if (check(a[pred], a[v], c)) { cerr << "zaranie\n"; cout << "NO\n"; exit(0); } a[pred] += a[v] * c; } int main() { file(); long long x, k; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) { cin >> x; a[i] -= x; } for (int i = 2; i <= n; ++i) { cin >> x >> k; g[x].push_back(make_pair(i, k)); } dfs(1, 0); if (a[0] >= 0) cout << "YES\n"; else cout << "NO\n"; }
#include <bits/stdc++.h> using namespace std; vector<long long> disc; const long long inf = 1e18; struct edge { int to; long long cost; }; vector<vector<edge> > g; bool ok = true; void dfs(int v) { for (edge& e : g[v]) { dfs(e.to); if (!ok) { return; } if (disc[e.to] > 0) { disc[v] += disc[e.to]; disc[e.to] = 0; } } for (edge& e : g[v]) { if (disc[e.to] < 0) { long long d = -disc[e.to]; if (e.cost >= (disc[v] + inf + d - 1) / d) { ok = false; break; } disc[v] -= d * e.cost; } } if (v == 0 && disc[v] < 0) { ok = false; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; disc.resize(n); for (int i = 0; i < n; ++i) { cin >> disc[i]; } for (int i = 0; i < n; ++i) { long long val; cin >> val; disc[i] -= val; } g.resize(n); for (int i = 1; i < n; ++i) { int p; cin >> p; --p; long long cost; cin >> cost; g[p].push_back({i, cost}); } ok = true; dfs(0); cout << (ok ? "YES\n" : "NO\n"); }
#include <bits/stdc++.h> #pragma GCC optimize("O") using namespace std; long long a[100005], b[100005], k[100005]; int pa[100005]; int sons[100005]; int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) cin >> b[i]; for (int i = 2; i <= n; ++i) { cin >> pa[i] >> k[i]; ++sons[pa[i]]; } queue<int> le; for (int i = 1; i <= n; ++i) if (sons[i] == 0) le.push(i); while (le.size()) { int now = le.front(); le.pop(); if (now == 1) { if (a[now] >= b[now]) cout << "YES" << endl; else cout << "NO" << endl; break; } if (b[now] == a[now]) ; else if (b[now] > a[now]) { long long need = b[now] - a[now]; long long tk = need * k[now]; if (tk / need != k[now]) exit((cout << "NO" << endl, 0)); a[pa[now]] -= tk; if (a[pa[now]] < -2e17) exit((cout << "NO" << endl, 0)); } else { long long mo = a[now] - b[now]; a[pa[now]] += mo; } --sons[pa[now]]; if (sons[pa[now]] == 0) le.push(pa[now]); } }
#include <bits/stdc++.h> using namespace std; const long long N = 2e6 + 5, inf = 1LL << 62; long long read() { long long x = 0, f = 1; char ch; while (!isdigit(ch = getchar())) (ch == '-') && (f = -f); for (x = ch ^ 48; isdigit(ch = getchar()); x = (x << 3) + (x << 1) + (ch ^ 48)) ; return x * f; } template <class T> T Max(T a, T b) { return a > b ? a : b; } template <class T> T Min(T a, T b) { return a < b ? a : b; } struct Edge { long long to, val; Edge *nxt; Edge(long long to, long long val, Edge *nxt) : to(to), val(val), nxt(nxt) {} } * head[N]; void add(long long x, long long y, long long z) { head[x] = new Edge(y, z, head[x]); } long long n, a[N], b[N], f[N]; void dfs(long long x) { f[x] = b[x] - a[x]; for (Edge *i = head[x]; i; i = i->nxt) { dfs(i->to); if (f[i->to] < 0) { if (inf / i->val <= -f[i->to]) f[x] = -inf; else { f[x] += f[i->to] * i->val; if (f[x] < -inf) f[x] = -inf; } } else f[x] += f[i->to]; } } signed main() { n = read(); for (long long i = 1; i <= n; ++i) b[i] = read(); for (long long i = 1; i <= n; ++i) a[i] = read(); for (long long i = 2, fa, val; i <= n; ++i) fa = read(), add(fa, i, read()); dfs(1); puts(f[1] >= 0 ? "YES" : "NO"); return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 7; const long long inf = (long long)1e18; long long a[N], b[N]; vector<pair<long long, long long> > g[N]; bool pos; void dfs(int s) { for (auto nd : g[s]) { dfs(nd.first); if (a[nd.first] <= b[nd.first]) b[s] = (b[s] + b[nd.first] - a[nd.first]); else { if (1.0 * nd.second * (a[nd.first] - b[nd.first]) + 1.0 * a[s] > 1e18) pos = false; a[s] = (a[s] + nd.second * (a[nd.first] - b[nd.first])); } } } int main() { ios_base::sync_with_stdio(0); cout.precision(15); cout.setf(ios::fixed); int n; cin >> n; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < n; i++) cin >> a[i]; long long x, k; for (int i = 1; i < n; i++) { cin >> x >> k; x--; g[x].push_back({i, k}); } pos = true; dfs(0); if (a[0] > b[0]) pos = false; if (pos) cout << "YES" << '\n'; else cout << "NO" << '\n'; }
#include <bits/stdc++.h> using namespace std; long long a[100010], b[100010], k[100010], c[100010]; int n, x[100010]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", &b[i]); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]), c[i] = b[i] - a[i]; for (int i = 2; i <= n; i++) scanf("%d%I64d", &x[i], &k[i]); for (int i = n; i; i--) { if (c[i] < 0) { if (i == 1 || 100000000000000000 / k[i] <= -c[i] || c[x[i]] + k[i] * c[i] <= -100000000000000000) { printf("NO\n"); return 0; } c[x[i]] += k[i] * c[i]; c[i] = 0; } else if (c[i] > 0) { c[x[i]] += c[i]; c[i] = 0; } } printf("YES\n"); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void read(T& x) { cin >> x; } template <class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } template <class A> void read(A* x, int size) { for (int i = (0); (1) > 0 ? i < (size) : i > (size); i += (1)) read(x[i]); } template <class A> void read(vector<A>& x) { for (auto& a : x) read(a); } template <class T> void print(T& x) { cout << x << ' '; } template <class H, class... T> void print(H h, T... t) { print(h); print(t...); } template <class A> void print(A* x, int size) { for (int i = (0); (1) > 0 ? i < (size) : i > (size); i += (1)) print(x[i]); cout << '\n'; } template <class A> void print(vector<A>& x) { for (auto& a : x) print(a); } template <class H, class... T> void debug(H h, T... t) { print(h, t...); cout << endl; } const int d4i[4] = {-1, 0, 1, 0}, d4j[4] = {0, 1, 0, -1}; const int d8i[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int N = 100010, mod = 1e9 + 7; int n, ans = 1; long long src[N], tar[N], INF = 2e17; vector<pair<int, int>> g[N]; bool overflow(long long p, long long q, long long x) { if (p > INF / q or q > INF / p) return 1; if (p * q - x > INF) return 1; return 0; } void dfs(int s, int p = 0, int w = 0) { for (auto nn : g[s]) dfs(nn.first, s, nn.second); if (!ans) return; if (p and src[s] != tar[s]) { if (src[s] < tar[s]) { if (overflow(tar[s] - src[s], 1LL * w, src[p])) { ans = 0; return; } src[p] -= (tar[s] - src[s]) * w; } else src[p] += src[s] - tar[s]; src[s] = tar[s]; } if (src[s] < tar[s]) ans = 0; } void solve() { read(n); for (int i = (0); (1) > 0 ? i < (n) : i > (n); i += (1)) read(src[i + 1]); for (int i = (0); (1) > 0 ? i < (n) : i > (n); i += (1)) read(tar[i + 1]); int v, w; for (int i = (0); (1) > 0 ? i < (n - 1) : i > (n - 1); i += (1)) { read(v, w); g[v].push_back({i + 2, w}); } dfs(1); cout << (ans ? "YES" : "NO"); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
#include <bits/stdc++.h> const int N = 100005; int n; long long fa[N], k[N]; double a[N]; int main() { scanf("%d", &n); long long x; for (int i = 1; i <= n; ++i) scanf("%lf", &a[i]); for (int i = 1; i <= n; ++i) scanf("%lld", &x), a[i] -= x; for (int i = 2; i <= n; ++i) scanf("%lld%lld", &fa[i], &k[i]); for (int i = n; i >= 2; --i) if (a[i] < 0) a[fa[i]] += a[i] * k[i]; else a[fa[i]] += a[i]; puts(a[1] < 0 ? "NO" : "YES"); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; long long a[MAX], b[MAX], k[MAX]; vector<int> adj[MAX]; int parent[MAX]; int n; void die() { puts("NO"); exit(0); } void dfs(int source) { for (auto &each : adj[source]) { dfs(each); } if (b[source] > a[source]) { b[parent[source]] += b[source] - a[source]; b[source] = a[source]; } else if (source > 1) { long long need = a[source] - b[source]; b[source] = a[source]; b[parent[source]] -= need * k[source]; if (b[parent[source]] <= (long long)-1e18) { die(); } } } int main() { scanf("%d", &n); long long sum_b = 0, sum_a = 0; for (int i = int(1); i < int(n + 1); i++) { scanf("%lld", b + i); sum_b += b[i]; } for (int i = int(1); i < int(n + 1); i++) { scanf("%lld", a + i); sum_a += a[i]; } if (sum_a > sum_b) { puts("NO"); return 0; } for (int i = int(2); i < int(n + 1); i++) { scanf("%d %lld", parent + i, k + i); adj[parent[i]].push_back(i); } dfs(1); bool ok = true; for (int i = int(1); i < int(n + 1); i++) { if (a[i] > b[i]) { ok = false; } } puts(ok ? "YES" : "NO"); return 0; }
#include <bits/stdc++.h> using namespace std; const long long int maxN = 1e5 + 326; long long int pa[maxN], wei[maxN], has[maxN], need[maxN], N, isLeaf[maxN], stillneed[maxN]; bool isViable[maxN]; vector<long long int> chi[maxN]; void dfs(long long int u = 0) { for (long long int v : chi[u]) { dfs(v); if (!isViable[v]) { isViable[u] = false; return; } if (stillneed[v]) { if (stillneed[v] >= ((long long int)1e17 + has[u] - need[u] + wei[v] - 1) / wei[v]) { isViable[u] = false; return; } has[u] -= stillneed[v] * wei[v]; has[v] += stillneed[v]; } } if (has[u] >= need[u]) { stillneed[u] = 0; if (pa[u] != u) has[pa[u]] += has[u] - need[u]; has[u] = need[u]; } else { stillneed[u] = need[u] - has[u]; } } signed main() { cin >> N; fill(isViable, isViable + N, true); fill(isLeaf, isLeaf + N, true); for (long long int i = 0; i < N; i++) cin >> has[i]; for (long long int i = 0; i < N; i++) cin >> need[i]; for (long long int i = 1; i < N; i++) { cin >> pa[i] >> wei[i]; pa[i]--; chi[pa[i]].push_back(i); isLeaf[pa[i]] = false; } dfs(); cout << (has[0] >= need[0] && isViable[0] ? "YES" : "NO") << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll oo = 1e18; const int N = 100005; vector<pair<int, int>> g[N]; int n; ll b[N], a[N]; ll dfs(int u) { ll me = b[u] - a[u]; for (auto [v, k] : g[u]) { ll tmp = dfs(v); if (tmp < 0) { if (-tmp > oo / k) { printf("NO\n"); exit(0); } tmp *= k; } me += tmp; if (abs(me) >= oo) { printf("NO\n"); exit(0); } } return me; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", b + i); for (int i = 1; i <= n; i++) scanf("%lld", a + i); for (int i = 2; i <= n; i++) { int x, k; scanf("%d %d", &x, &k); g[x].emplace_back(i, k); } printf("%s\n", dfs(1) >= 0 ? "YES" : "NO"); }
#include <bits/stdc++.h> using namespace std; long long n, x, y, t; long long a[110000], h[110000]; struct edge { long long y, nxt, w; } e[110000]; void add(long long i) { t++; e[t].y = i; e[t].nxt = h[x]; e[t].w = y; h[x] = t; } void dfs(long long k, long long x, long long s) { for (long long i = h[k]; i; i = e[i].nxt) dfs(e[i].y, k, e[i].w); if (a[k] >= 0) a[x] += a[k]; else { double y = double(s) * double(a[k]); if (!x || y < -23333333333333333) { puts("NO"); exit(0); } a[x] += s * a[k]; if (a[x] < -23333333333333333) { puts("NO"); exit(0); } } } signed main() { cin >> n; for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]); for (long long i = 1; i <= n; i++) scanf("%lld", &x), a[i] -= x; for (long long i = 2; i <= n; i++) scanf("%lld%lld", &x, &y), add(i); dfs(1, 0, 0); puts("YES"); return 0; }
#include <bits/stdc++.h> using namespace std; struct RR { long long to, k, nex; } r[211111]; int hea[111111] = {0}, cnt = 0; long long n, a[111111], t; double tem; void R(long long &x) { scanf("%I64d", &x); } void dfs(int x, int f, long long kk) { int y; for (int i = hea[x]; i > 0; i = r[i].nex) { y = r[i].to; dfs(y, x, r[i].k); } if (a[x] >= 0) { a[f] += a[x]; } else { if (f != 0) { tem = (double)a[x] * kk; if (tem < -110000000000000000) { printf("NO\n"); exit(0); } a[f] += a[x] * kk; if (a[f] < -110000000000000000) { printf("NO\n"); exit(0); } } else { if (a[x] < 0) { printf("NO\n"); exit(0); } } } } int main() { R(n); for (int i = 1; i <= n; i++) R(a[i]); for (int i = 1; i <= n; i++) { R(t); a[i] -= t; } long long x, y; for (int i = 2; i <= n; i++) { R(x); R(y); r[++cnt] = (RR){i, y, hea[x]}; hea[x] = cnt; } dfs(1, 0, 0); printf("YES\n"); }
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; const long long thr = 200000000000000000; long long a[maxn], b[maxn], x[maxn], k[maxn]; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> b[i]; } for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 2; i <= n; i++) { cin >> x[i] >> k[i]; } for (int i = n; i >= 2; i--) { if (b[i] >= a[i]) { b[x[i]] += b[i] - a[i]; } else { long long diff = a[i] - b[i]; long long ex = b[x[i]]; if (b[x[i]] < -thr) { cout << "NO" << endl; return 0; } if (log(ex + thr) < log(k[i]) + log(diff)) { cout << "NO" << endl; return 0; } b[x[i]] -= k[i] * diff; } } if (b[1] >= a[1]) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
#include <bits/stdc++.h> const double Pi = acos(-1.0); using namespace std; const int maxN = 100005; int n; long long b[maxN]; long long k[maxN]; long double req[maxN]; vector<int> G[maxN]; void dfs(int cur) { for (int i = 0; i < (int)G[cur].size(); i++) { int nxt = G[cur][i]; dfs(nxt); if (req[nxt] < 0) req[cur] += (long double)k[nxt] * req[nxt]; else req[cur] += req[nxt]; } } int main(int argc, char** argv) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &b[i]); long long a; for (int i = 1; i <= n; i++) { scanf("%lld", &a); req[i] = b[i] - a; } int x; for (int i = 2; i <= n; i++) { scanf("%d %lld", &x, &k[i]); G[x].push_back(i); } dfs(1); puts((req[1] < 0) ? "NO" : "YES"); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const long long INF = (1LL << 60) - 1; vector<pair<int, int> > e[MAXN]; long long a[MAXN], b[MAXN], dp[MAXN]; void dfs(int u) { dp[u] = b[u] - a[u]; for (int i = 0; i < (int)e[u].size(); i++) { int v = e[u][i].first, r = e[u][i].second; dfs(v); if (dp[v] < 0) { if (-dp[v] < INF / r) dp[u] += dp[v] * r; else dp[u] = -INF; if (dp[u] < -INF) dp[u] = -INF; } else dp[u] += dp[v]; } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &b[i]); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 2; i <= n; i++) { int x, k; scanf("%d%d", &x, &k); e[x].push_back(make_pair(i, k)); } dfs(1); return 0 * printf("%s\n", (dp[1] >= 0 ? "YES" : "NO")); }
#include <bits/stdc++.h> const int maxn = 1e5 + 10; const int N = 2e5 + 10; using namespace std; long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); } long long qmul(long long p, long long q) { long long f = 0; while (q) { if (q & 1) f = f + p; if (f > 5e18) return -1; p = p + p; q >>= 1; } return f; } long long qpow(long long p, long long q, long long mo) { long long f = 1; while (q) { if (q & 1) f = f * p % mo; p = p * p % mo; q >>= 1; } return f; } int n, m, t, du[maxn], x[maxn], k[maxn]; long long a[maxn], b[maxn]; int main() { int i, j; scanf("%d", &n); for (i = 1; i <= (int)n; i++) scanf("%lld", &b[i]); for (i = 1; i <= (int)n; i++) scanf("%lld", &a[i]); for (i = 2; i <= (int)n; i++) scanf("%d%d", &x[i], &k[i]), du[x[i]]++; queue<int> pq; for (i = 1; i <= (int)n; i++) if (du[i] == 0) pq.push(i); while (!pq.empty()) { int p = pq.front(); pq.pop(); if (p == 1) return 0 * puts(a[p] <= b[p] ? "YES" : "NO"); if (a[p] <= b[p]) { b[x[p]] += b[p] - a[p]; } else { if (b[x[p]] - 1.0 * (a[p] - b[p]) * k[p] < -(1LL << 60)) b[x[p]] = -(1LL << 60); else b[x[p]] -= (a[p] - b[p]) * k[p], a[p] = b[p]; if (b[x[p]] < -(1LL << 60)) b[x[p]] = -(1LL << 60); } if (--du[x[p]] == 0) pq.push(x[p]); } return 0; }
#include <bits/stdc++.h> using namespace std; struct Edge { int to; long long int k; }; int N; long long int as[114514]; long long int bs[114514]; vector<Edge> es[114514]; long long int sum; void dfs(int v, int p, long long int k2) { long long int k = 0; for (Edge &e : es[v]) { int u = e.to; if (u == p) { k = e.k; continue; } dfs(u, v, e.k); } if (v == p) return; if (as[v] > bs[v]) { if (as[v] - bs[v] > sum) return; long long int req = (as[v] - bs[v]) * k; bs[p] -= req; bs[v] = as[v]; } else { long long int cnt = (bs[v] - as[v]) / k2; bs[p] += cnt; bs[v] -= cnt * k2; } } int main() { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%lld", &bs[i]); sum += bs[i]; } for (int i = 0; i < N; i++) { scanf("%lld", &as[i]); } for (int i = 1; i < N; i++) { int x; long long int k; scanf("%d%lld", &x, &k); --x; es[i].emplace_back(Edge{x, k}); es[x].emplace_back(Edge{i, 1}); } dfs(0, 0, 0); for (int i = 0; i < N; i++) { if (as[i] > bs[i]) { puts("NO"); return 0; } } puts("YES"); }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 1e6 + 5; long long int power(long long int base, long long int exp) { long long int res = 1; while (exp) { if (exp % 2) res *= base; base *= base; res %= mod; base %= mod; exp /= 2; } return res; } long long int u[maxn], v[maxn], a[maxn], b[maxn]; vector<pair<long long int, long long int> > adj[maxn]; void dfs(long long int i) { if (b[i] < -1e18) { cout << "NO"; exit(0); } for (auto next : adj[i]) { dfs(next.first); } if (i == 1) { if (a[i] > b[i]) { cout << "NO"; exit(0); } else { cout << "YES"; exit(0); } } if (a[i] > b[i]) { long long int how = a[i] - b[i]; if (1.0 * b[u[i]] - 1.0 * v[i] * how < -2e17) { cout << "NO"; exit(0); } b[u[i]] -= (v[i] * how); } else { long long int how = b[i] - a[i]; b[u[i]] += how; } b[i] = a[i]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, i; cin >> n; for (i = 1; i <= n; i++) cin >> b[i]; for (i = 1; i <= n; i++) cin >> a[i]; for (i = 2; i <= n; i++) { cin >> u[i] >> v[i]; adj[u[i]].push_back({i, v[i]}); } dfs(1); return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> e[100005]; long long a[100005], b[100005]; int n; long long safemul(long long a, long long b) { double x = a * 1.0 * b; if (-4e18 < x && x < 4e18) { return a * b; } else if (x < 0) { return -4e18; } else { return 4e18; } } long long safeadd(long long a, long long b) { long long c = a + b; if (c < -4e18) { return -4e18; } if (c > 4e18) { return 4e18; } return c; } long long dfs(int x) { long long supply = 0; for (auto ee : e[x]) { long long r = dfs(ee.first); if (r > 0) { supply = safeadd(supply, r); } else { supply = safeadd(supply, safemul(r, ee.second)); } } supply = safeadd(supply, b[x] - a[x]); return supply; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) { cin >> b[i]; } for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 2; i <= n; i++) { int x, k; cin >> x >> k; e[x].push_back({i, k}); } long long r = dfs(1); if (r < 0) { cout << "NO\n"; } else { cout << "YES\n"; } }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; int n; long long b[100000], a[100000]; vector<pair<int, int> > adj[100000]; long long dfs(int u) { long long ret = b[u] - a[u]; for (auto v : adj[u]) { long long res = dfs(v.first); if (res < 0) { res = -res; if (res > INF / v.second) ret = -INF; else { ret = ret - res * v.second; ret = max(ret, -INF); } } else ret = ret + res; } return ret; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &b[i]); for (int i = 0; i < n; i++) scanf("%lld", &a[i]); for (int i = 0; i < n - 1; i++) { int x; long long k; scanf("%d %lld", &x, &k); int u = x - 1; int v = i + 1; adj[u].push_back({v, k}); } long long res = dfs(0); if (res >= 0) puts("YES"); else puts("NO"); return 0; }
#include <bits/stdc++.h> using namespace std; char s[101][3]; int main() { int n, i, k = 0, c1 = 0, c2 = 0; cin.get(s[0], 3); cin >> n; for (i = 1; i <= n; i++) { cin.get(); cin.get(s[i], 3); } for (i = 1; i <= n && k < 2; i++) { if (s[0][0] == s[i][1] && c1 == 0) { k++; c1 = 1; } if (s[0][1] == s[i][0] && c2 == 0) { k++; c2 = 1; } if (strcmp(s[0], s[i]) == NULL) k = 2; } if (k == 2) cout << "YES"; else cout << "NO"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string pass; int n; cin >> pass >> n; string bark; bool solved = false, e = false, s = false; for (int i = 0; i < n; i++) { cin >> bark; if (bark == pass) solved = true; if (bark.at(1) == pass.at(0)) e = true; if (bark.at(0) == pass.at(1)) s = true; } cout << ((solved || (s && e)) ? "YES" : "NO"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char a, b; scanf("%c%c", &a, &b); int n; scanf("%d", &n); getchar(); char s[n][10]; for (int i = 0; i < n; i++) { gets(s[i]); } bool mark[2]; memset(mark, 0, sizeof(mark)); for (int i = 0; i < n; i++) { if (s[i][0] == a && s[i][1] == b) { printf("Yes\n"); return 0; } if (s[i][1] == a) { mark[1] = 1; } if (s[i][0] == b) { mark[0] = 1; } if (mark[1] && mark[0]) { printf("Yes\n"); return 0; } } printf("No\n"); }
#include <bits/stdc++.h> using namespace std; int main() { int c = 0, s = 0, ss = 0; string q; cin >> q; cin >> c; vector<string> a(c); for (int i = 0; i < c; i++) { cin >> a[i]; if (a[i] == q) { cout << "YES"; return 0; } } for (int i = 0; i < c; i++) { if (q[0] == a[i][1]) s++; if (q[1] == a[i][0]) ss++; } if ((s > 0) && (ss > 0)) { cout << "YES"; return 0; } cout << "NO"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; string m[n]; int i, j, k; for (i = 0; i < n; i++) { cin >> m[i]; } string tp = ""; string save = ""; int key = 0; for (i = 0; i < n; i++) { tp = tp + m[i]; save = tp; for (j = 0; j < n; j++) { tp = save; tp = tp + m[j]; for (k = 0; k < tp.size() - 1; k++) { if (tp[k] == s[0] && tp[k + 1] == s[1]) { key = 1; } } } } if (key == 1) cout << "YES"; else cout << "NO"; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e5 + 5; const long long mod = 1e9 + 7; long long pw(long long b, long long r, long long md = mod) { b = b % md; long long ans = 1; while (r) { if (r & 1) ans = (ans * b) % md; b = (b * b) % md; r >>= 1; } return ans; } const long long N = 5e5 + 5; bool vis[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long T = 1; while (T--) { string s; cin >> s; map<string, long long> m; long long n; cin >> n; string arr[n]; for (long long i = 0; i <= n - 1; ++i) { cin >> arr[i]; m[arr[i]]++; } for (long long i = 0; i <= n - 1; ++i) { m[arr[i]]++; for (long long j = 0; j <= n - 1; ++j) { string ns = ""; ns.push_back(arr[i][1]); ns.push_back(arr[j][0]); m[ns]++; } } if (m[s]) cout << "YES" << '\n'; else cout << "NO" << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; char s[111][3]; int main() { char t[3]; scanf("%s", t); int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", s[i]); } for (int i = 0; i < n; i++) { if (t[0] == s[i][0] && t[1] == s[i][1]) { puts("YES"); return 0; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (t[0] == s[i][1] && t[1] == s[j][0]) { puts("YES"); return 0; } } } puts("NO"); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T& x) { x = 0; char c; int sign = 1; do { c = getchar(); if (c == '-') sign = -1; } while (c < '0' || c > '9'); do { x = x * 10 + c - '0'; c = getchar(); } while (c <= '9' && c >= '0'); x *= sign; } string s, a[103]; int n; int main() { cin >> s; read(n); for (register int i = 1; i <= n; ++i) cin >> a[i]; for (register int i = 1; i <= n; ++i) if (s == a[i]) return puts("YES"), 0; for (register int i = 1; i <= n; ++i) for (register int j = 1; j <= n; ++j) if (a[i][1] == s[0] && a[j][0] == s[1]) return puts("YES"), 0; return puts("NO"), 0; }
#include <bits/stdc++.h> using namespace std; long double pi = 3.14159265358979323846264338327950; vector<string> v; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); string s; cin >> s; int n; cin >> n; bool f = 0; for (int i = 0; i < n; i++) { string barks, r; cin >> barks; r = barks; reverse(r.begin(), r.end()); if (s == r || s == barks) f = 1; v.push_back(barks); } if (f == 1) cout << "YES" << endl; else { bool F = 0, S = 0; for (int i = 0; i < v.size(); i++) { if (v[i][1] == s[0]) F = 1; } for (int i = 0; i < v.size(); i++) { if (v[i][0] == s[1]) S = 1; } if (F == 1 && S == 1) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; string s, t; bool ok, ok1, ok2; int n; int main() { ios::sync_with_stdio(false); cout.tie(0); cin.tie(0); cin >> s >> n; for (int i = 1; i <= n; i++) { cin >> t; if (t == s) { ok = true; } if (t[0] == s[1]) { ok1 = true; } if (t[1] == s[0]) { ok2 = true; } } if (ok) { cout << "YES" << endl; return 0; } if (ok1 && ok2) { cout << "YES" << endl; return 0; } cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<string> v; vector<string>::iterator it; string s, p; cin >> s; int n, c = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> p; v.push_back(p); if (s[0] == p[0] && s[1] == p[1]) { cout << "YES"; return 0; } if (s[0] == p[1] && s[1] == p[0]) { cout << "YES"; return 0; } } for (int i = 0; i < n; i++) { if (v[i][1] == s[0]) { c = 1; } } for (int i = 0; i < n; i++) { if (v[i][0] == s[1] && c == 1) { cout << "YES"; return 0; } } cout << "NO"; }
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; int n; cin >> n; vector<string> vec(n); for (int i = 0; i < n; i++) { cin >> vec[i]; } int f1 = 0; int f2 = 0; for (int i = 0; i < n; i++) { if (str == vec[i]) { f1 = 1; f2 = 1; break; } else { if (str[0] == vec[i][1]) f1 = 1; if (str[1] == vec[i][0]) f2 = 1; } } if (f1 && f2) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, v0, v1; string a, b; int main() { cin >> a >> n; while (n--) { cin >> b; if (b == a) v0 = v1 = 1; if (b[1] == a[0]) v0 = 1; if (b[0] == a[1]) v1 = 1; } cout << (v0 && v1 ? "YES" : "NO"); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int INF = 1e9 + 7; const long double PI = 3.141592653589793; inline void bye() { exit(0); } int main(void) { cout << fixed << setprecision(16); srand(INF); ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string password; cin >> password; int n; cin >> n; vector<string> can(n); for (int i = 0; i < n; i++) { cin >> can[i]; } int canf = -1, cans = -1; for (int i = 0; i < n; i++) { if (can[i][1] == password[0]) { canf = i; } if (can[i][0] == password[1]) { cans = i; } if (can[i] == password) { cout << "YES\n"; bye(); } } if (-1 == canf || cans == -1) { cout << "NO\n"; } else { cout << "YES\n"; } bye(); }
#include <bits/stdc++.h> using namespace std; int main() { string a, b[200]; int n; cin >> a; cin >> n; for (int i = 1; i <= n; i++) { cin >> b[i]; } int c1 = 0, c2 = 0; for (int i = 1; i <= n; i++) { if (b[i] == a) { cout << "YES" << endl; return 0; } if (b[i][0] == a[1]) c1 = 1; if (b[i][1] == a[0]) c2 = 1; } if (c1 + c2 == 2) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
#include <bits/stdc++.h> using namespace std; string toupper_str(string s) { transform(s.begin(), s.end(), s.begin(), ::toupper); return s; } string tolower_str(string s) { transform(s.begin(), s.end(), s.begin(), ::tolower); return s; } int main() { string p, s; int n, f = 0; cin >> p >> n; map<char, int> mp1, mp2; for (int i = 0; i < n; i++) { cin >> s; if (s == p) { cout << "YES\n"; f = 1; } mp1[s[0]]++, mp2[s[1]]++; } if (f == 0) { if (mp2[p[0]] > 0 && mp1[p[1]] > 0) cout << "YES\n"; else cout << "NO\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 100010; const int MOD = 1000000007; const int INF = 0x3f3f3f3f; const long long LLINF = 0x3f3f3f3f3f3f3f3f; const long double EPS = 1e-7; int main() { int n; string p; cin >> p; cin >> n; bool win = false, p1 = false, p2 = false; for (int i = 0; i < (int)n; i++) { string s; cin >> s; if (p == s) win = true; if (s[1] == p[0]) p1 = true; if (s[0] == p[1]) p2 = true; } if (p1 and p2) win = true; if (win) cout << "YES" << '\n'; else cout << "NO" << '\n'; return 0; }
#include <bits/stdc++.h> int main() { char s1[3] = {'\0'}, s2[101][3] = {'\0'}; int i = 0, k = 0, j = 0, x; scanf("%s", s1); scanf("%d", &x); for (i = 0; i < x; i++) { scanf("%s", s2[i]); if ((s2[i][0] == s1[0] && s2[i][1] == s1[1]) || (s2[i][0] == s1[1]) && (s2[i][1] == s1[0])) { k = 1; break; } } for (i = 0; i < x; i++) { if (s2[i][1] == s1[0]) { for (j = 0; j < x; j++) if (s2[j][0] == s1[1]) k = 1; } } if (k == 0) printf("NO\n"); else printf("YES\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string ans, iptco; cin >> ans; int num; cin >> num; string *ptr = new string[num]; for (int i = 0; i < num; i++) { cin >> ptr[i]; if (ptr[i] == ans) { cout << "YES\n"; delete[] ptr; return 0; } } for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { iptco = ptr[i] + ptr[j]; if (iptco[1] == ans[0] && iptco[2] == ans[1]) { cout << "YES\n"; delete[] ptr; return 0; } } } delete[] ptr; cout << "No\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; int x = 0; int y = 0; for (int i = 0; i < n; i++) { string two; cin >> two; if (two[0] == s[0] && two[1] == s[1]) { cout << "YES" << endl; return 0; } if (s[0] == two[1]) { if (x == 0) { x += 1; } } if (two[0] == s[1]) { if (y == 0) { y += 1; } } } if (x + y == 2) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
#include <bits/stdc++.h> const int N = 1005; using namespace std; int n; char str[N]; char s[N][N]; char tmp[N]; int main() { cin >> str; cin >> n; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { strcpy(tmp, s[i]); strcat(tmp, s[j]); if (strstr(tmp, str)) { cout << "YES" << endl; return 0; } } cout << "NO"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string key; cin >> key; int t; cin >> t; vector<string> barks; for (int i = 0; i < t; i++) { string bark; cin >> bark; barks.push_back(bark); } bool ans = false; for (int i = 0; i < barks.size(); i++) { if (barks[i] == key) ans = true; } for (int i = 0; i < barks.size(); i++) { for (int j = 0; j < barks.size(); j++) { string test; test = barks[i] + barks[j]; if (test.find(key) != string::npos) { ans = true; } } } if (ans) cout << "YES"; else cout << "NO"; }
#include <bits/stdc++.h> using namespace std; void pl(long long a) { printf("%lld\n", a); } void pll(long long a, long long b) { printf("%lld %lld\n", a, b); } void plll(long long a, long long b, long long c) { printf("%lld %lld %lld ", a, b, c); } void sss(string s) { cout << s, printf("\n"); } long long string_to_ll(string s) { stringstream ss; ss << s; long long n; ss >> n; return n; } string ll_to_string(long long n) { stringstream ss; ss << n; string s; ss >> s; return s; } double DIS(double x1, double y1, double x2, double y2) { return sqrt(pow(x1 - x2, 2.0) + pow(y1 - y2, 2.0)); } int flag, flag1; int main() { string s, s1; int i, n; cin >> s >> n; while (n--) { cin >> s1; if (s[0] == s1[1]) flag = 1; if (s[1] == s1[0]) flag1 = 1; if (s == s1) { flag = 1; flag1 = 1; } } if (flag && flag1) sss("YES"); else sss("NO"); return 0; }
#include <bits/stdc++.h> using namespace std; bool isPrime(long long int p) { if (p < 2) return false; if (p == 2) return true; if (p % 2 == 0) return false; for (int i = 3; i <= sqrt(p); i += 2) { if (p % i == 0) return false; } return true; } long long int j, x2, d, m, n, s, a, b, c, t, x, y, f, g, h = 0, i, mid, mid2, o, p, lo, sum = 0, cnt2 = 0, cnt1 = 0, q, dip, certi, maxi = 0, maxi2 = INT_MIN, maxi3 = INT_MIN; long long int l, r, k; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); string a1; cin >> a1; cin >> n; for (i = 0; i < n; i++) { string a2; cin >> a2; if (a2[1] == a1[0]) { cnt1 = 1; } if (a2[0] == a1[1]) { cnt2 = 1; } if (a2[0] == a1[0] && a2[1] == a1[1]) { cout << "YES" << endl; return 0; } } if (cnt2 == 1 && cnt1 == 1) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
#include <bits/stdc++.h> char s[3], t[110][3]; int n; int main() { scanf("%s%d", s, &n); for (int i = 1; i <= n; i++) scanf("%s", t[i]); for (int i = 1; i <= n; i++) if (t[i][0] == s[0] && t[i][1] == s[1]) { printf("YES\n"); return 0; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (t[i][1] == s[0] && t[j][0] == s[1]) { printf("YES\n"); return 0; } printf("NO\n"); return 0; }
#include <bits/stdc++.h> using namespace std; string a[102]; int main() { ios::sync_with_stdio(false), cin.tie(0); string s; cin >> s; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } int x = 0, y = 0; for (int i = 0; i < n; i++) { if (a[i] == s) { cout << "YES\n"; return 0; } if (a[i][0] == s[1]) { x++; } if (a[i][1] == s[0]) { y++; } } if (x > 0 && y > 0) { cout << "YES\n"; } else { cout << "NO\n"; } }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e7; const long long MOD = 1e9 + 7; const long long maxn = 1e5 + 1; long long RAND(long long n) { return abs((rand() << 15) + rand()) % n; } void solveTask() { long long n; string second, s1[1000]; int res = 0, res1 = 0; bool ans = 0, c = 0, z = 0, x = 0, y = 0; cin >> second; cin >> n; res1 = (int)second[0] + (int)second[1]; for (int i = 0; i < n; ++i) { cin >> s1[i]; if (s1[i][1] == second[0]) x = 1; if (s1[i][0] == second[1]) y = 1; if (s1[i] == second || (s1[i][0] == second[1] && s1[i][1] == second[0])) ans = 1; } if (ans || (x && y)) cout << "YES"; else cout << "NO"; } int main() { ios_base::sync_with_stdio(0); srand(time(0)); solveTask(); return 0; }
#include <bits/stdc++.h> using namespace std; char ch1[5]; char ch2[105][5]; int main() { cin >> ch1; int n; cin >> n; int f = 0; for (int i = 0; i < n; i++) { cin >> ch2[i]; if (strcmp(ch1, ch2[i]) == 0) { f = 1; } } if (f) { cout << "YES" << endl; } else { for (int i = 0; i < n; i++) { if (ch2[i][1] == ch1[0]) for (int j = 0; j < n; j++) { if (ch2[j][0] == ch1[1]) { f = 1; } } } if (f) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string a; cin >> a; int n, c = 0, d = 0; cin >> n; string b; while (n--) { cin >> b; if (b == a) { c = 1; d = 1; } if (b[1] == a[0]) { d = 1; } if (b[0] == a[1]) { c = 1; } } if (c == 1 && d == 1) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
#include <bits/stdc++.h> using namespace std; char ch[3], a[300]; int main() { int n; scanf("%s", ch); scanf("%d", &n); getchar(); int i; for (i = 0; i < n; ++i) { scanf("%c%c", &a[i * 2], &a[i * 2 + 1]); getchar(); } a[i * 2] = a[0]; a[i * 2 + 1] = a[1]; a[i * 2 + 2] = '\n'; int f1 = 0; int f = 0; int f2 = 0; for (int i = 0; i <= n; ++i) { if (a[i * 2] == ch[0] && a[i * 2 + 1] == ch[1]) { f = 1; break; } if (a[i * 2] == ch[1]) f2 = 1; if (a[i * 2 + 1] == ch[0]) f1 = 1; if (f1 && f2) { f = 1; break; } } if (f) printf("YES\n"); else printf("NO\n"); return 0; }
#include <bits/stdc++.h> using namespace std; string s, a; bool dpn, blk; int n; int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> s; cin >> n; dpn = false; blk = false; for (int i = 0; i < n; i++) { cin >> a; if (a == s) { dpn = true; blk = true; } if (a[1] == s[0]) dpn = true; if (a[0] == s[1]) blk = true; } if (blk && dpn) cout << "YES\n"; else cout << "NO\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const int inf = 0x3f3f3f3f; const double esp = 1e-6; const int maxn = 100005; const int mod = 1e9 + 7; int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long inv(long long b) { if (b == 1) return 1; return (mod - mod / b) * inv(mod % b) % mod; } long long fpow(long long n, long long k) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % mod; n = n * n % mod; } return r; } int main() { int i, j, k, n; char a, b, c, d; string map[105]; while (cin >> a >> b) { cin >> n; int flag = 0, flag2 = 0; for (i = 0; i < n; i++) { cin >> map[i]; } for (i = 0; i < n && !flag; i++) { if (map[i][0] == a && map[i][1] == b) { flag = 1; break; } for (j = 0; j < n && !flag; j++) { if (map[i][1] == a && map[j][0] == b) { flag = 1; break; } } } if (flag) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n, f = 0; cin >> n; string st[n]; for (int i = 0; i < n; ++i) { cin >> st[i]; if (st[i] == s) f = 1; } if (f == 1) { cout << "YES"; return 0; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if ((st[i][1] == s[0] && st[j][0] == s[1])) { cout << "YES"; return 0; } } } cout << "NO"; }
#include <bits/stdc++.h> using namespace std; int n, mar1, mar2; string a, b; int main() { int i, j; cin >> a; cin >> n; for (i = 1; i <= n; i++) { cin >> b; if (b[0] == a[1]) mar1 = 1; if (b[1] == a[0]) mar2 = 1; if (a == b) { cout << "YES" << endl; return 0; } } if (mar1 && mar2) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> int main() { int i, j, n, first = 0, second = 0; char flag = 1, Pass[10]; gets(Pass); scanf("%d", &n); char S[n][10]; for (i = 0; i < n; i++) { scanf("%s", S[i]); } for (i = 0; (flag) && (i < n); i++) { if (strstr(Pass, S[i])) { printf("YES\n"); flag = 0; } } for (i = 0; (flag) && (i < n); i++) { if (S[i][1] == Pass[0]) { first = 1; for (j = 0; (j < n); j++) { if (S[j][0] == Pass[1]) { second = 1; } } } } if (flag) { if ((first) && (second)) printf("YES\n"); else printf("NO\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string pw; cin >> pw; int n; cin >> n; vector<string> v(n); for (long p = 0; p < n; p++) { cin >> v[p]; } bool found(false); for (long a = 0; a < n; a++) { if (found) { break; } for (long b = 0; b < n; b++) { string s = v[a] + v[b]; if (s.find(pw) != std::string::npos) { found = true; break; } } } cout << (found ? "YES" : "NO") << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void setMyPrecision(int n) { cout << fixed << setprecision(n); } bool test = 0; void solve() { string pass; cin >> pass; int n; set<char> f, l; cin >> n; bool flag = false; for (int i = 0; i < n; i++) { char o, t; cin >> o >> t; if (pass[0] == o && pass[1] == t) flag = true; f.insert(o); l.insert(t); } if (flag) { cout << "YES"; return; } if ((f.find(pass[1]) != f.end()) && (l.find(pass[0]) != l.end())) cout << "YES"; else cout << "NO"; } int main() { ios::sync_with_stdio(0); cout.tie(0); cin.tie(0); if (test) { int t; cin >> t; while (t--) solve(); } else solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 2; const int inf = 1e9 + 7; const string ABC = "0123456789"; int main() { char c1, c2; cin >> c1 >> c2; int n; cin >> n; pair<char, char> d[MAXN]; for (int i = 0; i < n; i++) { cin >> d[i].first >> d[i].second; if (d[i].first == c1 && d[i].second == c2) { cout << "YES"; return 0; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (d[i].second == c1 && d[j].first == c2) { cout << "YES"; return 0; } } } cout << "NO"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; int a = 0, b = 0; while (n--) { string y; cin >> y; if (y[1] == s[0]) a = 1; if (y[0] == s[1]) b = 1; if (y == s) { a = 1; b = 1; } } if (a == 1 && b == 1) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, c1 = 0, c2 = 0; bool b = 0, b1 = 0, b2 = 0; string s; cin >> s >> a; string ss, ss1 = s; reverse(ss1.begin(), ss1.end()); for (int i = 0; i < a; ++i) { cin >> ss; if (ss == s || ss == ss1) { b = 1; } if (ss[1] == s[0]) { b1 = 1; } if (ss[0] == s[1]) { b2 = 1; } } if (b == 1) { cout << "YES\n"; } else if (b1 == 1 && b2 == 1) { cout << "YES\n"; } else { cout << "NO\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, factor = 0; string pass, bark[100]; cin >> pass; cin >> n; for (int i = 0; i < n; i++) { cin >> bark[i]; if (bark[i] == pass) { cout << "YES" << endl; return 0; } } for (int i = 0; i < n; i++) { if (bark[i][1] == pass[0]) { factor = 1; break; } } if (!factor) { cout << "NO" << endl; return 0; } for (int i = 0; i < n; i++) { if (bark[i][0] == pass[1]) { cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int n; string w; cin >> s >> n; bool a, b; a = b = false; for (int i = 0; i < n; ++i) { cin >> w; if (w == s) { puts("YES"); return 0; } if (w[0] == s.back()) a = true; if (w.back() == s[0]) b = true; } if (a && b) puts("YES"); else puts("NO"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); string s; cin >> s; int n; cin >> n; vector<string> a(n); bool found = false; for (int i = 0; i < n; ++i) { cin >> a[i]; found |= a[i] == s; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { found |= a[i][0] == s[1] && a[j][1] == s[0]; } } cout << (found ? "YES" : "NO") << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); string str; cin >> str; int n; cin >> n; bool f1 = false; bool f2 = false; for (int i = 0; i < n; ++i) { string s; cin >> s; if (s == str) { f1 = true; f2 = true; } if (s[1] == str[0]) { f2 = true; } if (s[0] == str[1]) { f1 = true; } } if (f1 && f2) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; char s[3]; char p[105][3]; int main() { int i, j = 0, k = 0, n; gets(s); cin >> n; scanf("\n"); for (i = 0; i < n; i++) { gets(p[i]); } for (i = 0; i < n; i++) { if (strcmp(p[i], s) == 0) { cout << "YES"; return 0; } } for (i = 0; i < n; i++) { if (p[i][0] == s[1]) { j = 1; } if (p[i][1] == s[0]) { k = 1; } } if (j == 1 && k == 1) { cout << "YES"; return 0; } cout << "NO"; return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); int main() { ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int n; cin >> n; bool x = false, y = false; for (int i = 0; i < n; i++) { string t; cin >> t; if (t == s) { cout << "YES\n"; return 0; } if (t[0] == s[1] && t[1] == s[0]) { cout << "YES\n"; return 0; } if (t[0] == s[1]) x = 1; if (t[1] == s[0]) y = 1; } if (x && y) { cout << "YES\n"; } else { cout << "NO\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int n; char pwd[5]; char word[105][5]; int main() { while (cin >> pwd) { cin >> n; bool flag = false; for (int i = 0; i < n; i++) { cin >> word[i]; if (word[i][0] == pwd[0] && word[i][1] == pwd[1]) flag = true; } if (flag) { cout << "YES\n"; continue; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (word[i][1] == pwd[0] && word[j][0] == pwd[1]) { flag = true; break; } } if (flag) break; } if (flag) cout << "YES\n"; else cout << "NO\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; string a[101]; set<string> ss; ss.clear(); for (int i = 0; i < n; i++) { cin >> a[i]; ss.insert(a[i]); } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { string t = " "; t[0] = a[i][1]; t[1] = a[j][0]; ss.insert(t); t[0] = a[j][1]; t[1] = a[i][0]; ss.insert(t); } cout << (ss.count(s) ? "YES" : "NO") << endl; }
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { string password; cin >> password; int n; cin >> n; vector<string> a(n, ""); for (int i = 0; i < n; ++i) { cin >> a[i]; if (a[i] == password) { cout << "YES\n"; return 0; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { string s = a[i] + a[j]; if (s.find(password) < 4) { cout << "YES\n"; return 0; } } } cout << "NO\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { string str; cin >> str; int cntf[26]; int cntl[26]; memset(cntf, 0, sizeof cntf); memset(cntl, 0, sizeof cntl); int n; cin >> n; bool check = false; while (n--) { string s; cin >> s; if (s == str) check = true; cntf[(s[0] - 'a')] = 1; cntl[(s[1] - 'a')] = 1; } if (check) cout << "YES"; else { int k1 = (str[0] - 'a'); int k2 = (str[1] - 'a'); if (cntl[k1] == 1 && cntf[k2] == 1) cout << "YES"; else cout << "NO"; } return 0; }
#include <bits/stdc++.h> using namespace std; void io() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(15); } string str[104]; int main(int argc, char* argv[]) { io(); string s; cin >> s; int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> str[i]; } for (int i = 1; i <= n; i++) { if (str[i] == s) { cout << "YES\n"; return 0; } for (int j = 1; j <= n; j++) { string ss = ""; ss.push_back(str[i][1]); ss.push_back(str[j][0]); if (ss == s) { cout << "YES\n"; return 0; } } } cout << "NO\n"; return 0; }
#include <bits/stdc++.h> using namespace std; string s[200]; bool f1, f2; int main() { cin >> s[0]; int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; } bool flag = false; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { string t = s[i] + s[j]; for (int k = 0; k < 3; k++) { if (t[k] == s[0][0] && t[k + 1] == s[0][1]) flag = true; } } } printf("%s\n", flag ? "YES" : "NO"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; string str; cin >> str; scanf("%d", &n); string str1[n + 1]; for (int i = 1; i <= n; i++) cin >> str1[i]; int c = 0; for (int i = 1; i <= n; i++) { if (str1[i] == str) { c = 1; break; } } if (c == 0) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if ((str1[i][str1[i].size() - 1] == str[0]) && (str1[j][0] == str[1])) { c = 1; break; } } } } if (c == 1) cout << "YES"; else cout << "NO"; return 0; }
#include <bits/stdc++.h> using namespace std; string a[1000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; string s; cin >> s; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) if (a[i] == s) { cout << "YES" << endl; return 0; } bool b1 = false; bool b2 = false; for (int i = 0; i < n; i++) { if (s[0] == a[i][1]) b2 = true; } bool b3 = false; bool b4 = false; for (int i = 0; i < n; i++) { if (s[1] == a[i][0]) b3 = true; } if (b2 && b3) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int n; cin >> s; cin >> n; int ig = 0, p = 0, se = 0; for (int i = 0; i < n; i++) { string t; cin >> t; if (t == s) ig = 1; if (t[1] == s[0]) p = 1; if (t[0] == s[1]) se = 1; } if ((p && se) || ig) printf("YES\n"); else printf("NO\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, st = 0, st1 = 0, st2 = 0; string s[200], pass; cin >> pass >> n; for (i = 0; i < n; i++) { cin >> s[i]; string t = s[i]; reverse(t.begin(), t.end()); if (t == pass || s[i] == pass) { st = 1; } if (s[i][0] == pass[1]) st1 = 1; if (s[i][1] == pass[0]) st2 = 1; } if (st1 && st2) st = 1; if (st) { cout << "YES" << endl; } else cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int n; cin >> s >> n; string ss; char t[n * 2]; bool con = 0; int hit = 0; int z = 0; int x = 0; for (int i = 0; i < n; i++) { cin >> ss; if (ss == s) { cout << "YES\n"; return 0; } if (s == "ah" && n == 1) { if (ss == "ha") cout << "YES\n"; return 0; } if (ss[0] == s[1]) { x++; } if (ss[1] == s[0]) { z++; } } if (z > 0 && x > 0) { cout << "YES\n"; return 0; } else cout << "NO\n"; return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { char c = getchar(); long long f = 1, x = 0; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ '0'); c = getchar(); } return x * f; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } char s[3]; char b[3]; int main() { scanf("%s", s + 1); long long flag = 0; long long ff = 0, f = 0, fff = 0; long long n = read(); for (int i = 1; i <= n; i++) { memset(b, '\0', sizeof(b)); scanf("%s", b + 1); if (b[1] == s[1] && b[2] == s[2]) { fff = 1; } if (b[1] == s[2] && b[2] == s[1]) { flag = 1; } if (b[2] == s[1]) { f = 1; } if (b[1] == s[2]) { ff = 1; } } if (flag != 0 || ff + f == 2 || fff == 1) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> int size(const T &x) { return x.size(); } const int INF = ~(1 << 31); const double EPS = 1e-9; const double pi = acos(-1); template <class T> T smod(T a, T b) { return (a % b + b) % b; } int main() { string s; cin >> s; int n; cin >> n; vector<string> words(n); for (decltype(0) i = (0); i < (n); ++i) cin >> words[i]; bool flag = false; for (decltype(0) i = (0); i < (n); ++i) if (!flag) for (decltype(0) j = (0); j < (n); ++j) { string t = words[i] + words[j]; if (t.find(s) != string::npos) { flag = true; } } cout << (flag ? "YES" : "NO") << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; string st[n]; for (int i = 0; i < n; ++i) { cin >> st[i]; if (st[i] == s) { cout << "YES"; return 0; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (st[i][1] == s[0] && st[j][0] == s[1]) { cout << "YES\n"; return 0; } } } cout << "NO"; }
#include <bits/stdc++.h> using namespace std; vector<string> v; int main() { string s; cin >> s; int n; scanf("%d", &n); while (n--) { string a; cin >> a; v.push_back(a); for (auto i : v) { string temp = a + i; if (temp.find(s) != -1) return puts("YES"), 0; temp = i + a; if (temp.find(s) != -1) return puts("YES"), 0; } } puts("NO"); }
#include <bits/stdc++.h> using namespace std; int main() { string s; while (cin >> s) { int n; scanf("%d", &n); vector<string> arr; for (int i = 0; i < n; i++) { string ss; cin >> ss; arr.push_back(ss); } bool bisa = false; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; if (arr[i][1] == s[0] && arr[j][0] == s[1]) { bisa = true; } if (arr[j][1] == s[0] && arr[i][0] == s[1]) { bisa = true; } } } for (int i = 0; i < n; i++) { if (arr[i] == s) { bisa = true; } } for (int i = 0; i < n; i++) { reverse(arr[i].begin(), arr[i].end()); if (arr[i] == s) { bisa = true; } } if (bisa) printf("YES\n"); else printf("NO\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int i, j, q, r, t; string s; cin >> s; int n; cin >> n; string a[n]; for (i = 0; i < n; i++) cin >> a[i]; int temp = 0; for (i = 0; i < n; i++) { if (a[i] == s) { temp = 1; cout << "YES"; break; } } if (!temp) { temp = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (a[i][1] == s[0] && a[j][0] == s[1]) { temp = 1; cout << "YES"; break; } } if (temp) break; } if (!temp) { cout << "NO"; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e6 + 5; const int inf = 0x3f3f3f3f; unsigned long long mod = 998244353; int main() { char a[5], b[102][5]; scanf("%s", a); int f1 = 0, f2 = 0, flag = 0, n; cin >> n; for (int i = 1; i <= n; i++) { scanf("%s", b[i]); if (a[0] == b[i][0] && a[1] == b[i][1]) flag = 1; if (a[0] == b[i][1]) f1++; if (a[1] == b[i][0]) f2++; } if (f1 && f2) puts("YES"); else if (flag) puts("YES"); else puts("NO"); }
#include <bits/stdc++.h> using namespace std; const int M = 1e5 + 10; string s, t; bool fir, sec; int n; int main() { cin >> s >> n; while (n--) { string t; cin >> t; if (t == s) return cout << "YES" << endl, 0; if (t[1] == s[0]) fir = true; if (t[0] == s[1]) sec = true; } if (fir && sec) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int i, n, c = 0, flag = 0, j; string s1, s2[105]; cin >> s1; cin >> n; for (i = 0; i < n; i++) { cin >> s2[i]; if (s2[i] == s1 || (s1[0] == s2[i][1] && s1[1] == s2[i][0])) flag = 1; } if (flag == 1) { cout << "YES"; return 0; } else { for (i = 0; i < n; i++) { if (s2[i][1] == s1[0]) { for (j = 0; j < n; j++) { if (s2[j][0] == s1[1]) { cout << "YES"; return 0; } } } } } cout << "NO"; return 0; }
#include <bits/stdc++.h> using namespace std; char a[100005]; char b[100005]; int main() { string s, ans, tmp; cin >> s; int n, i, j; cin >> n; for (i = 0; i < n; i++) { cin >> tmp; a[i] = tmp[0]; b[i] = tmp[tmp.size() - 1]; if (i == 0) ans = tmp; else ans = ans + tmp; } int len = ans.size(); int flag = 0; for (i = 0; i < len - 1; i++) { if (ans[i] == s[0] && ans[i + 1] == s[1]) { flag = 1; break; } } if (flag) printf("YES"); else { for (i = 0; i < n; i++) { if (b[i] == s[0]) { for (j = 0; j < n; j++) { if (a[j] == s[1]) { flag = 1; break; } } if (flag) break; } } if (flag) printf("YES"); else printf("NO"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1e5 + 5; int n; int main() { char s1[5], s2[5]; scanf("%s", s1); scanf("%d", &n); bool f1, f2; f1 = f2 = false; for (int i = 0; i < n; i++) { scanf("%s", s2); if (s2[0] == s1[0] && s2[1] == s1[1]) { f1 = f2 = true; } if (s2[0] == s1[1]) f1 = true; if (s2[1] == s1[0]) f2 = true; } if (f1 && f2) puts("YES"); else puts("NO"); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e3; const int INF = 1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); char c1, c2; cin >> c1 >> c2; int n; bool f1 = 0, f2 = 0; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; if (s[1] == c1) f2 = 1; if (s[0] == c2) f1 = 1; if (s[0] == c1 && s[1] == c2) { f1 = 1; f2 = 1; } } if (f1 && f2) cout << "YES" << "\n"; else cout << "NO" << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string x; cin >> x; int n, i, j; cin >> n; string s[n], c; string p; for (int i = 0; i < n; i++) { cin >> s[i]; } for (i = 0; i < n; i++) for (j = 0; j < n; j++) p += s[i] + s[j]; if (p.find(x) != std::string::npos) { { cout << "YES"; return 0; } } cout << "NO"; }
#include <bits/stdc++.h> using namespace std; const int N = 257; const int INF = 1e9 + 123; const int MOD = 1e9 + 7; const double EPS = 1e-9; const int dx[] = {0, 0, 1, -1}; const int dy[] = {1, -1, 0, 0}; string pass; int n, first[N], last[N]; int main() { ios_base ::sync_with_stdio(0); cin >> pass; cin >> n; bool ok = 0; for (int i = 1; i <= n; ++i) { string s; cin >> s; if (s == pass) ok = 1; first[s[0]] = 1; last[s[1]] = 1; } int fp = pass[0], lp = pass[1]; if ((last[fp] && first[lp]) || ok) cout << "YES\n"; else cout << "NO\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string p; cin >> p; int n; cin >> n; bool a = false, b = false; while (n--) { string s; cin >> s; if (s == p) { a = true; b = true; } if (s[0] == p[1]) a = true; if (s[1] == p[0]) b = true; } if (a && b) cout << "YES"; else cout << "NO"; return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 1000; bool flag1[105], flag2[105]; int main() { memset(flag1, 0, sizeof flag1); memset(flag2, 0, sizeof(flag2)); char s[10], s1[10]; cin >> s1; int t; cin >> t; int k = t; int flag = 0; while (t--) { cin >> s; flag1[s[1] - 'a'] = true; flag2[s[0] - 'a'] = true; if (strcmp(s, s1) == 0) flag = 1; } if (flag1[s1[0] - 'a'] == true && flag2[s1[1] - 'a'] == true || flag == 1) { cout << "YES"; } else if (strcmp(s, s1) == 0 && k == 1) cout << "YES"; else cout << "NO"; }
#include <bits/stdc++.h> using namespace std; char ss[100][10]; char psd[10]; int main() { scanf("%s", psd); int n; scanf("%d", &n); bool fg = false; for (int i = 1; i <= n; i++) { scanf("%s", ss[i]); if (ss[i][0] == psd[0] && ss[i][1] == psd[1]) fg = true; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (ss[i][1] == psd[0] && ss[j][0] == psd[1]) fg = true; } if (fg) puts("YES"); else puts("NO"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; string me[n]; int i, flag = 0; for (i = 0; i < n; i++) { cin >> me[i]; if (me[i] == s) flag = 1; } int j, flag1 = 0, flag2 = 0; for (i = 0; i < n; i++) { if (me[i][1] == s[0]) flag1 = 1; if (me[i][0] == s[1]) flag2 = 1; } if (flag1 + flag2 == 2 || flag == 1) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e18; const int infInt = 1e9; const long long int mod = 1000000007; string s[110], s1; int n; int main() { cin.sync_with_stdio(0); cin.tie(0); cin >> s1; cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; } bool yes = false; for (int i = 1; i <= n; i++) { if (s1 == s[i]) { yes = true; break; } for (int j = 1; j <= n; j++) { string t = s[i] + s[j]; if (t.substr(1, 2) == s1) yes = true; } } if (yes) cout << "YES" << "\n"; else cout << "NO" << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char pass[3]; cin >> pass; pass[2] = '\0'; int n; cin >> n; char a[n][3]; int s[27] = {0}; int end[27] = {0}; bool flag = false; ; for (int i = 0; i < n; i++) { cin >> a[i]; s[a[i][0] - 97]++; end[a[i][1] - 97]++; if (pass[0] == a[i][0] && pass[1] == a[i][1]) { flag = true; } } if (flag == true) { cout << "YES\n"; } else if (end[pass[0] - 97] > 0 && s[pass[1] - 97] > 0) { cout << "YES\n"; } else { cout << "NO\n"; } }