There are n railway stations in Berland. They are connected to each other by n−1 railway sections. The railway network is connected, i.e. can be represented as an undirected tree.
You have a map of that network, so for each railway section you know which stations it connects.
Each of the n−1 sections has some integer value of the scenery beauty. However, these values are not marked on the map and you don’t know them. All these values are from 1 to inclusive.
You asked m passengers some questions: the j-th one told you three values:
his departure station ;
his arrival station ;
minimum scenery beauty along the path from to (the train is moving along the shortest path from to ).
You are planning to update the map and set some value on each railway section — the scenery beauty. The passengers’ answers should be consistent with these values.
Print any valid set of values , which the passengers’ answer is consistent with or report that it doesn’t exist.
Input
The first line contains a single integer — the number of railway stations in Berland.
The next lines contain descriptions of the railway sections: the i-th section description is two integers and , where and are the indices of the stations which are connected by the i-th railway section. All the railway sections are bidirected. Each station can be reached from any other station by the railway.
The next line contains a single integer — the number of passengers which were asked questions. Then m lines follow, the j-th line contains three integers , and — the departure station, the arrival station and the minimum scenery beauty along his path.
Output
If there is no answer then print a single integer .
Otherwise, print integers , where is some valid scenery beauty along the i-th railway section.
If there are multiple answers, you can print any of them.
Examples
input
4
1 2
3 2
3 4
2
1 2 5
1 3 3
output
5 3 5
input
6
1 2
1 6
3 1
1 5
4 1
4
6 1 3
3 4 1
6 5 2
1 2 5
output
5 3 1 2 1
input
6
1 2
1 6
3 1
1 5
4 1
4
6 1 1
3 4 3
6 5 3
1 2 4
output
-1
將詢問按g從小到大排序,然後對於每個詢問,將對應路徑中的邊改爲g,最後再檢查一下。
#include<bits/stdc++.h>
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 5e3 + 10;
int head[N], ver[N << 1], Next[N << 1], edge[N << 1], tot = 1;
int n, m, fa[N], ans[N];
struct Q {
int a, b, g;
};
bool cmp(Q a, Q b) {
return a.g < b.g;
}
vector<Q> seq;
namespace LCA {
int t, f[N][20], d[N];
void dfs(int x) {
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if (d[y])continue;
d[y] = d[x] + 1;
f[y][0] = x;
fa[y] = i ^ 1;
for (int j = 1; j <= t; j++)
f[y][j] = f[f[y][j - 1]][j - 1];
dfs(y);
}
}
inline int lca(int x, int y) {
if (d[x] > d[y])swap(x, y);
for (int i = t; i >= 0; i--)
if (d[f[y][i]] >= d[x])
y = f[y][i];
if (x == y)
return x;
for (int i = t; i >= 0; i--)
if (f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
return f[x][0];
}
};
inline void add(int x, int y, int z) {
ver[++tot] = y;
edge[tot] = z;
Next[tot] = head[x];
head[x] = tot;
}
inline void read() {
n = qr();
repi(i, 1, n - 1) {
int x = qr(), y = qr();
add(x, y, i), add(y, x, i);
}
m = qr();
}
void out() {
repi(i, 1, n - 1)printf("%d%c", ans[i], ce(i, n - 1));
}
int main() {
read();
LCA::d[1] = 1;
LCA::t = log2(n) + 1;
LCA::dfs(1);
repi(i, 1, n - 1)ans[i] = 1;
repi(i, 1, m) {
int a = qr(), b = qr(), g = qr();
seq.pb({a, b, g});
}
sort(seq.begin(), seq.end(), cmp);
for (auto it:seq) {
int a = it.a, b = it.b, g = it.g;
int lca = LCA::lca(a, b);
while (a != lca)ans[edge[fa[a]]] = g, a = ver[fa[a]];
while (b != lca)ans[edge[fa[b]]] = g, b = ver[fa[b]];
}
for (auto it:seq) {
int a = it.a, b = it.b, g = it.g;
int lca = LCA::lca(a, b), mn = INF;
while (a != lca)mn = min(mn, ans[edge[fa[a]]]), a = ver[fa[a]];
while (b != lca)mn = min(mn, ans[edge[fa[b]]]), b = ver[fa[b]];
if (mn != g) {
puts("-1");
return 0;
}
}
repi(i, 1, n - 1)printf("%d%c", ans[i], ce(i, n - 1));
return 0;
}