正文
增加 39 行删除 176 行
73 行未改动747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727873 行未改动## Code```cpp#include <iostream>#include <climits>#include <vector>using namespace std;namespace io{#ifdef LOCALconstexpr int len = 1;#elseconstexpr int len = 1 << 21;#endifclass 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++));}template<class T>inline void getuint(T &v){v = 0;char ch,ch2;for (;(ch = gc()) < '0'||ch > '9'&&ch != EOF;);for (;ch >= '0'&&ch <= '9';ch = gc()) v = (v << 1) + (v << 3) + ch - 48;}template<class T>inline void getint(T &v){v = 0;char ch,ch2;bool op = 0;for (;(ch = gc()) < '0'||ch > '9'&&ch != EOF;) op ^= (ch == '-');for (;ch >= '0'&&ch <= '9';ch = gc()) v = (v << 1) + (v << 3) + ch - 48;if (op) v = -v;}template<class T>inline T getint(){T ret = 0;getint(ret);return ret;}template<class T>inline T getuint(){T ret = 0;getuint(ret);return ret;}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 (;top2;pc(buf2[--top2]));}inline void putstr(const char *s,int len){for (;len--;pc(*(s++)));}inline void putstr(const string &s){for (auto i:s) pc(i);}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;}~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;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;}fout << (nd[x].d < nd[y].d?x:y) << '\n';}}```73 行未改动747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014173 行未改动## Code```cpp#include <bits/stdc++.h>using namespace std;int main (){ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);int n,m,s;cin >> n >> m >> s;vector<vector<int>> a(n + 1);for (int i = n;--i;){int x,y;cin >> x >> y;a[x].push_back(y);a[y].push_back(x);}vector<int> sz(n + 1),to(n + 1),at(n + 1),f(n + 1);int dftop = 0;auto dfs = [&](auto &&self,int x,int y) -> int {f[x] = y;sz[x] = 1;at[x] = ++dftop;int mxsz = 0,vv = x;for (auto &i:a[x])if (i != y){int v = self(self,i,x);sz[x] += sz[i];if (sz[i] > mxsz)mxsz = sz[i],swap(vv,v);for (;v != x;v = f[v])to[v] = x;}for (;sz[vv] * 2 <= sz[x];vv = f[vv]) to[vv] = x;return vv;};dfs(dfs,s,0);for (;m--;){int x,y;cin >> x >> y;for (;;){if (sz[x] > sz[y]) swap(x,y);if (at[y] <= at[x]&&at[x] < at[y] + sz[y]) break;x = to[x];}cout << y << '\n';}}```