编程介的小学生 2017-07-30 08:21 采纳率: 20.5%
浏览 751
已采纳

Keep In Touch

Problem Description
There are n cities numbered with successive integers from 1 to n in Byteland. Also, there are m one-way roads connecting the cities. The starting point of the i-th road is ui while the ending point is vi.

There are 3 spies : 007, 008 and 009. They are going to start q secret missions.

During each mission, they may be in three different cities, and they contact each other using interphones. The radio frequency of the i-th city is wi. Two spies can contact with each other if and only if the absolute value of the difference of radio frequency between the cities where they are in is no more than K. At each moment, three spies must choose a road and go to another city. The time for traveling each road is only a unit of time.

They can choose to end the mission in any city, even in the city where they started the mission. But they are not allowed to end mission in the middle of the roads. Now they want to know, for each mission whose start points are given, what's the number of possible ways, during which they can contact with each other at any moment when they are not on roads?

Two ways are considered different if and only if there exists at least one spy at different cities on the same moment.

Note : 3 spies must end the mission at the same time.

Input
The first line of the input contains an integer T (1≤T≤10), denoting the number of test cases.

In each test case, the first line of the input contains four integers n (1≤n≤50),m(0≤m≤n(n−1)2),K(0≤K≤109),q(1≤q≤125000), denoting the number of cities, the number of roads, the upper limit of interphone and the number of missions.

The second line of the input contains n integers w1,w2,...,wn (1≤wi≤109), denoting the radio frequency of the i-th city.

Each of the next m lines contains two integers ui,vi (1≤ui<vi≤n), denoting an one-way road. There are no multiple edges in the graph.

Each of the next q lines contains three integers x,y,z(1≤x,y,z≤n), denoting the starting point of the three spies in each mission. You can assume that there are at least one possible way in each mission.

Output
For each test case, print q lines with one integer per line. For each mission, print the number of possible ways modulo 998244353.

Sample Input
1
4 4 2 10
8 8 4 1
1 3
1 4
2 3
2 4
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2
3 3 3
4 4 4

Sample Output
3
3
3
3
3
3
3
3
1
1

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-08-15 21:59
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组