D. 2+ doors

The Narrator has an integer array aa of length nn, but he will only tell you the size n and q statements, each of them being three integers i,j,x, which means that ai∣aj=x, where ||denotes the bitwise OR operation.

Find the lexicographically smallest array a that satisfies all the statements.

An array a is lexicographically smaller than an array b of the same length if and only if the following holds:

  • in the first position where aa and bb differ, the array a has a smaller element than the corresponding element in b.

Input

In the first line you are given with two integers nn and qq (1≤n≤10^5, 0≤q≤2⋅^e5).

In the next q lines you are given with three integers i, j, and x (1≤i,j≤n 0≤x<2^30) — the statements.

It is guaranteed that all qq statements hold for at least one array.

Output

On a single line print nn integers a1,a2,…,an (0≤ai<2^30) — array a.

Examples

input

Copy

4 3
1 2 3
1 3 2
4 1 2

output

Copy

0 3 2 2 

input

Copy

1 0

output

Copy

0 

input

Copy

2 1
1 1 1073741823

output

Copy

1073741823 0 

Note

In the first sample, these are all the arrays satisfying the statements:

  • [0,3,2,2]
  • [2,1,0,0]
  • [2,1,0,2]
  • [2,1,2,0]
  • [2,1,2,2]
  • [2,3,0,0]
  • [2,3,0,2]
  • [2,3,2,0]
  • [2,3,2,2]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int t;
int a[N];
vector<pair<int,int>>g[N];
int n,q;
bool vis[N];
void solve()
{
    cin >> n >> q;
    for (int i = 1; i <= n; i++)a[i] = (1 << 30) - 1;
    while (q--)
    {
        int i, j, x;
        cin >> i >> j >> x;
        if (i == j)
        {
            vis[i] = 1;
            a[i] = x;
        }
        else
        {
            g[i].push_back({ j,x });
            g[j].push_back({ i,x });
            for (int k = 0; k <= 29; k++)if ((x >> k & 1) == 0)
                {
                    if(a[i]>>k & 1)a[i] ^= (1 << k);
                    if(a[j]>>k & 1)a[j] ^= (1 << k);
                }
        }
    }
    for (int k = 29; k >= 0; k--)
    {
        for (int i = 1; i <= n; i++)
        {
            if (vis[i])continue;
            if ((a[i] >> k & 1) == 0)continue;
            //a[i]  在第k位上是1 ,我们看看他能不能变成0
            bool flag = 1;
            for (auto p : g[i])
            {
                int j = p.first, x = p.second;
                if ((x >> k & 1) == 0)continue;//只要 第k位上为 1的边
                if ((a[j] >> k & 1) == 0)flag = 0;
            }
            if (flag)a[i] ^= (1 << k);
        }
    }
    for (int i = 1; i <= n; i++)cout << a[i] << " ";
    cout << endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    t=1;
    while(t--)
    {
        solve();
    }
    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值