Codeforces Round #617 (Div. 3) (A ~ F)

                                                                A. Array with Odd Sum

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array aa consisting of nn integers.

In one move, you can choose two indices 1≤i,j≤n1≤i,j≤n such that i≠ji≠j and set ai:=ajai:=aj. You can perform such moves any number of times (possibly, zero). You can choose different indices in different operations. The operation := is the operation of assignment (i.e. you choose ii and jj and replace aiai with ajaj).

Your task is to say if it is possible to obtain an array with an odd (not divisible by 22) sum of elements.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤20001≤t≤2000) — the number of test cases.

The next 2t2t lines describe test cases. The first line of the test case contains one integer nn (1≤n≤20001≤n≤2000) — the number of elements in aa. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤20001≤ai≤2000), where aiai is the ii-th element of aa.

It is guaranteed that the sum of nn over all test cases does not exceed 20002000 (∑n≤2000∑n≤2000).

Output

For each test case, print the answer on it — "YES" (without quotes) if it is possible to obtain the array with an odd sum of elements, and "NO" otherwise.

Example

input

Copy

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

output

Copy

YES
NO
YES
NO
NO

题目大意:

N个数,可以将某个数替换成另外一个数,也不可以替换,该操作可以执行无数次问是否能使数组之和为奇数

思路 :

先看看原数组的和是否是奇数,是就可行,如果不是,看看是否全是偶数,如果全是偶数就不可行,最后看下数组中是否存在奇数,如果存在,那么一定可行,否则不可行

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

int main()
{
	int T; cin >> T;
	while (T--) {
		int n; sc("%d", &n);
		int ans = 0;
		bool flag = false;
		bool f = false;
		for (int i = 0; i < n; i++) {
			int tmp;
			sc("%d", &tmp);
			ans += tmp;
			if (tmp & 1)
				flag = true;   // 存在奇数
			else
				f = true;  // 存在偶数
		}
		if (ans & 1) {
			cout << "YES" << endl;  // 原数组和为奇数
			continue;
		}
		if (flag) {
			if (!f)
				cout << "NO" << endl;  
			else
				cout << "YES" << endl; // 存在奇数
		}
		else
			cout << "NO" << endl;   // 全是偶数
	}
	return 0;  // 改数组大小!!!
}

                                                                               B. Food Buying

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Mishka wants to buy some food in the nearby shop. Initially, he has ss burles on his card.

Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number 1≤x≤s1≤x≤s, buy food that costs exactly xx burles and obtain ⌊x10⌋⌊x10⌋ burles as a cashback (in other words, Mishka spends xx burles and obtains ⌊x10⌋⌊x10⌋ back). The operation ⌊ab⌋⌊ab⌋ means aa divided by bb rounded down.

It is guaranteed that you can always buy some food that costs xx for any possible value of xx.

Your task is to say the maximum number of burles Mishka can spend if he buys food optimally.

For example, if Mishka has s=19s=19 burles then the maximum number of burles he can spend is 2121. Firstly, he can spend x=10x=10 burles, obtain 11 burle as a cashback. Now he has s=10s=10 burles, so can spend x=10x=10 burles, obtain 11 burle as a cashback and spend it too.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The next tt lines describe test cases. Each test case is given on a separate line and consists of one integer ss (1≤s≤1091≤s≤109) — the number of burles Mishka initially has.

Output

For each test case print the answer on it — the maximum number of burles Mishka can spend if he buys food optimally.

Example

input

Copy

6
1
10
19
9876
12345
1000000000

output

Copy

1
11
21
10973
13716
1111111111

题目大意 :

有N块钱,你可以用其中任意金额进行交易,每次交易后会返还你 N / 10块钱, 输出你最多可以花多少钱

思路 :

每次按整十地花,多出来的加上剩下的继续花,直到花光

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
int main()
{
	ll T; cin >> T;
	while (T--) {
		ll n; cin >> n;
		ll ans = 0;
		while (n) {
			ll tot = n / 10;
			ans += tot * 10;  // 花了多少
			if (tot == 0) {
				ans += n;  // 加上剩余的退出
				break;
			}
			n = tot + n % 10;  // 继续花
		}
		cout << ans << endl;
	}
	return 0;  // 改数组大小!!!
}

                                                                C. Yet Another Walking Robot

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0)(0,0). Its path is described as a string ss of length nn consisting of characters 'L', 'R', 'U', 'D'.

Each of these characters corresponds to some move:

  • 'L' (left): means that the robot moves from the point (x,y)(x,y) to the point (x−1,y)(x−1,y);
  • 'R' (right): means that the robot moves from the point (x,y)(x,y) to the point (x+1,y)(x+1,y);
  • 'U' (up): means that the robot moves from the point (x,y)(x,y) to the point (x,y+1)(x,y+1);
  • 'D' (down): means that the robot moves from the point (x,y)(x,y) to the point (x,y−1)(x,y−1).

The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point (xe,ye)(xe,ye), then after optimization (i.e. removing some single substring from ss) the robot also ends its path at the point (xe,ye)(xe,ye).

This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string ss).

Recall that the substring of ss is such string that can be obtained from ss by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL".

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

The next 2t2t lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the robot's path. The second line of the test case contains one string ss consisting of nn characters 'L', 'R', 'U', 'D' — the robot's path.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).

Output

For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers ll and rr such that 1≤l≤r≤n1≤l≤r≤n — endpoints of the substring you remove. The value r−l+1r−l+1 should be minimum possible. If there are several answers, print any of them.

Example

input

Copy

4
4
LRUD
4
LURD
5
RRUDU
5
LLDDR

output

Copy

1 2
1 4
3 4
-1

题目大意 :

控制一个机器人朝四个方向移动,“L”表示向左, “R”表示向右, “U”表示向上, “D”表示向下, 你需要删除长度最小的连续串,使得机器人所在终点不变,输出该串的起始位置和结束位置,如果无法删除,输出-1

思路 :

很显然如果一个位置经过多次,那么这个位置就是可以进行优化的,所以用map存一下每个位置的最后经过时刻,当下一次经过的时候,记录一下和上一次的时间差,并更新当前位置的时刻。

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
map <pir, int> mp;
char str[MAXN];
int L, R, n, min_;
 
int main()
{
	int T; cin >> T;
	getchar();
	while (T--) {
		sc("%d%s", &n, str + 1);
		mp.clear();
		int x = 0, y = 0;
		mp[MK(x, y)] = 0;  // 初始位置时刻为0
		min_ = INF;
		for (int i = 1; i <= n; i++) {
			if (str[i] == 'L')
				x--;
			else if (str[i] == 'R')
				x++;
			else if (str[i] == 'U')
				y++;
			else if (str[i] == 'D')
				y--;
			if (mp.count(MK(x, y))) {
				if (i - mp[MK(x, y)] < min_) {
					min_ = i - mp[MK(x, y)];  // 时间差
					L = mp[MK(x, y)] + 1;
					R = i;
				}
			}
			mp[MK(x, y)] = i;
		}
		if (min_ == INF)
			printf("-1\n");
		else
			printf("%d %d\n", L, R);
	}
	return 0;  // 改数组大小!!!
}

                                                                              D. Fight with Monsters

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn monsters standing in a row numbered from 11 to nn. The ii-th monster has hihi health points (hp). You have your attack power equal to aa hp and your opponent has his attack power equal to bb hp.

You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to 00.

The fight with a monster happens in turns.

  1. You hit the monster by aa hp. If it is dead after your hit, you gain one point and you both proceed to the next monster.
  2. Your opponent hits the monster by bb hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster.

You have some secret technique to force your opponent to skip his turn. You can use this technique at most kk times in total (for example, if there are two monsters and k=4k=4, then you can use the technique 22 times on the first monster and 11 time on the second monster, but not 22 times on the first monster and 33 times on the second monster).

Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.

Input

The first line of the input contains four integers n,a,bn,a,b and kk (1≤n≤2⋅105,1≤a,b,k≤1091≤n≤2⋅105,1≤a,b,k≤109) — the number of monsters, your attack power, the opponent's attack power and the number of times you can use the secret technique.

The second line of the input contains nn integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤1091≤hi≤109), where hihi is the health points of the ii-th monster.

Output

Print one integer — the maximum number of points you can gain if you use the secret technique optimally.

Examples

input

Copy

6 2 3 3
7 10 50 12 1 8

output

Copy

5

input

Copy

1 1 100 99
100

output

Copy

1

input

Copy

7 4 2 1
1 3 5 4 2 7 6

output

Copy

6

题目大意 :

你和一个对手在打怪兽,你的攻击力为A, 对手的攻击力为B,N个怪兽的生命分别为hp[i],对怪兽造成的血量就是攻击 - 生命,从第一个怪兽开始打,击败怪兽后(怪兽的生命 ≤ 0视为击败)接着打下一个,每一只怪兽都是你先打,然后对手打这样轮着来,你击败怪兽后可以获得一个积分,对手击败你不得积分,你有K次机会可以让对手跳过他当前的击打怪兽的机会,输出你可以获得的最大积分

思路 :

很简单的贪心,如果按照正常的规则打可以击败怪兽,那么直接得一分,否则记下需要使用多少次机会才能从这只怪兽上获得一分,最后将所有的次数排序,看最多可以获得多少分即可

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
vector <ll> e;
 
int main()
{
	ll n, a, b, k, ans = 0;
	cin >> n >> a >> b >> k;
	for (int i = 0; i < n; i++) {
		ll hp;
		sc("%lld", &hp);
		hp %= (a + b);
		if (hp == 0)
			hp = a + b;   // 对手刚好击败
		if (a >= hp)   // 你可以击败
			ans++;
		else {
			if (hp % a == 0)   // 注意特判
				e.push_back(hp / a - 1);  
			else
				e.push_back(hp / a);
		}
	}
	sort(ALL(e));
	for (auto it : e) {
		k -= it;
		if (k < 0)
			break;
		ans++;
	}
	cout << ans << endl;
	return 0;  // 改数组大小!!!
}

                                                              E1. String Coloring (easy version)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.

You are given a string ss consisting of nn lowercase Latin letters.

You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in ss).

After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.

The goal is to make the string sorted, i.e. all characters should be in alphabetical order.

Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.

Input

The first line of the input contains one integer nn (1≤n≤2001≤n≤200) — the length of ss.

The second line of the input contains the string ss consisting of exactly nn lowercase Latin letters.

Output

If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print "NO" (without quotes) in the first line.

Otherwise, print "YES" in the first line and any correct coloring in the second line (the coloring is the string consisting of nn characters, the ii-th character should be '0' if the ii-th character is colored the first color and '1' otherwise).

Examples

input

Copy

9
abacbecfd

output

Copy

YES
001010101

input

Copy

8
aaabbcbb

output

Copy

YES
01011011

input

Copy

7
abcdedc

output

Copy

NO

input

Copy

5
abcde

output

Copy

YES
00000

题目大意 :

一个字符串,两种颜色, 你需要给每个字符染色,只有不同颜色的字符才可以相互交换位置,现在你想整个字符串有序,输出每个字符应该被染成什么颜色, 多个解输出任意一个,无解则输出-1

思路 :

说到两种颜色的染色很容易想到二分染色, N又很小, 所以每次把该字符和他前面比他大的字符之间建一条边,跑一遍BFS就行了

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e3 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
vector <int> G[MAXN << 1];
char str[MAXN];
int color[MAXN], n;
bool flag = true;
 
void bfs(int x) {
	queue <int> q;
	q.push(x);
	color[x] = 1;
	while (!q.empty()) {
		int ans = q.front();
		q.pop();
		for (auto it : G[ans]) {
			if (!color[it]) {
				if (color[ans] == 1)
					color[it] = 2;
				else
					color[it] = 1;
				q.push(it);
			}
			else if (color[it] == color[ans]) {
				flag = false;
				return;
			}
		}
	}
}
 
int main()
{
	sc("%d%s", &n, str + 1);
	for (int i = 1; i < n; i++) {
		for (int j = i + 1; j <= n; j++) {
			if (str[i] > str[j])
				G[i].push_back(j), G[j].push_back(i);  // 需要交换的建边
		}
	}
	for (int i = 1; i <= n; i++) {
		if (!flag)
			break;
		if (!color[i]) // 没染过就染,考虑图不连通
			bfs(i);
	}
	if (!flag)
		printf("NO\n");
	else {
		printf("YES\n");
		for (int i = 1; i <= n; i++) {
			if (color[i] == 1)
				printf("0");
			else
				printf("1");
		}
		cout << endl;
	}
	return 0;  // 改数组大小!!!
}

                                                                 E2. String Coloring (hard version)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This is a hard version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.

You are given a string ss consisting of nn lowercase Latin letters.

You have to color all its characters the minimum number of colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in ss).

After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.

The goal is to make the string sorted, i.e. all characters should be in alphabetical order.

Your task is to find the minimum number of colors which you have to color the given string in so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.

Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of ss.

The second line of the input contains the string ss consisting of exactly nn lowercase Latin letters.

Output

In the first line print one integer resres (1≤res≤n1≤res≤n) — the minimum number of colors in which you have to color the given string so that after coloring it can become sorted by some sequence of swaps.

In the second line print any possible coloring that can be used to sort the string using some sequence of swaps described in the problem statement. The coloring is the array cc of length nn, where 1≤ci≤res1≤ci≤res and cici means the color of the ii-th character.

Examples

input

Copy

9
abacbecfd

output

Copy

2
1 1 2 1 2 1 2 1 2 

input

Copy

8
aaabbcbb

output

Copy

2
1 2 1 2 1 2 1 1

input

Copy

7
abcdedc

output

Copy

3
1 1 1 1 1 2 3 

input

Copy

5
abcde

output

Copy

1
1 1 1 1 1 

题目大意 :除了染的颜色数量不受限制以外, 其余的都和E1一样

思路 :

由于E1是拿二分染色做的,写E2的时候也一直往染色上想,不过应该是行不通的,其实这是一个简单的贪心,染色按照编号记的话,一定是能染的越小越好,所以每次判断一下在在他以前,比他大的字符最大染的颜色编号是多少,那么该字符染的颜色就是这个编号 + 1,样例写一遍就理解了

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
int res[MAXN], ans[MAXN], n, cnt;
char str[MAXN];
 
int main()
{
	sc("%d%s", &n, str + 1);
	for (int i = 1; i <= n; i++) {
		int now = str[i] - 'a';
		int max_ = 0;
		for (int j = now + 1; j < 26; j++)  // 比当前字符大的字符
			Max(max_, res[j]);   // 最大染的编号
		ans[i] = max_ + 1;        // 记录答案
		Max(res[now], max_ + 1);
		Max(cnt, max_ + 1);
	}
	cout << cnt << endl;
	for (int i = 1; i <= n; i++)
		printf("%d ", ans[i]);
	cout << endl;
	return 0;  // 改数组大小!!!
}

                                                                                   F. Berland Beauty

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn railway stations in Berland. They are connected to each other by n−1n−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−1n−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 11 to 106106 inclusive.

You asked mm passengers some questions: the jj-th one told you three values:

  • his departure station ajaj;
  • his arrival station bjbj;
  • minimum scenery beauty along the path from ajaj to bjbj (the train is moving along the shortest path from ajaj to bjbj).

You are planning to update the map and set some value fifi on each railway section — the scenery beauty. The passengers' answers should be consistent with these values.

Print any valid set of values f1,f2,…,fn−1f1,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 nn (2≤n≤50002≤n≤5000) — the number of railway stations in Berland.

The next n−1n−1 lines contain descriptions of the railway sections: the ii-th section description is two integers xixi and yiyi (1≤xi,yi≤n,xi≠yi1≤xi,yi≤n,xi≠yi), where xixi and yiyi are the indices of the stations which are connected by the ii-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 mm (1≤m≤50001≤m≤5000) — the number of passengers which were asked questions. Then mm lines follow, the jj-th line contains three integers ajaj, bjbj and gjgj (1≤aj,bj≤n1≤aj,bj≤n; aj≠bjaj≠bj; 1≤gj≤1061≤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−1n−1 integers f1,f2,…,fn−1f1,f2,…,fn−1 (1≤fi≤1061≤fi≤106), where fifi is some valid scenery beauty along the ii-th railway section.

If there are multiple answers, you can print any of them.

Examples

input

Copy

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

output

Copy

5 3 5

input

Copy

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

Copy

5 3 1 2 1 

input

Copy

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

Copy

-1

题目大意 :

有一棵带权树, M个条件,每个条件表示从点U 到 点V之间路径上的最小点权为W, 如果M个条件都能满足,输出一种解决方案,即每条边的权值, 如果无法满足, 输出-1

思路 :

点只有5000个, 可以N * N 暴力写,通过LCA一个个染, 每次染取最大值, 最后判断一下是否满足所有的条件即可

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 5e3 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
struct Edge
{
	int x, y, w;
}s[MAXN];
int color[MAXN][MAXN], n, m, M;
int dep[MAXN], f[MAXN];
vector <int> G[MAXN << 1];
pair <int, int> c[MAXN];
 
void dfs(int x, int fa) {
	dep[x] = dep[fa] + 1;
	f[x] = fa;
	for (auto it : G[x]) {
		if (it != fa)
			dfs(it, x);
	}
}
void solve(int x, int w) {  // 路径染色
	Max(color[x][f[x]], w);
	Max(color[f[x]][x], w);
}
void LCA(int x, int y, int w) {  // 一个个跳
	if (dep[x] > dep[y])
		swap(x, y);
	while (dep[x] < dep[y])
		solve(y, w), y = f[y];
	while (x != y)
		solve(x, w), solve(y, w), x = f[x], y = f[y];
}
bool check(int x, int y, int w) {  // 判断路径上的最小值是否符合
	int min_ = INF;
	if (dep[x] > dep[y])
		swap(x, y);
	while (dep[x] < dep[y]) {
		Min(min_, color[y][f[y]]);
		y = f[y];
	}
	while (x != y) {
		Min(min_, color[y][f[y]]);
		Min(min_, color[x][f[x]]);
		x = f[x], y = f[y];
	}
	if (min_ != w)
		return false;
	return true;
}
 
int main()
{
	cin >> n;
	for (int i = 1; i < n; i++) {
		int ui, vi;
		sc("%d %d", &ui, &vi);
		G[ui].push_back(vi);
		G[vi].push_back(ui);
		c[i] = MK(ui, vi);
	}
	dfs(1, 0);
	cin >> m;
	for (int i = 0; i < m; i++) {
		int ui, vi, wi;
		sc("%d %d %d", &ui, &vi, &wi);
		LCA(ui, vi, wi);
		s[i] = { ui, vi, wi };
		Max(M, wi);
	}
	for (int i = 0; i < m; i++) {
		int ui = s[i].x, vi = s[i].y, wi = s[i].w;
		if (!check(ui, vi, wi))
			printf("-1\n"), exit(0);
	}
	for (int i = 1; i < n; i++) {
		int ui = c[i].first, vi = c[i].second;
		if (color[ui][vi])
			printf("%d ", color[ui][vi]);
		else
			printf("%d ", M);
	}
	cout << endl;
	return 0;  // 改数组大小!!!
}
发布了213 篇原创文章 · 获赞 264 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/104235906