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;  // 改数组大小!!!
}
发布了213 篇原创文章 · 获赞 264 · 访问量 2万+

猜你喜欢

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