45分求助(Sub.4、6、7、8没过,卡TLE)
查看原帖
45分求助(Sub.4、6、7、8没过,卡TLE)
770891
NTT__int128楼主2024/5/10 22:13
#include <bits/stdc++.h>
using namespace std ;
#define m(a , b) (a < b ? a : b)
#define rg register
#define sp(x , y) {rg int t = x ; x = y , y = t ;}
inline int read()
{
	rg int f = 1 , x = 0 ;
	rg char ch = getchar() ;
	while(ch < '0' || ch > '9')
	{
		f *= (ch == '-' ? -1 : 1) , ch = getchar() ;
	}
	while('0' <= ch && ch <= '9')
	{
		x = x * 10 + ch - '0' , ch = getchar() ;
	}
	return x * f ;
}
const int N = 2e5 + 5 , inf = 1e7 ;
int n , q , x2[N] , x5[N] , l1[N] , l2[N] , l5[N] , lg[N] , dep[N] , dp[N][35] , x , y , k ;
double z ;
bool b[N] ;
struct node
{
	int to ;
	double v ;
} ;
vector <node> v[N] ;
inline void dfs(rg int cur , rg int fa)
{
	dep[cur] = dep[fa] + 1 ;
	dp[cur][0] = fa ;
	for(rg int i = 1 ; i <= 30 ; i++)	dp[cur][i] = dp[dp[cur][i - 1]][i - 1] ;
	for(rg int i = 0 ; i < v[cur].size() ; i++)
	{
		rg int to = v[cur][i].to ;
		if(to == fa)	continue ;
		rg double val = v[cur][i].v ;
		l1[to] = l1[cur] , l2[to] = l2[cur] , l5[to] = l5[cur] ;
		if(val == 0)	l2[to] += inf , l5[to] += inf ;
		else
		{
			val *= 10000 ;
			rg int v = int(val) ;
			l1[to] += 4 ;
    		while(v % 10 == 0)
    		{
    			l1[to]-- ;
    			v /= 10 ;
    		}
    		while(v % 2 == 0)
    		{
    			v /= 2 ;
    			l2[to]++ ;
    		}
    		while(v % 5 == 0)
    		{
    			v /= 5 ;
    			l5[to]++ ;
    		}
		}
		dfs(to , cur) ;
	}
	return ;
}
inline int lca(rg int x , rg int y)
{
	if(dep[x] < dep[y])	sp(x , y) ;
	while(dep[x] > dep[y])	x = dp[x][lg[dep[x] - dep[y]]] ;
	if(x == y)	return x ;
	for(rg int i = 30 ; i >= 0 ; i--)	if(dp[x][i] != dp[y][i])	x = dp[x][i] , y = dp[y][i] ;
	return dp[x][0] ;
}
string s ;
int main()
{
	n = read() , q = read() ;
	for(rg int i = 1 ; i <= n ; i++)
	{
		k = read() ;
		if(k == 0)	x5[i] = x2[i] = inf ;
		else
		{
			while(k % 2 == 0)
			{
				k /= 2 ;
				x2[i]++ ;
			}
			while(k % 5 == 0)
			{
				k /= 5 ;
				x5[i]++ ;
			}
		}
	}
	for(rg int i = 2 ; i <= n ; i++)
	{
		x = read() , y = read() ;
		cin >> z ;
		v[x].push_back((node){y , z}) ;
		v[y].push_back((node){x , z}) ;
	}
	dfs(1 , 0) ;
	while(q--)
	{
		x = read() , y = read() ;
		rg int t = lca(x , y) ;
		rg int c1 = l1[x] + l1[y] - l1[t] * 2 ;
		rg int c2 = x2[x] + l2[x] + l2[y] - l2[t] * 2 ;
		rg int c5 = x5[x] + l5[x] + l5[y] - l5[t] * 2 ;
		s += (c1 <= m(c2 , c5) ? "Yes" : "No") ;
		s += "\n" ;
	}
	cout << s ;
	return 0 ;
}
2024/5/10 22:13
加载中...