malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include<stdio.h>   #include<vector>   #include<algorithm>   using namespace std;   #define R register int   #define I inline   #define N 100001   vector<int>G[N],C[N],H[N];   int dep[N],g[448][N],fa[N],b[448],sz[N];   char ans[N];   bool f[448][N];   struct Painter{    char Character;    int LeftNum;   };   I void DFS(int x){    dep[x]=dep[fa[x]]+1;    C[dep[x]].push_back(x);    sz[x]=1;    for(vector<int>::iterator T=G[x].begin();T!=G[x].end();T++){     DFS(*T);     sz[x]+=sz[*T];    }   }   I bool Compare(int x,int y){    return sz[x]<sz[y];   }   I void Swap(Painter&A,Painter&B){    Painter Tem=A;    A=B;    B=Tem;   }   int main(){    Painter A,B;    A.Character= a ;    B.Character= b ;    int n,maxdep=0;    scanf( %d%d ,&n,&A.LeftNum);    B.LeftNum=n-A.LeftNum;    for(int i=2;i<=n;i++){     scanf( %d ,fa+i);     G[fa[i]].push_back(i);    }    DFS(1);    for(R i=1;i<=n;i++){     if(dep[i]>maxdep){      maxdep=dep[i];     }    }    for(int i=1;i<=maxdep;i++){     H[C[i].size()].push_back(i);    }    f[0][0]=true;    int tot=0;    for(R i=1;i<=n;i++){     if(H[i].empty()==false){      tot++;      b[tot]=i;      int ct=H[i].size();      for(R j=0;j!=i;j++){       f[tot][j]=f[tot-1][j];      }      for(R j=i;j<=n;j++){       if(f[tot-1][j]==true){        f[tot][j]=true;       }else{        if(f[tot][j-i]==true){         g[tot][j]=g[tot][j-i]+1;         if(g[tot][j]<=ct){          f[tot][j]=true;         }        }       }      }     }    }    if(f[tot][A.LeftNum]==true){     printf( %d n ,maxdep);     int p=A.LeftNum;     for(R i=1;i<=n;i++){      ans[i]= b ;     }     for(R i=tot;i!=0;i--){      int t=g[i][p];      p-=t*b[i];      for(R j=0;j!=t;j++){       int d=H[b[i]][j];       for(vector<int>::iterator T=C[d].begin();T!=C[d].end();T++){        ans[*T]= a ;       }      }     }    }else{     printf( %d n ,maxdep+1);     for(R i=1;i<=maxdep;i++){      sort(C[i].begin(),C[i].end(),Compare);      if(A.LeftNum<B.LeftNum){       Swap(A,B);      }      while(C[i].empty()==false&&A.LeftNum!=0){       ans[C[i].back()]=A.Character;       A.LeftNum--;       C[i].pop_back();      }      while(C[i].empty()==false){       ans[C[i].back()]=B.Character;       B.LeftNum--;       C[i].pop_back();      }     }    }    for(R i=1;i<=n;i++){     putchar(ans[i]);    }    return 0;   }
#include <bits/stdc++.h>   using namespace std;   string s, u;   stack<char> t;   vector<char> mins;   int main() {     cin >> s;     u =   ;     int inicio = 0, tam = s.length();     char ant =  z  + 1;     for (int i = tam - 1; i >= 0; i--) {       ant = min(ant, s[i]);       mins.push_back(ant);     }     reverse(mins.begin(), mins.end());     while (inicio < tam) {       if (t.empty()) {         t.push(s[inicio]);         inicio++;       } else {         if (t.top() <= mins[inicio]) {           u += t.top();           t.pop();         } else {           t.push(s[inicio]);           inicio++;         }       }     }     while (!t.empty()) {       u += t.top();       t.pop();     }     if (inicio < tam) {       u += s[inicio];     }     cout << u <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 1e18;   const long double eps = 1e-6;   const long long maxn = 1e6 + 100;   vector<long long> g[maxn];   long long szc1[maxn], szc2[maxn];   vector<long long> used;   void dfsc(long long v, long long col) {     used[v] = 1;     szc1[col]++;     szc2[col]++;     for (long long u : g[v]) {       if (used[u]) continue;       dfsc(u, col);     }   }   bitset<maxn> dp;   signed main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);     long long n, k;     cin >> n >> k;     vector<long long> p(n);     for (long long i = 0; i < n; ++i) {       cin >> p[i], --p[i];       g[i].push_back(p[i]);       g[p[i]].push_back(i);     }     long long idxc = 0;     used.assign(n, 0);     for (long long i = 0; i < n; ++i) {       if (!used[i]) {         dfsc(i, idxc);         ++idxc;       }     }     long long ansl = 0, ansr = 0;     long long kl = k, kr = k;     for (long long i = 0; i < idxc; ++i) {       if (szc1[i] >= 2 && kr) {         --kr;         ansr += 2;       }     }     long long add = 0;     for (long long i = 0; i < idxc && kr; ++i) {       long long t = szc1[i];       long long x = t / 2 - 1;       if (t >= 3 && t % 2) {         ++add;       }       x = min(x, kr);       if (x >= 1) {         ansr += 2 * x;         kr -= x;       }     }     add = min(add, kr);     ansr += add;     kr -= add;     vector<pair<long long, long long>> gg;     vector<long long> cnt(n + 1, 0), us(n + 1, 0);     for (long long i = 0; i < idxc; ++i) {       cnt[szc2[i]]++;     }     for (long long i = 0; i < idxc; ++i) {       if (us[szc2[i]]) continue;       gg.push_back({szc2[i], cnt[szc2[i]]});       us[szc2[i]] = 1;     }     dp[0] = 1;     for (long long i = 0; i < (long long)gg.size(); ++i) {       for (long long t = 1; t <= gg[i].second; t *= 2) {         long long x = t * gg[i].first;         auto bb = dp;         bb <<= x;         dp |= bb;         gg[i].second -= t;       }       long long x = gg[i].second * gg[i].first;       auto bb = dp;       bb <<= x;       dp |= bb;     }     if (dp[kl])       ansl = kl;     else       ansl = kl + 1;     cout << ansl <<     << ansr;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<vector<int> > adj(1e5 + 1);   set<int> rem;   vector<int> ord;   void search(int pred1, int pred2) {     if (ord.size() == n) return;     int nxt = 0;     for (int j = 0; j < 4 && nxt == 0; j++) {       for (int k = 0; k < 4; k++) {         int x = adj[pred1][j], y = adj[pred2][k];         if (x == y && rem.find(x) != rem.end()) {           nxt = x;           rem.erase(x);           break;         }       }     }     if (nxt) {       ord.push_back(nxt);       search(nxt, pred1);     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cin >> n;     for (int i = 0; i < 2 * n; i++) {       int a, b;       cin >> a >> b;       adj[a].push_back(b);       adj[b].push_back(a);     }     for (int i = 1; i <= n; i++) {       if (adj[i].size() != 4) {         cout << -1 <<   n ;         return 0;       }     }     for (int i = 0; i < 4; i++) {       for (int j = i + 1; j < 4; j++) {         rem.clear();         ord.clear();         for (int k = 2; k <= n; k++) {           rem.insert(k);         }         ord.push_back(adj[1][i]);         rem.erase(adj[1][i]);         ord.push_back(1);         ord.push_back(adj[1][j]);         rem.erase(adj[1][j]);         search(adj[1][j], 1);         if (ord.size() == n) {           for (int i = 0; i < n; i++) {             cout << ord[i] <<    ;           }           cout <<   n ;           return 0;         }       }     }     cout << -1 <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 1e5 + 6.66;   const int MAXN = MAX << 2;   const int N = MAX << 1;   pair<int, int> p[MAX];   int up[MAXN];   int down[MAXN];   int uppre[MAXN], upsuf[MAXN], downpre[MAXN], downsuf[MAXN];   int main() {     memset(down, 63, sizeof down);     memset(downpre, 63, sizeof downpre);     memset(downsuf, 63, sizeof downsuf);     memset(up, -63, sizeof up);     memset(uppre, -63, sizeof uppre);     memset(upsuf, -63, sizeof upsuf);     int n;     cin >> n;     for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second;     for (int i = 0; i < n; i++) {       int x = p[i].first + p[i].second + N;       int y = p[i].first - p[i].second + N;       up[x] = max(up[x], y);       down[x] = min(down[x], y);     }     for (int i = 0; i < MAXN; i++) uppre[i] = upsuf[i] = up[i];     for (int i = 0; i < MAXN; i++) downpre[i] = downsuf[i] = down[i];     for (int i = 0; i < MAXN - 2; i++) {       uppre[i + 2] = max(uppre[i + 2], uppre[i]);       downpre[i + 2] = min(downpre[i + 2], downpre[i]);     }     for (int i = MAXN - 1; i > 1; i--) {       upsuf[i - 2] = max(upsuf[i], upsuf[i - 2]);       downsuf[i - 2] = min(downsuf[i - 2], downsuf[i]);     }     for (int i = 0; i < MAXN; i++)       down[i] = max((i - 1 > -1 && i - 1 < MAXN ? downpre[i - 1] : -1e9 - 7),                     (i + 1 > -1 && i + 1 < MAXN ? downsuf[i + 1] : -1e9 - 7));     for (int i = 0; i < MAXN; i++)       up[i] = min((i - 1 > -1 && i - 1 < MAXN ? uppre[i - 1] : 1e9 + 7),                   (i + 1 > -1 && i + 1 < MAXN ? upsuf[i + 1] : 1e9 + 7));     long long ans = 0;     for (int i = 1; i < MAXN - 1; i++) {       ans += max(0, up[i] - down[i]) / 2;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long dx[] = {-1, 0, 0, 1};   const long long dy[] = {0, -1, 1, 0};   const int Mass = (int)2e5 + 5;   const int inf = (int)1e9 + 5;   const long long linf = (long long)1e18 + 5;   const long long Mod = (long long)1e9 + 7;   long long ModFix(long long a, long long Mod) {     return ((((a) % (Mod)) + (Mod)) % (Mod));   }   long long ModSum(long long a, long long b, long long Mod) {     return ModFix(((a) + (b)), (Mod));   }   long long ModSub(long long a, long long b, long long Mod) {     return ModFix(((a) - (b)), (Mod));   }   long long ModMul(long long a, long long b, long long Mod) {     return ModFix(((a) * (b)), (Mod));   }   int a[150];   int main() {     ios_base ::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n;     cin >> m;     int res = 0;     for (int i = 1; i <= n; ++i) cin >> a[i];     for (int i = 1; i <= m; ++i) {       int l, r;       cin >> l >> r;       int sum = 0;       for (int j = l; j <= r; ++j) {         sum += a[j];       }       if (sum >= 0) res += sum;     }     cout << res;     return 0;   }
#include <bits/stdc++.h>   const int N = 1000 + 10;   struct tnode {     int x, y;     long long val;     tnode() {}     tnode(int _x, int _y, long long _val) {       x = _x;       y = _y;       val = _val;     }   };   int n, m, r, c;   int a[N][N], b[N][N], f[N][N];   long long sum[N][N];   int Q[N], head, tail;   std::vector<tnode> ans, out;   bool vis[N][N];   long long S(int x1, int y1, int x2, int y2) {     return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];   }   bool cmp(const tnode& i, const tnode& j) {     if (i.val != j.val)       return i.val < j.val;     else if (i.x != j.x)       return i.x < j.x;     else       return i.y < j.y;   }   int main() {     scanf( %d%d%d%d , &n, &m, &r, &c);     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) {         scanf( %d , &a[i][j]);         sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];       }     for (int i = 1; i <= n; ++i) {       head = tail = 0;       for (int j = 1; j <= m; ++j) {         while (head != tail && a[i][Q[tail - 1]] >= a[i][j]) {           if (--tail < 0) tail = N - 1;         }         while (head != tail && Q[head] < j - c + 1) {           if (++head == N) head = 0;         }         Q[tail++] = j;         if (tail == N) tail = 0;         if (j >= c) b[i][j - c + 1] = a[i][Q[head]];       }     }     for (int j = 1; j + c - 1 <= m; ++j) {       head = tail = 0;       for (int i = 1; i <= n; ++i) {         while (head != tail && b[Q[tail - 1]][j] >= b[i][j]) {           if (--tail < 0) tail = N - 1;         }         while (head != tail && Q[head] < i - r + 1) {           if (++head == N) head = 0;         }         Q[tail++] = i;         if (tail == N) tail = 0;         if (i >= r) f[i - r + 1][j] = b[Q[head]][j];       }     }     for (int i = 1; i + r - 1 <= n; ++i)       for (int j = 1; j + c - 1 <= m; ++j) {         ans.push_back(tnode(             i, j, S(i, j, i + r - 1, j + c - 1) - (long long)f[i][j] * r * c));       }     std::sort(ans.begin(), ans.end(), cmp);     for (int i = 0; i < ans.size(); ++i) {       if (vis[ans[i].x][ans[i].y]) continue;       out.push_back(ans[i]);       for (int j = std::max(1, ans[i].x - r + 1); j <= ans[i].x + r - 1; ++j)         for (int k = std::max(1, ans[i].y - c + 1); k <= ans[i].y + c - 1; ++k)           vis[j][k] = true;     }     printf( %d n , out.size());     for (int i = 0; i < out.size(); ++i) {       printf( %d %d %I64d n , out[i].x, out[i].y, out[i].val);     }     return 0;   }
#include <bits/stdc++.h>   using LL = long long;   const LL inf = 1LL << 60;   const int kN = 14;   int a[kN], n;   int delta[kN];   int cnt[1 << kN];   LL f[1 << kN];   int decode(char c) {     if (c >=  0  && c <=  9 ) return c -  0 ;     return c -  a  + 10;   }   char encode(int x) {     if (x < 10) return x +  0 ;     return x - 10 +  a ;   }   LL solve() {     for (int mask = 1; mask < 1 << n; ++mask) {       int p = __builtin_popcount((mask & -mask) - 1);       cnt[mask] = cnt[mask ^ 1 << p] - delta[p];       f[mask] = inf;       if (cnt[mask] < 0 || cnt[mask] >= 16) continue;       for (int i = 0; i < n; ++i)         if (mask >> i & 1) {           int q = cnt[mask ^ 1 << i];           if (q < 0 || q >= 16) continue;           f[mask] =               std::min(f[mask], f[mask ^ 1 << i] + (cnt[mask] * 1LL << 4 * i));         }     }     return f[(1 << n) - 1];   }   int main() {     char str[15];     scanf( %s , str);     n = strlen(str);     for (int i = 0; i < n; ++i) a[i] = decode(str[n - 1 - i]);     LL result = inf;     for (int mask = 0; mask < 1 << n - 1; ++mask) {       bool ok = true;       int sum = 0;       for (int i = 0; i < n; ++i) {         int d = a[i];         if (mask >> i & 1) d -= 16;         if (i && (mask >> i - 1 & 1)) d += 1;         ok &= -16 < d && d < 16;         sum += d;         delta[i] = d;       }       if (ok && sum == 0) result = std::min(result, solve());     }     if (result == inf)       puts( NO );     else {       for (int i = n - 1; i >= 0; --i) putchar(encode(result >> 4 * i & 15));       puts(  );     }   }
#include <bits/stdc++.h>   using namespace std;   int n, b, g, ans;   int main() {     scanf( %d%d%d , &b, &g, &n);     for (register int i = 0; i <= n; ++i) {       register int a = i, f = n - i;       if (a <= b && f <= g) ++ans;     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int t, a, b, c;   int main() {     cin >> t;     for (; t; t--) {       cin >> a >> b >> c;       int res = 0;       if (c >= b) {         swap(b, c);       }       if (b >= a) {         swap(a, b);       }       if (c > 0) {         c--;         res++;       }       if (b > 0) {         b--;         res++;       }       if (a > 0) {         a--;         res++;       }       if ((a > 0) && (b > 0)) {         a--;         b--;         res++;       }       if ((c > 0) && (a > 0)) {         a--;         c--;         res++;       }       if ((c > 0) && (b > 0)) {         c--;         b--;         res++;       }       if ((c > 0) && (b > 0) && (a > 0)) {         a--;         b--;         c--;         res++;       }       cout << res <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     long long v;     bool c;   } arr[12000];   bool comparator(node a, node b) { return a.v < b.v; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long i, j, p, v, l, n = 0, a, ans = 0;     cin >> a;     string s;     cin >> s;     l = s.length();     s =  +  + s;     for (i = 0; i < l; i += 3) {       v = 0;       p = s[i++] ==  +  ? 1 : -1;       if (s[i] >=  0  and s[i] <=  9 )         for (; s[i] >=  0  and s[i] <=  9 ; i++) v = v * 10 + s[i] - 48;       else         v = 1;       if (s[i] ==  * ) i++;       n++;       arr[n].v = v * p;       arr[n].c = s[i] ==  a ;     }     sort(arr + 1, arr + 1 + n, comparator);     for (i = 1; i <= n; i++) {       if (arr[i].c)         ans += arr[i].v * a++;       else         ans += arr[i].v * ++a;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3000 + 50;   struct Edge {     int to, next;     long long w;   } edge[maxn * 2];   int k = 1, head[maxn];   void add(int u, int v) {     edge[++k].to = v;     edge[k].next = head[u];     head[u] = k;   }   long long n, m, b[maxn], w[maxn], v[maxn], sz[maxn], T;   pair<long long, long long> dp[maxn][maxn], temp[maxn];   void update(pair<long long, long long> &x, pair<long long, long long> y) {     if (y > x) x = y;   }   void dfs(int u, int f) {     sz[u] = 1;     dp[u][1] = make_pair(0, v[u]);     for (int i = head[u]; i; i = edge[i].next) {       if (edge[i].to == f) continue;       dfs(edge[i].to, u);       for (int j = 1; j <= sz[u] + sz[edge[i].to]; j++)         temp[j] = make_pair(-1, 0);       for (int j = 1; j <= sz[u]; j++) {         for (int k = 1; k <= sz[edge[i].to]; k++) {           update(temp[j + k], make_pair(dp[u][j].first + dp[edge[i].to][k].first +                                             (dp[edge[i].to][k].second > 0),                                         dp[u][j].second));           update(temp[j + k - 1],                  make_pair(dp[u][j].first + dp[edge[i].to][k].first,                            dp[u][j].second + dp[edge[i].to][k].second));         }       }       for (int j = 1; j <= sz[u] + sz[edge[i].to]; j++) dp[u][j] = temp[j];       sz[u] += sz[edge[i].to];     }   }   int main() {     cin >> T;     while (T--) {       cin >> n >> m;       for (int i = 1; i <= n; i++) cin >> b[i], v[i] -= b[i];       for (int i = 1; i <= n; i++) cin >> w[i], v[i] += w[i];       for (int i = 1; i < n; i++) {         int a, b;         cin >> a >> b;         add(a, b);         add(b, a);       }       dfs(1, 0);       long long ans = dp[1][m].first + (dp[1][m].second > 0);       cout << ans << endl;       k = 1;       memset(head, 0, sizeof(int) * (n + 10));       memset(v, 0, sizeof(long long) * (n + 10));       memset(sz, 0, sizeof(long long) * (n + 10));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   priority_queue<pair<int, int> > p1, p2, p12;   int n, s1, s2;   int num1[3005];   int num2[3005];   bool vis[3005];   int ans1[3005];   int ans2[3005];   int main() {     cin >> n >> s1 >> s2;     for (int i = 0; i < n; i++) {       scanf( %d , &num1[i]);       p1.push(make_pair(num1[i], i));     }     for (int i = 0; i < n; i++) {       scanf( %d , &num2[i]);       p2.push(make_pair(num2[i], i));     }     long long ans = 0;     for (int i = 0; i < s1; i++) {       pair<int, int> u;       u = p1.top();       p1.pop();       vis[u.second] = true;       ans1[i] = u.second;       ans += u.first;       p12.push(make_pair(num2[u.second] - num1[u.second], i));     }     for (int i = 0; i < s2; i++) {       pair<int, int> u, v, w;       while (!p1.empty() && vis[p1.top().second]) p1.pop();       while (!p2.empty() && vis[p2.top().second]) p2.pop();       if (!p1.empty()) u = p1.top();       if (!p2.empty()) v = p2.top();       w = p12.top();       if (p2.empty() || u.first + w.first > v.first) {         ans2[i] = ans1[w.second];         ans1[w.second] = u.second;         vis[u.second] = true;         ans += u.first + w.first;         p1.pop();         int idx = u.second;         p12.push(make_pair(num2[idx] - num1[idx], w.second));         p12.pop();       } else {         ans += v.first;         ans2[i] = v.second;         vis[v.second] = true;         p2.pop();       }     }     cout << ans << endl;     for (int i = 0; i < s1; i++) {       cout << ans1[i] + 1 <<    ;     }     cout << endl;     for (int i = 0; i < s2; i++) {       cout << ans2[i] + 1 <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int mx = 2e6 + 10;   int n, m;   bool vis[mx];   int sta[mx], siz, is;   int dfn[mx], id[mx];   int be[mx], ty;   vector<int> g[mx];   void tarjan(int u) {     dfn[u] = id[u] = ++is;     sta[++siz] = u;     vis[u] = 1;     for (int v : g[u]) {       if (!dfn[v]) {         tarjan(v);         id[u] = min(id[u], id[v]);       } else if (vis[v])         id[u] = min(id[u], dfn[v]);     }     if (id[u] == dfn[u]) {       ty++;       while (sta[siz] != u) {         be[sta[siz]] = ty;         vis[sta[siz]] = 0;         siz--;       }       be[u] = ty, vis[u] = 0, siz--;     }   }   int cnt_one() {     int cnt = 0;     for (int i = 1; i <= n; i++)       if (be[i] == 1) cnt++;     return cnt;   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       scanf( %d%d , &n, &m);       for (int i = 1; i <= 2 * n; i++) g[i].clear();       ty = is = siz = 0;       for (int i = 1; i <= 2 * n; i++) dfn[i] = vis[i] = 0;       for (int i = 0, u, v; i < m; i++) {         scanf( %d%d , &u, &v);         if (u == v)           g[v + n].push_back(u);         else           g[u].push_back(v + n);       }       if (n == 1) {         puts( No );         continue;       }       for (int i = 1; i <= n; i++)         if (!dfn[i]) tarjan(i);       int cnt = cnt_one();       if (cnt == n)         puts( No );       else {         puts( Yes );         printf( %d %d n , cnt, n - cnt);         for (int i = 1; i <= n; i++)           if (be[i] == 1) printf( %d  , i);         puts(  );         for (int i = 1; i <= n; i++)           if (be[i] != 1) printf( %d  , i);         puts(  );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long n, m, k, i;     bool t = 0;     cin >> n >> m >> k;     if ((2 * m * n) % k > 0)       cout <<  NO ;     else {       if (k == 2)         cout <<  YES n0 0 n  << n <<   0 n0   << m;       else {         k = (2 * m * n) / k;         if (m >= n) {           for (i = max(1, (int)(k / m)); i <= n; i++) {             if (k % i == 0 && k / i <= m) {               t = 1;               break;             }           }           if (!t)             cout <<  NO ;           else             cout <<  YES n0 0 n  << i <<   0 n0   << k / i;         } else {           for (i = max(1, (int)(k / n)); i <= m; i++) {             if (k % i == 0 && k / i <= n) {               t = 1;               break;             }           }           if (!t)             cout <<  NO ;           else             cout <<  YES n0 0 n  << k / i <<   0 n0   << i;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline long long read(long long num = 0, char c = 0) {     while (c >  9  || c <  0 ) c = getchar();     while (c >=  0  && c <=  9 ) num = num * 10 + c - 48, c = getchar();     return num;   }   struct node {     int x, y, val, num;     void in() { x = read(), y = read(), val = read(); }   } p[500010];   int f[2005], n, m, q, l, r;   int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }   bool cmp(node a, node b) { return a.val > b.val; }   int work() {     for (register int i = 1; i <= m; ++i) {       if (p[i].num >= l && p[i].num <= r) {         if (find(p[i].x) == find(p[i].y)) {           return p[i].val;         } else {           f[find(p[i].x + n)] = f[find(p[i].y)];           f[find(p[i].y + n)] = f[find(p[i].x)];         }       }     }     return -1;   }   int main() {     n = read(), m = read(), q = read();     for (register int i = 1; i <= m; ++i) p[i].in(), p[i].num = i;     sort(p + 1, p + 1 + m, cmp);     for (register int i = 1; i <= q; ++i) {       l = read(), r = read();       for (register int i = 1; i <= 2 * n; ++i) f[i] = i;       printf( %d n , work());     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool isSafe(const int n, const int m, const int row, const int col) {     return row >= 0 && row < n && col >= 0 && col < m;   }   int main(int argc, char* argv[]) {     int n, m;     static pair<int, int> pos[] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0},                                    {0, 1},   {1, -1}, {1, 0},  {1, 1}};     int posLen = sizeof(pos) / sizeof(pos[0]);     cin >> n >> m;     vector<string> vec(n);     for (int i = 0; i < n; i++) {       cin >> vec[i];     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int count = 0;         if (vec[i][j] ==  * ) {           continue;         }         for (int k = 0; k < posLen; k++) {           if (isSafe(n, m, i + pos[k].first, j + pos[k].second) &&               vec[i + pos[k].first][j + pos[k].second] ==  * ) {             count++;           }         }         if ((count == 0 && vec[i][j] !=  . ) ||             (count != 0 && count != vec[i][j] -  0 )) {           cout <<  NO ;           return 0;         }       }     }     cout <<  YES ;     return 0;   }