Codeforces Round #547 (Div. 3)

A. Game 23

Description:

Polycarp plays “Game 23”. Initially he has a number n n n and his goal is to transform it to m m m. In one move, he can multiply n n n by 2 2 2 or multiply n n n by 3 3 3. He can perform any number of moves.
Print the number of moves needed to transform n n n to m m m. Print -1 if it is impossible to do so.
It is easy to prove that any way to transform n n n to m m m contains the same number of moves (i.e. number of moves doesn’t depend on the way of transformation).

Input:

The only line of the input contains two integers n n n and m m m ( 1 n m 5 1 0 8 1 \le n \le m \le 5\cdot10^8 1nm5108).

Output

Print the number of moves to transform n n n to m m m, or -1 if there is no solution.

Sample Input:

120 51840

Sample Output:

7

Sample Input:

42 42

Sample Output:

0

Sample Input:

48 72

Sample Output:

-1

题目链接

询问 n n n 是否能够通过 × 2 \times 2 ×2 × 3 \times 3 ×3 变为 m m m

我这里赛时 5 m i n 5min 5min 写了一个从 n n n m m m 的广搜,其实可以从 m m m 先不断 ÷ 2 \div 2 ÷2 再不断 ÷ 3 \div 3 ÷3 来判断

AC代码:

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

int main() {
  ios::sync_with_stdio(false); cin.tie(0);
  long long n, m; cin >> n >> m;
  if (n > m) swap(n, m);
  else if (n == m) {
    cout << 0 << endl;
    return 0;
  }
  queue<pair<long long, int>> que;
  que.push(make_pair(n, 0));
  while (!que.empty()) {
    pair<long long, int> cur = que.front(); que.pop();
    if (cur.first == m) {
      cout << cur.second << endl;
      return 0;
    }
    if (cur.first * 2 <= m) que.push(make_pair(cur.first * 2, cur.second + 1));
    if (cur.first * 3 <= m) que.push(make_pair(cur.first * 3, cur.second + 1));
  }
  cout << -1 << endl;
  return 0;
}

B. Maximal Continuous Rest

Description:

Each day in Berland consists of n n n hours. Polycarp likes time management. That’s why he has a fixed schedule for each day — it is a sequence a 1 , a 2 , , a n a_1, a_2, \dots, a_n a1,a2,,an (each a i a_i ai is either 0 0 0 or 1 1 1), where a i = 0 a_i=0 ai=0 if Polycarp works during the i i i-th hour of the day and a i = 1 a_i=1 ai=1 if Polycarp rests during the i i i-th hour of the day.
Days go one after another endlessly and Polycarp uses the same schedule for each day.
What is the maximal number of continuous hours during which Polycarp rests? It is guaranteed that there is at least one working hour in a day.

Input:

The first line contains n n n ( 1 n 2 1 0 5 1 \le n \le 2\cdot10^5 1n2105) — number of hours per day.
The second line contains n n n integer numbers a 1 , a 2 , , a n a_1, a_2, \dots, a_n a1,a2,,an ( 0 a i 1 0 \le a_i \le 1 0ai1), where a i = 0 a_i=0 ai=0 if the i i i-th hour in a day is working and a i = 1 a_i=1 ai=1 if the i i i-th hour is resting. It is guaranteed that a i = 0 a_i=0 ai=0 for at least one i i i.

Output

Print the maximal number of continuous hours during which Polycarp rests. Remember that you should consider that days go one after another endlessly and Polycarp uses the same schedule for each day.

Sample Input:

5
1 0 1 0 1

Sample Output:

2

Sample Input:

6
0 1 0 1 1 0

Sample Output:

2

Sample Input:

7
1 0 1 1 1 0 1

Sample Output:

3

Sample Input:

3
0 0 0

Sample Output:

0

题目链接

给出每天 n n n 个小时的作息表(休息或工作), 求连续多天(不间断衔接)的最长休息时间

把数组扩大 2 2 2 倍直接找最大区间即可

AC代码:

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

int main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int n; cin >> n;
  vector<int> a(n * 2, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[n + i] = a[i];
  }
  int ans = 0, cnt = 0;
  for (int i = 0; i < 2 * n; ++i) {
    if (a[i] == 1) cnt++;
    else cnt = 0;
    ans = max(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}

C. Polycarp Restores Permutation

Description:

An array of integers p 1 , p 2 , , p n p_1, p_2, \dots, p_n p1,p2,,pn is called a permutation if it contains each number from 1 1 1 to n n n exactly once. For example, the following arrays are permutations: [ 3 , 1 , 2 ] [3, 1, 2] [3,1,2], [ 1 ] [1] [1], [ 1 , 2 , 3 , 4 , 5 ] [1, 2, 3, 4, 5] [1,2,3,4,5] and [ 4 , 3 , 1 , 2 ] [4, 3, 1, 2] [4,3,1,2]. The following arrays are not permutations: [ 2 ] [2] [2], [ 1 , 1 ] [1, 1] [1,1], [ 2 , 3 , 4 ] [2, 3, 4] [2,3,4].
Polycarp invented a really cool permutation p 1 , p 2 , , p n p_1, p_2, \dots, p_n p1,p2,,pn of length n n n. It is very disappointing, but he forgot this permutation. He only remembers the array q 1 , q 2 , , q n 1 q_1, q_2, \dots, q_{n-1} q1,q2,,qn1 of length n 1 n-1 n1, where q i = p i + 1 p i q_i=p_{i+1}-p_i qi=pi+1pi.
Given n n n and q = q 1 , q 2 , , q n 1 q=q_1, q_2, \dots, q_{n-1} q=q1,q2,,qn1, help Polycarp restore the invented permutation.

Input:

The first line contains the integer n n n ( 2 n 2 1 0 5 2 \le n \le 2\cdot10^5 2n2105) — the length of the permutation to restore. The second line contains n 1 n-1 n1 integers q 1 , q 2 , , q n 1 q_1, q_2, \dots, q_{n-1} q1,q2,,qn1 ( n &lt; q i &lt; n -n &lt; q_i &lt; n n<qi<n).

Output

Print the integer -1 if there is no such permutation of length n n n which corresponds to the given array q q q. Otherwise, if it exists, print p 1 , p 2 , , p n p_1, p_2, \dots, p_n p1,p2,,pn. Print any such permutation if there are many of them.

Sample Input:

3
-2 1

Sample Output:

3 1 2

Sample Input:

5
1 1 1 1

Sample Output:

1 2 3 4 5

Sample Input:

4
-1 2 2

Sample Output:

-1

题目链接

给出 n n n 个不重复数字 a i [ 1 , n ] a_{i}\in[1,n] ai[1,n] 某个排列的差分数组,求此排列

假设第一位为 0 0 0 ,利用差分数字求出每位数将其最小值改变为 1 1 1 ,再把所求数列所有数字加上最小值的改变量输出(注意特判不合法情况),需注意这里不合法情况会有溢出 i n t int int 数据范围的情况

赛后看到网友还有一种更巧妙的解法,设第一位为 1 1 1 利用差分数组求出排列之后对所求数组进行排序再判断

AC代码:

#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f;

int main() {
  ios::sync_with_stdio(false); cin.tie(0);
  long long n; cin >> n;
  vector<long long> q(n - 1, 0);
  for (auto &it : q) cin >> it;
  vector<long long> p(n, 0);
  set<long long> vis; vis.insert(0);
  long long _max = 0, _min = 0;
  for (int i = 1; i < n; ++i) {
    p[i] = p[i - 1] + q[i - 1];
    _max = max(_max, p[i]);
    _min = min(_min, p[i]);
    if (vis.find(p[i]) == vis.end()) vis.insert(p[i]);
    else {
      cout << -1 << endl;
      return 0;
    }
  }
  long long diff = abs(1 - _min);
  if (_max + diff > n) {
    cout << -1 << endl;
    return 0;
  }
  for (auto &it : p) cout << it + diff << " ";
  cout << endl;
  return 0;
}

D. Colored Boots

Description:

There are n n n left boots and n n n right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark (’?’). Thus, you are given two strings l l l and r r r, both of length n n n. The character l i l_i li stands for the color of the i i i-th left boot and the character r i r_i ri stands for the color of the i i i-th right boot.
A lowercase Latin letter denotes a specific color, but the question mark (’?’) denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.
For example, the following pairs of colors are compatible: (‘f’, ‘f’), (’?’, ‘z’), (‘a’, ‘?’) and (’?’, ‘?’). The following pairs of colors are not compatible: (‘f’, ‘g’) and (‘a’, ‘z’).
Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.
Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair.

Input:

The first line contains n n n ( 1 n 150000 1 \le n \le 150000 1n150000), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots).
The second line contains the string l l l of length n n n. It contains only lowercase Latin letters or question marks. The i i i-th character stands for the color of the i i i-th left boot.
The third line contains the string r r r of length n n n. It contains only lowercase Latin letters or question marks. The i i i-th character stands for the color of the i i i-th right boot.

Output

Print k k k — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors.
The following k k k lines should contain pairs a j , b j a_j, b_j aj,bj ( 1 a j , b j n 1 \le a_j, b_j \le n 1aj,bjn). The j j j-th of these lines should contain the index a j a_j aj of the left boot in the j j j-th pair and index b j b_j bj of the right boot in the j j j-th pair. All the numbers a j a_j aj should be distinct (unique), all the numbers b j b_j bj should be distinct (unique).
If there are many optimal answers, print any of them.

Sample Input:

10
codeforces
dodivthree

Sample Output:

5
7 8
4 9
2 2
9 10
3 1

Sample Input:

7
abaca?b
zabbbcc

Sample Output:

5
6 5
2 3
4 6
7 4
1 2

Sample Input:

9
bambarbia
hellocode

Sample Output:

0

Sample Input:

10
code???
???test

Sample Output:

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

题目链接

两个字符串相同字符可以匹配,问号可以和任意字符匹配,求最大匹配方案

模拟,优先进行相同字符匹配,之后分别进行问号与字符匹配,最后再对余下问号进行两两匹配

AC代码:

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

int main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int n; cin >> n;
  vector<char> l(n), r(n);
  vector<int> _l, _r;
  vector<vector<int>> c(26);
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    cin >> l[i];
    if (l[i] == '?') _l.emplace_back(i + 1);
    else {
      c[l[i] - 'a'].emplace_back(i + 1);
      cnt++;
    }
  }
  vector<pair<int, int>> ans;
  vector<int> last;
  for (int i = 0; i < n; ++i) {
    cin >> r[i];
    if (r[i] == '?') _r.emplace_back(i + 1);
    else {
      if (!c[r[i] - 'a'].empty()) {
        ans.emplace_back(make_pair(c[r[i] - 'a'].back(), i + 1));
        c[r[i] - 'a'].pop_back();
        cnt--;
      }
      else last.emplace_back(i + 1);
    }
  }
  while (!_l.empty() && !last.empty()) {
    ans.emplace_back(make_pair(_l.back(), last.back()));
    _l.pop_back(); last.pop_back();
  }
  while (cnt > 0 && !_r.empty()) {
    for (int i = 0; i < 26; ++i) {
      while (!c[i].empty()) {
        ans.emplace_back(make_pair(c[i].back(), _r.back()));
        c[i].pop_back(); _r.pop_back();
        cnt--;
        if (cnt <= 0 || _r.empty()) break;
      }
      if (cnt <= 0 || _r.empty()) break;
    }
  }
  while (!_l.empty() && !_r.empty()) {
    ans.emplace_back(make_pair(_l.back(), _r.back()));
    _l.pop_back(); _r.pop_back();
  }
  cout << ans.size() << endl;
  for (auto &it : ans) cout << it.first << " " << it.second << endl;
  return 0;
}

E. Superhero Battle

Description:

A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly n n n minutes. After a round ends, the next round starts immediately. This is repeated over and over again.
Each round has the same scenario. It is described by a sequence of n n n numbers: d 1 , d 2 , , d n d_1, d_2, \dots, d_n d1,d2,,dn ( 1 0 6 d i 1 0 6 -10^6 \le d_i \le 10^6 106di106). The i i i-th element means that monster’s hp (hit points) changes by the value d i d_i di during the i i i-th minute of each round. Formally, if before the i i i-th minute of a round the monster’s hp is h h h, then after the i i i-th minute it changes to h : = h + d i h := h + d_i h:=h+di.
The monster’s initial hp is H H H. It means that before the battle the monster has H H H hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to 0 0 0. Print -1 if the battle continues infinitely.

Input:

The first line contains two integers H H H and n n n ( 1 H 1 0 12 1 \le H \le 10^{12} 1H1012, 1 n 2 1 0 5 1 \le n \le 2\cdot10^5 1n2105). The second line contains the sequence of integers d 1 , d 2 , , d n d_1, d_2, \dots, d_n d1,d2,,dn ( 1 0 6 d i 1 0 6 -10^6 \le d_i \le 10^6 106di106), where d i d_i di is the value to change monster’s hp in the i i i-th minute of a round.

Output

Print -1 if the superhero can’t kill the monster and the battle will last infinitely. Otherwise, print the positive integer k k k such that k k k is the first minute after which the monster is dead.

Sample Input:

1000 6
-100 -200 -300 125 77 -4

Sample Output:

9

Sample Input:

1000000000000 5
-1 0 0 0 0

Sample Output:

4999999999996

Sample Input:

10 4
-3 -6 5 4

Sample Output:

-1

题目链接

一个怪物初始有 h h h 的血量,每回合 n n n 分钟,每分钟后怪物血量变化为 h n o w = h l a s t + a i ( i [ 1 , n ] ) h_{now}=h_{last}+a_{i}(i\in[1,n]) hnow=hlast+ai(i[1,n]) ,求打败怪物的最短时间

特判无法打败怪兽的情况

枚举怪物死亡节点,利用周期性求出回合数取时间最小值即可

AC代码:

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

int main() {
  ios::sync_with_stdio(false); cin.tie(0);
  long long h, n; cin >> h >> n;
  vector<long long> d(n);
  long long cur = h, sum = 0, book = 0;
  for (int i = 0; i < n; ++i) {
    cin >> d[i];
    cur += d[i];
    sum += d[i];
    if (cur <= 0) {
      book = i + 1;
      break;
    }
  }
  if (book) {
    cout << book << endl;
    return 0;
  }
  else if (sum >= 0) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = 1e18 + 5;
  for (int i = 0; i < n; ++i) {
    cur += d[i];
    long long cnt = (cur / (-sum)) * n;
    if (cur + (cnt / n) * sum <= 0) ans = min(ans, n + cnt + i + 1);
    else if (cur + ((cnt / n) + 1) * sum <= 0) ans = min(ans, n + cnt + n + i + 1);
  }
  cout << ans << endl;
  return 0;
}

F. Same Sum Blocks

Description:

This problem is given in two editions, which differ exclusively in the constraints on the number n n n.
You are given an array of integers a [ 1 ] , a [ 2 ] , , a [ n ] . a[1], a[2], \dots, a[n]. a[1],a[2],,a[n]. A block is a sequence of contiguous (consecutive) elements a [ l ] , a [ l + 1 ] , , a [ r ] a[l], a[l+1], \dots, a[r] a[l],a[l+1],,a[r] ( 1 l r n 1 \le l \le r \le n 1lrn). Thus, a block is defined by a pair of indices ( l , r ) (l, r) (l,r).
Find a set of blocks ( l 1 , r 1 ) , ( l 2 , r 2 ) , , ( l k , r k ) (l_1, r_1), (l_2, r_2), \dots, (l_k, r_k) (l1,r1),(l2,r2),,(lk,rk) such that:

  • They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks ( l i , r i ) (l_i, r_i) (li,ri) and ( l j , r j (l_j, r_j (lj,rj) where i j i \neq j i̸=j either r i &lt; l j r_i &lt; l_j ri<lj or r j &lt; l i r_j &lt; l_i rj<li.
  • For each block the sum of its elements is the same. Formally, a [ l 1 ] + a [ l 1 + 1 ] + + a [ r 1 ] = a [ l 2 ] + a [ l 2 + 1 ] + + a [ r 2 ] = a[l_1]+a[l_1+1]+\dots+a[r_1]=a[l_2]+a[l_2+1]+\dots+a[r_2]= a[l1]+a[l1+1]++a[r1]=a[l2]+a[l2+1]++a[r2]= = \dots = = a [ l k ] + a [ l k + 1 ] + + a [ r k ] . a[l_k]+a[l_k+1]+\dots+a[r_k]. a[lk]+a[lk+1]++a[rk].
  • The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks ( l 1 , r 1 ) , ( l 2 , r 2 ) , , ( l k , r k ) (l_1&#x27;, r_1&#x27;), (l_2&#x27;, r_2&#x27;), \dots, (l_{k&#x27;}&#x27;, r_{k&#x27;}&#x27;) (l1,r1),(l2,r2),,(lk,rk) satisfying the above two requirements with k &gt; k k&#x27; &gt; k k>k. The picture corresponds to the first example. Blue boxes illustrate blocks.

The picture corresponds to the first example. Blue boxes illustrate blocks.

Write a program to find such a set of blocks.

Input:

The first line contains integer n n n E a s y : Easy: Easy: ( 1 n 50 1 \le n \le 50 1n50) H a r d : Hard: Hard: ( 1 n 1500 1 \le n \le 1500 1n1500) — the length of the given array. The second line contains the sequence of elements a [ 1 ] , a [ 2 ] , , a [ n ] a[1], a[2], \dots, a[n] a[1],a[2],,a[n] ( 1 0 5 a i 1 0 5 -10^5 \le a_i \le 10^5 105ai105).

Output

In the first line print the integer k k k ( 1 k n 1 \le k \le n 1kn). The following k k k lines should contain blocks, one per line. In each line print a pair of indices l i , r i l_i, r_i li,ri ( 1 l i r i n 1 \le l_i \le r_i \le n 1lirin) — the bounds of the i i i-th block. You can print blocks in any order. If there are multiple answers, print any of them.

Sample Input:

7
4 1 2 2 1 5 3

Sample Output:

3
7 7
2 3
4 5

Sample Input:

11
-5 -4 -3 -2 -1 0 1 2 3 4 5

Sample Output:

2
3 4
1 1

Sample Input:

4
1 1 1 1

Sample Output:

4
4 4
1 1
2 2
3 3

题目链接

给出一组 n n n 个元素的数列,求连续不相交区间和相同的最大区间数及其区间

首先 O ( n 2 ) O(n^{2}) O(n2) 地提取每个区间内的和并将区间按照其区间和进行分类

之后对每种和情况贪心地取最多的区间(按照右端点升序排序取不相交区间)

最后输出最多区间数及其方案即可

AC代码:

#include <bits/stdc++.h>

struct seg {int l, r;};
bool operator < (seg k1, seg k2) {
  if (k1.r == k2.r) return k1.l < k2.l;
  return k1.r < k2.r;
}

int main() {
  std::ios::sync_with_stdio(false); std::cin.tie(0);

  int n; std::cin >> n;
  std::vector<int> a(n);
  for (auto &it : a) std::cin >> it;

  std::map<int, std::set<seg>> mp;
  for (int l = 0; l < n; ++l) {
    int sum = 0;
    for (int r = l; r < n; ++r) {
      sum += a[r];
      mp[sum].insert((seg){l + 1, r + 1});
    }
  }

  std::vector<seg> ans, temp;
  for (auto &i : mp) {
    temp.clear();
    for (auto &j : i.second) {
      if (j.l > (temp.empty() ? 0 : temp.back().r)) temp.emplace_back(j);
    }
    if (temp.size() > ans.size()) std::swap(ans, temp);
  }

  std::cout << ans.size() << '\n';
  for (auto &it : ans) std::cout << it.l << " " << it.r << '\n';
  return 0;
}

G. Privatization of Roads in Treeland

Description:

Treeland consists of n n n cities and n 1 n-1 n1 roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right — the country’s topology is an undirected tree.
There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads.
The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn’t exceed k k k and the number of companies taking part in the privatization is minimal.
Choose the number of companies r r r such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most k k k. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal r r r that there is such assignment to companies from 1 1 1 to r r r that the number of cities which are not good doesn’t exceed k k k.

The picture illustrates the first example ( n = 6 , k = 2 n=6, k=2 n=6,k=2). The answer contains r = 2 r=2 r=2 companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number 3 3 3) is not good. The number of such vertices (just one) doesn’t exceed k = 2 k=2 k=2. It is impossible to have at most k = 2 k=2 k=2 not good cities in case of one company.

Input:

The first line contains two integers n n n and k k k ( 2 n 200000 , 0 k n 1 2 \le n \le 200000, 0 \le k \le n - 1 2n200000,0kn1) — the number of cities and the maximal number of cities which can have two or more roads belonging to one company.
The following n 1 n-1 n1 lines contain roads, one road per line. Each line contains a pair of integers x i x_i xi, y i y_i yi ( 1 x i , y i n 1 \le x_i, y_i \le n 1xi,yin), where x i x_i xi, y i y_i yi are cities connected with the i i i-th road.

Output

In the first line print the required r r r ( 1 r n 1 1 \le r \le n - 1 1rn1). In the second line print n 1 n-1 n1 numbers c 1 , c 2 , , c n 1 c_1, c_2, \dots, c_{n-1} c1,c2,,cn1 ( 1 c i r 1 \le c_i \le r 1cir), where c i c_i ci is the company to own the i i i-th road. If there are multiple answers, print any of them.

Sample Input:

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

Sample Output:

2
1 2 1 1 2

Sample Input:

4 2
3 1
1 4
1 2

Sample Output:

1
1 1 1

Sample Input:

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

Sample Output:

3
1 1 2 3 2 3 1 3 1

题目链接

对一棵树的边进行染色,若一个节点有两条或以上相同颜色的边则此节点不合法,求在节点不合法数量最大为 k k k 的情况下所需的最少染色数及其染色方案

显然优先使度数大的节点不合法,利用尽可能的 k k k 的此数之后合法节点的最大度数(大于此度数的节点在染色时均被染色为不合法,其余节点均可染色为合法)即为所需的染色数

之后对树进行 d f s dfs dfs 并对边进行周期循环染色即可

AC代码:

#include <bits/stdc++.h>

int n, k;
std::vector<std::vector<int>> g;
std::map<std::pair<int, int>, int> id;
int ans;
std::vector<int> col;

void Dfs(int cur, int pre, int ban) {
  int color = 0;
  for (auto v : g[cur]) {
    if (v == pre) continue;
    if (color == ban) {
      color = (color + 1) % ans;
      ban = -1;
    }
    col[id[{cur, v}]] = color;
    Dfs(v, cur, color);
    color = (color + 1) % ans;
  }
}

int main() {
  std::ios::sync_with_stdio(false); std::cin.tie(0);

  std::cin >> n >> k;
  g.resize(n);
  std::vector<int> deg(n, 0);
  for (int i = 0, u, v; i < n - 1; ++i) {
    std::cin >> u >> v;
    --u; --v;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
    ++deg[u]; ++deg[v];
    id[{u, v}] = i; id[{v, u}] = i;
  }

  std::map<int, int> deg_cnt;
  for (auto &it : deg) deg_cnt[it]++;
  int cnt = n;
  for (auto &it : deg_cnt) {
    if (cnt > k) {
      ans = it.first;
      cnt -= it.second;
    }
    else break;
  }

  col.resize(n - 1);
  Dfs(0, -1, -1);

  std::cout << ans << '\n';
  for (auto &it : col) std::cout << it + 1 << " ";
  return 0;
}
全部评论

相关推荐

Bug压路:老哥看得出来你是想多展示一些项目,但好像一般最多两个就够了😂页数一般一页,多的也就2页;这些项目应该是比较同质化的,和评论区其他大佬一样,我也觉得应该展示一些最拿手的(质量>数量)😁😁😁专业技能部分也可以稍微精简一些
点赞 评论 收藏
分享
gcniz:一天写两千行你闹呢
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务