The 14th Blue Bridge Cup Competition Software Competition Provincial Competition (C/C++ Group B)


At present, except for questions B and F that have not been filled, the rest of the questions have been updated, and they can all be AC ​​after the unofficial data test. Welcome to exchange


Question A. Date Statistics

1. Description of the topic

  Xiaolan now has an array with a length of 100, and the value of each element in the array is within the
range of 0 to 9. The elements in the array are as follows from left to right:

5 6 8 6 9 1 6 1 2 4 9 1 9 8 2 3 6 4 7 7 5 9 5 0 3 8 7 5 8 1 5 8 6 1 8 3 0 3 7 9 2
7 0 5 8 8 5 7 0 9 9 1 9 4 4 6 8 6 3 3 8 5 1 6 3 4 6 7 0 7 8 2 7 6 8 9 5 6 5 6 1 4 0 1
0 0 9 4 8 0 9 1 2 8 5 0 2 5 3 3

Now he wants to find some subsequences from this array that satisfy the following conditions:

    1. The length of the subsequence is 8 88
    1. . This subsequence can form a yyyymmdd yyyymmdd in subscript orderDate in yyyy mm dd format, and
      the date is required to be2023 2023The date of a day in 2023, such as 20230902 , 20231223 20230902, 2023122320230902,20231223 yyyy yyyy _
      yyyy represents the year,mm mmmm means month,dd dddd represents the number of days, and when the length of the month or the number of days
      is only one digit, a leading zero is required to be added.

  Please help Xiaolan calculate how many different dates in 2023 can be found according to the above conditions.
You only need to count once for the same date.

2. Problem-solving ideas

  Consider the enumeration of the eight-layer cycle. In the middle, you need to cut branches to speed up the search steps. It is not recommended to write dfs, or you will write badly in the examination room like me. Note that the answer needs to be deduplicated. The answer is 235.

3. Template code

  Temporary change

Item B.01 Entropy of Strings

1. Description of the topic

  I have never studied mathematics.

2. Problem-solving ideas

3. Template code

Question C. Smelting Metals

1. Description of the topic

  Xiaolan has a magic furnace for ooming common metalsO smelted into a special metalXXx . This
furnace has a property called conversion rateVVVVVV is a positive integer, which means consumeVVV common
metalOOO happens to be able to smelt a special metalXXX , when common metalOOThe number of O is less than VVAt V ,
smelting cannot be continued.
  Now givesNNN smelting records, each record contains two integersAAA andBBB , which means that AAhas been invested this timeA common metalOOO , and finally smeltedBBB special metalXXx . Each record is independentOO
that was not consumed last timeO will not be added to the next smelting.
  According to thisNNN smelting records, please guess the conversion rateVVWhat are the minimum and maximum values ​​of V , and the title ensures that there is no unsolvable situation in the evaluation data.

2. Problem-solving ideas

  If you have seen the example, it is obvious that the two upper and lower bounds of the answer can be directly dichotomized. Because the structure of the formula is AC = B \frac{A}{C} = BCA=B A A A is unchanged, we first consider the smallestCCC , because it is necessary to guarantee the BBof all formulasB are unchanged, ifCCC is too small, obviously there will be a certain group ofBBB increases, so it is necessary to ensure that each group is consistenta[i]/x <= b[i]. Conversely consider seeking the largestCCC , ifCCC is too big, obviously there will be a certain group ofBBB becomes smaller, and it is necessary to ensure that each group is consistenta[i]/x >= B.

3. Template code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
#define pb(s) push_back(s);
#define SZ(s) ((int)s.size());
#define ms(s,x) memset(s, x, sizeof(s))
#define all(s) s.begin(),s.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int N = 200010;

int n;
int a[N], b[N];
bool check(LL x) {
    
    
	for (int i = 1; i <= n; ++i) {
    
    
		if (a[i] / x > b[i]) return false;
	}
	return true;
}
bool check2(LL x) {
    
    
	for (int i = 1; i <= n; ++i) {
    
    
		if (a[i] / x < b[i]) return false;
	}
	return true;
}
void solve()
{
    
    
	cin >> n;
	for (int i = 1; i <= n; ++i) cin >> a[i] >> b[i];
	LL l = 1, r = 1e9;
	while (l < r) {
    
    
		LL mid = l + r >> 1;
		if (check(mid)) r = mid;
		else l = mid + 1;
	}
	int s = r;
	l = 1, r = 1e9;
	while (l < r) {
    
    
		LL mid = l + r + 1 >> 1;
		if (check2(mid)) l = mid;
		else r = mid - 1;
	}
	cout << s << " " << r << '\n';
}
int main()
{
    
    
	ios_base :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t = 1;
	while (t--)
	{
    
    
		solve();
	}
	return 0;
}

Question D. Airplane Landing

1. Description of the topic

   N N N planes are about to land at an airport with only one runway. of whichiiAirplane i is at T i TiT i arrives at the sky above the airport at any time, and when it arrives, its remaining fuel can continue to circleD i DiD i units of time, that is, it
can be atT i TiThe landing starts at T i time, and can be landed atT i + D i Ti + DiT i+D i starts to land at the moment. The landing process requiresL i LiL i
unit time.
  As soon as one plane finishes landing, another plane can immediately begin landing at the same time, but not
before the previous plane has finished landing.
  Please judgeNNWhether all N planes can land safely.

2. Problem-solving ideas

   see NNThe maximum N10 is ,Tand the maximum is10N. Considering the full arrangement and enumerating all the falling situations, as long as there is one matching situation, the complexity of calculation is roughly10 ! × 10 × 10 10 ! \times10\times1010!×10×10 equals3e8, the theory can pass.

3. Template code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
#define pb(s) push_back(s);
#define SZ(s) ((int)s.size());
#define ms(s,x) memset(s, x, sizeof(s))
#define all(s) s.begin(),s.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int N = 200010;


int n;
int a[N], b[N], c[N];
void solve()
{
    
    
	cin >> n;
	for (int i = 0; i < n; ++i) cin >> a[i] >> b[i] >> c[i];
	std::vector<int> d(n);
	auto check = [&]() {
    
    
		int v = 0;
		for (int i = 0; i < n; ++i) {
    
    
			int x = d[i];
			if (i == 0) {
    
    
				v = a[x] + c[x];
			} else {
    
    
				if (a[x] + b[x] < v) return false;
				v = max(v, a[x]) + c[x];
			}
		}
		return true;
	};
	iota(all(d), 0);
	bool f = false;
	do {
    
    
		if (check()) {
    
    
			f = true;
			break;
		}
	} while (next_permutation(all(d)));
	if (f) cout << "YES" << '\n';
	else cout << "NO" << '\n';
}
int main()
{
    
    
	ios_base :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t = 1;
	cin >> t;
	while (t--)
	{
    
    
		solve();
	}
	return 0;
}

Question E. Solitaire sequence

1. Description of the topic

   For a length KKInteger sequence of K : A 1 , A 2 , . . . , AK A_1, A_2, . . . , A_KA1,A2,...,AK, which we call the Solitaire sequence if and only if A i A_iAiThe first digit of is exactly equal to A i − 1 A_{i-1}Ai1The last digit of ( 2 ≤ i ≤ K ) (2 ≤ i ≤ K)(2iK ) .
   eg12, 23, 35, 56, 61, 11 12, 23, 35, 56, 61, 1112,23,35,56,61,11 is the sequence of Solitaire;12, 23, 34, 56 12, 23, 34, 5612,23,34,56 is not a Solitaire sequence, because56 56The first digit of 56 is not equal to 34 34The last digit of 34 . All lengths are1 1The integer sequence of 1 is a Solitaire sequence.
   Now given a lengthNNN -like sequenceA 1 , A 2 , . . . , AN A_1, A_2, . . . , A_NA1,A2,...,AN, please calculate how many numbers are deleted from it at least, so that the remaining sequence is a Solitaire sequence?

2. Problem-solving ideas

   When I finished reading the questions in the examination room, I felt a little strange, but found that the thinking was relatively simple. First of all, we only need to pay attention to the first digit and the last digit of a number, which is defined as f [ i ] f[i]f [ i ] is the numberiithe length of the longest Solitaire sequence ending in i , iiThe range of i is[ 0 , 9 ] [0,9][0,9 ] . For each number, set its first digit toaaa , the number at the end isbbb , then there is a transfer equation:
f [ b ] = max ( f [ b ] , f [ a ] ​​+ 1 ) f[b]=max(f[b],f[a]+1)f[b]=max(f[b],f[a]+1 )
   Finally atf[0], f[1] . . . f[9] f[0], f[1]...f[9]f [ 0 ] , f [ 1 ] ... f [ 9 ] take a maximumans ansan s , the answer isn − ans n-ansnans

3. Template code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
#define pb(s) push_back(s);
#define SZ(s) ((int)s.size());
#define ms(s,x) memset(s, x, sizeof(s))
#define all(s) s.begin(),s.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int N = 200010;

int n;
int a[N], b[N];
int f[10];
void solve()
{
    
    
	cin >> n;
	for (int i = 1; i <= n; ++i) {
    
    
		int x;
		cin >> x;
		b[i] = x % 10;
		string s = to_string(x);
		a[i] = s[0] - '0';
	}
	for (int i = 1; i <= n; ++i) {
    
    
		f[b[i]] = max(f[b[i]], f[a[i]] + 1);
	}
	int ans = 0;
	for (int i = 0; i <= 9; ++i) ans = max(ans, f[i]);
	cout << n - ans << '\n';
}
int main()
{
    
    
	ios_base :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t = 1;
	while (t--)
	{
    
    
		solve();
	}
	return 0;
}

Question F. Number of islands

1. Description of the topic

Temporary update, I feel bad to write

2. Problem-solving ideas

3. Template code

Question G. Substring shorthand

1. Description of the topic

   A very new shorthand method is popular in the circle of programmers: for a string, only the first and last characters are reserved, and all characters between the first and last characters are replaced by the length of this part. For example international − alization international-alizationin t er and t i o na l i z a t i o n is abbreviated asi 18 n i18ni18n K u b e r n e t e s Kubernetes K u b er n e t es (note that the hyphen is not part of the string) is abbreviated as K 8 s , L anqiao K8s, LanqiaoK8s,L an q ia o abbreviated asL 5 o L5oL 5 o et al.
   In this question, we stipulate that the length is greater than or equal toKKAll strings of K can use this shorthand method (the length is less thanKKK strings are not eligible for this shorthand).
   Given a stringSSS and two charactersc 1 c1c 1 andc 2 c2c 2 , please calculateSSHow many S havec 1 c1c 1 starts withc 2 c2Can the substring at the end of c 2 use this shorthand?

2. Problem-solving ideas

   It feels even more strange to put this Gquestion , a prefix and template question. Suppose the subscript is iiThe character of i isc 1 c1c 1 , then we only need to count in the interval[ i + k − 1 , n ] [i+k-1,n][i+k1,n ] how manyc 2 c2c 2 is enough, prefix and preprocessc 2 c2c 2 characters, just add the answer directly, note that the answer will explodeint, complexityO ( n ) O(n)O ( n )

3. Template code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
#define pb(s) push_back(s);
#define SZ(s) ((int)s.size());
#define ms(s,x) memset(s, x, sizeof(s))
#define all(s) s.begin(),s.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int N = 500010;

int k;
string s;
char c1, c2;
int a[N];
void solve()
{
    
    
	cin >> k >> s >> c1 >> c2;
	int n = s.size();
	s = '?' + s;
	for (int i = 1; i <= n; ++i) {
    
    
		a[i] = (s[i] == c2);
		a[i] += a[i - 1];
	}
	LL ans = 0;
	for (int i = 1; i + k - 2 < n; ++i) {
    
    
		if (s[i] == c1) ans += a[n] - a[i + k - 2];
	}
	cout << ans << '\n';
}
int main()
{
    
    
	ios_base :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t = 1;
	while (t--)
	{
    
    
		solve();
	}
	return 0;
}

Question H. Integer Deletion

1. Description of the topic

   Given a length NNInteger sequence of N : A 1 , A 2 , . . . , AN A_1, A_2, . . . , A_NA1,A2,...,AN. You have to repeat the following operations KKK times:
   Each time select the smallest integer in the sequence (if there are more than one minimum value, select the first one), and delete it. And add the deleted value to the integer adjacent to it.
   OutputKKSequence after K operations.

2. Problem-solving ideas

   It seems to be a relatively typical topic. Use the priority queue to maintain, store the value and subscript, and then use an array to cntaccumulate the sum of each subscript. When the smallest value pops up and the subscript iis if it cnt[i]is not equal at this time 0, it means it The actual value needs to be added cnt[i]. We will increase it and put it back into the priority column. Note that it needs to be cleared cnt[i]. If it cnt[i]is equal to 0at this time, then we have successfully popped the current smallest element. At this time, we need to increase the value of the previous element and the next element. We need to simulate a linked list to record who the front and back elements of each element are, indicating that the subscript is pre[i]the iupper Who is an element ne[i]indicates who is the next element iwhose is, and n-kthe loop ends when the number of elements in the heap is exhausted. It is not difficult to imagine that the number of entry and exit of heap elements is linear.


3. Template code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
#define pb(s) push_back(s);
#define SZ(s) ((int)s.size());
#define ms(s,x) memset(s, x, sizeof(s))
#define all(s) s.begin(),s.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int N = 500010;

int n, k;
int pre[N], ne[N];
LL cnt[N];
void solve()
{
    
    
	cin >> n >> k;
	priority_queue<pair<LL, int>, vector<pair<LL, int>>, greater<pair<LL, int>> >q;
	for (int i = 1; i <= n; ++i) {
    
    
		LL v;
		cin >> v;
		q.push({
    
    v, i});
		pre[i] = i - 1;
		ne[i] = i + 1;
	}
	int g = n - k;
	while (q.size() > g) {
    
    
		auto p = q.top(); q.pop();
		LL v = p.first, ix = p.second;
		if (cnt[ix]) {
    
    
			q.push({
    
    v + cnt[ix], ix});
			cnt[ix] = 0;
		} else {
    
    
			int l = pre[ix], r = ne[ix];
			cnt[l] += v;
			cnt[r] += v;
			ne[l] = r;
			pre[r] = l;
		}
	}
	std::vector<LL> a(n + 1);
	for (int i = 0; i < g; ++i) {
    
    
		auto p = q.top(); q.pop();
		a[p.second] = p.first + cnt[p.second];
	}
	for (int i = 1; i <= n; ++i) {
    
    
		if (a[i]) cout << a[i] << " ";
	}
}
int main()
{
    
    
	ios_base :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t = 1;
	while (t--)
	{
    
    
		solve();
	}
	return 0;
}

Question I. Scenic Tourism

1. Description of the topic

   A scenic spot has a total of NNN attractions, numbered1 11 toNNN. _ There are N − 1 N − 1among the attractionsNA two-way shuttle bus line is connected to form a tree structure. Traveling between scenic spots can only be done by these shuttle buses, which will take a certain amount of time. Xiao Ming is a senior tour guide of this scenic spot. He takes guests to visit KK
   in a fixed order every day.K attractions:A 1 , A 2 , . . . , AK A_1, A_2, . . . , A_KA1,A2,...,AK. Today, due to time constraints, Xiao Ming decided to skip one of the scenic spots and only take tourists to visit K − 1 K − 1 in orderK1 attraction. Specifically, if Xiao Ming chooses to skipA i A_iAi, A 1 , A 2 , ... , A i − 1 , A i + 1 , ... , AK , ( 1 ≤ i ≤ K ) A_1, A_2, ... , A_{i−1}, A_{i+1}, . . . , A_K, (1 ≤ i ≤ K)A1,A2,...,Ai1,Ai+1,...,AK,(1iK ) .
   Please tell me about anyA i AiA i , calculate how much time Xiao Ming needs to spend on the shuttle bus between attractions if he skips this attraction?

2. Problem-solving ideas

   L C A LCA For the L C A template question (the problem is that the competition cannot write the board), you must first consider finding the distance between any two points on the tree. set pointu , vu,vu,The nearest common ancestor of v is zzz , definef [ i ] f[i]f [ i ] is pointiii to the root node, then there is a formula:
dist ( u , v ) = f [ u ] + f [ v ] − 2 ∗ f [ z ] dist(u,v)=f[u]+f[v ]-2*f[z]dist(u,v)=f[u]+f[v]2f [ z ]
   first calculate the distance that needs to be walked when not skipping any pointansand the distance between any two adjacent jump points. Suppose we skip four points area , b , c , da,b,c,da,b,c,d . When the skipped point isaaa , we only need toanssubtractaaa tobbThe distance of b , when the skipped point isddd , we only need toanssubtractccc toddThe distance of d , this is the case of the first and last two points.
   So what if the skipped point is the middle point? For example, what we skip isbbb , then you need toanssubtractaaa tobbb andbbb toccThe distance of c , and also need to addaaa toccc , the rest of the intermediate points are treated in the same way.

3. Template code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
#define pb(s) push_back(s);
#define SZ(s) ((int)s.size());
#define ms(s,x) memset(s, x, sizeof(s))
#define all(s) s.begin(),s.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int N = 200010;

int n, m;
std::vector<PII> e[N];
int depth[N], fa[N][32];
LL f[N];
int root;
void bfs(int root)
{
    
    
	memset(depth, 0x3f, sizeof depth);
	depth[0] = 0, depth[root] = 1;
	queue<int> q;
	q.push(root);
	while (!q.empty()) {
    
    
		auto t = q.front();
		q.pop();
		for (auto [j, c] : e[t]) {
    
    
			if (depth[j] > depth[t] + 1) {
    
    
				depth[j] = depth[t] + 1;
				q.push(j);
				fa[j][0] = t;
				for (int k = 1; k <= 15; k++) {
    
    
					fa[j][k] = fa[fa[j][k - 1]][k - 1];
				}
			}
		}
	}
}
void dfs(int u, int fa) {
    
    
	for (auto [v, c] : e[u]) {
    
    
		if (v == fa) continue;
		f[v] = f[u] + c;
		dfs(v, u);
	}
}
int lca(int a, int b) {
    
    
	if (depth[a] < depth[b]) swap(a, b);
	for (int k = 15; k >= 0; k--) {
    
    
		if (depth[fa[a][k]] >= depth[b]) {
    
    
			a = fa[a][k];
		}
	}
	if (a == b) return a;
	for (int k = 15; k >= 0; --k) {
    
    
		if (fa[a][k] != fa[b][k]) {
    
    
			a = fa[a][k];
			b = fa[b][k];
		}
	}
	return fa[a][0];
}
void solve()
{
    
    
	cin >> n >> m;
	for (int i = 0; i < n - 1; ++i) {
    
    
		int u, v , c;
		cin >> u >> v >> c;
		e[u].push_back({
    
    v, c});
		e[v].push_back({
    
    u, c});
	}
	bfs(1);
	dfs(1, -1);
	std::vector<LL> g(m + 1), w(m + 1);
	for (int i = 1; i <= m; ++i) cin >> g[i];
	LL ans = 0;
	for (int i = 1; i < m; ++i) {
    
    
		int u = g[i], v = g[i + 1];
		int z = lca(u, v);
		w[i] = f[u] + f[v] - 2 * f[z];
		ans += w[i];
	}
	for (int i = 1; i <= m; ++i) {
    
    
		if (i == 1) cout << ans - w[1] << " ";
		else if (i == m) cout << ans - w[m - 1] << " ";
		else {
    
    
			LL res = ans - w[i] - w[i - 1];
			int u = g[i - 1], v = g[i + 1];
			int z = lca(u, v);
			res += f[u] + f[v] - 2 * f[z];
			cout << res << " ";
		}
	}
}
int main()
{
    
    
	ios_base :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t = 1;
	while (t--)
	{
    
    
		solve();
	}
	return 0;
}

Question J. Cutting Trees

1. Description of the topic

   given a tree by nnA tree composed of n nodes andmmm non-repeating random number pairs( a 1 , b 1 ) , ( a 2 , b 2 ) , . . . , ( am , bm ) (a1, b1), (a_2, b_2),. . . , ( a_m, b_m)( a 1 ,b 1 ) ,(a2,b2),...,(am,bm) , among whichai aiai are different from each other,bi b_ibidifferent from each other, ai , bj ( 1 ≤ i , j ≤ m ) a_i , b_j(1 ≤ i, j ≤ m)ai,bj(1i,jm ) .
   Xiao Ming wants to know whether it is possible to choose a tree edge to cut, so that for each( ai , bi ) (ai , bi)(ai,bi ) fullai aiai sumbi bibi is not connected, if possible, output the number of the edge that should be broken (the number is from1 to 11 ), otherwise output− 1 -11

2. Problem-solving ideas

LCA LCA    AgainL C A template question, but I have never done it before (I can’t writeLCA LCALC A ) . Consider a pair of unordered pairs of pointsxxxyyy , if we cut off an edge to make these two points disconnected, then this edge must be fromxxxyyA point on the y path, we can make from xxxyyThe edge weights of the y1 path are added . We can usetree difference for this operation. formmm unordered numbers operate in this way for us, and finally if the weight of a certain edge ismmm means that it meets the conditions. We select the side with the largest number that meets the conditions as the answer. If there is no weight, the value ismmThe edge of m means that there is no solution.

3. Template code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> PII;
#define pb(s) push_back(s);
#define SZ(s) ((int)s.size());
#define ms(s,x) memset(s, x, sizeof(s))
#define all(s) s.begin(),s.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int N = 200010;

int n, m;
std::vector<int> e[N];
int depth[N], fa[N][32];
int f[N];
int root;
int ans;
map<PII, int> mp;
void bfs(int root)
{
    
    
	ms(depth, 0x3f);
	depth[0] = 0, depth[root] = 1;
	queue<int> q;
	q.push(root);
	while (!q.empty()) {
    
    
		auto t = q.front();
		q.pop();
		for (int j : e[t]) {
    
    
			if (depth[j] > depth[t] + 1) {
    
    
				depth[j] = depth[t] + 1;
				q.push(j);
				fa[j][0] = t;
				for (int k = 1; k <= 15; k++) {
    
    
					fa[j][k] = fa[fa[j][k - 1]][k - 1];
				}
			}
		}
	}
}
int lca(int a, int b) {
    
    
	if (depth[a] < depth[b]) swap(a, b);
	for (int k = 15; k >= 0; k--) {
    
    
		if (depth[fa[a][k]] >= depth[b]) {
    
    
			a = fa[a][k];
		}
	}
	if (a == b) return a;
	for (int k = 15; k >= 0; --k) {
    
    
		if (fa[a][k] != fa[b][k]) {
    
    
			a = fa[a][k];
			b = fa[b][k];
		}
	}
	return fa[a][0];
}
int dfs(int u, int fa) {
    
    
	int res = f[u];
	for (auto v : e[u]) {
    
    
		if (v == fa) continue;
		int g = dfs(v, u);
		if (g == m) {
    
    
			ans = max(ans, mp[ {
    
    v, u}]);
		}
		res += g;
	}
	return res;
}
void solve()
{
    
    
	cin >> n >> m;
	for (int i = 0; i < n - 1; ++i) {
    
    
		int u, v;
		cin >> u >> v;
		mp[ {
    
    u, v}] = mp[ {
    
    v, u}] = i + 1;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	bfs(1);
	for (int i = 0; i < m; ++i) {
    
    
		int u, v;
		cin >> u >> v;
		int z = lca(u, v);
		f[u]++;
		f[v]++;
		f[z] -= 2;
	}
	dfs(1, -1);
	cout << (ans == 0 ? -1 : ans) << '\n';
}
int main()
{
    
    
	ios_base :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t = 1;
	while (t--)
	{
    
    
		solve();
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_57487901/article/details/130036113