A.count
本场比赛最难的题...
隔板法组合数容斥 xjb 搞搞就好了
//by Judge
#include
#include
#define Rg register
#define fp(i,a,b) for(Rg int i=(a),I=(b)+1;iI;--i)
#define ll long long
using namespace std;
const int mod=998244353;
const int M=1e7+3;
ll n; int m,k,mx,r,ans,fac[M],inv[M];
inline int mul(int x,int y){return 1ll*x*y%mod;}
inline void Pls(int& x,int y){if((x+=y)>=mod)x-=mod;}
inline int qpow(int x,int p=mod-2){ int s=1;
for(;p;p>>=1,x=mul(x,x)) if(p&1) s=mul(s,x); return s;
}
inline int C(ll n,int m){
if(n
tree
很水的题 \(n^2\) 的复杂度,随便搜搜就好了
//by Judge
#include
#include
#include
#define Rg register
#define fp(i,a,b) for(Rg int i=(a),I=(b)+1;iI;--i)
#define go(u) for(Rg int i=head[u],v=e[i].to;i;v=e[i=e[i].nxt].to)
#define open(S) freopen(S".in","r",stdin),freopen(S".out","w",stdout)
#define ll long long
using namespace std;
const int mod=998244353;
const int M=1e5+3;
typedef int arr[M];
#ifndef Judge
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
char buf[1<<21],*p1=buf,*p2=buf;
inline int Max(int x,int y){return x>y?x:y;}
inline int Min(int x,int y){return x=mod)x-=mod;}
inline int read(){ int x=0,f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f;
} int n,x,y,root,ans,a[M]; int pat,head[M];
struct Edge{ int to,nxt; }e[M<<1];
inline void add(int u,int v){
e[++pat]={v,head[u]},head[u]=pat;
e[++pat]={u,head[v]},head[v]=pat;
}
void dfs(int u,int fa,int mn,int mx){
if(u>=root) Pls(ans,mul(mn,mx));
go(u) if(v^fa) dfs(v,u,Min(mn,a[v]),Max(mx,a[v]));
}
int main(){ n=read(); fp(i,1,n) a[i]=read();
fp(i,2,n) x=read(),y=read(),add(x,y);
fp(i,1,n) dfs(root=i,0,a[i],a[i]); return !printf("%d\n",ans);
}
distance
n·q ,水题,注意别炸精度就好了
//by Judge
#include
#include
#include
#define Rg register
#define fp(i,a,b) for(Rg int i=(a),I=(b)+1;iI;--i)
#define go(u) for(Rg int i=head[u],v=e[i].to;i;v=e[i=e[i].nxt].to)
#define ll long long
using namespace std;
const int M=2003;
typedef int arr[M];
#ifndef Judge
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
char buf[1<<21],*p1=buf,*p2=buf;
inline void cmax(ll& x,ll y){if(x1<<20)Ot();if(x<0)sr[++CCF]=45,x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++CCF]=z[Z],--Z);sr[++CCF]=chr;
} int n,q,root; ll ans,dis[M][M];
int pat,head[M];
struct Edge{ int to,val,nxt; }e[M<<1];
inline void add(int u,int v,int w){
e[++pat]={v,w,head[u]},head[u]=pat;
e[++pat]={u,w,head[v]},head[v]=pat;
}
void dfs(int u,int fa,ll d){ dis[root][u]=d;
go(u) if(v^fa) dfs(v,u,d+e[i].val);
}
int main(){ n=read(),q=read(); Rg int x,y,z;
fp(i,2,n) x=read(),y=read(),z=read(),add(x,y,z);
fp(i,1,n) dfs(root=i,0,0);
while(q--){ x=read(),y=read(),ans=0;
fp(i,1,n) cmax(ans,Min(dis[x][i],dis[y][i])); print(ans);
} return Ot(),0;
}