编程介的小学生 2017-09-23 15:42 采纳率: 20.5%
浏览 822
已采纳

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

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-10-11 10:39
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改