题解
干脆题解套题解好了
毕竟我的hash方法是抄小迪的
https://www.cnblogs.com/RabbitHu/p/9165770.html
小迪太巨了%%%
之前模数是八位的WA了几个点,换成9位模数就都过了
代码
#include <bits/stdc++.h>
//#define ivorysi
#define enter putchar('
')
#define space putchar(' ')
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eps 1e-8
#define ba 11
#define mo 999999137
#define MAXN 100005
#define pii pair<int,int>
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {putchar('-');x = -x;}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
struct node {
int to,next;
}E[MAXN * 2];
int sumE,head[MAXN],N,fa[MAXN],siz[MAXN],D[MAXN],ans = 1000000000;
int64 F[MAXN],B[MAXN + 10];
set<int64> S;
vector<int64> son[MAXN],pre[MAXN],suf[MAXN];
bool isB;
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
void dfs1(int u) {
siz[u] = 1;
son[u].clear();
son[u].pb(0);
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(v != fa[u]) {
fa[v] = u;
dfs1(v);
siz[u] += siz[v];
son[u].pb(F[v]);
}
}
F[u] = 0;
if(siz[u] == 1) {F[u] = 1;return;}
sort(son[u].begin(),son[u].end());
int s = son[u].size();
for(int i = 1 ; i < s ; ++i) {
F[u] = (F[u] + B[i] * son[u][i]) % mo;
}
F[u] = F[u] * siz[u] % mo;
}
void dfs2(int u,int64 Fa) {
if(fa[u]) son[u].pb(Fa);
sort(son[u].begin(),son[u].end());
int s = son[u].size();
pre[u].clear();suf[u].clear();
pre[u].resize(s);
suf[u].resize(s + 1);
pre[u][0] = 0;suf[u][s] = 0;
for(int i = 1 ; i < s ; ++i) {
pre[u][i] = (pre[u][i - 1] + B[i] * son[u][i]) % mo;
}
for(int i = s - 1 ; i >= 1 ; --i) {
suf[u][i] = (suf[u][i + 1] + B[i - 1] * son[u][i]) % mo;
}
if(!isB) S.insert(pre[u][s - 1] * siz[1] % mo);
if(isB && D[u] == 1) {
if(!fa[u]) {
if(S.find(son[u][1]) != S.end()) {ans = min(ans,u);}
}
else if(S.find(Fa) != S.end()) {ans = min(ans,u);}
}
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(v != fa[u]) {
int t = lower_bound(son[u].begin(),son[u].end(),F[v]) - son[u].begin();
int64 T = (pre[u][t - 1] + suf[u][t + 1]) % mo;
T = T * (siz[1] - siz[v]) % mo;
if(siz[1] - siz[v] == 1) T = 1;
dfs2(v,T);
}
}
}
void Solve() {
read(N);
B[0] = 1;
for(int i = 1 ; i <= N + 3 ; ++i) B[i] = B[i - 1] * ba % mo;
int u,v;
for(int i = 1 ; i < N ; ++i) {
read(u);read(v);
add(u,v);add(v,u);
}
dfs1(1);
dfs2(1,0);
memset(head,0,sizeof(head));
memset(fa,0,sizeof(fa));
memset(siz,0,sizeof(siz));
sumE = 0;
for(int i = 1 ; i <= N ; ++i) {
read(u);read(v);
add(u,v);add(v,u);
D[u]++;D[v]++;
}
isB = 1;
dfs1(1);
dfs2(1,0);
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}