CF159A.Friends or Not

普及/提高-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Polycarpus has a hobby — he develops an unusual social network. His work is almost completed, and there is only one more module to implement — the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time _t_1, and user B sent user A a message at time _t_2. If 0 < _t_2 - _t_1 ≤ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message.

You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends.

波利卡普斯有一个爱好——他正在开发一个独特的社交网络。他的工作已接近完成,只剩下一个模块有待实现——即用于判定好友关系的模块。是的,在这个社交网络中,用户无需手动添加好友!好友关系对是通过如下方式推断得出的:假设用户 A 在时刻 _t_₁ 向用户 B 发送了一条消息,而用户 B 又在时刻 _t_₂ 向用户 A 发送了一条消息。若满足 0<t2−t1≤d0 < t_2 - t_1 \leq d,则认为用户 B 的这条消息是对用户 A 消息的回复。当且仅当用户 A 至少回复了用户 B 的一条消息,或用户 B 至少回复了用户 A 的一条消息时,用户 A 和 B 才被视为好友。

现给出按时间顺序排列的消息日志以及一个数 d,请找出所有将被判定为好友的用户对。

输入格式

The first line of the input contains two integers n and d (1 ≤ n, d ≤ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as "A__i B__i t__i" (without the quotes), which means that user A__i sent a message to user B__i at time t__i (1 ≤ i ≤ n). A__i and B__i are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and t__i is an integer (0 ≤ t__i ≤ 10000). It is guaranteed that the lines are given in non-decreasing order of t__i's and that no user sent a message to himself. The elements in the lines are separated by single spaces.

输入的第一行包含两个整数 nn 和 dd(1≤n,d≤10001 \leq n, d \leq 1000)。接下来的 nn 行包含消息日志。第 ii 行包含一条格式为 "A_i B_i t_i"(不含引号)的日志记录,表示用户 AiA_i 在时刻 tit_i 向用户 BiB_i 发送了一条消息(1≤i≤n1 \leq i \leq n)。其中 AiA_i 和 BiB_i 是非空字符串,长度至多为 20,仅由小写字母('a' ... 'z')组成;tit_i 是一个整数(0≤ti≤100000 \leq t_i \leq 10000)。保证各行按 tit_i 的非递减顺序给出,且不存在用户向自己发送消息的情况。各行中的各元素以单个空格分隔。

输出格式

In the first line print integer k — the number of pairs of friends. In the next k lines print pairs of friends as "A__i B__i" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once.

第一行输出整数 kk —— 朋友对的数量。接下来的 kk 行中,每行输出一对朋友,格式为 “AiA_i BiB_i”(不含引号)。你可以以任意顺序输出每对中的用户,以及这些朋友对本身。每对朋友必须恰好输出一次。

输入输出样例

  • 输入#1

    4 1
    vasya petya 1
    petya vasya 2
    anya ivan 2
    ivan anya 4

    输出#1

    1
    petya vasya
  • 输入#2

    1 1000
    a b 0

    输出#2

    0

说明/提示

In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.

在第一个样例测试用例中,瓦西里和佩佳是朋友,因为他们的消息发送时间相隔恰好一秒。阿尼娅和伊万则不是朋友,因为他们的消息发送时间相差超过一秒。

输入解题思路,AI测评打分。不知道怎么写?

首页