malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const long long int maxN = 5005;   long long int n;   vector<long long int> a, coords, keys;   long long int cnt[maxN];   long long int getKey(long long int x) {     if (x == 0) return 0;     vector<long long int> ans;     if (x < 0) ans.push_back(-1);     x = abs(x);     for (long long int i = 2; i * i <= x; i++) {       if (x % i == 0) {         long long int cnt = 0;         while (x % i == 0) {           cnt++;           x /= i;         }         if (cnt % 2 == 1) ans.push_back(i);       }     }     if (x > 1) ans.push_back(x);     long long int res = 1;     for (auto i : ans) res *= i;     return res;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     a.resize(n);     keys.resize(n);     for (long long int i = 0; i < n; i++) cin >> a[i];     for (long long int i = 0; i < n; i++) {       long long int key = getKey(a[i]);       keys[i] = key;       coords.push_back(key);     }     sort(coords.begin(), coords.end());     coords.erase(unique(coords.begin(), coords.end()), coords.end());     for (long long int i = 0; i < n; i++)       keys[i] =           (lower_bound(coords.begin(), coords.end(), keys[i]) - coords.begin());     vector<long long int> ans(n + 1, 0);     for (long long int i = 0; i < n; i++) {       for (long long int j = 0; j < maxN; j++) cnt[j] = 0;       long long int cntZ = 0, cntD = 0;       for (long long int j = i; j < n; j++) {         long long int key = keys[j];         if (coords[key] == 0)           cntZ++;         else {           cnt[key]++;           if (cnt[key] == 1) cntD++;         }         if (cntD > 0)           ans[cntD]++;         else           ans[cntZ > 0]++;       }     }     for (long long int i = 1; i <= n; i++) cout << ans[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int h[5005], pref[5005], dp[5005];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , &h[i]);       pref[i] = pref[i - 1] + h[i];       for (int j = i - 1; j >= 0; j--) {         if (pref[i] - pref[j] >= h[j]) {           dp[i] = dp[j] + i - j - 1;           h[i] = pref[i] - pref[j];           break;         }       }     }     printf( %d n , dp[n]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 12;   const int M = 305;   const int INF = 1e9;   int n;   int a[N][N];   pair<int, int> pos[N * N];   pair<int, int> dist[M][M];   pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) {     return make_pair(a.first + b.first, a.second + b.second);   }   int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};   int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};   bool in(int first, int second) {     return (0 <= first && first < n && 0 <= second && second < n);   }   int get(int first, int second, int p) { return first * n * 3 + second * 3 + p; }   pair<int, int> dp[N * N][3];   int main() {     scanf( %d , &n);     for (int i = 0; i < int(n); i++)       for (int j = 0; j < int(n); j++) {         scanf( %d , &a[i][j]);         --a[i][j];         pos[a[i][j]] = make_pair(i, j);       }     for (int i = 0; i < int(M); i++)       for (int j = 0; j < int(M); j++) dist[i][j] = make_pair(INF, INF);     for (int i = 0; i < int(M); i++) dist[i][i] = make_pair(0, 0);     for (int first = 0; first < int(n); first++)       for (int second = 0; second < int(n); second++) {         for (int i = 0; i < int(8); i++) {           int nx = first + dx[i];           int ny = second + dy[i];           if (in(nx, ny))             dist[get(first, second, 0)][get(nx, ny, 0)] = make_pair(1, 0);         }         for (int i = -n + 1; i <= n - 1; ++i) {           int nx = first + i;           int ny = second + i;           if (in(nx, ny))             dist[get(first, second, 1)][get(nx, ny, 1)] = make_pair(1, 0);           ny = second - i;           if (in(nx, ny))             dist[get(first, second, 1)][get(nx, ny, 1)] = make_pair(1, 0);         }         for (int i = 0; i < int(n); i++) {           int nx = first;           int ny = i;           dist[get(first, second, 2)][get(nx, ny, 2)] = make_pair(1, 0);           nx = i;           ny = second;           dist[get(first, second, 2)][get(nx, ny, 2)] = make_pair(1, 0);         }         for (int i = 0; i < int(3); i++)           for (int j = 0; j < int(3); j++) {             if (i != j) {               dist[get(first, second, i)][get(first, second, j)] =                   make_pair(1, 1);             }           }       }     for (int k = 0; k < int(M); k++)       for (int i = 0; i < int(M); i++)         for (int j = 0; j < int(M); j++)           dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);     for (int i = 0; i < int(N * N); i++)       for (int j = 0; j < int(3); j++) dp[i][j] = make_pair(INF, INF);     dp[0][0] = dp[0][1] = dp[0][2] = make_pair(0, 0);     for (int i = 0; i < int(n * n - 1); i++)       for (int j = 0; j < int(3); j++)         for (int k = 0; k < int(3); k++)           dp[i + 1][k] =               min(dp[i + 1][k],                   dp[i][j] + dist[get(pos[i].first, pos[i].second, j)]                                  [get(pos[i + 1].first, pos[i + 1].second, k)]);     pair<int, int> ans = make_pair(INF, INF);     ans = min(ans, dp[n * n - 1][0]);     ans = min(ans, dp[n * n - 1][1]);     ans = min(ans, dp[n * n - 1][2]);     printf( %d %d n , ans.first, ans.second);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   long long a[N], sum[N];   long long int gcd(long long int a, long long int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   int main() {     int n;     scanf( %d , &n);     int i;     long long ans = 0;     for (i = 1; i <= n; i++) {       scanf( %I64d , &a[i]);       ans = ans + a[i];     }     sort(a + 1, a + n + 1);     for (i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];     for (i = 1; i <= n; i++) {       int l = 1, r = n;       int idx = -1;       while (l <= r) {         int mid = (l + r) / 2;         if (a[mid] <= a[i]) {           l = mid + 1;           idx = mid;         } else           r = mid - 1;       }       ans = ans + (idx)*1LL * a[i] - sum[idx] + sum[n] - sum[idx] -             (n - idx) * 1LL * a[i];     }     long long g = gcd(ans, 1LL * n);     ans = ans / g;     n = n / g;     printf( %I64d %d n , ans, n);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MAXN = 3e5 + 5;   long long a[MAXN], kkorz[MAXN], bit[MAXN];   long long n;   long long sum(long long idx) {     long long res = 0;     for (; idx > 0; idx -= (idx & (-idx))) res += bit[idx];     return res;   }   void upd(long long idx, long long x) {     for (; idx <= n; idx += (idx & (-idx))) bit[idx] += x;   }   long long countInv() {     vector<pair<long long, long long>> v;     for (long long i = 0; i < n; i++) v.push_back(make_pair(kkorz[i], i));     sort(v.begin(), v.end());     for (long long i = 0; i < n; i++) kkorz[v[i].second] = i + 1;     long long res = 0;     for (long long i = 1; i <= n; i++) bit[i] = 0;     for (long long i = n - 1; i >= 0; i--) {       res += sum(kkorz[i] - 1);       upd(kkorz[i], 1);     }     return res;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (long long i = 0; i < n; i++) cin >> a[i];     long long maxbit = 32 - __builtin_clz(*max_element(a, a + n));     for (long long i = 0; i < n; i++) kkorz[i] = a[i];     long long ans = countInv(), minx = 0, x = 0;     for (long long b = 0; b <= maxbit; b++) {       long long tmp = x | (1 << b);       for (long long i = 0; i < n; i++) kkorz[i] = a[i] ^ tmp;       long long i1 = countInv();       if (i1 < ans) {         x |= (1 << b);         ans = i1;         minx = x;       }     }     cout << ans <<     << minx <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 7;   const int inf = INT_MAX;   const long long inff = 1e18;   const long long mod = 1e9 + 7;   vector<int> dx = {0, 1, 1, 1, 0, -1, -1, -1};   vector<int> dy = {1, 1, 0, -1, -1, -1, 0, 1};   int can[507][507];   set<pair<int, int> > second[32][9];   int32_t main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     int a[35];     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int j = 250; j <= 250 + a[1] - 1; j++) can[250][j] = 1;     second[1][0].insert(make_pair(250, 250 + a[1] - 1));     for (int i = 1; i < n; i++) {       for (int j = 0; j < 8; j++) {         for (pair<int, int> k : second[i][j]) {           for (int l = 1; l <= a[i + 1]; l++) {             can[k.first + dx[(j + 1) % 8] * l][k.second + dy[(j + 1) % 8] * l] =                 1;             can[k.first + dx[(j + 7) % 8] * l][k.second + dy[(j + 7) % 8] * l] =                 1;           }           second[i + 1][(j + 1) % 8].insert(               make_pair(k.first + dx[(j + 1) % 8] * a[i + 1],                         k.second + dy[(j + 1) % 8] * a[i + 1]));           second[i + 1][(j + 7) % 8].insert(               make_pair(k.first + dx[(j + 7) % 8] * a[i + 1],                         k.second + dy[(j + 7) % 8] * a[i + 1]));         }       }     }     int ans = 0;     for (int i = 0; i <= 500; i++)       for (int j = 0; j <= 500; j++) ans += can[i][j];     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, a[55], b[55], ans[5000] = {}, k = 0;     cin >> n;     for (int i = 0; i < n; ++i) cin >> a[i];     cin >> m;     for (int i = 0; i < m; ++i) cin >> b[i];     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         if (b[j] % a[i] == 0) {           ans[k] = b[j] / a[i];           k++;         }       }     }     sort(ans, ans + k);     int c = 0;     for (int i = k - 1; i >= 0; --i)       if (ans[i] == ans[k - 1]) c++;     cout << c;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[100005], n;   int main() {     while (cin >> n) {       for (int i = 1; i <= n; i++) {         cin >> a[i];         while (a[i] % 2 == 0) a[i] /= 2;         while (a[i] % 3 == 0) a[i] /= 3;       }       int i;       for (i = 2; i <= n; i++)         if (a[i] != a[1]) break;       if (i > n)         cout <<  YES n ;       else         cout <<  NO n ;     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC target( avx2 )   #pragma GCC optimization( O3 )   #pragma GCC optimization( unroll-loops )   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t, n, m, i, j, k, sum, cnt, temp, ans;     cin >> t;     while (t--) {       long long n;       cin >> n;       if (n % 3 == 0) {         cout << n / 3 <<     << n / 3 <<   n ;       } else if (n % 3 == 1) {         cout << n / 3 + 1 <<     << n / 3 <<   n ;       } else {         cout << n / 3 <<     << n / 3 + 1 <<   n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int a[15], b[15];   int cnt[15];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     for (int i = 1; i <= m; i++) {       int k;       cin >> k;       cnt[k] = 1;     }     vector<int> ans;     for (int i = 1; i <= n; i++) {       if (cnt[a[i]] == 1) {         ans.push_back(a[i]);       }     }     for (auto e : ans) cout << e <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int read() {     char ch;     int sig = 1;     for (ch = getchar(); ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) sig = -1;     int x = ch -  0 ;     for (ch = getchar(); ch >=  0  && ch <=  9 ; ch = getchar())       x = x * 10 + ch -  0 ;     return x * sig;   }   const int N = 1e6 + 5;   const long long Mo = 1e9 + 7;   struct P {     long long x, y;     P(long long _x = 0, long long _y = 0) {       x = _x;       y = _y;     }     friend P operator+(P a, P b) { return P(a.x + b.x, a.y + b.y); }     friend P operator-(P a, P b) { return P(a.x - b.x, a.y - b.y); }     friend long long operator^(P a, P b) { return a.x * b.y - a.y * b.x; }   } p[N];   int n;   long long S, sq[N], sx[N], sy[N];   int main() {     n = read();     for (int i = 1; i <= n; i++) p[i].x = read(), p[i].y = read();     for (int i = 1; i <= n; i++) p[i + n] = p[i];     for (int i = 1; i <= n; i++) S += p[i + 1] ^ p[i];     long long ans = 0, sum = 0;     for (int i = 1; i <= n * 2; i++) {       (sum += p[i] ^ (p[i - 1])) %= Mo;       sq[i] = (sq[i - 1] + sum) % Mo;       sx[i] = (sx[i - 1] + p[i].x) % Mo;       sy[i] = (sy[i - 1] + p[i].y) % Mo;     }     long long ns = S % Mo;     int pos = 3;     sum = 0;     for (int i = 2; i <= pos; i++) sum += p[i] ^ p[i - 1];     long long now = 0;     for (int i = 1; i <= n; i++) {       (now += p[i + 1] ^ p[i]) %= Mo;       sum -= p[i + 1] ^ p[i];       if (pos < i + 2) pos++, sum += p[pos] ^ p[pos - 1];       while (pos <= i + n - 2) {         long long ar = sum;         ar -= p[i] ^ p[i + 1];         ar += p[i] ^ p[pos];         if (ar < S - ar)           pos++, sum += p[pos] ^ p[pos - 1];         else           break;       }       int ls = i + 2, rs = pos - 1;       long long dx = (sx[rs] - sx[ls - 1]) % Mo, dy = (sy[rs] - sy[ls - 1]) % Mo;       if (ls <= rs) {         (ans += ns * (rs - ls + 1) % Mo) %= Mo;         (ans -= (sq[rs] - sq[ls - 1]) % Mo * 2 % Mo) %= Mo;         (ans += now * 2 % Mo * (rs - ls + 1) % Mo) %= Mo;         (ans += (p[i] ^ p[i + 1]) % Mo * (rs - ls + 1) % Mo * 2 % Mo) %= Mo;         (ans -= (p[i] ^ P(dx, dy)) % Mo * 2 % Mo) %= Mo;       }       ls = pos, rs = i + n - 2, dx = (sx[rs] - sx[ls - 1]) % Mo,       dy = (sy[rs] - sy[ls - 1]) % Mo;       if (ls <= rs) {         (ans -= ns * (rs - ls + 1) % Mo) %= Mo;         (ans += (sq[rs] - sq[ls - 1]) % Mo * 2 % Mo) %= Mo;         (ans -= now * 2 % Mo * (rs - ls + 1) % Mo) %= Mo;         (ans -= (p[i] ^ p[i + 1]) % Mo * (rs - ls + 1) % Mo * 2 % Mo) %= Mo;         (ans += (p[i] ^ P(dx, dy)) % Mo * 2 % Mo) %= Mo;       }     }     ans = ans * (Mo + 1) / 2 % Mo;     printf( %lld n , (ans + Mo) % Mo);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     char str[8][10];     int n, k, i, j, l, mx, mn, mind;     int p[] = {1, 2, 3, 4, 5, 6, 7, 8};     cin >> n >> k;     for (i = 0; i < n; i++) cin >> str[i];     mx = 0;     mn = 1000000000;     mind = mn - mx;     do {       int arr[8] = {0};       mx = 0;       mn = 1000000000;       for (i = 0; i < n; i++) {         for (j = 0; j < k; j++) {           arr[i] *= 10;           arr[i] += (int)(str[i][p[j] - 1] -  0 );         }         if (arr[i] > mx) mx = arr[i];         if (arr[i] < mn) mn = arr[i];       }       mind = min(mind, (mx - mn));     } while (std::next_permutation(p, p + k));     cout << mind << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     int a;     cin >> a;     string s;     cin >> s;     map<int, int> Freq;     for (int i = 0; i < s.size(); i++) {       int sum = 0;       bool zero = false;       for (int j = i; j < s.size(); j++) {         sum += (s[j] -  0 );         Freq[sum]++;       }     }     long long ans = 0;     if (a == 0) {       for (int i = 0; i < s.size(); i++) {         int sum = 0;         for (int j = i; j < s.size(); j++) {           sum += s[j] -  0 ;           if (sum == 0)             ans += (s.size() * (s.size() + 1)) / 2;           else             ans += Freq[0];         }       }       cout << ans;       return 0;     }     for (auto &x : Freq) {       if (x.first && a % x.first == 0)         ans += (long long)Freq[a / x.first] * x.second;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long pf(int v) { return (long long)1 * v * v; }   struct sd {     int a, b;     bool operator<(const sd& w) const { return a < w.a; }   } x[100005];   long long maxx(long long a, long long b) { return a > b ? a : b; }   long long minx(long long a, long long b) { return a < b ? a : b; }   int n, fr[2][100005], ba[2][100005];   long long ans = 1e18;   bool jud(long long v) {     int mid = sqrt(v);     for (int i = 1; i <= n; i++) {       if (x[i].a > 0) break;       int l = 1, r = n, R = minx(mid + x[i].a, -x[i].a);       while (l < r) {         int mid = ((l + r) >> 1) + 1;         if (x[mid].a <= R)           l = mid;         else           r = mid - 1;       }       if (i == 1 && l == n) return 1;       int ymin = minx(fr[0][i], ba[0][l]), ymax = maxx(fr[1][i], ba[1][l]);       if (maxx(maxx(pf(ymax), pf(ymin)) + pf(x[i].a), pf(ymax - ymin)) <= v)         return 1;     }     return 0;   }   void work() {     sort(x + 1, x + 1 + n);     fr[0][1] = 1e9;     fr[1][1] = -1e9;     ba[0][n] = 1e9;     ba[1][n] = -1e9;     for (int i = 2; i <= n; i++)       fr[0][i] = minx(fr[0][i - 1], x[i - 1].b),       fr[1][i] = maxx(fr[1][i - 1], x[i - 1].b);     for (int i = n - 1; i >= 1; i--)       ba[0][i] = minx(ba[0][i + 1], x[i + 1].b),       ba[1][i] = maxx(ba[1][i + 1], x[i + 1].b);     long long l = 0, r = 1e18;     while (l < r) {       long long mid = (l + r) / 2;       if (jud(mid))         r = mid;       else         l = mid + 1;     }     ans = minx(ans, l);   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d%d , &x[i].a, &x[i].b);     work();     for (int i = 1; i <= n; i++) x[i].a *= -1;     work();     int ymax = -1e9, ymin = 1e9;     for (int i = 1; i <= n; i++)       ymax = maxx(ymax, x[i].b), ymin = minx(ymin, x[i].b);     ans = minx(ans, pf(ymax - ymin));     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       long long int x1, x2, y1, y2;       long long int k;       cin >> x1 >> y1 >> x2 >> y2;       if (x1 == x2 || y1 == y2)         k = abs(x1 - x2) + abs(y1 - y2);       else         k = 2 + abs(x1 - x2) + abs(y1 - y2);       cout << k << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N;   string S[100001];   int cnt[26];   int from[26];   int find(int ind, char C) {     for (int i = 0; i < S[ind].length(); i++)       if (S[ind][i] == C) return i;     return -1;   }   void merge(int I, int J, char C) {     int indI = find(I, C);     int indJ = find(J, C);     int lenI = S[I].length() - indI;     int lenJ = S[J].length() - indJ;     int L = min(lenI, lenJ);     if (min(indI, indJ) > 0 || S[I].substr(indI, L) != S[J].substr(indJ, L)) {       cout <<  NO  <<   n ;       exit(0);     }     string s;     if (indI > indJ) {       s = S[I];       if (indJ + L < (int)S[J].length())         s += S[J].substr(indJ + L, (int)S[J].length() - (indJ + L));     } else {       s = S[J];       if (indI + L < (int)S[I].length())         s += S[I].substr(indI + L, (int)S[I].length() - (indI + L));     }     S[J] = s;     S[I] =  ! ;   }   int main() {     cin >> N;     for (int i = 0; i < N; i++) {       cin >> S[i];       for (int j = 0; j < 26; j++) cnt[j] = 0;       for (int j = 0; j < S[i].length(); j++) cnt[S[i][j] -  a ]++;       for (int j = 0; j < 26; j++)         if (cnt[j] > 1) {           cout <<  NO  <<   n ;           return 0;         }     }     for (int i = 0; i < 26; i++) from[i] = -1;     for (int i = 0; i < N; i++) {       for (int j = 0; j < S[i].length(); j++) {         if (from[S[i][j] -  a ] == -1)           from[S[i][j] -  a ] = i;         else if (from[S[i][j] -  a ] != i) {           merge(from[S[i][j] -  a ], i, S[i][j]);           int temp = from[S[i][j] -  a ];           for (int k = 0; k < 26; k++)             if (from[k] == temp) from[k] = i;         }       }     }     vector<string> v;     for (int i = 0; i < N; i++)       if (S[i] !=  ! ) v.push_back(S[i]);     sort(v.begin(), v.end());     string ans =   ;     for (auto s : v) ans += s;     for (int i = 0; i < 26; i++) cnt[i] = 0;     for (int i = 0; i < ans.length(); i++) cnt[ans[i] -  a ]++;     for (int i = 0; i < 26; i++)       if (cnt[i] > 1) {         cout <<  NO  <<   n ;         return 0;       }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int vis[400000], cur, path[500000], block[500000], x, y, n, m, q, k, s, t,       seen[400010], ans[400010];   vector<int> g[4000];   map<pair<int, int>, vector<pair<int, int> > > M;   void color(int x) {     if (block[x]) return;     block[x] = 1;     for (int i = 0; i < g[x].size(); i++) color(g[x][i]);   }   void dfs(int x, int dep) {     if (vis[x]) {       int now = dep - 1;       while (path[now] != x && now > 0) color(path[now--]);       color(path[now]);     }     if (seen[x]) return;     path[dep] = x;     vis[x] = seen[x] = 1;     if (M.find(make_pair(cur, x)) != M.end()) {       if (!block[x]) {         vector<pair<int, int> > tmp = M[make_pair(cur, x)];         for (int i = 0; i < tmp.size(); i++)           if (tmp[i].first <= dep) ans[tmp[i].second] = path[tmp[i].first];       }     }     for (int i = 0; i < g[x].size(); i++) dfs(g[x][i], dep + 1);     vis[x] = 0;   }   int main() {     scanf( %d%d%d , &n, &m, &q);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &x, &y);       g[x].push_back(y);     }     for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end());     for (int i = 1; i <= q; i++) {       scanf( %d%d%d , &s, &t, &k);       M[make_pair(s, t)].push_back(make_pair(k, i));     }     memset(ans, 0xff, sizeof(ans));     for (int i = 1; i <= n; i++) {       cur = i;       memset(seen, 0, sizeof(seen));       memset(block, 0, sizeof(block));       dfs(i, 1);     }     for (int i = 1; i <= q; i++) printf( %d n , ans[i]);     return 0;   }