LCA一直T。。
查看原帖
LCA一直T。。
172370
fzj2007楼主2020/8/12 20:32

RT,每次T的点都不一样,加上氧气都过不了。。求教啊(前面是快读,可以直接跳~)

//#include<bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<stack>
#include<vector>
//#include<conio.h>
#include<string>
#include<map>
#include<cstdlib>
#include<queue>
#include<math.h>
#include<time.h>
#include<set>
#include<cstdio>
#include<stdio.h>
#include<algorithm>
using namespace std; 
using std::cin;
using std::cout;
using std::endl;
namespace IN{
    const int MAX_INPUT = 1000000;
    #define getc() (p1==p2&&(p2=(p1=buf)+inbuf->sgetn(buf,MAX_INPUT),p1==p2)?EOF:*p1++)
    char buf[MAX_INPUT],*p1,*p2;
    template<typename T>inline bool read(T &x) {
        static std::streambuf *inbuf=cin.rdbuf();
        x=0;
        register int f=0,flag=false;
        register char ch=getc();
        while(!isdigit(ch)){
            if (ch=='-') f=1;
        	ch=getc();
        }
        if(isdigit(ch)) x=x*10+ch-'0',ch=getc(),flag=true;
        while(isdigit(ch)) {
            x=x*10+ch-48;
            ch=getc();
        }
        x=f?-x:x;
        return flag;
    }
    template<typename T,typename ...Args>inline bool read(T& a,Args& ...args) {
       return read(a)&&read(args...);
    }
    #undef getc
}

namespace OUT{
    template<typename T>inline void put(T x){
        static std::streambuf *outbuf=cout.rdbuf();
        static char stack[21];
        static int top=0;
        if(x<0){
            outbuf->sputc('-');
            x=-x;
        }
        if(!x){
            outbuf->sputc('0');
            outbuf->sputc('\n');
            return;
        }
        while(x){
            stack[++top]=x%10+'0';
            x/=10;
        }
        while(top){
            outbuf->sputc(stack[top]);
            --top;
        }
        outbuf->sputc('\n');
    }
    inline void putc(const char ch){
        static std::streambuf *outbuf=cout.rdbuf();
        outbuf->sputc(ch);
    }
    inline void putstr(string s){
    	for(register int i=0;i<s.length();i++) putc(s[i]);
	}
    template<typename T>inline void put(const char ch,T x){
        static std::streambuf *outbuf=cout.rdbuf();
        static char stack[21];
        static int top = 0;
        if(x<0){
            outbuf->sputc('-');
            x=-x;
        }
        if(!x){
            outbuf->sputc('0');
            outbuf->sputc(ch);
            return;
        }
        while(x){
            stack[++top]=x%10+'0';
            x/=10;
        }
        while(top){
            outbuf->sputc(stack[top]);
            --top;
        }
        outbuf->sputc(ch);
    }
    template<typename T,typename ...Args> inline void put(T a,Args ...args){
        put(a);put(args...);
    }
    template<typename T,typename ...Args> inline void put(const char ch,T a,Args ...args){
        put(ch,a);put(ch,args...);
    }
}
using namespace IN;
using namespace OUT;
#define N 300005
#define int long long
int n,q,mod=998244353,f[N][55],dep[N],pre[N][55],lg;
vector<int> e[N];
inline long long power(long long a,long long b,long long c){
	long long re=1;
	while(b){
		if(b&1) re=re*a%c;
		b>>=1;
		a=a*a%c;
	}
	return re;
}
void dfs(int x,int fath){
	dep[x]=dep[fath]+1,f[x][0]=fath;
	for(int i=1;(1<<i)<=n;i++) f[x][i]=f[f[x][i-1]][i-1];
	for(int i=1;i<=50;i++) pre[x][i]=(mod+pre[fath][i]+power((long long)dep[x]-1ll,(long long)i,(long long)mod))%mod;
	for(int i=0;i<e[x].size();i++)
		if(fath!=e[x][i]) dfs(e[x][i],x);
}
inline int LCA(int x,int y){
	if(dep[x]<dep[y]) swap(x,y);
	for(int i=19;~i;i--) 
		if(dep[x]-(1<<i)>=dep[y]) x=f[x][i];
	if(x==y) return x;
	for(int i=19;~i;i--)
		if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
	return f[x][0];
}
signed main(signed argc, char const *argv[]){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    read(n);
    for(int i=1,a,b;i<n;i++) read(a,b),e[a].push_back(b),e[b].push_back(a);
    dfs(1,0);
    read(q);
    for(int i=1,a,b,c,l;i<=q;i++){
    	read(a,b,c);
    	l=LCA(a,b);
    	put('\n',((pre[a][c]-pre[l][c]+pre[b][c]-pre[f[l][0]][c])%mod+mod)%mod);
	}
    return 0;
}
2020/8/12 20:32
加载中...