malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, max = 0, count = 0;     cin >> n >> k;     vector<int> v(n);     for (int i = 0; i < n; i++) {       cin >> v[i];       if (v[i - 1] != v[i])         count++;       else if (count > max) {         max = count;         count = 1;       } else         count = 1;     }     if (max < count) max = count;     cout << max;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mx = 1e5 + 1;   bool vis[mx];   vector<int> adj[mx];   int n;   bool star() {     bool ans;     int count1 = 0;     int other = 0;     for (int i = 1; i <= n; i++) {       if (adj[i].size() == 1) {         count1++;       } else if (adj[i].size() == n - 1) {         other++;       }     }     if (other == 1 && count1 == n - 1) {       ans = true;     } else {       ans = false;     }     return ans;   }   bool ring() {     bool ans = true;     for (int i = 1; i <= n; i++) {       if (adj[i].size() != 2) {         ans = false;         break;       }     }     return ans;   }   bool bus() {     bool ans;     int one = 0;     int two = 0;     for (int i = 1; i <= n; i++) {       if (adj[i].size() == 1) {         one++;       } else if (adj[i].size() == 2) {         two++;       }     }     if (one == 2 && two == n - 2) {       ans = true;     } else {       ans = false;     }     return ans;   }   int main() {     int m, k, u, v;     cin >> n >> m;     for (int i = 0; i < m; i++) {       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     if (star()) {       cout <<  star topology  << endl;     } else if (bus()) {       cout <<  bus topology  << endl;     } else if (ring()) {       cout <<  ring topology  << endl;     } else {       cout <<  unknown topology  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   long long a[2000005];   long long sum[2000005];   int main() {     long long m, n, i, j, k, ans = 0;     cin >> m;     for (i = 1; i <= m; i++) scanf( %lld , &a[i]);     sort(a + 1, a + 1 + m);     for (i = m; i >= 1; i--) sum[i] = sum[i + 1] + a[i];     for (i = 1; i <= m; i *= 4) {       ans += sum[m - i + 1];     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 1;   const int N = 5003;   vector<int> adj[N];   vector<pair<int, int> > stores[N];   bool done[N];   int dist[N];   void clear(int n) {     for (int i = 1; i <= n; ++i) done[i] = 0;   }   void modify(multiset<pair<int, int> >& tiendas, const int& to,               long long& rpta) {     for (pair<int, int> p : stores[to]) {       auto it = tiendas.rbegin();       pair<int, int> q = *it;       int left = p.second;       while (!tiendas.empty() && q.first > p.first) {         if (q.second >= left) {           tiendas.erase(tiendas.find(q));           rpta -= ((long long)q.first * q.second);           if (q.second != left) {             tiendas.insert({q.first, q.second - left});             rpta += ((long long)q.first * (q.second - left));           }           left = 0;           break;         } else {           tiendas.erase(tiendas.find(q));           left -= q.second;           rpta -= ((long long)q.first * q.second);         }         if (!tiendas.empty()) {           it = tiendas.rbegin();           q = *it;         }       }       if (p.second != left) {         tiendas.insert({p.first, p.second - left});         rpta += ((long long)p.first * (p.second - left));       }     }     return;   }   int bfs(int source, int prod, long long price) {     multiset<pair<int, int> > tiendas;     tiendas.insert({INF, prod});     long long rpta = (long long)INF * prod;     queue<int> Q;     Q.push(source);     dist[source] = 0;     done[source] = 1;     modify(tiendas, source, rpta);     if (rpta <= price) return dist[source];     while (!Q.empty()) {       int from = Q.front();       Q.pop();       for (int to : adj[from]) {         if (done[to]) continue;         dist[to] = dist[from] + 1;         done[to] = 1;         Q.push(to);         modify(tiendas, to, rpta);         if (rpta <= price) return dist[to];       }     }     return -1;   }   void solve() {     int n, m;     cin >> n >> m;     for (int i = 0; i < m; ++i) {       int u, v;       cin >> u >> v;       adj[u].push_back(v);       adj[v].push_back(u);     }     int w;     cin >> w;     for (int i = 0; i < w; ++i) {       int c, k, p;       cin >> c >> k >> p;       stores[c].push_back({p, k});     }     int q;     cin >> q;     for (int i = 0; i < q; ++i) {       clear(n);       int g, r, a;       cin >> g >> r >> a;       cout << bfs(g, r, a) <<   n ;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(nullptr);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 220;   bool vis[maxn];   vector<int> G[maxn];   int ans;   int dfs(int v, int x) {     int sum = 0;     int max1 = 0, max2 = 0;     for (int i = 0; i < G[v].size(); i++) {       if (G[v][i] != x) {         sum = max(sum, dfs(G[v][i], v));         if (ans > max1) {           max2 = max1;           max1 = ans;         } else           max2 = ans > max2 ? ans : max2;       }     }     sum = max(sum, max1 + max2);     ans = max1 + 1;     return sum;   }   int main() {     int n, a, b;     while (scanf( %d , &n) != EOF) {       for (int i = 0; i <= n; i++) G[i].clear();       for (int i = 0; i < n - 1; i++) {         scanf( %d%d , &a, &b);         G[a].push_back(b);         G[b].push_back(a);       }       int last_ans = 0;       for (int i = 0; i < n; i++) {         int num = G[i].size(), temp;         for (int j = 0; j < num; j++) {           int ans = 0;           temp = dfs(i, G[i][j]) * dfs(G[i][j], i);           last_ans = max(last_ans, temp);         }       }       printf( %d n , last_ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int d1, d2, d3, i;     cin >> d1 >> d2 >> d3;     int k[4] = {(d1 + d2 + d3), (2 * d1 + 2 * d2), (2 * d2 + 2 * d3),                 (2 * d1 + 2 * d3)};     int min = 1e9;     for (i = 0; i < 4; i++) {       if (k[i] < min) min = k[i];     }     cout << min;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double eps = 1e-9;   const double pi = acos(-1.0);   const int inf = 0x3f3f3f3f;   const int N = 100010;   int n, q;   int a[N], b[N], c[N];   struct node {     int low, high;     vector<pair<int, int> > v;   } tree[N << 2];   void build(int p, int low, int high, int c[]) {     tree[p].low = low;     tree[p].high = high;     tree[p].v.clear();     for (int k = low; k <= high; ++k) {       int i = c[k];       vector<pair<int, int> >& v = tree[p].v;       while (!v.empty()) {         int j = v.back().second;         if (b[j] >= b[i]) break;         int t = (a[j] - a[i]) / (b[i] - b[j]) + 1;         if (v.back().first >= t)           v.pop_back();         else {           v.push_back(make_pair(t, i));           break;         }       }       if (v.empty()) v.push_back(make_pair(0, i));     }     if (low == high) return;     int mid = (low + high) >> 1;     stable_partition(c + low, c + high + 1, bind2nd(less<int>(), mid + 1));     build(p * 2 + 1, low, mid, c);     build(p * 2 + 2, mid + 1, high, c);   }   pair<long long, int> query(int p, int low, int high, int t) {     node& cur = tree[p];     if (low <= cur.low && cur.high <= high) {       int id = lower_bound(cur.v.begin(), cur.v.end(), make_pair(t, inf)) -                cur.v.begin();       id = cur.v[id - 1].second;       return make_pair(a[id] + (long long)t * b[id], id);     }     pair<long long, int> ans = make_pair(0, 0);     int mid = (cur.low + cur.high) >> 1;     if (low <= mid) ans = max(ans, query(p * 2 + 1, low, high, t));     if (mid < high) ans = max(ans, query(p * 2 + 2, low, high, t));     return ans;   }   bool cmp(int i, int j) {     if (a[i] != a[j]) return a[i] > a[j];     return b[i] < b[j];   }   void solve() {     for (int i = 0; i < (int)(n); ++i) scanf( %d%d , a + i, b + i);     for (int i = 0; i < (int)(n); ++i) c[i] = i;     sort(c, c + n, cmp);     build(0, 0, n - 1, c);     int l, r, t;     for (int i = 0; i < (int)(q); ++i) {       scanf( %d%d%d , &l, &r, &t);       printf( %d n , query(0, l - 1, r - 1, t).second + 1);     }   }   int main() {     while (scanf( %d%d , &n, &q) != EOF) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a, b;   map<long long, bool> mp;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     for (long long i = 1; i <= 1000; i++) mp[i * i] = 1;     cin >> a >> b;     for (long long i = -1; i > -a; i--) {       if (mp[a * a - i * i] == 0) continue;       long long j = sqrt(a * a - i * i);       for (long long x = 1; x < b; x++) {         if (mp[b * b - x * x] == 0) continue;         long long y = sqrt(b * b - x * x);         if (-i * x == j * y && j != y) {           cout <<  YES n ;           cout << 0 <<     << 0 << endl;           cout << i <<     << j << endl;           cout << x <<     << y;           return 0;         }       }     }     cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int Nmax = 1e6 + 7;   int n;   string s;   int dp[Nmax];   vector<int> v;   int ans, ans1, ans2;   inline int get(string s) {     int cnt = 0;     int mx = 4 * n;     ans = 0;     for (int i = 0; i < n; i++) {       if (s[i] ==  ) )         cnt--;       else         cnt++;       if (cnt < mx) {         mx = cnt;         ans = i;       }     }     int pos = ans;     if (cnt != 0) return 0;     s = s + s;     dp[0] = 0;     ans = 0;     ans1 = 1, ans2 = 1;     for (int i = pos + 1, j = 0; j < n; j++, i++) {       if (s[i] ==  ( )         dp[j + 1] = dp[j] + 1;       else         dp[j + 1] = dp[j] - 1;     }     for (int i = 0; i <= n; i++) {       if (!dp[i]) {         v.push_back(i);         ans++;       }     }     ans--;     int zero = ans;     int sum = v.size();     for (int i = 0; i < sum - 1; i++) {       int j = i + 1;       int cnt = 0;       for (int k = v[i]; k <= v[j]; k++) {         if (dp[k] == 1) cnt++;       }       if (cnt > ans) {         ans = cnt;         ans1 = v[i] + 1, ans2 = v[j];       }     }     v.clear();     for (int i = 1; i <= n; i++)       if (dp[i] == 1) v.push_back(i);     sum = v.size();     for (int i = 0; i < sum - 1; i++) {       int j = i + 1;       int cnt = 0;       for (int k = v[i]; k <= v[j]; k++) {         if (dp[k] == 2) cnt++;         if (dp[k] < 1) {           cnt = 0;           break;         }       }       if (cnt + zero > ans) {         ans = cnt + zero;         ans1 = v[i] + 1, ans2 = v[j];       }     }     pos++;     ans1 += pos, ans2 += pos;     if (ans1 > n) ans1 %= n;     if (ans2 > n) ans2 %= n;     cout << ans << endl;     cout << ans1 <<     << ans2 << endl;     return ans;   }   int main() {     cin >> n;     cin >> s;     int ans = 0, ans1 = 1, ans2 = 1;     if (get(s) == 0) {       cout << ans << endl;       cout << ans1 <<     << ans2 << endl;       return 0;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, k, a, b;   vector<long long> v;   long long rec(long long l, long long r) {     long long i = lower_bound(v.begin(), v.end(), l) - v.begin();     long long j = upper_bound(v.begin(), v.end(), r) - v.begin();     j--;     long long na = j - i + 1;     long long val1 = b * (r - l + 1) * na;     if (na == 0) return a;     if (l == r) {       if (na == 0)         return a;       else         return val1;     }     long long mid = (r + l) / 2;     long long val2 = rec(l, mid) + rec(mid + 1, r);     long long ans = min(val1, val2);     return ans;   }   int main() {     cin >> n >> k >> a >> b;     long long x;     for (long long i = 0; i < k; i++) {       cin >> x;       v.push_back(x);     }     sort(v.begin(), v.end());     long long l = 1, r = pow(2, n);     long long ans = rec(l, r);     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int N;     cin >> N;     int* results = new int[N];     for (int i = 0; i < N; i++) {       int num, pos;       cin >> num >> pos;       results[i] = pos * 2;     }     for (int i = 0; i < N; i++) cout << results[i] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> v[100000 + 1];   vector<int> sol[100000 + 1];   int is[100000 + 1];   int pos[100000 + 1];   int main() {     int i, j, first, N, res = 0;     scanf( %d , &N);     for (i = 1; i <= N; ++i) {       scanf( %d , &first);       v[i] = {first, i};     }     sort(v + 1, v + N + 1);     for (i = 1; i <= N; ++i)       if (!is[i]) {         ++res;         is[i] = 1;         for (j = v[i].second; j != i; j = v[j].second)           is[j] = 1, sol[res].push_back(j);         sol[res].push_back(i);       }     printf( %d n , res);     for (i = 1; i <= res; ++i) {       printf( %d  , sol[i].size());       for (j = 0; j < sol[i].size(); ++j) printf( %d  , sol[i][j]);       printf(  n );     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   using ll = long long;   using ull = unsigned long long;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ll n;     int k;     cin >> n >> k;     ll s = n * (n - 1) / 2;     s %= n;     ll t = 0;     ll cnt = 0;     for (int i = 0; i < k; i++) {       ll a, b;       cin >> a >> b;       (t += a * b) %= n;       cnt += b;     }     if (cnt < n) {       cout <<  1 n ;     } else if (cnt > n) {       cout <<  -1 n ;     } else {       if (s == t) {         cout <<  1 n ;       } else {         cout <<  -1 n ;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, q;     cin >> n >> m >> q;     int a[n][m];     struct a {       int o;       int p;     } arr[q];     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> a[i][j];       }     }     int fo[n];     for (int i = 0; i < n; i++) {       int c = 0;       int max = 0;       for (int j = 0; j < m; j++) {         if (a[i][j] == 1) c++;         if (c > max) max = c;         if (a[i][j] == 0) c = 0;       }       fo[i] = max;     }     for (int i = 0; i < q; i++) {       cin >> arr[i].o;       cin >> arr[i].p;       arr[i].o--;       arr[i].p--;     }     for (int i = 0; i < q; i++) {       a[arr[i].o][arr[i].p] = a[arr[i].o][arr[i].p] ^ 1;       int max = 0;       int c = 0;       for (int j = 0; j < m; j++) {         if (a[arr[i].o][j] == 1) c++;         if (c > max) max = c;         if (a[arr[i].o][j] == 0) c = 0;       }       fo[arr[i].o] = max;       int maxi = 0;       for (int j = 0; j < n; j++) {         if (fo[j] > maxi) maxi = fo[j];       }       cout << maxi << endl;     }     return 0;   }
#include <bits/stdc++.h>   const long long int maxn = 2e7 + 5;   const long long mod = 1e9 + 7;   using namespace std;   long long int power(long long int a, long long int b) {     long long int res = 1;     while (b > 0) {       if (b % 2 == 1) res = res * a;       a = a * a;       b = b / 2;     }     return res;   }   long long po(long long x, long long y) {     long long res = 1;     x = x % mod;     while (y > 0) {       if (y & 1) res = ((res % mod) * (x % mod)) % mod;       y = y >> 1;       x = ((x % mod) * (x % mod)) % mod;     }     return res;   }   int dp[3001][3001];   int n;   vector<pair<long long int, long long int> > p(3001);   int fun(int i, int pp) {     if (i >= n) return 0;     if (dp[i][pp] != -1) return dp[i][pp];     return dp[i][pp] = min(p[i].second + fun(i + 1, i),                            abs(p[i].first - p[pp].first) + fun(i + 1, pp));   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int t = 1;     while (t--) {       memset(dp, -1, sizeof(dp));       cin >> n;       for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second;       p.resize(n);       sort((p).begin(), (p).end());       cout << p[0].second + fun(1, 0) <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, c = 0, i, aa = 0, bb = 0;     cin >> n;     int a[n], b[n];     for (i = 0; i < n; i++) {       cin >> a[i] >> b[i];       aa = aa + a[i];       bb = bb + b[i];     }     if (aa % 2 == 1 && bb % 2 == 1) {       for (i = 0; i < n; i++) {         if ((a[i] + b[i]) % 2 == 1) {           c++;           break;         }       }     }     if (aa % 2 == 0 && bb % 2 == 0)       cout <<  0 ;     else if (c == 1)       cout <<  1 ;     else       cout <<  -1 ;   }
#include <bits/stdc++.h>   using namespace std;   inline bool SR(int &x) { return scanf( %d , &x) == 1; }   inline bool SR(long long &x) { return scanf( %lld , &x) == 1; }   inline bool SR(double &x) { return scanf( %lf , &x) == 1; }   inline bool SR(char *s) { return scanf( %s , s) == 1; }   inline bool RI() { return true; }   template <typename I, typename... T>   inline bool RI(I &x, T &...tail) {     return SR(x) && RI(tail...);   }   inline void SP(const int x) { printf( %d , x); }   inline void SP(const long long x) { printf( %lld , x); }   inline void SP(const double x) { printf( %.16lf , x); }   inline void SP(const char *s) { printf( %s , s); }   inline void PL() { puts(  ); }   template <typename I, typename... T>   inline void PL(const I x, const T... tail) {     SP(x);     if (sizeof...(tail)) putchar(   );     PL(tail...);   }   template <typename I>   void _DOING(const char *s, I &&x) {     cerr << s <<   =   << x << endl;   }   template <typename I, typename... T>   void _DOING(const char *s, I &&x, T &&...tail) {     int c = 0;     while (*s !=  ,  || c != 0) {       if (*s ==  (  || *s ==  [  || *s ==  { ) c++;       if (*s ==  )  || *s ==  ]  || *s ==  } ) c--;       cerr << *s++;     }     cerr <<   =   << x <<   ,  ;     _DOING(s + 1, tail...);   }   const int INF = 0x3f3f3f3f;   const long long INF64 = 0x3f3f3f3f3f3f3f3fll;   inline int RAND() {     static int x = 880301;     return (x = x * 0xdefaced + 1) % 0x7fffffff;   }   const int MAX_N = 100000 + 7;   int main() {     int n;     RI(n);     vector<int> v;     for (int i = 0; i < int(2 * n); i++) {       int x;       RI(x);       v.push_back(x);     }     sort((v).begin(), (v).end());     long long ans = 1ll * (v[n - 1] - v[0]) * (v[2 * n - 1] - v[n]);     for (int i = 1; i + n - 1 < 2 * n; i++) {       long long t = 1ll * (v.back() - v[0]) * (v[i + n - 1] - v[i]);       ans = min(ans, t);     }     PL(ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1000100;   const int MOD = 1000000007;   vector<long long> al[N], sum[N];   vector<pair<int, int> > adj[N];   pair<int, int> p[N];   int cnt(int u, long long ls) {     return upper_bound(al[u].begin(), al[u].end(), ls) - al[u].begin();   }   long long hp(int u, long long h, long long ls) {     long long c1 = cnt(u, ls);     if (c1 == 0) return 0;     return h * c1 - sum[u][c1 - 1];   }   int n, k, l[N], q, a, h;   void dfs(int u, long long cost) {     al[u].push_back(0);     for (int i = 0; i < adj[u].size(); ++i) {       int v = adj[u][i].first;       dfs(v, cost + adj[u][i].second);       for (int k = 0; k < al[v].size(); ++k) {         al[u].push_back(al[v][k] + adj[u][i].second);       }     }     sort(al[u].begin(), al[u].end());     for (int i = 0; i < al[u].size(); ++i) {       sum[u].push_back((i ? sum[u].back() : 0) + (al[u][i]));     }   }   int main() {     memset(p, -1, sizeof p);     scanf( %d%d , &n, &q);     for (int i = 1; i <= n - 1; ++i) {       scanf( %d , &l[i]);       int u = (i + 1) / 2;       int v = (i + 1);       p[v] = make_pair(u, l[i]);       adj[u].push_back(make_pair(v, l[i]));     }     dfs(1, 0);     while (q--) {       scanf( %d%d , &a, &h);       long long cur = a, prev = -1, rem = h;       long long add = 0;       while (1) {         add += hp(cur, rem, rem);         if (prev != -1) {           long long kk = rem - p[prev].second;           add -= hp(prev, kk, kk);         }         prev = cur;         rem = rem - p[cur].second;         cur = p[cur].first;         if (cur == -1 || rem < 0) break;       }       printf( %lld n , add);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 300100, maxv = 100, mod = 1e9 + 7, maxa = 1005,                   maxs = 820, maxb = 20, base = 737, base2 = 3079,                   mod3 = 998244353, delt = 10513;   const long long inf = 2e16;   const long long infint = 1e9 + 11;   long long max(long long x, long long y) { return (x > y ? x : y); }   long long min(long long x, long long y) { return (x < y ? x : y); }   vector<long long> v[maxn];   long long dp[maxn][2];   bool visited[maxn];   long long ans = 0;   void dfs(long long x) {     visited[x] = 1;     long long kid = v[x].size();     vector<long long> tmp;     if (x != 0) {       kid--;     }     for (auto u : v[x]) {       if (!visited[u]) {         dfs(u);         ans = max(ans, dp[u][1] + 1);         dp[x][0] = max(dp[x][0], dp[u][0] + kid);         tmp.push_back(dp[u][0]);         sort(tmp.begin(), tmp.end());         reverse(tmp.begin(), tmp.end());         if (tmp.size() > 2) {           tmp.pop_back();         }       }     }     dp[x][1] = dp[x][0];     if (tmp.size() == 2) {       dp[x][1] = max(dp[x][1], tmp[0] + tmp[1] + kid - 1);     }     ans = max(ans, dp[x][1]);   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long q;     cin >> q;     while (q--) {       ans = 1;       long long n;       cin >> n;       for (long long i = 0; i < n; i++) {         visited[i] = 0;         v[i].clear();         dp[i][0] = 1;         dp[i][1] = 1;       }       for (long long i = 0; i < n - 1; i++) {         long long a, b;         cin >> a >> b;         a--;         b--;         v[a].push_back(b);         v[b].push_back(a);       }       dfs(0);       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const int N = 2e5 + 123;   int a[N];   vector<int> g[N];   int n, node, stdist = 0;   void dfs(int k, int par, int dist) {     if (dist > stdist) {       node = k;       stdist = dist;     }     for (auto it : g[k]) {       if (it == par) continue;       dfs(it, k, dist + (a[k] ^ a[it]));     }   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n;     for (int i = 1; i <= n; i++) cin >> a[i];     for (int i = 1; i <= n - 1; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     node = 1;     dfs(node, node, 0);     dfs(node, node, 0);     cout << (stdist + 1) / 2;     return false;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 510;   const long long INF = 1e18;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   int n, m, now;   bitset<N> f[2][70][N], g, tmp;   long long ans, fac[N];   int main() {     n = read();     m = read();     for (int i = 1, u, v, w; i <= m; i++) {       u = read();       v = read();       w = read();       f[w][0][u][v] = 1;     }     for (int i = 1; i <= 60; i++)       for (int u = 1; u <= n; u++)         for (int v = 1; v <= n; v++) {           if (f[0][i - 1][u][v] != 0) f[0][i][u] |= f[1][i - 1][v];           if (f[1][i - 1][u][v] != 0) f[1][i][u] |= f[0][i - 1][v];         }     if (f[0][60][1].count()) {       puts( -1 );       return 0;     }     fac[0] = 1;     for (int i = 1; i <= 60; i++) fac[i] = fac[i - 1] * 2;     now = 0;     tmp[1] = 1;     for (int i = 60; i >= 0; i--) {       g.reset();       for (int j = 1; j <= n; j++)         if (tmp[j]) g |= f[now][i][j];       if (g.count() != 0) {         now ^= 1;         tmp = g;         ans += fac[i];       }     }     if (ans > INF)       puts( -1 );     else       printf( %lld n , ans);     return 0;   }