malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int P = 998244353;   int add(int a, int b) {     if ((a += b) >= P) a -= P;     return a < 0 ? a + P : a;   }   int mul(int a, int b) { return 1ll * a * b % P; }   int kpow(int a, int b) {     int r = 1;     for (; b; b >>= 1, a = mul(a, a)) {       if (b & 1) r = mul(r, a);     }     return r;   }   const int N = 3e5 + 7;   int f[N][2][2], n, u, v, pre[N], suf[N];   vector<int> g[N];   void dfs(int u, int fa) {     int p = -1;     for (int i = (0); i < ((int)g[u].size()); i++)       if (g[u][i] == fa) {         p = i;         break;       }     if (p >= 0) g[u].erase(g[u].begin() + p);     f[u][0][0] = 1;     f[u][1][0] = 1;     for (auto v : g[u]) {       dfs(v, u);       f[u][0][0] = mul(f[u][0][0], add(f[v][0][0], f[v][1][1]));       f[u][1][0] = mul(f[u][1][0], add(f[v][0][0], mul(f[v][1][1], 2)));       pre[v] = suf[v] = add(f[v][0][0], mul(f[v][1][1], 2));     }     f[u][1][0] = add(f[u][1][0], -f[u][0][0]);     for (int i = (1); i < ((int)g[u].size()); i++)       pre[g[u][i]] = mul(pre[g[u][i]], pre[g[u][i - 1]]);     for (int i = ((int)g[u].size() - 1) - 1; i >= (0); i--)       suf[g[u][i]] = mul(suf[g[u][i]], suf[g[u][i + 1]]);     for (int i = (0); i < ((int)g[u].size()); i++) {       int v = g[u][i], t = add(f[v][1][0], f[v][0][0]);       if (i) t = mul(t, pre[g[u][i - 1]]);       if (i + 1 < (int)g[u].size()) t = mul(t, suf[g[u][i + 1]]);       f[u][1][1] = add(f[u][1][1], t);     }   }   int main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     cin >> n;     for (int i = (1); i < (n); i++) {       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1, 0);     cout << add(f[1][0][0], f[1][1][1]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MX = 500005;   const long long MOD = 1000000007;   void setIO(string name) {     ios_base::sync_with_stdio(0);     cin.tie(0);     freopen((name +  .in ).c_str(),  r , stdin);     freopen((name +  .out ).c_str(),  w , stdout);   }   int t;   int n;   void solve() {     cin >> n;     long long x = 0, even = 0, odd = 0;     long long sum = 0, improv = 0;     for (int i = 0; i < (n); i++) {       int a;       cin >> a;       if (i % 2 == 0) {         x -= a;         improv = max(improv, x - even);         even = min(x, even);         sum += a;       }       if (i % 2 == 1) {         x += a;         improv = max(improv, x - odd);         odd = min(x, odd);       }     }     cout << sum + improv << endl;   }   int main() {     cin >> t;     for (int i = 0; i < (t); i++) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   long long n, sol = 0, modul = 1000000007;   vector<long long> parent, depth;   long long where(long long pos) {     if (parent[pos] == -1) return pos;     long long a = depth[pos];     long long b = parent[pos];     long long c = where(parent[pos]);     parent[pos] = c;     depth[pos] = (a + depth[b]) % modul;     return c;   }   void uniona(long long a, long long x, long long b) {     long long wa = where(a);     if (wa == b) return;     sol = (sol + x + depth[a]) % modul;     depth[wa] = (x + depth[a]) % modul;     parent[wa] = b;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n;     parent.assign(n, -1);     depth.assign(n, 0);     for (long long i = 0; i < n; i++) {       long long k;       cin >> k;       for (long long j = 0; j < k; j++) {         long long a, b;         cin >> a >> b;         a--;         uniona(a, b, i);       }     }     cout << (sol + modul) % modul << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1000000007;   const int N = 3e5 + 5;   const int INF = 1e9 + 7;   vector<int> graph[N];   multiset<int, greater<int>> st;   int n, a, b, dat[N], ans, cur;   void rem(int val) {     auto it = st.find(val);     st.erase(it);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);     ans = INF;     cin >> n;     for (int i = (0); i < (n); i++) {       cin >> dat[i];       st.insert(dat[i]);     }     for (int i = (0); i < (n - 1); i++) {       cin >> a >> b;       --a;       --b;       graph[a].push_back(b);       graph[b].push_back(a);     }     for (int i = (0); i < (n); i++) {       rem(dat[i]);       for (auto it : graph[i]) rem(dat[it]);       cur = -INF;       cur = max(cur, dat[i]);       if (!st.empty()) cur = max(cur, *st.begin() + 2);       st.insert(dat[i]);       for (auto it : graph[i]) {         st.insert(dat[it]);         cur = max(cur, dat[it] + 1);       }       ans = min(ans, cur);     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int k;     cin >> k;     int x;     cin >> x;     int* a;     a = new int[n + 1];     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     int index;     index = n;     while (k > 0) {       a[index] = x;       index--;       k--;     }     int sum;     sum = 0;     for (int i = 1; i <= n; i++) {       sum = sum + a[i];     }     cout << sum;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     vector<int> V;     int px = 0;     while (px < n) {       if (a[px] == 1) {         if (px < n && a[px + 1] == 1) {           px += 2;           continue;         }         V.push_back(px);       }       px++;     }     px = 0;     cout << (n - V.size()) <<   n ;     for (int i = 0; i < n; i++) {       while (px < (V.size()) && V[px] < i) px++;       if (px < (V.size()) && V[px] == i) continue;       cout << a[i] <<    ;     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t) {       t--;       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   signed main() {     long long a[4];     for (long long i = 0; i < 4; i++) cin >> a[i];     sort(a, a + 4);     if ((a[3] + a[0] == a[2] + a[1]) || (a[3] == a[0] + a[1] + a[2]))       cout <<  YES  << endl;     else       cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string str[51], temp;   int main() {     int t, f, a;     int cnt = 0, ans = 0x3f3f3f3f;     scanf( %d , &t);     getchar();     for (int i = 0; i < t; i++) {       cin >> str[i];     }     for (int i = 0; i < t; i++) {       cnt = 0;       for (int j = 0; j < t; j++) {         temp = str[j] + str[j];         if (temp.find(str[i]) == string::npos) {           cout << -1 << endl;           return 0;         }         cnt += temp.find(str[i]);       }       ans = min(ans, cnt);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int Maxn = 2e5 + 5;   vector<int> adj[Maxn];   int sz[Maxn];   int n;   long long ret, ans;   void dfs_init(int u, int f) {     sz[u] = 1;     for (int i = 0; i < adj[u].size(); i++) {       int v = adj[u][i];       if (v == f) continue;       dfs_init(v, u);       sz[u] += sz[v];     }     ret += sz[u];   }   void dfs(int u, int f, long long ret) {     ans = max(ans, ret);     for (int i = 0; i < adj[u].size(); i++) {       int v = adj[u][i];       if (v == f) continue;       dfs(v, u, ret + n - 2 * sz[v]);     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int u, v;       scanf( %d%d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);     }     ans = ret = 0;     dfs_init(1, 0);     dfs(1, 0, ret);     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 7;   long long int sum[N << 2][11], sum2[11], a[N];   int lazy[N << 2][11], vis[11];   void pushdown(int rt) {     for (int i = 0; i < 10; i++)       vis[i] = lazy[rt << 1][i], sum2[i] = sum[rt << 1][i];     for (int i = 0; i < 10; i++)       if (lazy[rt][i] != i) {         for (int j = 0; j < 10; j++)           if (lazy[rt << 1][j] == i) vis[j] = lazy[rt][i];         sum2[lazy[rt][i]] += sum[rt << 1][i];         sum2[i] -= sum[rt << 1][i];       }     for (int i = 0; i < 10; i++)       sum[rt << 1][i] = sum2[i], lazy[rt << 1][i] = vis[i];     for (int i = 0; i < 10; i++)       vis[i] = lazy[rt << 1 | 1][i], sum2[i] = sum[rt << 1 | 1][i];     for (int i = 0; i < 10; i++)       if (lazy[rt][i] != i) {         for (int j = 0; j < 10; j++)           if (lazy[rt << 1 | 1][j] == i) vis[j] = lazy[rt][i];         sum2[lazy[rt][i]] += sum[rt << 1 | 1][i];         sum2[i] -= sum[rt << 1 | 1][i];       }     for (int i = 0; i < 10; i++)       sum[rt << 1 | 1][i] = sum2[i], lazy[rt << 1 | 1][i] = vis[i];     for (int i = 0; i < 10; i++) lazy[rt][i] = i;   }   void pushup(int rt) {     for (int i = 0; i < 10; i++)       sum[rt][i] = sum[rt << 1][i] + sum[rt << 1 | 1][i];   }   void build(int rt, int l, int r) {     for (int i = 0; i < 10; i++) sum[rt][i] = 0;     for (int i = 0; i < 10; i++) lazy[rt][i] = i;     if (l == r) {       for (long long int t = 1; a[l]; a[l] /= 10, t *= 10)         sum[rt][a[l] % 10] += t;       return;     }     int m = r + l >> 1;     build(rt << 1, l, m);     build(rt << 1 | 1, m + 1, r);     pushup(rt);   }   void update(int rt, int l, int r, int L, int R, int x, int y) {     if (L <= l && r <= R) {       for (int i = 0; i < 10; i++)         if (lazy[rt][i] == x) {           sum[rt][y] += sum[rt][x];           sum[rt][x] = 0;           lazy[rt][i] = y;         }       return;     }     pushdown(rt);     int m = r + l >> 1;     if (L <= m) update(rt << 1, l, m, L, R, x, y);     if (R > m) update(rt << 1 | 1, m + 1, r, L, R, x, y);     pushup(rt);   }   long long int query(int rt, int l, int r, int L, int R) {     if (L <= l && r <= R) {       long long int ans = 0;       for (long long int i = 1; i < 10; i++) ans += sum[rt][i] * i;       return ans;     }     pushdown(rt);     int m = r + l >> 1;     long long int ans = 0;     if (L <= m) ans += query(rt << 1, l, m, L, R);     if (R > m) ans += query(rt << 1 | 1, m + 1, r, L, R);     pushup(rt);     return ans;   }   int n, m;   int main() {     while (~scanf( %d%d , &n, &m)) {       for (int i = 1; i <= n; i++) scanf( %lld , &a[i]);       build(1, 1, n);       int op, l, r, x, y;       for (int i = 1; i <= m; i++) {         scanf( %d%d%d , &op, &l, &r);         if (1 == op) {           scanf( %d%d , &x, &y);           if (x == y) continue;           update(1, 1, n, l, r, x, y);         } else           printf( %lld n , query(1, 1, n, l, r));       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long labs(long long a) { return a < 0 ? (-a) : a; }   vector<pair<long long, long long> > v;   vector<long long> c;   vector<bool> was;   int main() {     int n, k;     cin >> n >> k;     long long b;     cin >> b;     v.resize(n);     c.resize(n);     for (int(i) = 0; (i) < (n); (i)++) {       cin >> c[i];       v[i].first = c[i];       v[i].second = i;     }     v.pop_back();     sort((v).begin(), (v).end());     reverse((v).begin(), (v).end());     was.assign(n, false);     long long sum = 0;     for (int(i) = 0; (i) < (k - 1); (i)++) {       was[v[i].second] = true;       sum += v[i].first;     }     int res = n - 1;     for (int(i) = 0; (i) < (n); (i)++) {       if (was[i]) {         if (sum + v[k - 1].first > b) {           res = min(i, res);         }       } else {         if (sum + c[i] > b) {           res = min(i, res);         }       }     }     cout << res + 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int a[N];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     sort(a + 1, a + n + 1);     int lim = n / 2 - 1, res = 1e9 + 7;     for (int i = 0; i <= lim; i++) {       res = min(res, a[n - (lim - i)] - a[i + 1]);     }     printf( %d n , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     ;     long long x, y;     cin >> x >> y;     long long n;     cin >> n;     long long ans;     switch (n % 6) {       case 1:         ans = x;         break;       case 2:         ans = y;         break;       case 3:         ans = y - x;         break;       case 4:         ans = -x;         break;       case 5:         ans = -y;         break;       case 0:         ans = x - y;         break;     }     cout << (ans + 2 * mod) % mod;   }
#include <bits/stdc++.h>   using namespace std;   int n, a, b, h[20], f[20][20][20][20], g[20][20][20][20][5];   void update(int &f1, int f2, int p[5], int i, int j, int k, int l, int w) {     if (f2 < f1) {       f1 = f2;       p[0] = w;       p[1] = i;       p[2] = j;       p[3] = k;       p[4] = l;     }   }   int main() {     cin >> n >> a >> b;     memset(h, 0, sizeof h);     for (int i = 0; i < n; i++) {       scanf( %d , h + i);       h[i]++;     }     memset(f, 0x3f, sizeof f);     f[1][h[0]][h[1]][h[2]] = 0;     memset(g, 0xff, sizeof g);     for (int i = 1; i < n; i++) {       for (int j = 16; j >= 0; j--) {         for (int k = 16; k >= 0; k--)           for (int l = 16; l >= 0; l--) {             if (j == 0)               update(f[i + 1][k][l][h[i + 2]], f[i][j][k][l],                      g[i + 1][k][l][h[i + 2]], g[i][j][k][l][1], g[i][j][k][l][2],                      g[i][j][k][l][3], g[i][j][k][l][4], g[i][j][k][l][0]);             if (i != n - 1) {               int j1 = j - b > 0 ? j - b : 0, k1 = k - a > 0 ? k - a : 0,                   l1 = l - b > 0 ? l - b : 0;               if (j <= b)                 update(f[i + 1][k1][l1][h[i + 2]], f[i][j][k][l] + 1,                        g[i + 1][k1][l1][h[i + 2]], i, j, k, l, i);               update(f[i][j1][k1][l1], f[i][j][k][l] + 1, g[i][j1][k1][l1], i, j,                      k, l, i);             }             if (i != 1) {               int j1 = j - a > 0 ? j - a : 0, k1 = k - b > 0 ? k - b : 0;               if (j <= a)                 update(f[i + 1][k1][l][h[i + 2]], f[i][j][k][l] + 1,                        g[i + 1][k1][l][h[i + 2]], i, j, k, l, i - 1);               update(f[i][j1][k1][l], f[i][j][k][l] + 1, g[i][j1][k1][l], i, j, k,                      l, i - 1);             }           }       }     }     cout << f[n - 1][0][0][0] << endl;     int i = n - 1, j = 0, k = 0, l = 0;     while (i != -1) {       if (g[i][j][k][l][0] == -1) break;       printf( %d  , g[i][j][k][l][0] + 1);       int i1 = g[i][j][k][l][1], j1 = g[i][j][k][l][2], k1 = g[i][j][k][l][3],           l1 = g[i][j][k][l][4];       i = i1, j = j1, k = k1, l = l1;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 300000;   const long long INF = (1 << 30) - 1;   int n;   long long a[N + 9], sum[N + 9];   void into() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %lld , &a[i]);       sum[i] = a[i] + sum[i - 1];     }   }   int ans;   void Get_ans(int l, int r) {     if (l <= 0 || r > n) return;     for (; 2333;) {       if ((a[l] ^ a[r]) == sum[r - 1] - sum[l]) ++ans;       int le = 1, ri = n, len = n + 1;       for (int mid; le <= ri;) {         mid = le + ri >> 1;         int nl = l - mid, nr = r + mid;         if (nl <= 0 || nr > n) {           len = mid;           ri = mid - 1;           continue;         }         sum[nr] - sum[nl - 1] - sum[r - 1] + sum[l] >= sum[r - 1] - sum[l]             ? (len = mid, ri = mid - 1)             : le = mid + 1;       }       l -= len;       r += len;       if (l <= 0 || r > n) return;       if (sum[r - 1] - sum[l] > INF) return;     }   }   void Get_ans() {     for (int i = 1; i <= n; ++i) Get_ans(i - 1, i + 1);     for (int i = 1; i < n; ++i) Get_ans(i - 1, i + 2);   }   void work() { Get_ans(); }   void outo() { printf( %d n , ans); }   int main() {     int T = 1;     for (; T--;) {       into();       work();       outo();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> X[105], Y[105];   char s[105];   int main() {     int n, i, j;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %s , s);       for (j = 0; j < n; j++) {         if (s[j] ==  . ) {           X[i].push_back(j);           Y[j].push_back(i);         }       }     }     for (i = 0; i < n; i++) {       if (X[i].size() == 0) break;     }     if (i == n) {       for (i = 0; i < n; i++) {         printf( %d %d n , i + 1, X[i][0] + 1);       }       return 0;     }     for (i = 0; i < n; i++) {       if (Y[i].size() == 0) break;     }     if (i < n) {       printf( -1 n );       return 0;     }     for (i = 0; i < n; i++) {       printf( %d %d n , Y[i][0] + 1, i + 1);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   bool umin(T& a, T b) {     if (a > b) {       a = b;       return 1;     }     return 0;   }   template <class T>   bool umax(T& a, T b) {     if (a < b) {       a = b;       return 1;     }     return 0;   }   int main() {     int t;     cin >> t;     while (t--) {       int n;       string s;       cin >> n >> s;       int a = n, b = n;       for (int i = 0; i < n; i++)         if (s[i] ==  > ) {           a = i;           break;         }       for (int i = n - 1; i >= 0; i--)         if (s[i] ==  < ) {           b = n - i - 1;           break;         }       printf( %d n , min(a, b));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   const int MOD = 1e9 + 7;   const long long M = 1e18;   long long qpow(long long a, long long b, long long m) {     long long ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % m;       b /= 2;       a = (a * a) % m;     }     return ans;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int t = 1;     while (t--) {       int n, q;       cin >> n >> q;       int a[n + 1];       map<int, int> mn, mx, ct;       for (int i = 1; i <= n; i++) {         cin >> a[i];         if (mn[a[i]] == 0) mn[a[i]] = i;         mx[a[i]] = i;         ct[a[i]]++;       }       int l = 1, r = 1;       int ans = 0;       for (int i = 1; i <= n; i++) {         r = max(r, mx[a[i]]);         if (i == r) {           int ans1 = N;           for (int j = l; j <= r; j++) {             ans1 = min(ans1, (r - l + 1) - ct[a[j]]);           }           ans += ans1;           l = r + 1;           r++;         }       }       cout << ans;     }   }
#include <bits/stdc++.h>   using namespace std;   int n;   bool f, a[(int)1e6 + 9];   int main() {     cin >> n;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 1; i < (n == 3 ? 2 : (n >> 1)); i++) {       if (n % i == 0) {         for (int j = 0; j < i; j++) {           if (!a[j]) continue;           int k = j + i;           f = 1;           while (k != j) {             if (!a[k]) {               f = 0;               break;             }             k = (k + i) % n;           }           if (f) break;         }         if (f) break;       }     }     if (f)       printf( YES );     else       printf( NO );     return 0;   }
#include<bits/stdc++.h>   #define int long long   #define float double   #define itn int   #define f(j,n) for(int i=j;i<n;i++)   #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)   #define endl   n    #define Endl   n    #define ednl   n    #define Ednl   n    #define pb push_back   #define mp make_pair   #define mod 1000000000000000000   #define mset(arr, i) memset(arr, i, sizeof(arr))   using namespace std;       typedef vector<pair<int, int>> vpi;   typedef vector<vector<int>> vvi;   typedef pair<int, int> pii;   typedef vector<int> vi;   typedef map<int,int> mii;   typedef priority_queue<int> pqi;   typedef priority_queue<int, vector<int>, greater<int>> pqim;       long long binpow(long long a, long long b) { a %= mod; long long res = 1; while (b > 0){if (b & 1)res = res * a % mod;a = a * a % mod;b >>= 1;}return res;}      int LCSubStr(string X, string  Y, int m, int n)   {        int LCSuff[m + 1][n + 1];       int result = 0;        for (int i = 0; i <= m; i++)       {           for (int j = 0; j <= n; j++)           {               if (i == 0 || j == 0)                   LCSuff[i][j] = 0;                   else if (X[i - 1] == Y[j - 1]) {                   LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1;                   result = max(result, LCSuff[i][j]);               }               else                   LCSuff[i][j] = 0;           }       }       return result;   }      int32_t main()   {       int t1;       fastio;       cin>>t1;        //t1=1;       while(t1--)       {           string a, b;           cin>>a>>b;           int n1=a.length(), n2 = b.length();           int ans = LCSubStr(a, b, n1, n2);           cout<<n1+n2-2*ans<<endl;       }          }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     int a[100000];     for (int i = 0; i < n; ++i) {       cin >> a[i];     }     sort(a, a + n);     cout << min(a[n - 1] - a[1], a[n - 2] - a[0]) <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   signed main(signed argc, const char* argv[]) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     int cnt = 0;     for (int i = 0; cnt < n; ++i) {       cout << 3 <<     << i << endl;       ++cnt;       if (cnt == n) break;       if (i % 2) {         ++cnt;         cout << 0 <<     << i << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int mn = 410, mm = 101000, inf = 1 << 30;   bool vis[mn];   int X[mm], Y[mm], du[mn];   int n, m, K, t, S, T, ans[mm], answer;   int dis[mm], pre[mm], Q[mm];   struct network {     int tt, la[mn], next[mm], y[mm], f[mm], co[mm];     void clear() {       tt = 1;       memset(la, 0, (T + 2) * 4);     }     void add(int i, int j, int u, int v) {       ++tt, next[tt] = la[i], la[i] = tt, y[tt] = j, f[tt] = u, co[tt] = v;       ++tt, next[tt] = la[j], la[j] = tt, y[tt] = i, f[tt] = 0, co[tt] = -v;     }   } e;   int spfa() {     memset(vis, 0, T + 2);     memset(pre, 0, (T + 2) * 4);     fill(dis + 1, dis + 1 + T, inf);     dis[S] = 0;     int st = 1, ed = 2;     Q[1] = S, vis[S] = 1;     while (st != ed) {       int r = Q[st];       for (int p = e.la[r], i; p; p = e.next[p]) {         if (!e.f[p]) continue;         i = e.y[p];         if (dis[r] + e.co[p] < dis[i]) {           dis[i] = dis[r] + e.co[p], pre[i] = p;           if (!vis[i]) {             Q[ed] = i, vis[i] = 1;             ed = (ed + 1) % (T + 2);           }         }       }       st = (st + 1) % (T + 2);       vis[r] = 0;     }     return dis[T];   }   void repair() {     for (int i = T, p; i != S; i = e.y[p ^ 1]) {       p = pre[i];       --e.f[p], ++e.f[p ^ 1];     }   }   int main() {     scanf( %d%d%d%d , &n, &m, &K, &t);     for (int i = 1; i <= K; ++i) {       scanf( %d%d , X + i, Y + i);       ++du[X[i]], ++du[Y[i] + n];     }     for (int i = 1; i <= n + m; ++i) answer += (du[i] % t) != 0;     S = n + m + 1, T = S + 1;     while (t) {       e.clear();       for (int i = 1; i <= K; ++i) {         if (ans[i]) continue;         e.add(X[i], Y[i] + n, 1, 0);       }       for (int i = 1; i <= n; ++i) {         e.add(S, i, du[i] / t, 0);         if (du[i] % t != 0) e.add(S, i, 1, 1);       }       for (int i = n + 1; i <= n + m; ++i) {         e.add(i, T, du[i] / t, 0);         if (du[i] % t != 0) e.add(i, T, 1, 1);       }       while (spfa() < inf) repair();       int tot = 0;       for (int i = 1; i <= K; ++i) {         if (ans[i]) continue;         ++tot;         if (e.f[tot * 2] == 0) {           ans[i] = t;           --du[X[i]], --du[Y[i] + n];         }       }       --t;     }     printf( %d n , answer);     for (int i = 1; i <= K - 1; ++i) printf( %d  , ans[i]);     printf( %d n , ans[K]);     return 0;   }