版本比较
题解:P3379 【模板】最近公共祖先(LCA)
正文
增加 39 行删除 176 行
73 行未改动747576777879807879808384858687888990919293948182838496979885868710088891021039091929310510610710894959697110989910011210110210310410511411510610711711811912012110810911011111212312412512612711311411512913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318411611711818618711918919019112012119319419512212312419719819920020120220320420520620720820921012512621221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426512712826812913027027127213113213313413513613727513813914014173 行未改动
## Code
```cpp#include <iostream>#include <climits>#include <vector>#include <bits/stdc++.h>using namespace std;
namespace io{#ifdef LOCAL constexpr int len = 1;#else constexpr int len = 1 << 21;#endif class input{ char buf[len],*bg = buf,*en = buf; public: inline char gc(){ return (bg == en&&(en = (bg = buf) + fread(buf,1,len,stdin)) == buf?EOF:*(bg++)); }int main (){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0);
template<class T>inline void getuint(T &v){ v = 0; char ch,ch2; int n,m,s; cin >> n >> m >> s;
for (;(ch = gc()) < '0'||ch > '9'&&ch != EOF;); vector<vector<int>> a(n + 1);
for (;ch >= '0'&&ch <= '9';ch = gc()) v = (v << 1) + (v << 3) + ch - 48; } for (int i = n;--i;){ int x,y; cin >> x >> y;
template<class T>inline void getint(T &v){ v = 0; char ch,ch2; bool op = 0; a[x].push_back(y); a[y].push_back(x); }
for (;(ch = gc()) < '0'||ch > '9'&&ch != EOF;) op ^= (ch == '-'); vector<int> sz(n + 1),to(n + 1),at(n + 1),f(n + 1); int dftop = 0;
for (;ch >= '0'&&ch <= '9';ch = gc()) v = (v << 1) + (v << 3) + ch - 48; auto dfs = [&](auto &&self,int x,int y) -> int { f[x] = y; sz[x] = 1; at[x] = ++dftop;
if (op) v = -v; } int mxsz = 0,vv = x;
template<class T>inline T getint(){ T ret = 0; getint(ret); return ret; } for (auto &i:a[x]) if (i != y){ int v = self(self,i,x); sz[x] += sz[i];
template<class T>inline T getuint(){ T ret = 0; getuint(ret); return ret; } if (sz[i] > mxsz) mxsz = sz[i],swap(vv,v);
inline int getstr(char *s,int len = INT_MAX){ int cnt = 0; for (;--len&&(*s = gc()) != ' '&&*s != '\n';cnt++,s++); *s = '\0'; return cnt; }
inline void getstr(string &s){ s.clear(); for (char ch;(ch = gc()) != ' '&&ch != '\n';s += ch); }
inline input &operator >> (short &x){getint(x);return *this;} inline input &operator >> (int &x){getint(x);return *this;} inline input &operator >> (long &x){getint(x);return *this;} inline input &operator >> (long long &x){getint(x);return *this;} inline input &operator >> (__int128 &x){getint(x);return *this;} inline input &operator >> (unsigned short &x){getint(x);return *this;} inline input &operator >> (unsigned &x){getint(x);return *this;} inline input &operator >> (unsigned long &x){getint(x);return *this;} inline input &operator >> (unsigned long long &x){getint(x);return *this;} inline input &operator >> (unsigned __int128 &x){getint(x);return *this;} inline input &operator >> (char &x){for (;(x = gc()) == ' '||x == '\n';);return *this;} inline input &operator >> (char *x){getstr(x);return *this;} inline input &operator >> (string &x){getstr(x);return *this;} }fin;
class output{ char buf[1 << 21],buf2[20],mp[1000][3]; int top = 0,top2 = 0; public: output(){ for (int i = 0,ci = 48;i < 1000;i += 100,ci++) for (int j = 0,cj = 48;j < 100;j += 10,cj++) for (int k = 0,ck = 48;k < 10;k++,ck++) mp[i + j + k][0] = ck,mp[i + j + k][1] = cj,mp[i + j + k][2] = ci; } inline void flush(){ fwrite(buf,1,top,stdout); top = 0; }
inline void pc(char ch){buf[top++] = ch;if (top >= 1 << 21) flush();}
template<class T>inline void putint(T x){ if (!x){pc('0');return ;} if (x < 0) pc('-'),x = -x; for (;x >= 1000;x /= 1000) buf2[top2++] = mp[x % 1000][0],buf2[top2++] = mp[x % 1000][1],buf2[top2++] = mp[x % 1000][2]; if (x){ buf2[top2++] = mp[x][0]; if (x >= 10){ buf2[top2++] = mp[x][1]; if (x >= 100) buf2[top2++] = mp[x][2]; } for (;v != x;v = f[v]) to[v] = x; } for (;top2;pc(buf2[--top2])); }
inline void putstr(const char *s,int len){ for (;len--;pc(*(s++))); } for (;sz[vv] * 2 <= sz[x];vv = f[vv]) to[vv] = x;
inline void putstr(const string &s){ for (auto i:s) pc(i); } return vv; };
inline output &operator << (short x){putint(x);return *this;} inline output &operator << (int x){putint(x);return *this;} inline output &operator << (long x){putint(x);return *this;} inline output &operator << (long long x){putint(x);return *this;} inline output &operator << (const __int128 &x){putint(x);return *this;} inline output &operator << (unsigned short x){putint(x);return *this;} inline output &operator << (unsigned int x){putint(x);return *this;} inline output &operator << (unsigned long x){putint(x);return *this;} inline output &operator << (unsigned long long x){putint(x);return *this;} inline output &operator << (const unsigned __int128 &x){putint(x);return *this;} inline output &operator << (char x){pc(x);return *this;} inline output &operator << (const char *x){putstr(x);return *this;} inline output &operator << (const string &x){putstr(x);return *this;} inline output &operator << (void (*func)(output &)){func(*this);return *this;} dfs(dfs,s,0);
~output(){flush();} }fout;
void flush(output &out){out.flush();} void endl(output &out){out << '\n';out.flush();}}
using namespace io;
int n = fin.getuint<int>();vector<vector<int>> a(n + 1);
struct info{ int hson,sz,to,d,f,dto;};
vector<info> nd(n + 1);vector<int> dfn(n);int top = -1;
void dfs(int x){ dfn[++top] = x; nd[x].sz = 1; nd[x].dto = nd[x].d;
nd[x].to = x; for (auto i:a[x]){ if (i == nd[x].f) continue; nd[i].f = x; nd[i].d = nd[x].d + 1; dfs(i); nd[x].sz += nd[i].sz;
if (nd[i].sz > nd[nd[x].hson].sz) nd[x].hson = i; }}
int main (){ int m,s; fin >> m >> s;
for (int i = n;--i;){ int x,y; fin >> x >> y; a[x].push_back(y); a[y].push_back(x); }
dfs(s); for (auto x:dfn) nd[nd[x].hson].to = nd[x].to,nd[nd[x].hson].dto = nd[x].dto;
for (;m--;){ int x,y; fin >> x >> y; cin >> x >> y;
for (;nd[x].to != nd[y].to;){ if (nd[x].dto < nd[y].dto){int v = x;x = y;y = v;} x = nd[nd[x].to].f; for (;;){ if (sz[x] > sz[y]) swap(x,y); if (at[y] <= at[x]&&at[x] < at[y] + sz[y]) break;
x = to[x]; }
fout << (nd[x].d < nd[y].d?x:y) << '\n'; cout << y << '\n'; }}```