我想知道为什么我的这个跑这么慢

P3379 【模板】最近公共祖先(LCA)

代码不具有可读性
by info___tion @ 2018-07-22 00:05:53


代码不具有可读性
by Dispwnl @ 2018-07-22 07:20:15


代码不具有可读性
by 密期望 @ 2018-07-22 07:53:53


``` include<iostream> include<cmath> include<algorithm> using namespace std; const int maxn= 1000005; int n,m,s,add_edge,head[maxn],vis[maxn],fa[maxn],ans[maxn]; struct EDEG{ int next,to; }edge[maxn]; int find(int u) { if(fa[u]==u) return u; else return fa[u]=find(fa[u]); } void try_to_start() { for(int i=1;i<=n;i++) fa[i]=i; } void add(int u,int v) { edge[++add_edge].to=v; edge[add_edge].next=head[u]; head[u]=add_edge; } void Init_Edge() { for(int i=1;i<=n-1;i++) {int x,y;cin>>x>>y;add(x,y);add(y,x);} } struct Ques{ int next,to,id; }Ques[maxn]; int Q_head[maxn],Q_edge; void add_Q(int u,int v,int id) { Ques[++Q_edge].to=v; Ques[Q_edge].next=Q_head[u]; Q_head[u]=Q_edge; Ques[Q_edge].id=id; } void Init_Ques() { int total=1; for(int i=1;i<=m;i++) {int a,b;cin>>a>>b;add_Q(a,b,total);add_Q(b,a,total++);} } void tarjan(int root) { vis[root]=1; for(int i=head[root];i;i=edge[i].next) { if(!vis[edge[i].to]) { tarjan(edge[i].to); fa[edge[i].to]=root; } } for(int j=Q_head[root];j;j=Ques[j].next) { if(vis[Ques[j].to]) { ans[Ques[j].id]=find(Ques[j].to); } } } void solve() { for(int i=1;i<=m;i++) cout<<ans[i]<<endl; } int main() { ios::sync_with_stdio(false); cin>>n>>m>>s; Init_Edge(); Init_Ques(); try_to_start(); tarjan(s); solve(); } ```
by ylxmf2020 @ 2018-07-22 08:08:29


inline register #pragma GCC optimaze("O2")
by ACAね @ 2018-07-22 08:29:54


``` cpp __inline__ __attribute__ ((always_inline)) ```
by Juanzhang @ 2018-07-22 08:55:09


``` cpp return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, maxn, stdin), p1 == p2) ? EOF : *p1++; ```
by Juanzhang @ 2018-07-22 08:56:03


@[小光](/space/show?uid=73934) Orz 您太强了%%%%
by colazcy @ 2018-07-22 11:31:32


@[colazcy](/space/show?uid=64456) $fAKe$ $\%\%\%$
by Juanzhang @ 2018-07-22 12:04:37


|