Meeting at the Agreed Minute
Time limit2sMemory limit512 MB
Count pairs of walks of length T from nodes 1 and N that meet only at minute T, modulo 9973.
- Level
Medium7 of 10
- Topics
- Matrix, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
Alice and Bob live in a city with intersections, numbered 1 through . The city has roads. Every road is bidirectional and joins two different intersections directly, and each pair of intersections is joined by at most one road.
Alice lives at intersection 1 and Bob lives at intersection . At noon they leave their houses and start wandering through the city. Every minute each of them moves from the intersection they are standing on to another intersection joined to it by a road. Staying in place is not allowed, so each of them moves exactly once per minute.
They agree to meet at some intersection exactly minutes after noon, and they do not want to run into each other before then. That is, at every minute from 0 to the two stand at different intersections, and at minute they stand at the same intersection. They travel fast, so passing each other on one road in opposite directions does not count as running into each other.
Count the ways the two can move through the city. Two ways differ if Alice's sequence of intersections differs, or Bob's does.
Input
The first line contains an integer , the number of test cases (). Then test cases follow in the format below.
The first line of each test case contains three integers , and (, , ). Each of the next lines contains two integers and , meaning that a bidirectional road joins intersection and intersection (, , ).
Output
For each test case, print on one line the number of ways Alice and Bob can move so that they stand at the same intersection at minute and never stand at the same intersection before then, modulo 9973.