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;
}