Codeforces Round #620 (Div. 2) (A ~ E)

                                                                                    A. Two Rabbits

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.

He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position xx, and the shorter rabbit is currently on position yy (x<yx<y). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by aa, and the shorter rabbit hops to the negative direction by bb.

For example, let's say x=0x=0, y=10y=10, a=2a=2, and b=3b=3. At the 11-st second, each rabbit will be at position 22 and 77. At the 22-nd second, both rabbits will be at position 44.

Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point.

Input

Each test contains one or more test cases. The first line contains the number of test cases tt (1≤t≤10001≤t≤1000).

Each test case contains exactly one line. The line consists of four integers xx, yy, aa, bb (0≤x<y≤1090≤x<y≤109, 1≤a,b≤1091≤a,b≤109) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.

Output

For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.

If the two rabbits will never be at the same position simultaneously, print −1−1.

Example


input

Copy

5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1

output

Copy

2
-1
10
-1
1

Note

The first case is explained in the description.

In the second case, each rabbit will be at position 33 and 77 respectively at the 11-st second. But in the 22-nd second they will be at 66 and 44 respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.

題目大意 :

兩隻兔子,大兔子在位置X,每單位時間跳N格,小兔子在位置Y,每單位時間跳M格,大兔子在左,小兔子在右,面對面跳,輸出相遇的時間,如果無法相遇,輸出-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 main()
{
	int T; cin >> T;
	while (T--) {
		ll x, y, a, b;
		cin >> x >> y >> a >> b;
		if ((y - x) % (a + b) == 0)
			cout << (y - x) / (a + b) << endl;
		else
			cout << -1 << endl;
	}
	return 0;  // 改數組大小!!!
}

                                                                      B. Longest Palindrome

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings "pop", "noon", "x", and "kkkkkk" are palindromes, while strings "moon", "tv", and "abab" are not. An empty string is also a palindrome.

Gildong loves this concept so much, so he wants to play with it. He has nn distinct strings of equal length mm. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.

Input

The first line contains two integers nn and mm (1≤n≤1001≤n≤100, 1≤m≤501≤m≤50) — the number of strings and the length of each string.

Next nn lines contain a string of length mm each, consisting of lowercase Latin letters only. All strings are distinct.

Output

In the first line, print the length of the longest palindrome string you made.

In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don't print this line at all.

Examples


input

Copy

3 3
tab
one
bat

output

Copy

6
tabbat

input

Copy

4 2
oo
ox
xo
xx

output

Copy

6
oxxxxo

input

Copy

3 5
hello
codef
orces

output

Copy

0

input

Copy

9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji

output

Copy

20
ababwxyzijjizyxwbaba

Note

In the first example, "battab" is also a valid answer.

In the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.

In the third example, the empty string is the only valid palindrome string.

題目大意 :

有N個字符串,每個字符串的長度是M,你可以選擇其中任何數量字符串拼接在一起(每個字符串只可以被選一次),使得最後拼接的是一個迴文串,輸出迴文串的最大長度和對應的串,迴文串也包括空串

思路 :

一開始想的有點複雜,當我看到最後一個樣例的時候發現,由於長度都相等,一個字符串本身就是迴文串的話,一定可以構成最終迴文串的一部分,而樣例也給的很友好,如果一個串只包含一個元素,可以作爲最終迴文串的中間位置,所以長度就是所有迴文串 + 只包含一個元素的串 + 所有迴文串,這樣拼接而成

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; }
 
string str[110];
int n, m;
string line, s, ch;
 
int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> str[i];
	for (int i = 0; i < n; i++) {
		string t = str[i];
		reverse(ALL(t)); 
		if (t == str[i])
			s += str[i];   
	}
	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) {
			string t = str[j];
			reverse(ALL(t));
			if (t == str[i])
				line += t;  // 迴文串拼接
		}
	}
	if (line == "" && s == "")
		cout << 0 << endl << endl;
	else {
		s = s.substr(0, m);   // 元素只包含一個的
		cout << SZ(line) * 2 + SZ(s) << endl;
		cout << line << s;  
		reverse(ALL(line));
		cout << line << endl;
	}
	return 0;  // 改數組大小!!!
}

                                                                                C. Air Conditioner

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it.

Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant.

The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially.

Each customer is characterized by three values: titi — the time (in minutes) when the ii-th customer visits the restaurant, lili — the lower bound of their preferred temperature range, and hihi — the upper bound of their preferred temperature range.

A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the ii-th customer is satisfied if and only if the temperature is between lili and hihi (inclusive) in the titi-th minute.

Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers.

Input

Each test contains one or more test cases. The first line contains the number of test cases qq (1≤q≤5001≤q≤500). Description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n≤1001≤n≤100, −109≤m≤109−109≤m≤109), where nn is the number of reserved customers and mm is the initial temperature of the restaurant.

Next, nn lines follow. The ii-th line of them contains three integers titi, lili, and hihi (1≤ti≤1091≤ti≤109, −109≤li≤hi≤109−109≤li≤hi≤109), where titi is the time when the ii-th customer visits, lili is the lower bound of their preferred temperature range, and hihi is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive.

The customers are given in non-decreasing order of their visit time, and the current time is 00.

Output

For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO".

You can print each letter in any case (upper or lower).

Example


input

Copy

4
3 0
5 1 2
7 3 5
10 -1 0
2 12
5 7 10
10 16 20
3 -100
100 0 0
100 -50 50
200 100 100
1 100
99 -100 0

output

Copy

YES
NO
YES
NO

Note

In the first case, Gildong can control the air conditioner to satisfy all customers in the following way:

  • At 00-th minute, change the state to heating (the temperature is 0).
  • At 22-nd minute, change the state to off (the temperature is 2).
  • At 55-th minute, change the state to heating (the temperature is 2, the 11-st customer is satisfied).
  • At 66-th minute, change the state to off (the temperature is 3).
  • At 77-th minute, change the state to cooling (the temperature is 3, the 22-nd customer is satisfied).
  • At 1010-th minute, the temperature will be 0, which satisfies the last customer.

In the third case, Gildong can change the state to heating at 00-th minute and leave it be. Then all customers will be satisfied. Note that the 11-st customer's visit time equals the 22-nd customer's visit time.

In the second and the fourth case, Gildong has to make at least one customer unsatisfied.

題目大意 :

有N個顧客,每個顧客都有到達店內的時間和對於空調溫度的舒適溫度範圍(L  ~ R),現在空調可以在一單位時間內進行三種操作,溫度上升、下降一單位,或者不變化,告訴你店內初始空調溫度,問是否能保證每位顧客來的時候,店內溫度對於他們都是舒適的

思路 :

不難想到要維護一個溫度區間,如果每次空調能夠到達的溫度範圍包含了該時間段所有顧客的要求,那麼就可行,並且一輪顧客來了之後,溫度的範圍不能超過他們的舒適溫度範圍,這樣維護一個溫度區間問題就解決了,注意特判相同時間的顧客如果溫度範圍不相交是不可行的

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 ll INF = 0x3f3f3f3f3f3f3f3f;
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 node
{
	ll t, l, r;
}p[110];
ll n, m;
unordered_map <ll, ll> h, d;
set <ll> st;
 
void init() {
	st.clear();
	h.clear(), d.clear();
}
 
int main()
{
	int T; cin >> T;
	while (T--) {
		sc("%lld %lld", &n, &m);
		init();
		for (int i = 1; i <= n; i++) {
			ll t, l, r;
			sc("%lld %lld %lld", &t, &l, &r);
			st.insert(t);
			h[t] = INF, d[t] = -INF;
			p[i] = { t, l, r };
		}
		for (int i = 1; i <= n; i++)
			Min(h[p[i].t], p[i].r), Max(d[p[i].t], p[i].l);  // 相同時間顧客溫度範圍取交集
		bool ok = true;
		for (int i = 1; i <= n; i++) {
			if (h[p[i].t] < p[i].l || d[p[i].t] > p[i].r) {
				ok = false;
				break;  // 特判相同時間顧客的要求有沒有不相交的
			}
		}
		if (!ok) {
			cout << "NO" << endl;
			continue;
		}
		ll now_time = 0, L = m, R = m;
		for (auto it : st) {
			ll c = it - now_time;  // 當前空調可以達到的溫度範圍
			L -= c; 
			R += c;
			if (L > h[it] || R < d[it]) {
				ok = false;
				break;
			}
			now_time = it;
			Max(L, d[it]); Min(R, h[it]);   // 不能超過顧客的期望範圍
		}
		if (ok)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;  // 改數組大小!!!
}

                                                                     D. Shortest and Longest LIS

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Gildong recently learned how to find the longest increasing subsequence (LIS) in O(nlogn)O(nlog⁡n) time for a sequence of length nn. He wants to test himself if he can implement it correctly, but he couldn't find any online judges that would do it (even though there are actually many of them). So instead he's going to make a quiz for you about making permutations of nn distinct integers between 11 and nn, inclusive, to test his code with your output.

The quiz is as follows.

Gildong provides a string of length n−1n−1, consisting of characters '<' and '>' only. The ii-th (1-indexed) character is the comparison result between the ii-th element and the i+1i+1-st element of the sequence. If the ii-th character of the string is '<', then the ii-th element of the sequence is less than the i+1i+1-st element. If the ii-th character of the string is '>', then the ii-th element of the sequence is greater than the i+1i+1-st element.

He wants you to find two possible sequences (not necessarily distinct) consisting of nn distinct integers between 11 and nn, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible.

Input

Each test contains one or more test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104).

Each test case contains exactly one line, consisting of an integer and a string consisting of characters '<' and '>' only. The integer is nn (2≤n≤2⋅1052≤n≤2⋅105), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is n−1n−1.

It is guaranteed that the sum of all nn in all test cases doesn't exceed 2⋅1052⋅105.

Output

For each test case, print two lines with nn integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between 11 and nn, inclusive, and should satisfy the comparison results.

It can be shown that at least one answer always exists.

Example


input

Copy

3
3 <<
7 >><>><
5 >>><

output

Copy

1 2 3
1 2 3
5 4 3 7 2 1 6
4 3 1 7 5 2 6
4 3 2 1 5
5 4 2 1 3

Note

In the first case, 11 22 33 is the only possible answer.

In the second case, the shortest length of the LIS is 22, and the longest length of the LIS is 33. In the example of the maximum LIS sequence, 44 '33' 11 77 '55' 22 '66' can be one of the possible LIS.

題目大意 :

給你一個長度爲N的字符串只包含“>” 和 “<”,將N + 1個數字分開,要求輸出兩個排列,不僅滿足字符串中的大小關係,且第一個排列的LIS最大,第二個排列的LIS最小

思路 :

從大佬那學到一個很妙的做法,思想是貪心,假設某位置是X,當前字符是 “ < ”,如果想要LIS最大,那麼最好的情況一定是下一個數爲X + 1, 這樣才能盡使得比X大的數儘量都能往後放,如果是 “ > ”, 那麼下一個數字應該有多小選多小,爲X - INF,又因爲是排列,所以離散化一下就好了,反過來就是小的LIS。

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 ll 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; }
 
ll a[MAXN], b[MAXN];
vector <ll> e1, e2;
char str[MAXN];
ll n;
 
int main()
{
	int T; cin >> T;
	while (T--) {
		sc("%lld %s", &n, str + 1);
		e1.clear(), e2.clear();
		a[1] = 1, b[1] = 1;
		e1.push_back(1), e2.push_back(1);
		for (int i = 1; i < n; i++) {
			if (str[i] == '<') 
				a[i + 1] = a[i] + 1, b[i + 1] = b[i] + INF; // a爲最大LIS,b爲最小LIS
			else 
				a[i + 1] = a[i] - INF, b[i + 1] = b[i] - 1;
			e1.push_back(a[i + 1]);
			e2.push_back(b[i + 1]);
		}

		sort(ALL(e1)); sort(ALL(e2));
		e1.erase(unique(ALL(e1)), e1.end());
		e2.erase(unique(ALL(e2)), e2.end());   // 離散化
		for (int i = 1; i <= n; i++) {
			a[i] = lower_bound(ALL(e1), a[i]) - e1.begin() + 1;
			b[i] = lower_bound(ALL(e2), b[i]) - e2.begin() + 1;
		}

		for (int i = 1; i <= n; i++)
			printf("%lld%c", a[i], i == n ? '\n' : ' ');
		for (int i = 1; i <= n; i++)
			printf("%lld%c", b[i], i == n ? '\n' : ' ');
	}
	return 0;  // 改數組大小!!!
}

                                                                    E. 1-Trees and Queries

time limit per test

4 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Gildong was hiking a mountain, walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree?

Then he found that such tree-like graphs are called 1-trees. Since Gildong was bored of solving too many tree problems, he wanted to see if similar techniques in trees can be used in 1-trees as well. Instead of solving it by himself, he's going to test you by providing queries on 1-trees.

First, he'll provide you a tree (not 1-tree) with nn vertices, then he will ask you qq queries. Each query contains 55 integers: xx, yy, aa, bb, and kk. This means you're asked to determine if there exists a path from vertex aa to bb that contains exactly kk edges after adding a bidirectional edge between vertices xx and yy. A path can contain the same vertices and same edges multiple times. All queries are independent of each other; i.e. the added edge in a query is removed in the next query.

Input

The first line contains an integer nn (3≤n≤1053≤n≤105), the number of vertices of the tree.

Next n−1n−1 lines contain two integers uu and vv (1≤u,v≤n1≤u,v≤n, u≠vu≠v) each, which means there is an edge between vertex uu and vv. All edges are bidirectional and distinct.

Next line contains an integer qq (1≤q≤1051≤q≤105), the number of queries Gildong wants to ask.

Next qq lines contain five integers xx, yy, aa, bb, and kk each (1≤x,y,a,b≤n1≤x,y,a,b≤n, x≠yx≠y, 1≤k≤1091≤k≤109) – the integers explained in the description. It is guaranteed that the edge between xx and yy does not exist in the original tree.

Output

For each query, print "YES" if there exists a path that contains exactly kk edges from vertex aa to bb after adding an edge between vertices xx and yy. Otherwise, print "NO".

You can print each letter in any case (upper or lower).

Example


input

Copy

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

output

Copy

YES
YES
NO
YES
NO

Note

The image below describes the tree (circles and solid lines) and the added edges for each query (dotted lines).

 

Possible paths for the queries with "YES" answers are:

  • 11-st query: 11 – 33 – 22
  • 22-nd query: 11 – 22 – 33
  • 44-th query: 33 – 44 – 22 – 33 – 44 – 22 – 33 – 44 – 22 – 3

題目大意 :

給你一棵樹,Q次詢問,每次詢問獨立,將X 和 Y之間連一條邊 (X != Y),問能否從 U 走 K 步到達 V,能輸出 “YES”,否則輸出 “NO”

思路 :

這個挺簡單,只是比判奇偶類型的題目多了個步數,首先如果U和V之間距離的奇偶性與K一樣,那麼判斷U和V之間的距離是否≤ 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 <int> G[MAXN << 1];
int dep[MAXN], f[MAXN][30], n, q;
int dis[MAXN];

void dfs(int x, int fa) {
	dep[x] = dep[fa] + 1;
	f[x][0] = fa;
	for (int i = 1; (1 << i) <= dep[x]; i++)
		f[x][i] = f[f[x][i - 1]][i - 1];
	for (auto it : G[x]) {
		if (it != fa)
			dis[it] = dis[x] + 1, dfs(it, x);
	}
}
int LCA(int x, int y) {
	if (dep[x] < dep[y])
		swap(x, y);
	for (int i = 21; i >= 0; i--) {
		if (dep[x] - (1 << i) >= dep[y])
			x = f[x][i];
	}
	if (x == y)
		return x;
	for (int i = 21; i >= 0; i--) {
		if (f[x][i] == f[y][i])
			continue;
		x = f[x][i], y = f[y][i];
	}
	return f[x][0];
}

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);
	}
	dfs(1, 0);
	cin >> q;
	while (q--) {
		int x, y, u, v, k;
		sc("%d %d %d %d %d", &x, &y, &u, &v, &k);
		int num = dis[x] + dis[y] - 2 * dis[LCA(x, y)] + 1; // 環的點數
		int uv = dis[u] + dis[v] - 2 * dis[LCA(u, v)];   // 名稱即兩點之間的距離
		int ux = dis[u] + dis[x] - 2 * dis[LCA(u, x)];
		int uy = dis[u] + dis[y] - 2 * dis[LCA(u, y)]; 
		int vx = dis[v] + dis[x] - 2 * dis[LCA(v, x)];
		int vy = dis[v] + dis[y] - 2 * dis[LCA(v, y)];
		if ((uv & 1) == (k & 1)) {  // 奇偶性相同
			if (uv <= k)
				printf("YES\n");
			else {    // 距離不夠看看添加的邊能否剪短距離
				if (!(num & 1) && (ux + 1 + vy <= k || (uy + 1 + vx <= k))) 
					printf("YES\n");
				else
					printf("NO\n");
			}
		}
		else {
			if (!(num & 1))
				printf("NO\n");   // 偶一定不行
			else {
				if (ux + 1 + vy <= k || uy + 1 + vx <= k)  // 兩種情況都走一下
					printf("YES\n");
				else
					printf("NO\n");
			}
		}
	}
	return 0;  // 改數組大小!!!
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章