malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long dp[1 << 18][100], d, f[20];   int main() {     int m, l, n, i, j, k, c[10];     char a[20];     memset(c, 0, sizeof(c));     for (f[0] = 1, i = 1; i < 20; i++) f[i] = f[i - 1] * i;     scanf( %s%d , a, &m);     l = strlen(a), n = 1 << l;     for (i = 0; i < l; i++) c[a[i] -=  0 ]++;     for (d = 1, i = 0; i < 10; i++) d *= f[c[i]];     dp[0][0] = 1;     for (i = 0; i < n; i++)       for (j = 0; j < l; j++)         if (!(i & 1 << j))           for (k = 0; k < m; k++)             if (!(i == 0 && a[j] == 0))               dp[i | 1 << j][(k * 10 + a[j]) % m] += dp[i][k];     printf( %I64d n , dp[n - 1][0] / d);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int N, M;     cin >> N >> M;     int a[N][M];     for (int i = 0; i < N; i++) {       for (int j = 0; j < M; j++) {         cin >> a[i][j];       }     }     long long l = 0, r = 1e9 + 1;     int i1 = 1, i2 = 1;     int vis[1 << M];     while (r - l > 1) {       long long m = (l + r) >> 1;       bool check = false;       memset(vis, 0, sizeof(vis));       for (int i = 0; i < N; i++) {         int bit = 0;         for (int j = 0; j < M; j++) {           if (a[i][j] >= m) {             bit ^= (1 << j);           }         }         vis[bit] = i + 1;       }       for (int i = 0; i < (1 << M); i++) {         for (int j = 0; j < (1 << M); j++) {           if (vis[i] && vis[j] && ((i | j) + 1) == (1 << M)) {             i1 = vis[i], i2 = vis[j];             check = true;             break;           }         }       }       if (check)         l = m;       else         r = m;     }     cout << i1 <<     << i2;   }
#include <bits/stdc++.h>   using namespace std;   int N;   int x, y, root = -1;   vector<int> g[200100];   int len[200100], viz[3][200100];   int dmax = 0, ind;   int stacky[200100], l = 0;   void dfs(int x) {     viz[0][x] = 1;     if (g[x].size() == 1) {       len[x] = 1;     } else {       int okk = 1;       int kiddo = -1, kiddo2 = -1;       for (auto y : g[x]) {         if (viz[0][y]) {           continue;         }         dfs(y);         if (x == root) {           if (len[y] == 0) {             okk = 0;           } else if (kiddo == -1) {             kiddo = len[y];           } else if (kiddo != len[y] && kiddo2 == -1) {             kiddo2 = len[y];           } else if (kiddo2 != len[y] && kiddo != len[y]) {             okk = 0;           }         } else {           if (len[y] == 0) {             okk = 0;             continue;           }           if (kiddo == -1) {             kiddo = len[y];           } else if (kiddo != len[y]) {             okk = 0;           }         }       }       if (okk) {         if (x != root || kiddo2 == -1) {           len[x] = kiddo + 1;         } else {           len[x] = kiddo + kiddo2 + 1;         }       }     }   }   void dfss(int x, int t, int d) {     stacky[++l] = x;     viz[t][x] = 1;     if (d > dmax) {       ind = x;       dmax = d;       root = stacky[(l + 1) / 2];     }     for (auto y : g[x])       if (!viz[t][y]) dfss(y, t, d + 1);     --l;   }   int main() {     cin.sync_with_stdio(false);     cin >> N;     if (N == 2) {       cout << 1;       return 0;     }     for (int i = 1; i < N; ++i) {       cin >> x >> y;       g[x].push_back(y);       g[y].push_back(x);     }     dfss(1, 1, 0);     dmax = 0;     dfss(ind, 2, 0);     dfs(root);     if (len[root]) {       int ret = len[root] - 1;       while (ret % 2 == 0) ret /= 2;       cout << ret;       return 0;     } else {       cout << -1;       return 0;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> a;   bool check(vector<int> &b) {     if (b.size() <= 1) return !b.empty();     int d = a[b[1]] - a[b[0]];     for (int i = 1; i < b.size(); ++i) {       if (d != a[b[i]] - a[b[i - 1]]) return false;     }     return true;   }   bool solve(int o, int p) {     vector<int> fir, sec;     fir.push_back(o);     fir.push_back(p);     if (p == 2) {       sec.push_back(1 - o);     }     for (int i = p + 1; i < n; ++i) {       if (a[i] == a[fir[1]] - a[fir[0]] + a[*fir.rbegin()]) {         fir.push_back(i);       } else {         sec.push_back(i);       }     }     if (check(fir) && check(sec)) {       for (auto &i : fir) {         cout << a[i] <<    ;       }       cout << endl;       for (auto &i : sec) {         cout << a[i] <<    ;       }       cout << endl;       return true;     }     sec.push_back(*fir.rbegin());     fir.pop_back();     sort(sec.begin(), sec.end());     if (check(fir) && check(sec)) {       for (auto &i : fir) {         cout << a[i] <<    ;       }       cout << endl;       for (auto &i : sec) {         cout << a[i] <<    ;       }       cout << endl;       return true;     }     return false;   }   int main() {     scanf( %d , &n);     a.resize(n);     for (int i = 0; i < n; ++i) {       scanf( %d , &a[i]);     }     if (n == 2) {       cout << a[0] << endl << a[1] << endl;     } else {       if (solve(0, 1)) return 0;       if (solve(0, 2)) return 0;       if (solve(1, 2)) return 0;       puts( No solution n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   void in(T &x) {     x = 0;     bool flag = 0;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) flag = 1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 3) + (x << 1) + (c ^ 48);       c = getchar();     }     if (flag) x = -x;   }   priority_queue<long long, vector<long long>, greater<long long> > q;   long long n, x, ans;   signed main() {     in(n);     for (register long long i = 1; i <= n; ++i) {       in(x);       q.push(x);       long long tmp = q.top();       if (x > tmp) ans += x - tmp, q.pop(), q.push(x);     }     printf( %lld , ans);   }
#include <bits/stdc++.h>   using namespace std;   int A;   int gcd(int a, int b) {     if (a < b) swap(a, b);     if (a % b == 0) return b;     return gcd(b, a % b);   }   pair<int, int> find(vector<pair<int, int> > &sets, int a) {     if (sets[a].first != a) sets[a].first = find(sets, sets[a].first).first;     return sets[a];   }   void unions(vector<pair<int, int> > &sets, int a, int b) {     pair<int, int> p = find(sets, a);     pair<int, int> q = find(sets, b);     if (p.second < q.second) swap(p, q);     sets[q.first].first = p.first;     if (p.second == q.second) sets[p.first].second++;   }   void solve() {     long long int n, i, a, b, x, y, a1, b1;     cin >> n;     vector<pair<int, int> > sets(n, {0, 1});     pair<int, int> p;     for (i = 0; i < n; i++) sets[i].first = i;     vector<pair<int, pair<int, int> > > v;     for (i = 0; i < n - 1; i++) {       cin >> a >> b;       a--;       b--;       p = find(sets, a);       a1 = p.first;       x = p.second;       p = find(sets, b);       b1 = p.first;       y = p.second;       if (a1 != b1)         unions(sets, a1, b1);       else {         v.push_back({a1, {a + 1, b + 1}});       }     }     set<int> s;     for (i = 0; i < n; i++) {       s.insert(find(sets, sets[i].first).first);     }     auto itr = s.begin();     x = *itr + 1;     itr++;     cout << v.size() <<   n ;     for (i = 0, itr; i < v.size(); i++, itr++)       cout << v[i].second.first <<     << v[i].second.second <<     << x <<                << *itr + 1 <<   n ;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int T = 1;     while (T--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /STACK:102400000,102400000 )   const int N = 200005;   int len, fa[26], sz[N];   char s1[N], s2[N];   int Find(int x) { return (fa[x] == x) ? x : fa[x] = Find(fa[x]); }   void Unite(int x, int y) {     int fax = Find(x), fay = Find(y);     if (fax != fay) fa[fax] = fay, sz[fay] += sz[fax];   }   vector<int> ans[26];   int main() {     for (int i = 0; i <= 25; ++i) fa[i] = i, sz[i] = 1;     scanf( %d , &len);     scanf( %s%s , s1 + 1, s2 + 1);     for (int i = 1; i <= len; ++i) {       if (s1[i] == s2[i]) continue;       if (s1[i] > s2[i]) swap(s1[i], s2[i]);       Unite(s1[i] -  a , s2[i] -  a );     }     int ans1 = 0;     for (int i = 0; i <= 25; ++i)       if (fa[i] != i) ans[fa[i]].push_back(i), ++ans1;     printf( %d n , ans1);     for (int i = 0; i <= 25; ++i)       if (ans[i].size() > 0)         for (int u : ans[i]) printf( %c %c n ,  a  + u,  a  + i);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y;     node(int x1 = 0, int y1 = 0) : x(x1), y(y1){};     bool operator=(const node &b) const { return (x == b.x) && (y == b.y); }   };   const int dx[5] = {0, 0, 0, 1, -1};   const int dy[5] = {0, 1, -1, 0, 0};   int N, M, Q;   char maze[1007][1007];   int ans = 0;   int vis[1007][1007];   int memo[1000007];   void dfs(int x, int y, int id) {     if (maze[x][y] ==  * ) return;     for (int i = 1; i <= 4; i++) {       int x1 = x + dx[i];       int y1 = y + dy[i];       if (x1 > 0 && x1 <= N && y1 > 0 && y1 <= M && maze[x1][y1] ==  * ) ans++;     }     for (int i = 1; i <= 4; i++) {       int x1 = x + dx[i];       int y1 = y + dy[i];       if (maze[x1][y1] ==  .  && vis[x1][y1] == 0 && x1 > 0 && x1 <= N &&           y1 > 0 && y1 <= M) {         vis[x1][y1] = id;         dfs(x1, y1, id);       }     }     return;   }   int main() {     scanf( %d%d%d , &N, &M, &Q);     for (int i = 1; i <= N; i++) {       for (int j = 1; j <= M; j++) {         cin >> maze[i][j];       }     }     int k = 1;     while (Q--) {       int x, y;       scanf( %d%d , &x, &y);       if (vis[x][y] == 0) {         ans = 0;         vis[x][y] = k;         dfs(x, y, k);         memo[k] = ans;         k++;       }       printf( %d n , memo[vis[x][y]]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MAX = 1e6 + 1;   void solve() {     long long n, m;     cin >> n >> m;     if (m == 0) {       cout << 0;       return;     }     long long total[n + 1];     memset(total, 0, sizeof(total));     long long ai, bi, ci;     for (long long i = 0; i < m; i++) {       cin >> ai >> bi >> ci;       total[ai] -= ci;       total[bi] += ci;     }     long long ans = 0;     for (long long i = 0; i < n + 1; i++) {       if (total[i] > 0) {         ans += total[i];       }     }     cout << ans;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t;     t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 300010, K = 18;   char s[N];   bool sm[K * K][N], b[N], vis[N];   int n, p, tot, ans, ss, w[K][K], mp[K][K], a[N], cnt[K], q[N];   int cal(int S) {     int res = 0;     for (int i = (1); i <= (p); i++)       if (S & (1 << (i - 1))) res += cnt[i];     return res;   }   void work(int x, int y, int k) {     int tot = 0;     for (int i = (1); i <= (n); i++)       if (s[i] -  a  + 1 == x || s[i] -  a  + 1 == y) a[++tot] = i;     for (int i = (1); i <= (tot - 1); i++)       if ((s[a[i]] -  a  + 1 == x && s[a[i + 1]] -  a  + 1 == y) ||           (s[a[i]] -  a  + 1 == y && s[a[i + 1]] -  a  + 1 == x)) {         int S = 0;         for (int j = (a[i] + 1); j <= (a[i + 1] - 1); j++) S |= 1 << (s[j] -  a );         S ^= ss;         sm[k][S] = 1;       }   }   int main() {     scanf( %d%d%s , &n, &p, s + 1);     ans = n + 1;     ss = (1 << p) - 1;     for (int i = (1); i <= (n); i++) cnt[s[i] -  a  + 1]++;     for (int i = (1); i <= (p); i++)       for (int j = (1); j <= (p); j++) scanf( %d , &mp[i][j]);     for (int i = (1); i <= (p); i++)       for (int j = (i); j <= (p); j++)         if (!mp[i][j]) w[i][j] = ++tot, work(i, j, tot);     for (int x = (1); x <= (tot); x++) {       for (int i = 1; i <= ss; i <<= 1)         for (int j = (1); j <= (ss); j++)           if (j & i) sm[x][j ^ i] |= sm[x][j];     }     for (int S = (0); S <= (ss); S++) {       b[S] = 1;       for (int i = (1); i <= (p); i++)         for (int j = (i); j <= (p); j++)           if (S & (1 << (i - 1)) && S & (1 << (j - 1)) && !mp[i][j] &&               sm[w[i][j]][S]) {             b[S] = 0;             break;           }     }     q[1] = ss;     vis[ss] = 1;     for (int st = 0, ed = 1; st != ed;) {       int S = q[++st];       ans = min(ans, cal(S));       for (int i = (1); i <= (p); i++) {         int S1 = S ^ (1 << (i - 1));         if (S & (1 << (i - 1)) && !vis[S1] && b[S1]) vis[S1] = 1, q[++ed] = S1;       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     while (t--) {       long long int x, k;       cin >> x >> k;       long long int temp = k % 4;       long long int i = 1;       while (temp--) {         if (x % 2) {           x += ((k / 4) * 4) + i;         } else {           x -= ((k / 4) * 4) + i;         }         i++;       }       cout << x << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string s;   vector<char> t;   int main() {     cin >> s;     for (int i = 0; i < s.size(); i++) {       if (t.size() != 0 and t.back() == s[i]) {         t.pop_back();       } else {         t.push_back(s[i]);       }     }     for (int i = 0; i < t.size(); i++) {       cout << t[i];     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n, d, i, j, k, s, t, ans;     cin >> n >> d;     s = 0;     for (i = 0; i < n; i++) {       cin >> t;       s += t;     }     if (s + (n - 1) * 10 > d)       ans = -1;     else       ans = (d - s) / 5;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) {     if (a % b == 0)       return b;     else       return gcd(b, a % b);   }   int a, b, c, d;   int main() {     scanf( %d %d %d %d , &a, &b, &c, &d);     int t, bo;     if (a * d >= c * b) {       t = b * c;       bo = d * a;     } else {       t = a * d;       bo = c * b;     }     t = bo - t;     int cc = gcd(t, bo);     printf( %d/%d n , t / cc, bo / cc);   }
#include <bits/stdc++.h>   using namespace std;   long long spf[101];   long long fac[101];   void sieve() {     spf[1] = 1;     for (long long i = 2; i < 101; i++) spf[i] = i;     for (long long i = 4; i < 101; i += 2) spf[i] = 2;     for (long long i = 3; i * i < 101; i++) {       if (spf[i] == i) {         for (long long j = i * i; j < 101; j += i)           if (spf[j] == j) spf[j] = i;       }     }   }   map<long long, long long> getfactor(long long a) {     map<long long, long long> m;     while (a > 1) {       m[spf[a]]++;       a /= spf[a];     }     return m;   }   long long power(long long x, long long y, long long p) {     long long res = 1;     x = x;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long gcd(long long a, long long b) {     if (a == 0) return b;     return gcd(b % a, a);   }   long long inverse(long long a, long long p) { return power(a, p - 2, p); }   long long ncr(long long n, long long r, long long p) {     if (r == 0) return 1;     return (fac[n] * inverse(fac[r], p) % p * inverse(fac[n - r], p) % p) % p;   }   long long pre1[200000] = {0}, pre2[200000] = {0};   void solve() {     long long n;     cin >> n;     for (long long i = 1; i <= n; i++) {       long long x;       cin >> x;       pre1[i] += pre1[i - 1] + (x == 1);       pre2[i] += pre2[i - 1] + (x == 2);     }     for (long long i = n + 1; i <= 150000; i++) {       pre1[i] += pre1[i - 1] + 1;       pre2[i] += pre2[i - 1] + 1;     }     vector<pair<long long, long long> > v;     for (long long i = 1; i <= n; i++) {       long long j = 0;       long long count1 = 0, count2 = 0;       long long counter = 0;       while (1) {         long long g = lower_bound(pre1 + j, pre1 + 140000, pre1[j] + i) - pre1;         long long l = lower_bound(pre2 + j, pre2 + 140000, pre2[j] + i) - pre2;         if (g == n && l > n) {           count1++;           if (count1 > count2) {             v.push_back(make_pair(count1, i));             break;           }         }         if (l == n && g > n) {           count2++;           if (count2 > count1) {             v.push_back(make_pair(count2, i));             break;           }         }         if (l > n && g > n) break;         if (l < g) {           count2++;           j = l;         } else {           count1++;           j = g;         }       }     }     sort(v.begin(), v.end());     cout << v.size() <<   n ;     for (auto it : v) cout << it.first <<     << it.second <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   unsigned long long hs[1050], hL[1 << 15], bs[50];   int main() {     mt19937_64 rnd(time(0));     srand(time(0));     int n, s, d;     scanf( %d%d%d , &n, &s, &d);     for (int i = 0; i < 1050; i++) hs[i] = rnd();     random_shuffle(hs, hs + 1050);     for (int i = 0; i < s; i++) {       int c;       scanf( %d , &c);       while (c--) {         int k;         scanf( %d , &k);         bs[i] ^= hs[k];       }     }     int mid = min(10, s / 2);     for (int bits = 0; bits < (1 << mid); bits++) {       for (int i = 0; i < mid; i++) {         if (bits >> i & 1) {           hL[bits] ^= bs[i];         }       }     }     unordered_map<unsigned long long, int> mp;     mp[0] = 0;     for (int bits = 0; bits < (1 << (s - mid)); bits++) {       unsigned long long val = 0;       for (int i = 0; i < s - mid; i++) {         if (bits >> i & 1) {           val ^= bs[i + mid];         }       }       int cnt = __builtin_popcount(bits);       if (!mp.count(val) || mp[val] > cnt) {         mp[val] = cnt;       }     }     while (d--) {       int c;       scanf( %d , &c);       unsigned long long v = 0;       while (c--) {         int k;         scanf( %d , &k);         v ^= hs[k];       }       int res = INT_MAX;       for (int bits = 0; bits < (1 << mid); bits++) {         unsigned long long val = hL[bits] ^ v;         if (mp.count(val)) res = min(res, mp[val] + __builtin_popcount(bits));       }       if (res == INT_MAX)         puts( -1 );       else         printf( %d n , res);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int x_array[5000], y_array[5000];   long long int dx[50001], dy[50001];   int main() {     int a;     char str[5000];     long long int cnt = 0;     scanf( %d , &a);     scanf( %s , str);     int len = strlen(str);     for (int i = 0; i < len; i++) {       int j = i + 1;       x_array[j] = x_array[j - 1] + str[i] -  0 ;       y_array[j] = y_array[j - 1] + str[i] -  0 ;     };     for (int i = 1; i <= len; i++) {       for (int j = i; j <= len; j++) {         dx[x_array[j] - x_array[i - 1]]++;         dy[y_array[j] - y_array[i - 1]]++;       }     }     if (a != 0) {       for (int i = 1; i < 50000; i++) {         if (dx[i] && a % i == 0) {           int temp = a / i;           if (temp >= 50000) continue;           cnt += dx[i] * dy[temp];         }       }     } else if (a == 0) {       for (int i = 1; i < 50000; i++) {         cnt += dx[0] * dy[i] + dx[i] * dy[0];       }       if (dx[0]) cnt += dx[0] * dy[0];     }     printf( %I64d n , cnt);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int64_t N = 1e6 + 10;   string second;   int64_t X, Y, n, m;   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n >> m;     for (int64_t i = 0; i < n; i++) {       cin >> second;       for (int64_t j = 0; j < second.size(); j++)         if (second[j] ==  * ) X ^= j, Y ^= i;     }     cout << Y + 1 <<     << X + 1;     return -0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, d[1002], s[1002], t, f, sum, k;   int main() {     cin >> m >> k;     for (int i = 2; i <= m + 1; i++) {       cin >> d[i];     }     for (int i = 1; i <= m; i++) {       cin >> s[i];     }     for (int i = 1; i <= m; i++) {       f += s[i];       t = max(t, s[i]);       while (f < d[i + 1]) {         sum += k, f += t;       }       sum += d[i + 1], f -= d[i + 1];     }     cout << sum;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       vector<long long> arr(3);       for (int i = 0; i < 3; i++) cin >> arr[i];       sort(arr.begin(), arr.end());       if (arr[2] <= arr[1] + arr[0] + 1)         cout <<  Yes  << endl;       else         cout <<  No  << endl;     }   }
#include <bits/stdc++.h>   const int mod = 1000000007;   using namespace std;   int dp[5009][5009];   char s1[5009], s2[5009];   int main() {     while (scanf( %s%s , s1, s2) == 2) {       memset(dp, 0, sizeof(dp));       int len2 = strlen(s2);       for (int i = 1; s1[i - 1]; i++) {         for (int j = 1; s2[j - 1]; j++) {           if (s1[i - 1] == s2[j - 1]) {             dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1] + 1) % mod;           } else             dp[i][j] = dp[i][j - 1];         }       }       int ans = 0;       for (int i = 1; s1[i - 1]; i++) ans = (ans + dp[i][len2]) % mod;       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v);   template <typename A, typename B>   ostream &operator<<(ostream &cout, pair<A, B> const &p) {     return cout <<  (  << p.first <<  ,  <<     << p.second <<  ) ;   }   template <typename A>   ostream &operator<<(ostream &cout, vector<A> const &v) {     cout <<  [ ;     for (int i = 0; i < v.size(); i++) {       if (i) cout <<  , ;       cout << v[i];     }     return cout <<  ]  << endl;   }   const long long mod = 1000000007;   bool compare1(vector<int> &a, vector<int> &b) {     if (a[0] == b[0]) {       return a[1] < b[1];     }     return a[0] < b[0];   }   bool compare2(vector<int> &a, vector<int> &b) {     if (a[1] == b[1]) {       return a[0] < b[0];     }     return a[1] < b[1];   }   void dfs(int cur, int par, vector<vector<int>> &tree, vector<int> &vis, int &c,            vector<int> &val) {     vis[cur] = 1;     c = min(c, val[cur]);     for (auto it : tree[cur]) {       if (!vis[it]) {         dfs(it, cur, tree, vis, c, val);       }     }   }   void solve() {     int n, k;     cin >> n >> k;     vector<vector<int>> val(n, vector<int>(4));     vector<vector<int>> tree(n);     vector<int> vis(n);     vector<int> cost(n);     for (int i = 0; i < n; i++) {       for (int j = 0; j < 3; j++) {         cin >> val[i][j];       }       val[i][3] = i;       cost[i] = val[i][2];     }     sort(val.begin(), val.end(), compare1);     for (int i = 1; i < n; i++) {       if (val[i][0] == val[i - 1][0] && abs(val[i][1] - val[i - 1][1]) <= k) {         tree[val[i][3]].push_back(val[i - 1][3]);         tree[val[i - 1][3]].push_back(val[i][3]);       }     }     sort(val.begin(), val.end(), compare2);     for (int i = 1; i < n; i++) {       if (val[i][1] == val[i - 1][1] && abs(val[i][0] - val[i - 1][0]) <= k) {         tree[val[i][3]].push_back(val[i - 1][3]);         tree[val[i - 1][3]].push_back(val[i][3]);       }     }     deque<int> st;     for (int i = 0; i < n; i++) {       if (!vis[i]) {         int c = INT_MAX;         dfs(i, i, tree, vis, c, cost);         st.push_back(c);       }     }     sort(st.begin(), st.end());     int t = 0;     while (((int)st.size())) {       while (((int)st.size()) && st.front() == t) {         st.pop_front();       }       if (((int)st.size())) {         st.pop_back();       }       if (((int)st.size())) {         t++;       }     }     cout << t <<   n ;   }   int32_t main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int tc = 1;     cin >> tc;     while (tc--) {       solve();     }     return 0;   }