CF159A.Friends or Not

普及/提高-

通过率:0%

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 AA sent user BB a message at time t1t_{1} , and user BB sent user AA a message at time t2t_{2} . If 0<t_{2}-t_{1}<=d , then user BB 's message was an answer to user AA 's one. Users AA and BB are considered to be friends if AA answered at least one BB 's message or BB answered at least one AA 's message.

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

输入格式

The first line of the input contains two integers nn and dd ( 1<=n,d<=10001<=n,d<=1000 ). The next nn lines contain the messages log. The ii -th line contains one line of the log formatted as " AiA_{i} BiB_{i} tit_{i} " (without the quotes), which means that user AiA_{i} sent a message to user BiB_{i} at time tit_{i} ( 1<=i<=n1<=i<=n ). AiA_{i} and BiB_{i} are non-empty strings at most 2020 characters long, consisting of lowercase letters ('a' ... 'z'), and tit_{i} is an integer ( 0<=ti<=100000<=t_{i}<=10000 ). It is guaranteed that the lines are given in non-decreasing order of tit_{i} 's and that no user sent a message to himself. The elements in the lines are separated by single spaces.

输出格式

In the first line print integer kk — the number of pairs of friends. In the next kk lines print pairs of friends as " AiA_{i} BiB_{i} " (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once.

输入输出样例

  • 输入#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.

首页