malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = 100000 + 7;   int n, m, comp[N], y, spre[N];   bool vis[N];   vector<int> g[N], ig[N], ord, rep[N];   set<int> sgn[N];   void dfs1(int a) {     vis[a] = 1;     for (auto &b : g[a]) {       if (!vis[b]) {         dfs1(b);       }     }     ord.push_back(a);   }   void dfs2(int a) {     rep[y].push_back(a);     comp[a] = y;     for (auto &b : ig[a]) {       if (!comp[b]) {         dfs2(b);       }     }   }   int este(int i, int j) {     cout <<  ?   << i <<     << j << endl;     int r;     cin >> r;     return r;   }   int main() {     cin >> n >> m;     for (int i = 1; i <= m; i++) {       int x, y;       cin >> x >> y;       g[x].push_back(y);       ig[y].push_back(x);     }     for (int i = 1; i <= n; i++) {       if (!vis[i]) {         dfs1(i);       }     }     reverse(ord.begin(), ord.end());     for (auto &i : ord) {       if (comp[i] == 0) {         y++;         dfs2(i);       }     }     for (int i = 1; i <= n; i++)       for (auto &j : g[i])         if (comp[i] != comp[j]) sgn[comp[i]].insert(comp[j]);     n = y;     for (int i = 1; i <= n; i++) {       g[i].clear();       for (auto &j : sgn[i]) {         g[i].push_back(j);         spre[j]++;       }     }     vector<int> stk;     for (int i = 1; i <= n; i++) {       if (!spre[i]) stk.push_back(i);     }     int ret = stk[0];     while ((int)stk.size() > 1) {       int now = stk.back();       while ((int)rep[now].size() > 0 && (int)rep[ret].size() > 0) {         int x = rep[now].back(), y = rep[ret].back();         if (este(x, y))           rep[ret].pop_back();         else           rep[now].pop_back();       }       if (rep[ret].empty()) {         swap(stk.back(), stk[0]);         swap(ret, now);       }       stk.pop_back();       for (auto &j : g[now]) {         spre[j]--;         assert(spre[j] >= 0);         if (!spre[j]) {           stk.push_back(j);         }       }     }     cout <<  !   << rep[ret].back() << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool func(int x) {     int cnt = 0;     while (x != 0) {       x = x / 10;       cnt++;     }     if (cnt == 1)       return true;     else if (cnt == 2)       return false;     else if (cnt == 0)       return true;   }   int main() {     int h1, h2, m1, m2;     int h, m, temp;     char ch;     cin >> h1;     cin >> ch;     cin >> m1;     cin >> h2;     cin >> ch;     cin >> m2;     int dh, dm, t;     dh = h2 - h1;     dm = m2 - m1;     if (dm == 0) {       float g = (double)dh / 2;       g *= 60;       if (g >= 60) {         int ho = g / 60;         int mi = (long long)g % 60;         h = h1 + ho;         m = mi + m1;       } else {         m = m1 + g;         if (m >= 60) {           h = h1 + (m / 60);           m = m % 60;         } else           h = h1;       }     } else if (dh == 0) {       h = h1;       m = m1 + dm / 2;     } else {       if (dm < 0) {         --dh;         t = dh + (60 - m1) + m2;       } else {         dh;         t = dh * 60 + (m2 - m1);       }       t = t / 2;       if (t + m1 >= 60) {         int u = h1 + t / 60;         int left = t % 60;         if (m1 + left >= 60) {           ++u;           m = (left - (60 - m1));         } else           m = m1 + left;         h = u;       } else {         h = h1;         m = m1 + t;       }     }     if (func(h) == true && func(m) == true) {       cout <<  0  << h <<  :  <<  0  << m << endl;     } else if (func(h) && !func(m)) {       cout <<  0  << h <<  :  << m << endl;     } else if (!func(h) && func(m)) {       cout << h <<  :  <<  0  << m << endl;     } else       cout << h <<  :  << m << endl;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:40000000 )   using namespace std;   const double EPS = 1e-10;   const double PI = acos(-1.0);   const int mod = 1000000007;   const int MAXN = 200 * 200;   int a[111];   int n;   int main() {     cin >> n;     bool p = 1;     for (int(i) = (0); (i) < (n); ++(i)) {       int x;       cin >> x;       a[x]++;       if (x == 25) continue;       if (x == 50) {         a[25]--;         if (a[25] < 0) p = 0;       }       if (x == 100) {         if (a[50] && a[25]) {           a[50]--;           a[25]--;         } else {           a[25] -= 3;           if (a[25] < 0) p = 0;         }       }     }     if (p)       cout <<  YES  << endl;     else       cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void Append(const string& w, string& a) {     a += w;     a +=  , ;   }   bool IsNumber(const string& w) {     for (char c : w) {       if (!isdigit(c)) return false;     }     return !w.empty() && (w ==  0  || w[0] !=  0 );   }   void Process(const string& s, int start, int end, string& a, string& b) {     assert(end - start + 1 >= 0);     const string w = s.substr(start, end - start + 1);     if (IsNumber(w)) {       Append(w, a);     } else {       Append(w, b);     }   }   void Output(string& s) {     if (s.empty())       cout <<  - n ;     else       cout <<     << s.substr(0, s.size() - 1) <<    n ;   }   int main() {     string s;     cin >> s;     s +=  , ;     string a, b;     int start = 0;     for (int i = 0; i < (int)s.size(); ++i) {       if (s[i] ==  ;  || s[i] ==  , ) {         Process(s, start, i - 1, a, b);         start = i + 1;       }     }     Output(a);     Output(b);   }
#include <bits/stdc++.h>   using namespace std;   long long f(long long x) {     int num = 0;     for (int i = 1; 1ll * i * i <= x; i++) {       if (x % i) continue;       num++;       if (1ll * i * i != x) num++;     }     return num;   }   long long solve(vector<int> v) {     int n = v.size();     long long num = 1ll * v[0] * v[n - 1];     for (int i = 1; i < n / 2 + (n % 2); i++) {       long long cur = 1ll * v[i] * v[n - i - 1];       if (cur != num) return -1;     }     if (f(num) != n + 2) return -1;     return num;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> a(n);       for (int i = 0; i < n; i++) {         cin >> a[i];       }       sort(a.begin(), a.end());       cout << solve(a) << endl;     }     return 0;   }
#include <bits/stdc++.h>   const int N = 1000005;   using namespace std;   long long n, k, sum, q, res, m, val, ans;   long long x, y;   double p;   long long xx[] = {1, 0, -1, 0};   long long yy[] = {0, 1, 0, -1};   void solve(long long no) {     cin >> n;     long long res = 0;     vector<long long> v[64];     for (int i = 0; i < n; i++) {       cin >> x;       res ^= x;       for (int j = 0; j < 64; j++)         if (x & (1LL << j)) v[j].push_back(i);     }     if (res == 0) {       cout <<  DRAW n ;       return;     }     vector<long long> v1;     for (int j = 63; j >= 0; j--) {       if (v[j].size() % 4 == 1) {         cout <<  WIN n ;         return;       } else if (v[j].size() % 4 == 3) {         if ((n - v[j].size()) % 2)           cout <<  WIN n ;         else           cout <<  LOSE n ;         return;       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     for (long long i = 1; i <= t; i++) solve(i);   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, m;     cin >> n >> m;     int sz = n + m - 2;     int mid = sz / 2;     int x;     vector<pair<int, int>> a(mid + 1, {0, 0});     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> x;         int id = i + j;         if (id > mid) {           id = sz - id;         }         if (x == 0) {           a[id].first++;         } else {           a[id].second++;         }       }     }     int ans = 0;     int ma = a.size();     if (sz % 2 == 0) {       ma--;     }     for (int i = 0; i < ma; i++) {       ans += min(a[i].first, a[i].second);     }     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     for (int i = 0; i < t; i++) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, t, last;   int f[6005][6005];   inline void dfs(int x, int y, int o) {     f[x][y] += o;     int O = f[x][y];     f[x][y] %= 4;     if (O < 4) return;     dfs(x, y + 1, O / 4);     dfs(x, y - 1, O / 4);     dfs(x + 1, y, O / 4);     dfs(x - 1, y, O / 4);   }   int main() {     scanf( %d%d , &n, &t);     dfs(2500, 2500, n);     for (int i = 1; i <= t; i++) {       int x, y;       scanf( %d%d , &x, &y);       x += 2500, y += 2500;       if (x > 5000 || y > 5000 || x < 0 || y < 0) {         printf( 0 n );         continue;       }       printf( %d n , f[x][y]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a, b, h[15], side = 0, dsh[15], cent = 52;   vector<int> sshot, cshot;   void recur(int now, int tot) {     if (now == n - 1) {       int ok = 1;       for (int i = 1; i < n - 1; i++)         if (dsh[i] * a + dsh[i - 1] * b + dsh[i + 1] * b <= h[i]) {           ok = 0;           break;         }       if (ok == 1 && tot < cent) {         cent = tot;         cshot.clear();         for (int i = 1; i < n - 1; i++)           for (int j = 0; j < dsh[i]; j++) cshot.push_back(i);       }       return;     }     if (tot >= cent) return;     for (int i = 0; i <= 8; i++) {       dsh[now] = i;       recur(now + 1, tot + i);     }   }   int main() {     scanf( %d%d%d , &n, &a, &b);     for (int i = 0; i < n; i++) {       scanf( %d , &h[i]);     }     while (h[0] >= 0) {       h[0] -= b;       h[2] -= b;       h[1] -= a;       side++;       sshot.push_back(1);     }     while (h[n - 1] >= 0) {       h[n - 1] -= b;       h[n - 3] -= b;       h[n - 2] -= a;       side++;       sshot.push_back(n - 2);     }     memset(dsh, 0, sizeof(dsh));     recur(1, 0);     printf( %d n , cent + side);     printf( %d , sshot[0] + 1);     for (int i = 1; i < sshot.size(); i++) printf(  %d , sshot[i] + 1);     for (int i = 0; i < cshot.size(); i++) printf(  %d , cshot[i] + 1);     printf(  n );   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n;     cin >> n;     vector<int> v(n);     for (long long i = 0; i < n; i++) cin >> v[i];     ;     vector<int> u(n);     for (long long i = 0; i < n; i++) cin >> u[i];     ;     sort((v).begin(), (v).end());     sort((u).begin(), (u).end());     bool turn = 0;     long long a = 0LL, b = 0LL;     while (!v.empty() || !u.empty()) {       if (!turn) {         if (v.back() >= u.back()) {           if (!v.empty()) {             a += v.back();             v.pop_back();           } else if (!u.empty()) {             u.pop_back();           } else {             break;           }         } else {           if (!u.empty()) {             u.pop_back();           } else if (!v.empty()) {             a += v.back();             v.pop_back();           } else {             break;           }         }         turn = 1;       } else {         if (u.back() >= v.back()) {           if (!u.empty()) {             b += u.back();             u.pop_back();           } else if (!v.empty()) {             v.pop_back();           } else {             break;           }         } else {           if (!v.empty()) {             v.pop_back();           } else if (!u.empty()) {             b += u.back();             u.pop_back();           } else {             break;           }         }         turn = 0;       }     }     cout << a - b << endl;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     scanf( %d%d , &n, &m);     int l = (n - 1) / m;     if ((n - 1) % m == 0)       printf( %d n , 2 * l);     else       printf( %d n , 2 * l + 1 + ((n - 1) % m >= 2));     int need = (n - 1) % m;     for (int i = 2; i <= n;) {       int maxn = min(n, i + l - 1 + (need-- > 0));       printf( %d %d n , 1, i);       int j = i;       while (j++ < maxn) printf( %d %d n , j - 1, j);       i = maxn + 1;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int t;   char a[9][9];   int dx[4] = {2, 2, -2, -2}, dy[4] = {2, -2, 2, -2};   int b[9][9];   int c[9][9];   int dem1, dem2;   int kt;   void ktrak1(int x, int y) {     int h, k;     dem1++;     for (int i = 0; i <= 3; i++) {       h = x + dx[i];       k = y + dy[i];       if (h >= 1 && h <= 8 && k >= 1 && k <= 8 && b[h][k] == 1) {         b[h][k] = 0;         ktrak1(h, k);       }     }   }   void ktrak2(int x, int y) {     int h, k;     dem2++;     for (int i = 0; i <= 3; i++) {       h = x + dx[i];       k = y + dy[i];       if (h >= 1 && h <= 8 && k >= 1 && k <= 8 && c[h][k] == 1) {         c[h][k] = 0;         ktrak2(h, k);       }     }   }   int main() {     cin >> t;     while (t) {       t--;       kt = 0;       dem1 = dem2 = 0;       int x1 = 0, y1 = 0;       int x2 = 0, y2 = 0;       for (int i = 1; i <= 8; i++) {         for (int j = 1; j <= 8; j++) {           cin >> a[i][j];           c[i][j] = b[i][j] = 1;           if (a[i][j] ==  K ) {             if (x1 == 0) {               x1 = i;               y1 = j;             } else {               x2 = i;               y2 = j;             }           }         }       }       b[x1][y1] = 0;       ktrak1(x1, y1);       c[x2][y2] = 0;       ktrak2(x2, y2);       for (int i = 1; i <= 8; i++) {         for (int j = 1; j <= 8; j++) {           if (b[i][j] == c[i][j] && c[i][j] == 0 && a[i][j] !=  # ) {             kt = 1;             break;           }         }         if (kt == 1) break;       }       if ((abs(x1 - x2) % 4) != 0 || (abs(y1 - y2) % 4) != 0) {         kt = 0;       }       if (kt == 0)         cout <<  NO n ;       else         cout <<  YES n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 50;   const int inf = 0x3f3f3f3f;   int A[maxn];   int n, k;   void solve(int l, int r) {     if (l + 1 == r || !k) return;     int mid = (l + r) >> 1;     k -= 2;     swap(A[mid], A[mid - 1]);     solve(l, mid);     solve(mid, r);   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) A[i] = i + 1;     k--;     if (k & 1) {       puts( -1 );       return 0;     }     solve(0, n);     if (k)       puts( -1 );     else {       for (int i = 0; i < n; i++) printf( %d  , A[i]);       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int Imx = 2147483647;   const long long Lbig = 2e18;   const int mod = 1e9 + 7;   struct fastio {     char s[100000];     int it, len;     fastio() { it = len = 0; }     inline char get() {       if (it < len) return s[it++];       it = 0;       len = fread(s, 1, 100000, stdin);       if (len == 0)         return EOF;       else         return s[it++];     }     bool notend() {       char c = get();       while (c ==     || c ==   n ) c = get();       if (it > 0) it--;       return c != EOF;     }   } _buff;   inline long long getnum() {     long long r = 0;     bool ng = 0;     char c;     c = _buff.get();     while (c !=  -  && (c <  0  || c >  9 )) c = _buff.get();     if (c ==  - ) ng = 1, c = _buff.get();     while (c >=  0  && c <=  9 ) r = r * 10 + c -  0 , c = _buff.get();     return ng ? -r : r;   }   template <class T>   inline void putnum(T x) {     if (x < 0) putchar( - ), x = -x;     register short a[20] = {}, sz = 0;     while (x) a[sz++] = x % 10, x /= 10;     if (sz == 0) putchar( 0 );     for (int i = sz - 1; i >= 0; i--) putchar( 0  + a[i]);   }   inline char getreal() {     char c = _buff.get();     while (c ==     || c ==   n ) c = _buff.get();     return c;   }   int n;   int ask(int x, int y) {     printf( ? %d %d n , x, y);     fflush(stdout);     int ans;     scanf( %d , &ans);     return ans;   }   int a[5011], b[5011];   int A[5011], B[5011];   bool hs[5011];   int tans[5011];   bool check() {     memset(hs, 0, sizeof(hs));     for (int i = 0; i < n; i++) {       if (A[i] < 0 || A[i] >= n || hs[A[i]]) return false;       hs[A[i]] = 1;     }     memset(hs, 0, sizeof(hs));     for (int i = 0; i < n; i++) {       if (B[i] < 0 || B[i] >= n || hs[B[i]]) return false;       hs[B[i]] = 1;     }     for (int i = 0; i < n; i++) {       if (A[B[i]] != i) return false;     }     return true;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       a[i] = ask(i, 0);     }     for (int i = 0; i < n; i++) {       b[i] = ask(0, i);     }     int cnt = 0;     for (int i = 0; i < n; i++) {       A[0] = i;       B[0] = A[0] ^ a[0];       for (int j = 1; j < n; j++) {         A[j] = A[0] ^ a[0] ^ a[j];         B[j] = B[0] ^ b[0] ^ b[j];       }       if (check()) {         cnt++;         for (int j = 0; j < n; j++) tans[j] = A[j];       }     }     printf( ! n );     printf( %d n , cnt);     for (int i = 0; i < n; i++) printf( %d%c , tans[i], i + 1 < n ?     :   n );     fflush(stdout);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e6 + 5;   const long long M = 1e9 + 7;   const long long inf = 1e18 + 5;   vector<int> prefix_function(string &s) {     int n = s.size();     vector<int> pre(n);     int pos = 0;     pre[0] = 0;     for (int i = 1; i < n; i++) {       while (pos and s[pos] != s[i]) pos = pre[pos - 1];       if (s[pos] == s[i]) pos++;       pre[i] = pos;     }     return pre;   }   int main() {     string s, t;     cin >> t >> s;     int n = s.size();     vector<int> pre = prefix_function(s);     int a[n][26];     for (int i = 0; i < n; i++) {       for (int c = 0; c < 26; c++) {         if (i and s[i] !=  a  + c)           a[i][c] = a[pre[i - 1]][c];         else           a[i][c] = i + (s[i] ==  a  + c);       }     }     int tz = t.size();     int dp[tz + 1][n];     memset(dp, -1, sizeof(dp));     dp[0][0] = 0;     for (int i = 0; i < tz; i++) {       for (int j = 0; j < n; j++) {         if (dp[i][j] != -1) {           if (t[i] ==  ? ) {             for (int ch = 0; ch < 26; ch++) {               int val = a[j][ch];               if (val == n)                 dp[i + 1][pre[n - 1]] = max(dp[i + 1][n - 1], dp[i][j] + 1);               else                 dp[i + 1][val] = max(dp[i + 1][val], dp[i][j]);             }           } else {             int val = a[j][t[i] -  a ];             if (val == n)               dp[i + 1][pre[n - 1]] = max(dp[i + 1][n - 1], dp[i][j] + 1);             else               dp[i + 1][val] = max(dp[i + 1][val], dp[i][j]);           }         }       }     }     int ans = 0;     for (int i = 0; i < n; i++) ans = max(ans, dp[tz][i]);     printf( %d  , ans);   }
#include <bits/stdc++.h>   using namespace std;   int test, n;   long long f[2000001][2];   const int P = 1000000007;   int main() {     memset(f, 0, sizeof(f));     f[3][0] = 0;     f[3][1] = 4;     for (int i = 4; i <= 2000000; i++)       f[i][0] = max(f[i - 1][0], f[i - 1][1]) + max(f[i - 2][0], f[i - 2][1]) * 2,       f[i][0] %= P, f[i][1] = 4 + f[i - 1][0] + 2 * f[i - 2][0], f[i][1] %= P;     scanf( %d , &test);     for (; test--;) {       scanf( %d , &n);       printf( %lld n , max(f[n][0], f[n][1]));     }   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const int N = 1e5 + 10;   string a;   int main() {     int n;     while (~scanf( %d , &n)) {       map<string, int> q;       q.clear();       int num = 0;       for (int i = 0; i < n; i++) {         cin >> a;         q[a]++;         num = max(num, q[a]);       }       printf( %d n , num);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, i;     cin >> n;     if (n == 1 || n == 2) {       cout <<  No ;     } else {       cout <<  Yes             <<   n ;       cout << 1 <<    ;       cout << n <<   n ;       cout << n - 1 <<    ;       for (i = 0; i < n - 1; i++) {         cout << i + 1 <<    ;       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int m, n;   string bs[50];   long long component[1000];   long long combo[1001][1001];   long long pseudopart[1001];   bool visited[1000];   int main() {     cin >> m >> n;     for (int i = 0; i < n; i++) cin >> bs[i];     for (int i = 0; i < m; i++)       for (int j = 0; j < n; j++) {         component[i] *= 2LL;         component[i] += (long long)((int)bs[j][i] - (int) 0 );       }     combo[0][0] = 1;     for (int i = 1; i < 1001; i++)       for (int j = 0; j <= i; j++) {         if (j == 0 || j == i)           combo[i][j] = 1;         else           combo[i][j] = combo[i - 1][j] + combo[i - 1][j - 1];         combo[i][j] %= 1000000007;       }     pseudopart[0] = 1;     pseudopart[1] = 1;     for (int i = 2; i <= m; i++) {       for (int j = 0; j < i; j++) {         pseudopart[i] += combo[i - 1][j] * pseudopart[i - 1 - j];         pseudopart[i] %= 1000000007;       }     }     long long ans = 1;     for (int i = 0; i < m; i++) {       if (visited[i]) continue;       long long temp = 0;       for (int j = 0; j < m; j++)         if (component[j] == component[i]) {           visited[j] = true;           temp++;         }       ans *= pseudopart[temp];       ans %= 1000000007;     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   const int maxn = 1e5 + 5;   const int MAX = 314000000;   int n, m;   int dis[maxn], tot[maxn], sum[maxn], dp[maxn];   vector<int> have[maxn];   queue<int> q;   priority_queue<pair<int, int>, vector<pair<int, int> >,                  greater<pair<int, int> > >       pq;   class Edge {    public:     int from, l, dia, cur;     vector<int> mons;   } ed[maxn];   int main() {     scanf( %d%d , &m, &n);     for (int i = (1); i <= (n); ++i) dis[i] = INF;     for (int i = (0); i < (m); ++i) {       scanf( %d%d , &ed[i].from, &ed[i].l);       for (int j = (0); j < (ed[i].l); ++j) {         int v;         scanf( %d , &v);         if (!~v) {           ++ed[i].dia;           continue;         }         ed[i].mons.push_back(v);         have[v].push_back(i);       }       if (!ed[i].mons.size()) {         dis[ed[i].from] = min(dis[ed[i].from], ed[i].dia);       }     }     for (int i = (1); i <= (n); ++i)       if (dis[i] != INF) pq.push(make_pair(dis[i], i));     for (; !pq.empty(); pq.pop()) {       int u = pq.top().second, d = pq.top().first;       if (d != dis[u]) continue;       for (int i = (0); i < (have[u].size()); ++i) {         int id = have[u][i];         ++ed[id].cur;         if (ed[id].cur == ed[id].mons.size()) {           int v = ed[id].from;           int w = ed[id].dia;           for (int j = (0); j < (ed[id].mons.size()); ++j) {             w += dis[ed[id].mons[j]];             w = min(w, MAX);           }           if (w < dis[v]) {             dis[v] = w;             pq.push(make_pair(dis[v], v));           }         }       }     }     for (int i = (0); i < (m); ++i) {       bool ok = true;       ed[i].cur = 0;       for (int j = (0); j < (ed[i].mons.size()); ++j)         ok &= (dis[ed[i].mons[j]] != INF);       if (ok) ++tot[ed[i].from];     }     for (int i = (1); i <= (n); ++i)       if (dis[i] != INF && !tot[i]) q.push(i);     for (int i = (0); i < (m); ++i) {       if (!ed[i].mons.size() && dis[ed[i].from] != INF) {         int w = ed[i].dia;         for (int j = (0); j < (ed[i].mons.size()); ++j) {           w += dis[ed[i].mons[j]];           w = min(w, MAX);         }         dp[ed[i].from] = max(dp[ed[i].from], w);         --tot[ed[i].from];         if (!tot[ed[i].from]) {           q.push(ed[i].from);         }       }     }     for (; !q.empty(); q.pop()) {       int u = q.front();       for (int i = (0); i < (have[u].size()); ++i) {         int id = have[u][i];         int v = ed[id].from;         if (dis[v] != INF) {           sum[id] += dp[u];           sum[id] = min(sum[id], MAX);           ++ed[id].cur;           if (ed[id].cur == ed[id].mons.size()) {             dp[v] = max(dp[v], min(MAX, sum[id] + ed[id].dia));             --tot[v];             if (!tot[v]) q.push(v);           }         }       }     }     for (int i = (1); i <= (n); ++i) {       if (dis[i] == INF) {         puts( -1 -1 );       } else {         printf( %d  , dis[i]);         if (!tot[i])           printf( %d , dp[i]);         else           printf( -2 );         puts(  );       }     }     return 0;   }