malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   #pragma GCC optimize( unroll-loops )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const long long mod = 1000000007;   const int mxN = 100010;   vector<int> g[mxN];   int main(int argc, char** argv) {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());     int N, M, u, v;     cin >> N >> M;     for (int i = 0; i < M; i++) {       cin >> u >> v;       u--, v--;       g[u].push_back(v);       g[v].push_back(u);     }     vector<int> a;     for (int i = 0; i < N; i++) a.push_back(i);     for (int iti = 0; iti < 1000; iti++) {       shuffle(a.begin(), a.end(), rng);       int prev = -10;       vector<pair<int, int> > res;       for (int i = 0; i + 1 < N && (int)res.size() < M; i++) {         u = a[i];         v = a[i + 1];         bool f = find(g[u].begin(), g[u].end(), v) == g[u].end();         if (!f && prev == -10) break;         if (f) {           res.push_back({u, v});           if (prev == -10) prev = i;         } else {           if (prev != i - 1) {             bool f2 = find(g[u].begin(), g[u].end(), a[prev]) == g[u].end();             if (f2) {               res.push_back({u, a[prev]});             }           }           prev = -10;         }       }       if (prev != -10 && prev != N - 2 && (int)res.size() + 1 == M) {         u = a[N - 1];         bool f2 = find(g[u].begin(), g[u].end(), a[prev]) == g[u].end();         if (f2) {           res.push_back({u, a[prev]});         }         prev = -10;       }       if ((int)res.size() == M) {         for (auto p : res) cout << p.first + 1 <<     << p.second + 1 <<   n ;         return 0;       }     }     cout <<  -1 n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, sz[100005], msz[100005], xsz[100005], lsz[100005], son[100005],       ans[100005];   vector<int> G[100005];   void dfs(int u) {     sz[u] = 1;     lsz[u] = n;     son[u] = -1;     for (int v : G[u]) {       dfs(v), sz[u] += sz[v];       if (son[u] == -1 || sz[v] > sz[son[u]]) son[u] = v;       if (sz[v] > msz[u])         xsz[u] = msz[u], msz[u] = sz[v];       else         xsz[u] = max(xsz[u], sz[v]);       lsz[u] = min(lsz[u], sz[v]);     }     if (n - sz[u] > msz[u])       xsz[u] = msz[u], msz[u] = n - sz[u];     else       xsz[u] = max(xsz[u], n - sz[u]);     if (n - sz[u]) lsz[u] = min(lsz[u], n - sz[u]);   }   multiset<int> st, st2;   void ser1(int u) {     st.insert(sz[u]);     for (int v : G[u]) ser1(v);   }   void dfs1(int u) {     for (int v : G[u])       if (v != son[u]) {         dfs1(v);         st.clear();       }     if (son[u] != -1) dfs1(son[u]);     if (n - sz[u] != msz[u]) {       int L = xsz[u], R = msz[u], mid;       for (; L < R;) {         mid = L + R >> 1;         multiset<int>::iterator it = st.lower_bound(msz[u] - mid);         if (it != st.end() && (*it) <= mid - lsz[u])           R = mid;         else           L = mid + 1;       }       ans[u] = L;     }     for (int v : G[u])       if (v != son[u]) ser1(v);     st.insert(sz[u]);   }   void ser2(int u) {     st.erase(st.find(sz[u]));     for (int v : G[u]) ser2(v);   }   void dfs2(int u) {     st.erase(st.find(sz[u]));     for (int v : G[u])       if (v != son[u]) {         st2.insert(sz[u]);         dfs2(v);         ser1(v);         st2.erase(st2.find(sz[u]));       }     if (son[u] != -1) {       int v = son[u];       st2.insert(sz[u]);       dfs2(v);       st2.erase(st2.find(sz[u]));     }     for (int v : G[u])       if (v != son[u]) ser2(v);     if (n - sz[u] == msz[u]) {       int L = xsz[u], R = msz[u], mid;       for (; L < R;) {         mid = L + R >> 1;         multiset<int>::iterator it = st.lower_bound(msz[u] - mid),                                 it2 = st2.lower_bound(msz[u] - mid + sz[u]);         if (it != st.end() && (*it) <= mid - lsz[u])           R = mid;         else if (it2 != st2.end() && (*it2) <= mid - lsz[u] + sz[u])           R = mid;         else           L = mid + 1;       }       ans[u] = L;     }   }   int main() {     scanf( %d , &n);     for (int i = (1), LIM = (n); i <= LIM; i++) {       int u, v;       scanf( %d%d , &u, &v);       G[u].push_back(v);     }     dfs(0);     dfs1(G[0][0]);     dfs2(G[0][0]);     for (int i = (1), LIM = (n); i <= LIM; i++) printf( %d n , ans[i]);   }
#include <bits/stdc++.h>   using namespace std;   long long gcd(long long first, long long second) {     return !second ? first : gcd(second, first % second);   }   long long x, y, z;   deque<long long> q, p;   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);     ;     cin >> x;     for (int i = 1; i <= x; i++) {       if (i % 2 == 0) {         z += i;         q.push_back(i);       } else {         y += i;         p.push_back(i);       }     }     if (gcd(y, z) > 1) {       cout <<  Yes  << endl;       cout << q.size() <<    ;       while (!q.empty()) {         cout << q.front() <<    ;         q.pop_front();       }       cout << endl;       cout << p.size() <<    ;       while (!p.empty()) {         cout << p.front() <<    ;         p.pop_front();       }       cout << endl;     } else {       cout <<  No  << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long ans, cr[40002], ar[40005], br[40005], nd[40005];   void build(int id, int l, int r) {     if (l == r) {       nd[id] = cr[l];       return;     }     int md = (l + r) >> 1;     build(id * 2, l, md);     build(id * 2 + 1, md + 1, r);     nd[id] = min(nd[2 * id], nd[2 * id + 1]);     ans -= nd[id];   }   int main() {     long long i, j, k, n, m, x, mx;     cin >> n;     queue<long long> Q;     m = (1 << (n + 1)) - 2;     Q.push(2);     Q.push(3);     mx = 0;     k = 0;     for (i = 1; i <= m; i++) {       cin >> x;       long long tp = Q.front();       Q.pop();       if (tp >= (1 << (n))) cr[k++] = x;       ar[tp] = x;       br[tp] = x + br[tp / 2];       mx = max(mx, br[tp]);       Q.push(2 * tp);       Q.push(2 * tp + 1);     }     ans = 0;     int p = (1 << n);     k = 1;     while (1) {       cr[k] = mx - br[p];       ans += cr[k];       k++;       p++;       if (p > (m + 1)) break;     }     build(1, 1, (m + 2));     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1002020, mod = 1e9 + 7;   int m, tot, ans = 1, mu[N], iv[N], pr[N];   bool vis[N];   void prework(int mx) {     iv[1] = mu[1] = 1;     for (int i = 2; i <= mx; i++) {       iv[i] = 1ll * iv[mod % i] * (mod - mod / i) % mod;       if (!vis[i]) pr[++tot] = i, mu[i] = -1;       for (int j = 1; j <= tot && i * pr[j] <= mx; j++) {         vis[i * pr[j]] = 1;         if (!(i % pr[j])) break;         mu[i * pr[j]] = -mu[i];       }     }   }   int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   int main() {     m = read();     prework(m);     for (int i = 2; i <= m; i++)       ans = (ans - 1ll * mu[i] * (m / i) * iv[m - m / i] % mod) % mod;     cout << (ans + mod) % mod << endl;     return 0;   }
#include <bits/stdc++.h>   int main() {     int HPY, ATKY, DEFY, HPM, ATKM, DEFM, h, a, d;     while (scanf( %d %d %d , &HPY, &ATKY, &DEFY) == 3) {       scanf( %d %d %d , &HPM, &ATKM, &DEFM);       scanf( %d %d %d , &h, &a, &d);       int mini = 1000000000;       for (int i = ATKY; i <= 301; i++)         for (int j = DEFY; j <= 301; j++) {           if (i - DEFM > 0) {             int cost = (i - ATKY) * a + (j - DEFY) * d;             int p1 = i - DEFM;             int k1 = HPM / p1;             if (HPM % p1 != 0) k1++;             int p2 = ATKM - j;             if (p2 > 0) {               int p3 = p2 * k1 + 1;               if (HPY < p3) cost += (p3 - HPY) * h;             }             if (cost < mini) mini = cost;           }         }       printf( %d n , mini);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double EPS = 1e-9;   int INF = 2000000000;   long long INFF = 8000000000000000000LL;   double PI = acos(-1);   int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};   int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};   inline string IntToString(int a) {     char x[100];     sprintf(x,  %d , a);     string s = x;     return s;   }   inline int StringToInt(string a) {     char x[100];     int res;     strcpy(x, a.c_str());     sscanf(x,  %d , &res);     return res;   }   inline string GetString(void) {     char x[1000005];     scanf( %s , x);     string s = x;     return s;   }   inline string uppercase(string s) {     int n = (int)s.size();     for (int(i) = (0); (i) < (n); ++(i))       if (s[i] >=  a  && s[i] <=  z ) s[i] = s[i] -  a  +  A ;     return s;   }   inline string lowercase(string s) {     int n = (int)s.size();     for (int(i) = (0); (i) < (n); ++(i))       if (s[i] >=  A  && s[i] <=  Z ) s[i] = s[i] -  A  +  a ;     return s;   }   inline void OPEN(string s) {     freopen((s +  .in ).c_str(),  r , stdin);     freopen((s +  .out ).c_str(),  w , stdout);   }   vector<int> angka;   int n, data[2005];   long long dewa[2005];   long long MOD = 1000000007;   bool sudah[2005];   int to[2005];   int conn;   int pisah;   long long risan;   long long C[2005][2005];   int main() {     scanf( %d , &n);     for (int(i) = (1); (i) <= (n); ++(i)) {       scanf( %d , &data[i]);       to[i] = data[i];     }     for (int(i) = (0); (i) <= (n); ++(i)) {       C[i][0] = C[i][i] = 1;       for (int(j) = (1); (j) <= (i - 1); ++(j))         C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;     }     dewa[1] = 0;     dewa[2] = 1;     dewa[0] = 1;     for (int(i) = (3); (i) <= (n); ++(i)) {       dewa[i] = (i - 1) * (dewa[i - 1] + dewa[i - 2]);       dewa[i] %= MOD;     }     memset(sudah, 0, sizeof(sudah));     for (int(i) = (1); (i) <= (n); ++(i)) {       if (to[i] == -1) continue;       int x = i;       while (x != -1) {         if (sudah[x]) goto hell;         sudah[x] = 1;         x = to[x];         if (x == i) goto hell;       }       ++conn;     hell:;     }     for (int(i) = (1); (i) <= (n); ++(i))       if (!sudah[i]) ++pisah;     for (int(i) = (0); (i) <= (conn); ++(i)) {       long long pos = (C[conn][i] * dewa[i + pisah]) % MOD;       risan = (risan + pos) % MOD;     }     printf( %d n , (int)risan);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10;   int n, tot, u, v;   int c[N], head[N], last[N * 2], ver[N * 2];   void add(int x, int y) {     ver[++tot] = y;     last[tot] = head[x];     head[x] = tot;   }   void dfs(int x, int fa) {     cout << x <<    ;     for (int i = head[x]; i; i = last[i]) {       int y = ver[i];       if (y == fa) continue;       c[y] *= -1;       dfs(y, x);     }     if (c[x] == -1 && x != 1) {       cout << fa <<     << x <<    ;       c[fa] *= -1;       c[x] *= -1;     }     if (x != 1) {       if (fa == 1) {         for (int i = 1; i <= n; i++) {           if (c[i] == -1) {             cout << fa <<    ;             c[fa] *= -1;             return;           }         }       } else {         cout << fa <<    ;         c[fa] *= -1;       }     }   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) cin >> c[i];     for (int i = 1; i < n; i++) {       cin >> u >> v;       add(u, v);       add(v, u);     }     dfs(1, -1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char board[2048][2048];   int rows[2][2048];   int cols[2][2048];   int main() {     int N;     scanf( %d , &N);     for (int i = 0; i < N; i++)       for (int j = 0; j < N; j++) scanf(  %c , &board[i][j]);     int ans = 0;     for (int x = 0; x < 2; x++) {       for (int i = 0; i < N - 1; i++)         for (int j = N - 1; j > i; j--) {           int d = rows[x][i] ^ cols[x][j];           if (!d != (board[i][j] ==  0 )) {             ans++;             rows[x][i] ^= 1;             cols[x][j] ^= 1;           }           board[i][j] = board[j][i];         }     }     for (int i = 0; i < N; i++)       if (!(rows[0][i] ^ rows[1][i] ^ cols[0][i] ^ cols[1][i]) !=           (board[i][i] ==  0 ))         ans++;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     char a[n][5];     for (int i = 0; i < n; i++) {       for (int j = 0; j < 5; j++) cin >> a[i][j];     }     int flag = 0;     for (int i = 0; i < n; i++) {       if ((a[i][0] ==  O  && a[i][1] ==  O )) {         a[i][0] =  + ;         a[i][1] =  + ;         flag = 1;         break;       } else if ((a[i][3] ==  O  && a[i][4] ==  O )) {         a[i][3] =  + ;         a[i][4] =  + ;         flag = 1;         break;       }     }     if (flag) {       cout <<  YES  << endl;       for (int i = 0; i < n; i++) {         for (int j = 0; j < 5; j++) {           cout << a[i][j];         }         cout << endl;       }     } else       cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   double l;   int x[1005], b[1005];   const double eps = 1e-8;   double dp[1005];   double check(double mid) {     for (int i = 1; i <= n; i++) dp[i] = -1e10;     dp[0] = 0;     for (int i = 1; i <= n; i++) {       for (int j = 0; j < i; j++) {         dp[i] = max(dp[i],                     dp[j] - mid * (sqrt(abs(l - (x[i] - x[j])))) + (double)b[i]);       }     }     return dp[n];   }   void dfs(int step, double mid) {     if (step == 0) return;     for (int i = 0; i < step; i++) {       if (abs(dp[step] - (dp[i] - mid * sqrt(abs(x[step] - x[i] - l)) +                           (double)b[step])) < eps)         dfs(i, mid);     }     printf( %d  , step);   }   int main() {     scanf( %d%lf , &n, &l);     for (int i = 1; i <= n; i++) {       scanf( %d%d , &x[i], &b[i]);     }     double l = 0, r = 100000000000;     while (l + eps < r) {       double mid = (l + r) / 2;       double qwq = check(mid);       if (qwq > -eps)         l = mid;       else         r = mid;     }     check(l);     dfs(n, l);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int t, n, m, k, a, b, ans;   int maxl = 1e9;   int num[300005];   int main() {     scanf( %d , &t);     while (t--) {       scanf( %d%d%d , &n, &m, &k);       if (m <= k) k = m - 1;       a = n - m + 1;       b = n - k;       for (int i = 1; i <= n; i++) {         scanf( %d , &num[i]);       }       ans = 0;       for (int i = 1; i <= n; i++) {         if (i + b - 1 > n) break;         int ansl = maxl;         for (int j = i; j <= i + b - 1; j++) {           if (j + a - 1 > i + b - 1) break;           ansl = min(ansl, max(num[j], num[j + a - 1]));         }         ans = max(ans, ansl);       }       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int n, c0, c1, h;       cin >> n >> c0 >> c1 >> h;       string s;       cin >> s;       int ans = 0;       int cnt = 0;       int mini = min(c0, c1);       for (int i = 0; i < n; i++) {         if (s[i] ==  1 ) {           ans += c1;           if (c1 != mini) cnt++;         } else {           ans += c0;           if (c0 != mini) cnt++;         }       }       ans = min(ans, ((n * mini) + (h * cnt)));       cout << ans << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 1e5 + 10;   long long n, Mx, B[N], S[N];   struct Node {     int32_t l, r, c;     Node() { l = r = c = 0; }   };   struct BST {     long long L = 32;     vector<Node> A;     BST(long long _L = 32) {       L = _L, A.clear();       A.push_back(Node());     }     void Add(long long val, long long w = 1) {       long long id = 0;       for (long long i = L - 1; ~i; i--) {         A[id].c += w;         if (val & (1ll << i)) {           if (!A[id].r) A[id].r = A.size(), A.push_back(Node());           id = A[id].r;           continue;         }         if (!A[id].l) A[id].l = A.size(), A.push_back(Node());         id = A[id].l;       }       A[id].c += w;     }     long long MaxXor(long long val) {       long long id = 0, ret = 0;       if (!A[id].c) return (-1);       for (long long i = L - 1; ~i; i--) {         if (A[id].r && (!A[id].l || !(val & (1ll << i))))           ret |= (1ll << i), id = A[id].r;         else           id = A[id].l;       }       return (ret);     }   };   int32_t main() {     scanf( %lld , &n);     BST T = BST(43);     T.Add(0);     for (long long i = 1; i <= n; i++) scanf( %lld , &B[i]);     for (long long i = n; i; i--) S[i] = S[i + 1] ^ B[i], T.Add(S[i]);     for (long long i = 0, r = 0; i <= n; i++, r ^= B[i])       Mx = max(Mx, T.MaxXor(r) ^ r), T.Add(S[i + 1], -1);     return !printf( %lld , Mx);   }
#include <bits/stdc++.h>   using namespace std;   unsigned long long k, n, d = 0, ans;   string s;   unsigned long long int sol1(int k);   int main() {     cin >> s;     n = s.size();     cin >> k;     ans = max(0ULL, min(2 * k, n + k - ((n + k) % 2)));     ans = max(ans, sol1(k));     cout << ans << endl;     const clock_t begin_time = clock();     return 0;   }   unsigned long long int sol1(int t) {     int bop = 0;     int anss = 0;     for (int i = 0; i < n; i++) {       for (int j = 1; j <= n; j++) {         bop = 0;         if (i + 2 * j - 1 >= n + t) {           bop = 1;         } else           for (int k = 0; k < j; k++) {             if (i + j + k < n) {               if (s[i + k] != s[i + j + k]) {                 bop = 1;                 break;               }             }           }         if (bop == 0) {           anss = max(anss, 2 * j);         }       }     }     return anss;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n;     while (cin >> n) {       int flag = 0;       long long int i;       for (int sx = 1; sx < 100; sx++) {         long long int x = (sqrt(sx * sx + 4 * n) - sx) / 2;         if (x * x + sx * x == n) {           int tem = x;           int t = 0;           while (tem > 0) {             t = t + tem % 10;             tem = tem / 10;           }           if (sx == t) {             flag = 1;             printf( %d n , x);             break;           }         }       }       if (!flag) printf( -1 n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Block {     long long l, r, c;     Block(long long l, long long r, long long c) : l(l), r(r), c(c) {}   };   bool operator<(Block a, Block b) {     if (a.l != b.l) return a.l < b.l;     if (a.r != b.r) return a.r < b.r;     return a.c < b.c;   }   const int maxn = 1000000 + 10;   long long tr_a[maxn], tr_b[maxn];   void add(long long tr[maxn], long long at, long long what) {     for (++at; at < maxn; at += -at & at) tr[at] += what;   }   void modify(long long l, long long r, long long c) {     add(tr_a, l, c);     add(tr_b, l, (l - 1) * c);     add(tr_a, r + 1, -c);     add(tr_b, r + 1, -r * c);   }   long long sum(long long tr[maxn], long long at) {     long long s = 0;     for (++at; at > 0; at -= -at & at) s += tr[at];     return s;   }   long long query(long long l, long long r) {     long long sr = r * sum(tr_a, r) - sum(tr_b, r);     long long sl = (l - 1) * sum(tr_a, l - 1) - sum(tr_b, l - 1);     return sr - sl;   }   int main() {     ios_base::sync_with_stdio(false);     memset((tr_a), (0), sizeof(tr_a)), memset((tr_b), (0), sizeof(tr_b));     int n, q;     cin >> n >> q;     set<Block> blocks;     for (int _n(n + 1), i(1); i < _n; i++) blocks.insert(Block(i, i, i));     for (int _n(q), _(0); _ < _n; _++) {       long long op, l, r, x;       cin >> op >> l >> r;       if (op == 1) {         cin >> x;         auto it = blocks.upper_bound(Block(l, 1011111111, 1011111111));         --it;         while (it != blocks.end()) {           if (it->l > r) break;           Block tmp = *it;           modify(max(l, tmp.l), min(r, tmp.r), labs(tmp.c - x));           if (tmp.l < l) {             blocks.insert(Block(tmp.l, l - 1, tmp.c));           }           if (tmp.r > r) {             blocks.insert(Block(r + 1, tmp.r, tmp.c));           }           ++it;           blocks.erase(blocks.find(tmp));         }         blocks.insert(Block(l, r, x));       } else {         long long ans = query(l, r);         cout << ans << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e3 + 5;   int n, a[maxn], belong[maxn], bn = 1;   int ans[maxn][maxn];   struct node {     int l, r;   } block[maxn];   bool cmp(node x, node y) { return (x.r - x.l) > (y.r - y.l); }   int main() {     int l, r, flag;     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i], belong[i] = 1;     belong[0] = 1;     block[1].l = 0;     block[1].r = n;     for (int i = 1; i <= n; i++) {       sort(block + 1, block + 1 + bn, cmp);       flag = 0;       int bto = bn;       for (int b = 1; b <= bto; b++) {         l = block[b].l;         r = block[b].r;         if (a[i] > bto - b + 1) {           if (flag == 0)             while (a[i] > bto - b && l < r) ans[i][l++] = 1, a[i]--;           else             while (a[i] > bto - b && l <= r) ans[i][l++] = 1, a[i]--;           if (--l < r) {             flag = 1;             block[++bn].l = block[b].l;             block[bn].r = l;             block[b].l = l + 1;             for (int j = block[bn].l; j <= l; j++) belong[j] = bn;           }         } else if (a[i] > 0) {           ans[i][l] = 1;           a[i]--;           if (l < r) {             flag = 1;             belong[l] = ++bn;             block[bn].l = block[bn].r = l;             block[b].l = l + 1;           }         }       }     }     cout << bn << endl;     for (int j = 0; j <= n; j++) {       for (int i = 1; i <= n; i++) {         cout << ans[i][j];       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 1505;   const int dx[] = {-1, 0, 1, 0};   const int dy[] = {0, 1, 0, -1};   int visx[MAX_N][MAX_N];   int visy[MAX_N][MAX_N];   char A[MAX_N][MAX_N];   int sx, sy;   int n, m;   queue<pair<int, int> > q;   bool in(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> A[i];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (A[i][j] ==  S ) {           A[i][j] =  . ;           sx = i, sy = j;         }       }     }     sx += n * m;     sy += n * m;     q.push(make_pair(sx, sy));     memset(visx, -1, sizeof(visx));     memset(visy, -1, sizeof(visy));     while (!q.empty()) {       pair<int, int> t = q.front();       q.pop();       if (A[(t.first + n) % n][(t.second + m) % m] ==  # ) continue;       if (visx[(t.first + n) % n][(t.second + m) % m] == t.first &&           visy[(t.first + n) % n][(t.second + m) % m] == t.second)         continue;       if (visx[(t.first + n) % n][(t.second + m) % m] == -1)         visx[(t.first + n) % n][(t.second + m) % m] = t.first;       if (visy[(t.first + n) % n][(t.second + m) % m] == -1)         visy[(t.first + n) % n][(t.second + m) % m] = t.second;       if (visx[(t.first + n) % n][(t.second + m) % m] != t.first ||           visy[(t.first + n) % n][(t.second + m) % m] != t.second) {         cout <<  Yes  << endl;         return 0;       }       for (int i = 0; i < 4; i++) {         int nx = dx[i] + t.first;         int ny = dy[i] + t.second;         q.push(make_pair(nx, ny));       }     }     cout <<  No  << endl;     return 0;   }