CodeForces - 1296 F Berland Beauty dfs暴力枚举

22 篇文章 0 订阅

一、内容

There are n railway stations in Berland. They are connected to each other by n−1railway 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−1sections 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 106inclusive.You asked mpassengers some questions: the j-th one told you three values: his departure station aj;his arrival station bj;minimum scenery beauty along the path from ajto bj (the train is moving along the shortest path from aj to bj  ).You are planning to update the map and set some value fion each railway section — the scenery beauty. The passengers' answers should be consistent with these values.
Print any valid set of values f1,f2,…,fn−1, which the passengers' answer is consistent with or report that it doesn't exist.

Input

The first line contains a single integer n(2≤n≤5000) — the number of railway stations in Berland.The next n−1lines contain descriptions of the railway sections: the i-th section description is two integers xi and yi (1≤xi,yi≤n,xi≠yi), where xiand yi 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 m(1≤m≤5000) — the number of passengers which were asked questions. Then m lines follow, the j-th line contains three integers aj, bj and gj (1≤aj,bj≤n; aj≠bj; 1≤gj≤106) — 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 -1.Otherwise, print n−1integers f1,f2,…,fn−1 (1≤fi≤106), where fi is some valid scenery beauty along the i  -th railway section.If there are multiple answers, you can print any of them.

Input

4
1 2
3 2
3 4
2
1 2 5
1 3 3

Output

5 3 5

二、思路

  • 每次查询我们将整个路径上的边权都更新为max(f, w[i])。 w[i]是已经存在的边权,初始为0.
  • 然后最后我们再重新扫描下所有查询,如u–>v的路径上 是否有最小值等于f,有的话代表成立。否则-1.

三、代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5e3 + 5, M = N * 2;
struct E {int v, next;} e[M];
int n, m, u, v, f[N], minv, st[N], et[N], len, h[N], w[N];
void add(int u, int v) {e[++len].v = v; e[len].next = h[u]; h[u] = len;}
bool dfs(int u, int et, int fa, int f, int c) {
	if (u == et) return true;
	//搜索出一条路径
	for (int j = h[u]; j; j = e[j].next) {
		int v = e[j].v;
		if (v == fa) continue; 
		if (dfs(v, et, u, f, c)) {
			if (c == 1) w[j / 2] = max(w[j / 2], f); //保留最大的值 
			else minv = min(minv, w[j / 2]);
			return true;	 		
		}
	}  
	return false;//没有搜到终点 
}
int main() {
	len = 1; //编号从23 45一对代表无向边 
	scanf("%d", &n);
	for (int i = 1; i < n; i++) {
		scanf("%d%d", &u, &v);
		add(u, v); add(v, u);
	}
	scanf("%d", &m);
	for (int i = 1; i <= m; i++) {
		scanf("%d%d%d", &st[i], &et[i], &f[i]);
		dfs(st[i], et[i], -1, f[i], 1);
	} 
	for (int i = 1; i <= m; i++) { //检查每条路径上是否合法 
		minv = 1e6;
		dfs(st[i], et[i], -1, 0, 2); 
		if (minv != f[i]) {printf("-1"); return 0;}
	}
	for (int i = 1; i < n; i++) printf("%d ", w[i] == 0 ? 1 : w[i]);
	return 0;
} 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值