Codeforces Round 886 (Div. 4)


A. To My Critics

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Suneet has three digits a a a, b b b, and c c c.

Since math isn’t his strongest point, he asks you to determine if you can choose any two digits to make a sum greater or equal to 10 10 10.

Output “YES” if there is such a pair, and “NO” otherwise.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) — the number of test cases.

The only line of each test case contains three digits a a a, b b b, c c c ( 0 ≤ a , b , c ≤ 9 0 \leq a, b, c \leq 9 0a,b,c9).

Output

For each test case, output “YES” if such a pair exists, and “NO” otherwise.

You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer).

Example
input

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

output

YES
NO
YES
NO
YES

Note

For the first test case, by choosing the digits 8 8 8 and 2 2 2 we can obtain a sum of 8 + 2 = 10 8 + 2 = 10 8+2=10 which satisfies the condition, thus the output should be “YES”.

For the second test case, any combination of chosen digits won’t be at least 10 10 10, thus the output should be “NO” (note that we can not choose the digit on the same position twice).

For the third test case, any combination of chosen digits will have a sum equal to 18 18 18, thus the output should be “YES”.

Tutorial
每个样例三个正数,三个数最大的两个数小于 10 就输出 NO, 反之输出 YES

Solution

for _ in range(int(input())):
    a = list(map(int, input().split()))
    print("YES" if sum(a) - min(a) >= 10 else "NO")

B. Ten Words of Wisdom

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

In the game show “Ten Words of Wisdom”, there are n n n participants numbered from 1 1 1 to n n n, each of whom submits one response. The i i i-th response is a i a_i ai words long and has quality b i b_i bi. No two responses have the same quality, and at least one response has length at most 10 10 10.

The winner of the show is the response which has the highest quality out of all responses that are not longer than 10 10 10 words. Which response is the winner?

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of test cases.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 50 1 \leq n \leq 50 1n50) — the number of responses.

Then n n n lines follow, the i i i-th of which contains two integers a i a_i ai and b i b_i bi ( 1 ≤ a i , b i ≤ 50 1 \leq a_i, b_i \leq 50 1ai,bi50) — the number of words and the quality of the i i i-th response, respectively.

Additional constraints on the input: in each test case, at least one value of i i i satisfies a i ≤ 10 a_i \leq 10 ai10, and all values of b i b_i bi are distinct.

Output

For each test case, output a single line containing one integer x x x ( 1 ≤ x ≤ n 1 \leq x \leq n 1xn) — the winner of the show, according to the rules given in the statement.

It can be shown that, according to the constraints in the statement, exactly one winner exists for each test case.

Example
input

3
5
7 2
12 5
9 3
9 4
10 1
3
1 2
3 4
5 6
1
1 43

output

4
3
1

Note

In the first test case, the responses provided are as follows:

  • Response 1: 7 7 7 words, quality 2 2 2
  • Response 2: 12 12 12 words, quality 5 5 5
  • Response 3: 9 9 9 words, quality 3 3 3
  • Response 4: 9 9 9 words, quality 4 4 4
  • Response 5: 10 10 10 words, quality 1 1 1

We can see that the responses with indices 1 1 1, 3 3 3, 4 4 4, and 5 5 5 have lengths not exceeding 10 10 10 words. Out of these responses, the winner is the one with the highest quality.

Comparing the qualities, we find that:

  • Response 1 has quality 2 2 2.
  • Response 3 has quality 3 3 3.
  • Response 4 has quality 4 4 4.
  • Response 5 has quality 1 1 1.

Among these responses, Response 4 has the highest quality.

Tutorial
每个样例 n 个回答,在所有 $ a \leq 10$ 的回答中,找出 b最大的是哪个回答

Solution

for _ in range(int(input())):
    n = int(input())
    ans, mx = 0, 0
    for i in range(n):
        a, b = map(int, input().split())
        if a <= 10 and b > mx:
            mx = b
            ans = i + 1
    print(ans)

C. Word on the Paper

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

On an 8 × 8 8 \times 8 8×8 grid of dots, a word consisting of lowercase Latin letters is written vertically in one column, from top to bottom. What is it?

Input

The input consists of multiple test cases. The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) — the number of test cases.

Each test case consists of 8 8 8 lines, each containing 8 8 8 characters. Each character in the grid is either . \texttt{.} . (representing a dot) or a lowercase Latin letter ( a \texttt{a} a z \texttt{z} z).

The word lies entirely in a single column and is continuous from the beginning to the ending (without gaps). See the sample input for better understanding.

Output

For each test case, output a single line containing the word made up of lowercase Latin letters ( a \texttt{a} a z \texttt{z} z) that is written vertically in one column from top to bottom.

Example
input

5
........
........
........
........
...i....
........
........
........
........
.l......
.o......
.s......
.t......
........
........
........
........
........
........
........
......t.
......h.
......e.
........
........
........
........
........
.......g
.......a
.......m
.......e
a.......
a.......
a.......
a.......
a.......
a.......
a.......
a.......

output

i
lost
the
game
aaaaaaaa

Tutorial
每个样例是一个 8 × 8 8 \times 8 8×8 的点阵,输入好点阵后从左至右,从上到下遍历每一个位置,依次输出所有字符,即不是点的字符

Solution

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

#define endl '\n'

char a[8][8];

void solve() {
    string ans = "";
    for (int i = 0; i < 8; ++i) {
        for (int j = 0; j < 8; ++j) {
            cin >> a[i][j];
        }
    }
    for (int j = 0; j < 8; ++j) {
        for (int i = 0; i < 8; ++i) {
            if (isalpha(a[i][j])) {
                ans += a[i][j];
            }
        }
    }
    cout << ans << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T; cin >> T; while (T--)
    solve();
    return 0;
}

D. Balanced Round

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

You are the author of a Codeforces round and have prepared n n n problems you are going to set, problem i i i having difficulty a i a_i ai. You will do the following process:

  • remove some (possibly zero) problems from the list;
  • rearrange the remaining problems in any order you wish.

A round is considered balanced if and only if the absolute difference between the difficulty of any two consecutive problems is at most k k k (less or equal than k k k).

What is the minimum number of problems you have to remove so that an arrangement of problems is balanced?

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) — the number of test cases.

The first line of each test case contains two positive integers n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105) and k k k ( 1 ≤ k ≤ 1 0 9 1 \leq k \leq 10^9 1k109) — the number of problems, and the maximum allowed absolute difference between consecutive problems.

The second line of each test case contains n n n space-separated integers a i a_i ai ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1ai109) — the difficulty of each problem.

Note that the sum of n n n over all test cases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single integer — the minimum number of problems you have to remove so that an arrangement of problems is balanced.

Example
input

7
5 1
1 2 4 5 6
1 2
10
8 3
17 3 1 20 12 5 17 12
4 2
2 4 6 8
5 3
2 3 19 10 8
3 4
1 10 5
8 1
8 3 1 4 5 10 7 3

output

2
0
5
0
3
1
4

Note

For the first test case, we can remove the first 2 2 2 problems and construct a set using problems with the difficulties [ 4 , 5 , 6 ] [4, 5, 6] [4,5,6], with difficulties between adjacent problems equal to ∣ 5 − 4 ∣ = 1 ≤ 1 |5 - 4| = 1 \leq 1 ∣54∣=11 and ∣ 6 − 5 ∣ = 1 ≤ 1 |6 - 5| = 1 \leq 1 ∣65∣=11.

For the second test case, we can take the single problem and compose a round using the problem with difficulty 10 10 10.

Tutorial
正难则反,由于可以按照我们想要的任何顺序排序,不妨直接将所有数字排序,寻找最长的字串满足相邻元素之间的差值小于 k,最后减去最长字串的长度就是要删去的字符个数

Solution

for _ in range(int(input())):
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    ans, cnt = 0, 1
    for i in range(1, n):
        if a[i] - a[i - 1] <= k:
            cnt += 1
        else:
            ans = max(ans, cnt)
            cnt = 1
    ans = max(ans, cnt)
    print(n - ans)

E. Cardboard for Pictures

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Mircea has n n n pictures. The i i i-th picture is a square with a side length of s i s_i si centimeters.

He mounted each picture on a square piece of cardboard so that each picture has a border of w w w centimeters of cardboard on all sides. In total, he used c c c square centimeters of cardboard. Given the picture sizes and the value c c c, can you find the value of w w w?

A picture of the first test case. Here c = 50 = 5 2 + 4 2 + 3 2 c = 50 = 5^2 + 4^2 + 3^2 c=50=52+42+32, so w = 1 w=1 w=1 is the answer.

Please note that the piece of cardboard goes behind each picture, not just the border.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \leq t \leq 1000 1t1000) — the number of test cases.

The first line of each test case contains two positive integers n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105) and c c c ( 1 ≤ c ≤ 1 0 18 1 \leq c \leq 10^{18} 1c1018) — the number of paintings, and the amount of used square centimeters of cardboard.

The second line of each test case contains n n n space-separated integers s i s_i si ( 1 ≤ s i ≤ 1 0 4 1 \leq s_i \leq 10^4 1si104) — the sizes of the paintings.

The sum of n n n over all test cases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Additional constraint on the input: Such an integer w w w exists for each test case.

Please note, that some of the input for some test cases won’t fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Output

For each test case, output a single integer — the value of w w w ( w ≥ 1 w \geq 1 w1) which was used to use exactly c c c squared centimeters of cardboard.

Example
input

10
3 50
3 2 1
1 100
6
5 500
2 2 2 2 2
2 365
3 4
2 469077255466389
10000 2023
10 635472106413848880
9181 4243 7777 1859 2017 4397 14 9390 2245 7225
7 176345687772781240
9202 9407 9229 6257 7743 5738 7966
14 865563946464579627
3654 5483 1657 7571 1639 9815 122 9468 3079 2666 5498 4540 7861 5384
19 977162053008871403
9169 9520 9209 9013 9300 9843 9933 9454 9960 9167 9964 9701 9251 9404 9462 9277 9661 9164 9161
18 886531871815571953
2609 10 5098 9591 949 8485 6385 4586 1064 5412 6564 8460 2245 6552 5089 8353 3803 3764

output

1
2
4
5
7654321
126040443
79356352
124321725
113385729
110961227

Note

The first test case is explained in the statement.

For the second test case, the chosen w w w was 2 2 2, thus the only cardboard covers an area of c = ( 2 ⋅ 2 + 6 ) 2 = 1 0 2 = 100 c = (2 \cdot 2 + 6)^2 = 10^2 = 100 c=(22+6)2=102=100 squared centimeters.

For the third test case, the chosen w w w was 4 4 4, which obtains the covered area c = ( 2 ⋅ 4 + 2 ) 2 × 5 = 1 0 2 × 5 = 100 × 5 = 500 c = (2 \cdot 4 + 2)^2 \times 5 = 10^2 \times 5 = 100 \times 5 = 500 c=(24+2)2×5=102×5=100×5=500 squared centimeters.

Tutorial
由于给定的正方形数量是未知的,纸板边框的宽度也是未知的,数据最大可达$ 10^{18} $,所以想要处理这么大的范围的同时还要找到精确的那个数,就需要用到二分答案来一次次向正确值逼近,直至最后找到正确的那个值,要注意在二分答案的过程中,一旦有一个超过 c 的值就要跳出这次二分,避免溢出,每个测试用例的复杂度为 O ( n log ⁡ ( 1 0 9 ) ) O(n \log(10^9)) O(nlog(109)) ,所以在具有某个常数因子的情况下等于 O ( n ) O(n) O(n) ,不会造成TLE。

Solution

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

#define endl '\n'
#define int long long

int n, c;

void solve() {
    cin >> n >> c;
    vector<int> s(n);
    int sum = 0;
    for (int i = 0; i < n; ++i) {
        cin >> s[i];
    }

    function<int(int)> check = [&](int mid) -> int {
        __int128 sum = 0;
        for (int i = 0; i < n; ++i) {
            if (s[i] + mid > 1e9) {
                return 1e18;
            }
            sum += (s[i] + mid) * (s[i] + mid);
        }
        if (sum > 1e18) {
            return 1e18;
        }
        return (int)(sum);
    };

    int left = 0, right = 1e9;
    while (left <= right) {
        int mid = (left + right) >> 1;
        if (check(mid) == c) {
            cout << mid / 2 << endl;
            return;
        } else if (check(mid) > c) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T; cin >> T; while (T--)
    solve();
    return 0;
}

F. We Were Both Children

time limit per test: 3 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

Mihai and Slavic were looking at a group of n n n frogs, numbered from 1 1 1 to n n n, all initially located at point 0 0 0. Frog i i i has a hop length of a i a_i ai.

Each second, frog i i i hops a i a_i ai units forward. Before any frogs start hopping, Slavic and Mihai can place exactly one trap in a coordinate in order to catch all frogs that will ever pass through the corresponding coordinate.

However, the children can’t go far away from their home so they can only place a trap in the first n n n points (that is, in a point with a coordinate between 1 1 1 and n n n) and the children can’t place a trap in point 0 0 0 since they are scared of frogs.

Can you help Slavic and Mihai find out what is the maximum number of frogs they can catch using a trap?

Input

The first line of the input contains a single integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1t100) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1n2105) — the number of frogs, which equals the distance Slavic and Mihai can travel to place a trap.

The second line of each test case contains n n n integers a 1 , … , a n a_1, \ldots, a_n a1,,an ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1ai109) — the lengths of the hops of the corresponding frogs.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case output a single integer — the maximum number of frogs Slavic and Mihai can catch using a trap.

Example
input

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

output

3
3
3
5
0
4
4

Note

In the first test case, the frogs will hop as follows:

  • Frog 1: 0 → 1 → 2 → 3 → 4 → ⋯ 0 \to 1 \to 2 \to 3 \to \mathbf{\color{red}{4}} \to \cdots 01234
  • Frog 2: 0 → 2 → 4 → 6 → 8 → ⋯ 0 \to 2 \to \mathbf{\color{red}{4}} \to 6 \to 8 \to \cdots 02468
  • Frog 3: 0 → 3 → 6 → 9 → 12 → ⋯ 0 \to 3 \to 6 \to 9 \to 12 \to \cdots 036912
  • Frog 4: 0 → 4 → 8 → 12 → 16 → ⋯ 0 \to \mathbf{\color{red}{4}} \to 8 \to 12 \to 16 \to \cdots 0481216
  • Frog 5: 0 → 5 → 10 → 15 → 20 → ⋯ 0 \to 5 \to 10 \to 15 \to 20 \to \cdots 05101520

Therefore, if Slavic and Mihai put a trap at coordinate 4 4 4, they can catch three frogs: frogs 1, 2, and 4. It can be proven that they can’t catch any more frogs.

In the second test case, Slavic and Mihai can put a trap at coordinate 2 2 2 and catch all three frogs instantly.

Tutorial
先记录 c n t i cnt_i cnti 每个跳跃距离有多少只青蛙🐸,再从 1 1 1 遍历到 n n n ,将每个 c n t i cnt_i cnti 添加到每个小于或等于 n 到倍数上,在遍历到过程中取最大值,时间复杂度为 O ( n l o g n ) O(nlogn) O(nlogn),注意:在 c o d e f o r c e s codeforces codeforces 上可能会在 unordered_map 上卡哈希导致超时,所以只能用 map 进行存储数据

Solution

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

#define endl '\n'
#define int long long

int n;

void solve() {
    cin >> n;
    int ans = 0;
    vector<int> a(n);
    map<int, int> mp1, mp2;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        mp1[a[i]]++;
    }
    for (auto [x, y] : mp1) {
        for (int i = x; i <= n; i += x) {
            mp2[i] += y;
        }
    }
    for (auto [_, y] : mp2) {
        ans = max(ans, y);
    }
    cout << ans << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T; cin >> T; while (T--)
    solve();
    return 0;
}

G. The Morning Star

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

A compass points directly toward the morning star. It can only point in one of eight directions: the four cardinal directions (N, S, E, W) or some combination (NW, NE, SW, SE). Otherwise, it will break.

The directions the compass can point.

There are n n n distinct points with integer coordinates on a plane. How many ways can you put a compass at one point and the morning star at another so that the compass does not break?

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104). The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \leq n \leq 2 \cdot 10^5 2n2105) — the number of points.

Then n n n lines follow, each line containing two integers x i x_i xi, y i y_i yi ( − 1 0 9 ≤ x i , y i ≤ 1 0 9 -10^9 \leq x_i, y_i \leq 10^9 109xi,yi109) — the coordinates of each point, all points have distinct coordinates.

It is guaranteed that the sum of n n n over all test cases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output a single integer — the number of pairs of points that don’t break the compass.

Example
input

5
3
0 0
-1 -1
1 1
4
4 5
5 7
6 9
10 13
3
-1000000000 1000000000
0 0
1000000000 -1000000000
5
0 0
2 2
-1 5
-1 10
2 11
3
0 0
-1 2
1 -2

output

6
2
6
8
0

Note

In the first test case, any pair of points won’t break the compass:

  • The compass is at ( 0 , 0 ) (0,0) (0,0), the morning star is at ( − 1 , − 1 ) (-1,-1) (1,1): the compass will point SW \text{SW} SW.
  • The compass is at ( 0 , 0 ) (0,0) (0,0), the morning star is at ( 1 , 1 ) (1,1) (1,1): the compass will point NE \text{NE} NE.
  • The compass is at ( − 1 , − 1 ) (-1,-1) (1,1), the morning star is at ( 0 , 0 ) (0,0) (0,0): the compass will point NE \text{NE} NE.
  • The compass is at ( − 1 , − 1 ) (-1,-1) (1,1), the morning star is at ( 1 , 1 ) (1,1) (1,1): the compass will point NE \text{NE} NE.
  • The compass is at ( 1 , 1 ) (1,1) (1,1), the morning star is at ( 0 , 0 ) (0,0) (0,0): the compass will point SW \text{SW} SW.
  • The compass is at ( 1 , 1 ) (1,1) (1,1), the morning star is at ( − 1 , − 1 ) (-1,-1) (1,1): the compass will point SW \text{SW} SW.

In the second test case, only two pairs of points won’t break the compass:

  • The compass is at ( 6 , 9 ) (6,9) (6,9), the morning star is at ( 10 , 13 ) (10,13) (10,13): the compass will point NE \text{NE} NE.
  • The compass is at ( 10 , 13 ) (10,13) (10,13), the morning star is at ( 6 , 9 ) (6,9) (6,9): the compass will point SW \text{SW} SW.

Tutorial
指南针和晨星一共连接了四个方向:垂直、水平、45°、135°,所以这时就存在四种情况:

  • 垂直:两个点需要有相同的 x 坐标,此时如果有 k 个具有相同 x 的坐标,那么指南针的位置就有 k 种,晨星的位置就有 k - 1 种,此时共存在 k ( k − 1 ) k(k - 1) k(k1) 对有效位置
  • 水平:两个点需要有相同的 y 坐标,即三方法同垂直一样计算
  • 45°:对于所有这样形式的线都可以写成 x − y = c x - y = c xy=c,则此时可以用 y − x y - x yx 来表示这样的线,具体计算同上
  • 145°:对于所有这样形式的线都可以写成 x + y = c x + y = c x+y=c,则此时可以用 x + y x + y x+y 来表示这样的线,具体计算同上
    此方法时间复杂度为 O ( n l o g n ) O(nlogn) O(nlogn)

Solution

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

#define endl '\n'
#define int long long
const int INF = 0x3f3f3f3f;

int n, x, y, ans;

void solve() {
    cin >> n;
    ans = 0;
    map<int, int> mp[4];
    for (int i = 0; i < n; ++i) {
        cin >> x >> y;
        mp[0][x + y]++;
        mp[1][y - x]++;
        mp[2][x]++;
        mp[3][y]++;
    }
    for (int i = 0; i < 4; ++i) {
        for (auto [a, b] : mp[i]) {
            ans += b * (b - 1);
        }
    }
    cout << ans << endl;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T; cin >> T; while (T--)
    solve();
    return 0;
}

H. The Third Letter

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
outputL: standard output

In order to win his toughest battle, Mircea came up with a great strategy for his army. He has n n n soldiers and decided to arrange them in a certain way in camps. Each soldier has to belong to exactly one camp, and there is one camp at each integer point on the x x x-axis (at points ⋯   , − 2 , − 1 , 0 , 1 , 2 , ⋯ \cdots, -2, -1, 0, 1, 2, \cdots ,2,1,0,1,2,).

The strategy consists of m m m conditions. Condition i i i tells that soldier a i a_i ai should belong to a camp that is situated d i d_i di meters in front of the camp that person b i b_i bi belongs to. (If d i < 0 d_i < 0 di<0, then a i a_i ai’s camp should be − d i -d_i di meters behind b i b_i bi’s camp.)

Now, Mircea wonders if there exists a partition of soldiers that respects the condition and he asks for your help! Answer “YES” if there is a partition of the n n n soldiers that satisfies all of the m m m conditions and “NO” otherwise.

Note that two different soldiers may be placed in the same camp.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 1t100) — the number of test cases.

The first line of each test case contains two positive integers n n n and m m m ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \leq n \leq 2 \cdot 10^5 2n2105; 1 ≤ m ≤ n 1 \leq m \leq n 1mn) — the number of soldiers, and the number of conditions respectively.

Then m m m lines follow, each of them containing 3 3 3 integers: a i a_i ai, b i b_i bi, d i d_i di ( a i ≠ b i a_i \neq b_i ai=bi; 1 ≤ a i , b i ≤ n 1 \leq a_i, b_i \leq n 1ai,bin; − 1 0 9 ≤ d i ≤ 1 0 9 -10^9 \leq d_i \leq 10^9 109di109) — denoting the conditions explained in the statement. Note that if d i d_i di is positive, a i a_i ai should be d i d_i di meters in front of b i b_i bi and if it is negative, a i a_i ai should be − d i -d_i di meters behind b i b_i bi.

Note that the sum of n n n over all test cases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output “YES” if there is an arrangement of the n n n soldiers that satisfies all of the m m m conditions and “NO” otherwise.

Example
input

4
5 3
1 2 2
2 3 4
4 2 -6
6 5
1 2 2
2 3 4
4 2 -6
5 4 4
3 5 100
2 2
1 2 5
1 2 4
4 1
1 2 3

output

YES
NO
NO
YES

Note

For the first test case, we can partition the soldiers into camps in the following way: soldier:

  • Soldier 1 1 1 in the camp with the coordinate x = 3 x = 3 x=3.
  • Soldier 2 2 2 in the camp with the coordinate x = 5 x = 5 x=5.
  • Soldier 3 3 3 in the camp with the coordinate x = 9 x = 9 x=9.
  • Soldier 4 4 4 in the camp with the coordinate x = 11 x = 11 x=11.

For the second test case, there is no partition that can satisfy all the constraints at the same time.

For the third test case, there is no partition that satisfies all the constraints since we get contradictory information about the same pair.

For the fourth test case, in order to satisfy the only condition, a possible partition is:

  • Soldier 1 1 1 in the camp with the coordinate x = 10 x = 10 x=10.
  • Soldier 2 2 2 in the camp with the coordinate x = 13 x = 13 x=13.
  • Soldier 3 3 3 in the camp with the coordinate x = − 2023 x = -2023 x=2023.
  • Soldier 4 4 4 in the camp with the coordinate x = − 2023 x = -2023 x=2023.

Tutorial
可以将士兵之间当作有向图,士兵代表节点,距离代表有向边的权值,如果给出 a i a_i ai b i b_i bi 前面 d i d_i di 米,则可以相当于添加两条边:

  • 一条从 a i a_i ai b i b_i bi 的边,权值为 d i d_i di
  • 一条从 b i b_i bi a i a_i ai 的边,权值为 − d i -d_i di
    所以我们可以从第一个士兵开始,然后依次访问其每个邻居,将其坐标设置为当前节点的坐标加上其与邻居之间的边的权重,在访问邻居的过程中就可以判断是否满足之前已经设定好的条件,如果不符合可以直接跳出

Solution
访问邻居有两种访问方法: d f s dfs dfs b f s bfs bfs

d f s dfs dfs

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

#define endl '\n'
#define int long long
const int INF = 0x3f3f3f3f;

int n, m, a, b, d;

void solve() {
    cin >> n >> m;
    vector<PII> g[n + 1];
    int dist[n + 1];
    for (int i = 0; i <= n; ++i) {
        dist[i] = INF;
    }
    for (int i = 0; i < m; ++i) {
        cin >> a >> b >> d;
        g[a].emplace_back(b, d);
        g[b].emplace_back(a, -d);
    }

    function<bool(int)> dfs = [&](int u) -> bool {
        for (auto [x, y] : g[u]) {
            if (dist[x] != INF) {
                if (dist[x] != dist[u] + y) {
                    return false;
                }
            } else {
                dist[x] = dist[u] + y;
                if (!dfs(x)) {
                    return false;
                }
            }
        }
        return true;
    };

    for (int i = 1; i <= n; ++i) {
        if (dist[i] != INF) {
            continue;
        }
        dist[i] = 0;
        if (!dfs(i)) { // 一个不满足就是 NO
            cout << "NO\n";
            return;
        }
    }
    cout << "YES\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T; cin >> T; while (T--)
    solve();
    return 0;
}

b f s bfs bfs

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

#define endl '\n'
#define int long long
#define PII pair<int, int>

int n, m, a, b, d;

void solve() {
    cin >> n >> m;
    vector<PII> g[n + 1];
    bool memo[n + 1];
    int dist[n + 1];
    memset(dist, 0, sizeof(dist));
    memset(memo, false, sizeof(memo));
    for (int i = 0; i < m; ++i) {
        cin >> a >> b >> d;
        g[a].emplace_back(b, d);
        g[b].emplace_back(a, -d);
    }

    for (int i = 0 ; i < n; ++i) {
        if (!memo[i]) {
            memo[i] = true;
            queue<int> q;
            q.push(i);
            while (q.size()) {
                int u = q.front();
                q.pop();
                for (auto [v, d] : g[u]) {
                    if (!memo[v]) {
                        memo[v] = true;
                        q.push(v);
                        dist[v] = dist[u] + d;
                    } else if (dist[v] != dist[u] + d) {
                        cout << "NO\n";
                        return;
                    }
                }
            }
        }
    }
    cout << "YES\n";
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T; cin >> T; while (T--)
    solve();
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值