The significance of travel (expected dp, graph traversal dfs - mark, inverse pre-treatment)

 

Knowledge Point: directed acyclic graph VS tree  , expect dp

L. significance of travel

time limit per test

1.0 s

memory limit per test

256 MB

input

standard input

output

standard output

Why do some people always want to travel, perhaps because, coincidence and warmth will flock to it in the next second.

Always wanted to travel every day to decide in the coming May Day holiday arrange a round the world trip. To this end, he has access to a lot of information in advance and ready to draw a road map for travel. First of all every day might go nn tourist cities are numbered labeled 1,2, ⋯, n1,2, ⋯, n. If there is a direct rail line from city to city AA BB, he would draw on the map a directed segment from AA to BB's. Because every day does not like to waste time traveling to and from the car, so he designed a tour route map is a directed acyclic graph.

11 are in the city every day, each of his arrival a tourist city will first spend one day play local tourist attractions. He then there is no clear destination, so the next day he would randomly choose a direct line to the city, spend a day at a time leading tourist city. Of course, if the town's tourist attractions so funny, he may choose to stay another day, but due to limited holiday, he can only stay in the current tourist city 22 days. For example, when CC every day in the city, if the city has 22 direct line CC, respectively, to the city and urban AA BB, then after the first day of play, the next day he had 1313 may choose to remain in the city CC more than a day trip, but the third day he will no longer stay in the city CC; 1313 while he may choose to immediately take the direct city AA high-speed rail; he also has 1313 may choose to immediately take the direct city BB high-speed rail.

After a day when all the tourist cities to play, he will only end this memorable May Day vacation travel. Please help you clever now calculate in advance every day, he expects the current travel time is how much?

Easy to prove that every desired travel time in the form of PQPQ, wherein the PP and QQ coprime, and Q≢0 (mod 998244353) Q≢0 (mod 998244353). Thus output as answers, P⋅Q-1 (mod 998244353) P⋅Q-1 (mod 998244353), wherein Q-1Q-1 represents the inverse element QQ in the modulo 998244353998244353.

Input

The first line enter a positive integer T (1≤T≤10) T (1≤T≤10), it represents the number of data sets. TT next set of data, each set of data are true:

  • The first line of the input two non-negative integer n (1≤n≤105) n (1≤n≤105) and m (0≤m≤105) m (0≤m≤105), represent the possible travel city day number of direct line number nn and mm between them.
  • Subsequently mm lines of input uu two positive integers and v (1≤u, v≤n) v (1≤u, v≤n), indicates there is a direct way to the line VV from city uu, ensure that the two only a maximum of 11 direct line between a tourist city.

Output

For each set of data, outputting a non-negative integer representing the desired travel time of the day, attention wrap.

Example

input

Copy

2
1 0
2 1
1 2

output

Copy

2
499122181

Note

The first set of sample only a tourist city. First of all, every day will play in the city day, the next day only one choice - to stay then play one day, then he can only after the end of the journey, it is expected that travel time is 22.

The second set of sample consists of two tourist cities, 11 cities from 22 cities to have a direct line. Every day, first in the city 11 day of fun, then there is the probability of 1212 to the city 22, which will take 11 days to take the high-speed rail; of course every day, there are probabilities 1212 stay in the multi-city 11 to play one day, the third day and then take the high-speed rail to the city 22. So just to reach the city 22 travel time every day to spend expectation is 1+ [12⋅1 + 12⋅ (1 + 1)] = 2.51+ [12⋅1 + 12⋅ (1 + 1)] = 2.5 days. Then every day will be a day of sightseeing in the city before 22, but then he had no other city can go, can only choose to continue to stay one day and then terminate the journey, easy calculate the total time expectations for this trip 4.54.5 days, that 92 = 9 ⋅2-1 (mod 998244353) = 49912218192 = 9⋅2-1 (mod 998244353) = 499122181.

 

topic

Solution: Suppose point is the end point u have no other connection point edge a few days so I started to play from the point u 2 is desirable for two days, the first day of play did not get elected to stay put can only continue to play one day, we pour push, provided D [u] expected number of days from u starting from the beginning of play, assuming u have sz edges connected to other points v, I must first play one day at a point u, d [u] = 1, then I have (sz +1) probability 1 / v to each point of connection, then d [u] + = 1 / (sz + 1) * (d [v] + 1), while I have 1 / (SZ + 1) probabilities continue playing at point u, then d [u] + = 1 / (sz + 1), if the point u play two days, then the connection must go to the next point v, then D [u ] + = 1 / (sz + 1) * 1 / sz * (d [v] + 1)

 

It reduces to:

d [u] = 1 + 1 / (c + 1) + (/ c 1 / c + 1 * 1 / c + 1 + 1) * d [v] +1;

     Initial expectations + + probability expected to remain into the next city under the city * + desired expect high-speed rail

 

--------------------- 
cat dog named orange: Author 
Source: CSDN 
Original: https: //blog.csdn.net/ccsu_cat/article/details/ 95448785 
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Important: When seeking other cities expect, before the city came to be multiplied by the probability (p) * (d [v] +1) where 1 plus the number of days required for high-speed rail wasted

#include<bits/stdc++.h> 
using namespace std;
int n,m;
const int N=100005;
#define mod 998244353
vector<int > vect[N];//储存有向关系 
int vis[N]; //标记是否遍历过   
long long int dp[N],ny[N];//逆元数组-预处理
long long int ksm(long long int i)//!!!ksm return  ->long long  
{
	long long res=1;
	int y=mod-2;
	long long x=i;
	while(y)
	{
		if(y&1) res=res*x%mod;
		x=x*x%mod;
		y/=2;
	}
	return res;
}
void dfs(int u)
{
	if(vis[u]) return ;//图的dfs遍历    有向无环图  vs  树!!!! 

	dp[u]=1;//期望值初始化为1 
	vis[u]=1;
	int sz=vect[u].size();
	if(sz)  dp[u]+=1;
	for(auto v:vect[u])
	{
		dfs(v);
		//加上后面城市的期望值=d[v]*p 
		dp[u]=(dp[u]  +   ((ny[sz+1]+ny[sz+1]*ny[sz]%mod)%mod)  *  (dp[v]) %mod)%mod;
	}
	dp[u]=(dp[u]+ny[sz+1])%mod;//加上第二天继续留下来的期望 
}
int main()
{
	ny[1]=1;///!!!!!1的逆元为1 
	//优化 
	for(int i=2;i<=N;i++)
	{
		ny[i]=ksm((long long )i);//逆元数组预处理
	}
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for (int i = 1; i <= n; i++)
		{
            vect[i].clear();//vect数组初始化 
             vis[i] = 0;//vis[]初始化 
		}
		if(n==1)//特判 
		{
			cout<<2<<endl;
			continue;
		}
		for(int i=0;i<m;i++)
		{
			long long int u,v;
			scanf("%d%d",&u,&v);
			vect[u].push_back(v);
		}
		
		dfs(1);
		cout<<dp[1]<<endl;//dp[1]即为所有城市期望和 
		
	}
}


 

 

 

 

Published 44 original articles · won praise 6 · views 1211

Guess you like

Origin blog.csdn.net/qq_43868883/article/details/95478835