malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long sum = 0, ff = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) ff = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) sum = sum * 10 + ch -  0 , ch = getchar();     return sum * ff;   }   const int N = 100007;   const int block = 500;   int n, m, a_st[N], a[N], x, y, num, belong[N], st[N / block + 7],       ed[N / block + 7], col[N], sum[N / block + 7][N * 2], tag[N / block + 7],       ans;   struct edge {     int to, nxt;   } e[N];   int cnt, he[N];   void add(int u, int v) {     e[++cnt].to = v;     e[cnt].nxt = he[u];     he[u] = cnt;   }   int deep[N], siz[N], fa[N], son[N], dfs_cnt, dfn[N], top[N];   void dfs1(int u, int fat) {     deep[u] = deep[fat] + 1;     siz[u] = 1;     fa[u] = fat;     for (int i = he[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (v == fat) continue;       dfs1(v, u);       siz[u] += siz[v];       if (siz[v] > siz[son[u]]) son[u] = v;     }   }   void dfs2(int u, int tp) {     top[u] = tp;     dfn[u] = ++dfs_cnt;     if (!son[u]) return;     dfs2(son[u], tp);     for (int i = he[u]; i; i = e[i].nxt) {       int v = e[i].to;       if (top[v]) continue;       dfs2(v, v);     }   }   void build() {     for (int i = 1; i <= n; i++) belong[i] = (i - 1) / block + 1;     num = belong[n];     for (int i = 1; i <= num; i++) st[i] = (i - 1) * block + 1, ed[i] = i * block;     ed[num] = n;   }   void change(int x, int y) {     col[x] ^= 1;     if (a[x] >= n + 1 - tag[belong[x]]) ans -= y;     sum[belong[x]][a[x]] -= y;   }   void upd(int l, int r, int val) {     for (int i = l; i <= r; i++) {       if (!col[i] && a[i] >= n + 1 - tag[belong[i]]) ans--;       if (!col[i]) sum[belong[i]][a[i]]--;       a[i] += val;       if (!col[i]) sum[belong[i]][a[i]]++;       if (!col[i] && a[i] >= n + 1 - tag[belong[i]]) ans++;     }   }   void modify(int l, int r, int val) {     if (belong[l] == belong[r]) {       upd(l, r, val);       return;     }     upd(l, ed[belong[l]], val), upd(st[belong[r]], r, val);     for (int i = belong[l] + 1; i < belong[r]; i++) {       if (val == 1)         tag[i]++, ans += sum[i][n + 1 - tag[i]];       else         ans -= sum[i][n + 1 - tag[i]], tag[i]--;     }   }   int main() {     n = read(), m = read();     for (int i = 2; i <= n; i++) fa[i] = read(), add(fa[i], i);     for (int i = 1; i <= n; i++) a_st[i] = read();     dfs1(1, 0);     dfs2(1, 1);     for (int i = 1; i <= n; i++) a[dfn[i]] = n - a_st[i];     build();     for (int i = 1; i <= n; i++) sum[belong[i]][a[i]]++;     for (int i = 1; i <= m; i++) {       x = read();       y = (x > 0) ? 1 : -1;       x = abs(x);       change(dfn[x], y);       while (x) {         modify(dfn[top[x]], dfn[x], y);         x = fa[top[x]];       }       cout << ans <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   pair<int, int> arr[3001];   const long long int INF = 1000000000000000;   int n;   long long int dp[3001][3001];   long long int solve(int pos, int prev) {     if (pos > n) return 0;     if (dp[pos][prev] != -INF) return dp[pos][prev];     long long int pin = arr[pos].second + solve(pos + 1, pos);     long long int unpin =         abs(arr[prev].first - arr[pos].first) + solve(pos + 1, prev);     return dp[pos][prev] = min(pin, unpin);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> arr[i].first >> arr[i].second;     }     sort(arr + 1, arr + n + 1);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) dp[i][j] = -INF;     }     long long int ans = arr[1].second + solve(2, 1);     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   char ch[100];   int main() {     scanf( %d , &n);     int l = 0, r = 1e9;     for (int i = 1; i <= min(29, n); i++) {       int mid = (l + r) >> 1;       printf( %d %d n , mid, 1);       fflush(stdout);       scanf( %s , ch);       if (ch[0] ==  b )         l = mid;       else         r = mid;     }     if (n <= 29) {       printf( %d %d %d %d n , l, 2, r, 0);     } else {       printf( %d %d n , l, 10);       fflush(stdout);       scanf( %s , ch);       if (ch[0] ==  b )         printf( %d %d %d %d n , l, 0, r, 2);       else         printf( %d %d %d %d n , l, 2, r, 0);     }     fflush(stdout);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1005;   double dis(long u, long v, long p, long q) {     return sqrt(((double)u - p) * (u - p) + ((double)q - v) * (q - v));   }   struct point {     int x;     int y;   };   int main() {     int n, m;     cin >> n >> m;     if (n == 0) {       cout << 0 <<     << 1 << endl;       cout << 0 <<     << m << endl;       cout << 0 <<     << 0 << endl;       cout << 0 <<     << m - 1 << endl;       return 0;     }     if (m == 0) {       cout << 1 <<     << 0 << endl;       cout << n <<     << 0 << endl;       cout << 0 <<     << 0 << endl;       cout << n - 1 <<     << 0 << endl;       return 0;     }     double tam1 = 2 * dis(0, 0, n - 1, m);     double tam2 = 2 * dis(0, 0, n, m - 1);     double tam3 = sqrt(double(m) * m + double(n) * n) + max(m, n);     if (tam3 >= tam1 && tam3 >= tam2) {       cout << 0 <<     << 0 << endl;       cout << n <<     << m << endl;       if (n > m) {         cout << 0 <<     << m << endl;         cout << n <<     << 0 << endl;       } else {         cout << n <<     << 0 << endl;         cout << 0 <<     << m << endl;       }       return 0;     }     if (n <= m) {       cout << n - 1 <<     << m << endl;       cout << 0 <<     << 0 << endl;       cout << n <<     << m << endl;       cout << 1 <<     << 0 << endl;     } else {       cout << n <<     << m - 1 << endl;       cout << 0 <<     << 0 << endl;       cout << n <<     << m << endl;       cout << 0 <<     << 1 << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 10;   const int INF = 0x3f3f3f3f;   const double PI = acos(-1.0);   const double eps = 1e-10;   const int M = 1e9 + 7;   int dcmp(double first) {     if (fabs(first) < eps) return 0;     return first < 0 ? -1 : 1;   }   int a[N];   int main() {     int n, f = 0;     scanf( %d , &n);     for (int i = 0; i <= n; i++) {       scanf( %d , &a[i]);       if (i && a[i] > 1 && a[i - 1] > 1) f = 1;     }     printf( %s n , f ?  ambiguous  :  perfect );     for (int l = 0; l < 2 && f; l++) {       int p = 0, p0 = 0, cnt = 1;       for (int i = 0; i <= n; i++) {         int p1 = 0, p2 = 0;         for (int k = 0; k < a[i]; k++) {           if (!k) p1 = cnt;           if (l && k == 1) p2 = cnt;           if (l && !k && p0)             printf( %d  , p0);           else             printf( %d  , p);           cnt++;         }         p = p1;         p0 = p2;       }       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using namespace std;   bool onSegment(pair<int, int> p, pair<int, int> q, pair<int, int> r) {     if (q.first <= max(p.first, r.first) && q.first >= min(p.first, r.first) &&         q.second <= max(p.second, r.second) &&         q.second >= min(p.second, r.second))       return true;     return false;   }   int orientation(pair<int, int> p, pair<int, int> q, pair<int, int> r) {     int val = (q.second - p.second) * (r.first - q.first) -               (q.first - p.first) * (r.second - q.second);     if (val == 0) return 0;     return (val > 0) ? 1 : 2;   }   bool intersect(pair<int, int> p1, pair<int, int> q1, pair<int, int> p2,                  pair<int, int> q2) {     int o1 = orientation(p1, q1, p2);     int o2 = orientation(p1, q1, q2);     int o3 = orientation(p2, q2, p1);     int o4 = orientation(p2, q2, q1);     if (o1 != o2 && o3 != o4) return true;     if (o1 == 0 && onSegment(p1, p2, q1)) return true;     if (o2 == 0 && onSegment(p1, q2, q1)) return true;     if (o3 == 0 && onSegment(p2, p1, q2)) return true;     if (o4 == 0 && onSegment(p2, q1, q2)) return true;     return false;   }   float area(int x1, int y1, int x2, int y2, int x3, int y3) {     return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0);   }   bool check(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4,              int x, int y) {     float A = area(x1, y1, x2, y2, x3, y3) + area(x1, y1, x4, y4, x3, y3);     float A1 = area(x, y, x1, y1, x2, y2);     float A2 = area(x, y, x2, y2, x3, y3);     float A3 = area(x, y, x3, y3, x4, y4);     float A4 = area(x, y, x1, y1, x4, y4);     return (A == A1 + A2 + A3 + A4);   }   int main() {     int a, b, c = 0, c1 = 0;     vector<pair<int, int> > v1, v2;     for (int i = 0; i < 4; i++) {       cin >> a >> b;       v1.push_back(pair<int, int>(a, b));     }     for (int i = 0; i < 4; i++) {       cin >> a >> b;       v2.push_back(pair<int, int>(a, b));     }     for (int i = 0; i < 4; i++) {       if (check(v1[0].first, v1[0].second, v1[1].first, v1[1].second, v1[2].first,                 v1[2].second, v1[3].first, v1[3].second, v2[i].first,                 v2[i].second))         c++;     }     for (int i = 0; i < 4; i++) {       if (check(v2[0].first, v2[0].second, v2[1].first, v2[1].second, v2[2].first,                 v2[2].second, v2[3].first, v2[3].second, v1[i].first,                 v1[i].second))         c1++;     }     if (c == 4 || c1 == 4) {       cout <<  YES ;     } else {       c = 0;       for (int i = 0; i < 4; i++) {         for (int j = 0; j < 4; j++) {           if (intersect(v1[i], v1[(i + 1) % 4], v2[j], v2[(j + 1) % 4])) c++;         }       }       if (c != 0)         cout <<  YES ;       else         cout <<  NO ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100010;   const int maxm = 100010;   struct Arc {     int to, next;   } arcs[maxm];   int head[maxn], ne, out[maxn], ans[maxn];   inline void add_arc(int u, int v);   priority_queue<int> Q;   int main() {     int n, m, i, u, v, k;     while (scanf( %d%d , &n, &m) > 0) {       memset(head, -1, sizeof(int) * (n + 1));       ne = 0;       memset(out, 0, sizeof(int) * (n + 1));       for (i = 0; i < m; i++) {         scanf( %d%d , &u, &v);         add_arc(v, u);         out[u]++;       }       while (!Q.empty()) Q.pop();       for (i = 1; i <= n; i++) {         if (out[i] == 0) Q.push(i);       }       for (k = n; k >= 1; k--) {         u = Q.top();         Q.pop();         ans[u] = k;         for (i = head[u]; i != -1; i = arcs[i].next) {           v = arcs[i].to;           if (--out[v] == 0) Q.push(v);         }       }       for (i = 1; i <= n; i++) printf( %d  , ans[i]);       printf(  n );     }     return 0;   }   inline void add_arc(int u, int v) {     arcs[ne].to = v;     arcs[ne].next = head[u];     head[u] = ne++;     return;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int z[6] = {1, 0, 0, 1, 2, 2};     int r[6] = {2, 2, 1, 0, 0, 1};     int l[6] = {0, 1, 2, 2, 1, 0};     long long int m, n, num;     while (cin >> m >> n) {       num = m % 6;       if (z[num] == n) {         cout << 1 << endl;       } else if (r[num] == n) {         cout << 2 << endl;       } else {         cout << 0 << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MaxN = 1005;   const int oo = 1000000000;   const long long Base = 1000000007;   typedef int Tarr[MaxN];   long long x, y, a, b, n, m, xx, yy, r1, r2, r3, r4;   void Ok(int Val) {     int xx = Val * a, yy = Val * b;     if (xx % 2 == 0) {       r1 = max((long long)0, min(n - xx, x - xx / 2));     } else       r1 = max((long long)0, min(n - xx, x - xx / 2 - 1));     if (yy % 2 == 0) {       r2 = max((long long)0, min(m - yy, y - yy / 2));     } else       r2 = max((long long)0, min(m - yy, y - yy / 2 - 1));     r3 = r1 + xx;     r4 = r2 + yy;   }   int main() {     cin >> n >> m >> x >> y >> a >> b;     int aa = a, bb = b, tmp;     do {       tmp = aa % bb;       aa = bb;       bb = tmp;     } while (bb != 0);     a /= aa;     b /= aa;     Ok(min((n) / a, (m) / b));     cout << r1 <<     << r2 <<     << r3 <<     << r4;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100 * 1000 + 13;   bool flagBlack = false, flagWhite = false;   int a[N];   int n;   int lacesB[N];   int lacesW[N];   inline void read() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (a[i] == 0) {         flagBlack = true;       } else {         flagWhite = true;       }     }   }   inline void solve() {     if (!flagBlack || !flagWhite) {       cout <<  YES  << endl;       return;     }     int mincnt = 100 * 1000 + 13;     int maxcnt = -100 * 1000 + 13;     int cnt = 0;     for (int i = 0; i < n; i++) {       if (i > 0) {         if (a[i] != a[i - 1]) {           if (cnt > maxcnt) {             maxcnt = cnt;           }           if (cnt < mincnt) {             mincnt = cnt;           }           cnt = 0;         }       }       cnt++;     }     if (cnt > maxcnt) {       maxcnt = cnt;     }     if (cnt < mincnt) {       mincnt = cnt;     }     if (mincnt == maxcnt) {       cout <<  YES  << endl;     } else {       cout <<  NO  << endl;     }   }   int main() {     read();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1e18;   const long long MAXN = 1e2 + 20;   long long n, w, ans[MAXN];   pair<long long, long long> a[MAXN];   long long sa(long long n, long long k) {     if (n % k) return n / k + 1;     return n / k;   }   void fail() {     cout << -1;     exit(0);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> w;     long long kol = 0;     for (long long i = 0; i < n; i++) {       cin >> a[i].first;       a[i].second = i;       kol += sa(a[i].first, 2);     }     if (kol > w) fail();     sort(a, a + n);     for (long long i = 0; i < n - 1; i++) {       ans[a[i].second] = sa(a[i].first, 2);       w -= ans[a[i].second];     }     if (w > a[n - 1].first) {       w -= a[n - 1].first;       ans[a[n - 1].second] = a[n - 1].first;       for (long long i = n - 2; i >= 0; i--) {         if (w > a[i].first - ans[a[i].second]) {           w -= a[i].first - ans[a[i].second];           ans[a[i].second] = a[i].first;         } else {           ans[a[i].second] += w;           break;         }       }     } else {       ans[a[n - 1].second] = w;     }     for (long long i = 0; i < n; i++) cout << ans[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   typedef vector<long long int> vi;   typedef vector<vi> vvi;   typedef vector<pair<long long int, long long int> > vii;   typedef vector<vii> vvii;   typedef vector<bool> vb;   const long long int dx4[] = {1, 0, -1, 0}, dy4[] = {0, -1, 0, 1};   const long long int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1},                       dy8[] = {1, -1, 1, 0, -1, 1, -0, -1};   long double dist(long long int a1, long long int b1, long long int a2,                    long long int b2) {     return sqrt(((a2 - a1) * (a2 - a1)) + ((b2 - b1) * (b2 - b1)));   }   signed main() {     long double r, x1, y1, x2, y2, rap, xap, yap, d, cosine, sine;     cin >> r >> x1 >> y1 >> x2 >> y2;     d = dist(x1, y1, x2, y2);     if (d > r) {       rap = r;       xap = x1;       yap = y1;     } else {       rap = (r + d) / (long double)(2);       if (d == 0) {         xap = x1 + r / (long double)(2);         yap = y1;       } else {         cosine = (x1 - x2) / d;         sine = (y1 - y2) / d;         xap = x2 + (rap * cosine);         yap = y2 + (rap * sine);       }     }     cout << setprecision(100) << xap <<     << yap <<     << rap << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     pair<long long, long long> a[1001];     a[0].second = 1;     for (long long i = 1; i < 1001; i++) {       a[i].first = i;       if (i < 10)         a[i].second = 1;       else if (i < 100)         a[i].second = 2;       else if (i < 1000)         a[i].second = 3;     }     long long n;     cin >> n;     long long count = 1;     long long i = 1;     while (count < n) {       count += a[i].second;       i++;     }     if (count == n) {       long long temp = i;       long long ans;       while (temp != 0) {         ans = temp % 10;         temp = temp / 10;       }       cout << ans << endl;     } else if (count > n) {       i--;       long long temp = a[i].first;       long long flag = count - n - 1;       while (flag > 0) {         temp = temp / 10;         flag--;       }       cout << temp % 10 << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     string s[t];     for (int i = 0; i < t; i++) {       cin >> s[i];     }     map<string, int> m;     for (int j = 0; j < t; j++) {       for (int l = 1; l <= 9; l++) {         for (int i = 0; i < 10 - l; i++) {           string p = s[j].substr(i, l);           if (m.find(p) != m.end()) {             if (m[p] != j) {               m[p] = -1;             }           } else {             m[p] = j;           }         }       }     }     string ans[t];     for (int i = 0; i < t; i++) {       ans[i] =  a ;     }     for (auto it = m.begin(); it != m.end(); it++) {       int x = it->second;       string y = it->first;       if (x == -1) {         continue;       }       if (ans[x] ==  a  || ans[x].size() > y.size()) {         ans[x] = y;       }     }     for (int i = 0; i < t; i++) {       cout << ans[i] << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   long long ceil(long long a, long long b) {     long long res = a / b;     if (a % b != 0) res++;     return res;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n, m, q;     cin >> n >> m >> q;     vector<int> a(n), b(m);     long long total = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       if (i % 2 == 0) {         total += a[i];       } else {         total -= a[i];       }     }     for (int i = 0; i < m; i++) {       cin >> b[i];     }     set<long long> pos_sum;     long long sum_even = 0, sum_odd = 0;     for (int i = 0; i < n; i++) {       if (i % 2 == 0) {         sum_even += b[i];       } else {         sum_odd += b[i];       }     }     for (int j = 0; j < m - n + 1; j++) {       if (j != 0) {         if ((j - 1) % 2 == 0) {           sum_even -= b[j - 1];         } else {           sum_odd -= b[j - 1];         }         if ((j + n - 1) % 2 == 0) {           sum_even += b[j + n - 1];         } else {           sum_odd += b[j + n - 1];         }       }       if (j % 2 == 0) {         pos_sum.insert(-sum_odd + sum_even);       } else {         pos_sum.insert(-sum_even + sum_odd);       }     }     auto find = [&]() {       auto ub = pos_sum.upper_bound(total);       long long res = LONG_LONG_MAX;       if (ub != pos_sum.end()) {         res = min(res, abs(total - *ub));       }       if (ub != pos_sum.begin()) {         ub--;         res = min(res, abs(total - *ub));       }       return res;     };     cout << find() <<   n ;     while (q--) {       int l, r, x;       cin >> l >> r >> x;       l--, r--;       long long len = r - l + 1;       if (l % 2 == 0) {         total += ceil(len, 2) * x;         total -= (len / 2) * x;       } else {         total -= ceil(len, 2) * x;         total += (len / 2) * x;       }       cout << find() <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string k;     cin >> k;     int st = 0;     while (st < (int)k.length() && k[st] ==  a  && 1) {       st++;     }     if (st == (int)k.length()) {       k[k.length() - 1] =  z ;       cout << k << endl;       return 0;     }     int se = st + 1;     while (se < (int)k.length() && k[se] !=  a  && 1) {       se++;     }     se--;     for (int i = st; i <= se; i++) {       k[i]--;     }     cout << k << endl;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     int n, x, y;     cin >> n >> x >> y;     vector<int> nums(n);     vector<int> a(n);     vector<vector<int>> freq(n + 1);     vector<vector<int>> ind(n + 1 + 1);     vector<bool> mis(n);     for (int i = 0; i < n; i++) {       cin >> nums[i];       mis[i] = false;       ind[nums[i]].push_back(i);     }     for (int i = 1; i <= n + 1; i++) {       freq[ind[i].size()].push_back(i);     }     int f = n;     for (int k = 0; k < x; k++) {       while (freq[f].empty()) {         f--;       }       int col = freq[f].back();       int idx = ind[col].back();       a[idx] = col;       ind[col].pop_back();       freq[f].pop_back();       freq[f - 1].push_back(col);     }     while (f > 0 && freq[f].empty()) f--;     if (2 * f > 2 * n - x - y) {       cout <<  NO n ;       return;     }     vector<int> ve;     for (int i = 1; i <= f; i++) {       for (int color : freq[i]) {         ve.insert(ve.end(), ind[color].begin(), ind[color].end());       }     }     int mismatch = n - y;     auto makemismatch = [&](int i) {       a[i] = freq[0][0];       mismatch--;       mis[i] = true;     };     for (int i = 0; i < n - x; i++) {       a[ve[i]] = nums[ve[(i + (n - x) / 2) % (n - x)]];       if (a[ve[i]] == nums[ve[i]]) {         makemismatch(ve[i]);       }     }     for (int i = 0; mismatch > 0; i++) {       if (!mis[ve[i]]) makemismatch(ve[i]);     }     cout <<  YES n ;     for (int i = 0; i < n; i++) {       cout << a[i] <<    ;     }     cout <<   n ;   }   int main() {     int t;     cin >> t;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     n--;     int a[n];     for (int i = 0; i < n; ++i) {       cin >> a[i];       if (a[i] == 0) --i;     }     int principi;     int b[n];     for (int i = 0; i < n; ++i) {       cin >> b[i];       if (b[i] == 0) --i;       if (b[i] == a[0]) principi = i;     }     bool bo = true;     for (int i = 1; bo and i < n; ++i) {       if (a[i] != b[(principi + i) % n]) bo = false;     }     if (bo)       cout <<  YES  << endl;     else       cout <<  NO  << endl;   }
#include <bits/stdc++.h>   using namespace std;   struct edge {     int x, y, z, id;   } e[200005];   vector<int> to[200005];   int fa[200005], f[200005][18], deep[200005], g[200005][18], id[200005],       ans[200005];   bool flag[200005];   int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }   void dfs(int x, int fa) {     f[x][0] = fa, deep[x] = deep[fa] + 1;     for (auto tar : to[x])       if (tar != fa) dfs(tar, x);   }   pair<int, int> lca(int x, int y) {     int res = 0;     if (deep[x] < deep[y]) swap(x, y);     for (int k = 17; k >= 0; k--)       if (deep[f[x][k]] >= deep[y]) res = max(res, g[x][k]), x = f[x][k];     if (x == y) return {x, res};     for (int k = 17; k >= 0; k--)       if (f[x][k] != f[y][k])         res = max(res, max(g[x][k], g[y][k])), x = f[x][k], y = f[y][k];     res = max(res, max(g[x][0], g[y][0]));     return {f[x][0], res};   }   void merge(int from, int to, int v) {     from = find(from);     while (deep[from] > deep[to]) {       ans[id[from]] = v;       fa[from] = find(f[from][0]);       from = find(from);     }   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++)       scanf( %d%d%d , &e[i].x, &e[i].y, &e[i].z), e[i].id = i;     sort(e + 1, e + m + 1, [&](edge a, edge b) { return a.z < b.z; });     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= m; i++) {       int fx = find(e[i].x), fy = find(e[i].y);       if (fx != fy)         to[e[i].x].push_back(e[i].y), to[e[i].y].push_back(e[i].x), fa[fx] = fy;       else         flag[i] = 1;     }     dfs(1, 0);     for (int i = 1; i <= m; i++)       if (!flag[i])         g[deep[e[i].x] < deep[e[i].y] ? e[i].y : e[i].x]          [0] = e[i].z,             id[deep[e[i].x] < deep[e[i].y] ? e[i].y : e[i].x] = e[i].id;     for (int k = 1; k <= 17; k++)       for (int i = 1; i <= n; i++)         f[i][k] = f[f[i][k - 1]][k - 1],         g[i][k] = max(g[i][k - 1], g[f[i][k - 1]][k - 1]);     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= m; i++)       if (flag[i]) {         auto g = lca(e[i].x, e[i].y);         ans[e[i].id] = g.second;         merge(e[i].x, g.first, e[i].z), merge(e[i].y, g.first, e[i].z);       }     for (int i = 1; i <= m; i++) printf( %d  , ans[i] ? ans[i] - 1 : -1);     return 0;   }