Subway
Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 860 Accepted Submission(s): 126
Special Judge
As a direction idiot, jiefangxuanyan felt helpless with this situation. He called yiyi cat for help. Because the subway map is so complicated, she can’t remember it either. Fortunately, only the names of the stations changed, the structure of subway lines is the same. So she picks out the old map to make a mapping.
But mapping such a confused subway map is definitely a difficult task. So she has to use the computer. Unfortunately, she just spilt wonton soup into her computer. So, yiyi cat asked you for help, hoping you can help her with this problem.
The subway in the city forms a tree, with N subway stations and N-1 subway lines. Any pair of stations are connected with one or more subway lines. You need to find a bijective mapping from the old names to the new names, that for each pair of stations connected by exactly one line in the old map, their new names are also connected by exactly one line in the new map.
For each test case, the first line is an integer N(1≤N≤100000).
In the following N−1 lines, each line has two space-separated string, as two stations connected by one line in the old map.
In the following N−1 lines, each line has two space-separated string, as two stations connected by one line in the new map.
Station names are no longer than 10 characters, and only consists of lowercase letters (a~z).
Each line consists two space-separated string, as the old name and its corresponding new name.
Both the names appeared in the old and new subway map should appear exactly once in the output.
You can output the names in any order. And if there are multiple valid mappings, output any one.
Names in the old map and the new map may be the same, but this does not mean these two stations are the same.
a b
b c
b a
a c
a b
c c
题意:
给两棵树,它们同构,要求为它们找一个双射。
题解:
判树的同构是很经典的了。
首先要找出树的重心。
为了简单起见,当有两个重心时,可以在它们中间加入一个点,这样重心就唯一了。 关于重心一般有三种说法:
1、删掉重心后剩下的子树最大的那个最小。
2、删掉重心后,剩下的子树大小都不超过原树的一半(floor(n/2))。
3、删掉重心后,剩下的子树尽量平衡(最大-最小子树的大小差最小)。
当然很容易证明,第三条是重心的性质而不是条件。
是必要不充分的。
而第一条和第二条是等价的。
当有两个重心时,它们必然连在一起,且子树中最大的那个一样小。 为了简便找出所有重心,一般使用第一条。 找出重心后对树进行hash。
树的hash有两种方法:
1、弄出树的括号序列,孩子子树小的排在前面,然后做字符串hash
在实现时无需将序列真的做出来。
2、对每个节点,它的hashcode为
hashcode[u] = 1 +
sigma(primes[i % numberOfPrimes]^i * hashcode[child[u][i]])
实际上primes只要随便选几个数就行,实现方便。一般不会错。
const int N = ;
int n; struct Bijection {
map<string, int> stringToInt;
map<string, int>::iterator intToString[N];
int tot; inline void init() {
stringToInt.clear(), tot = ;
} inline int queryInt(const string &t) {
if(stringToInt.count(t)) return stringToInt[t];
intToString[tot] = stringToInt.insert(mk(t, tot)).first;
return tot++;
} inline const string& queryString(const int t) const {
return intToString[t]->first;
}
} nameA, nameB; const int PRIMESTOT = ;
const unsigned int PRIMES[PRIMESTOT] = {
,
,
,
,
,
, }; struct cmpByHashCode {
unsigned int *keys;
cmpByHashCode(unsigned int *keys):keys(keys) {}
inline bool operator ()(const int a, const int b) const {
return keys[a] < keys[b];
}
}; struct TreeHash {
/**
* 1. It will find tree's barycentre firstly and treat it as root.
* 2. If it has two barycentres, it will add a new nodes between them.
* 3. Then Hash Every nodes by it subtree's structure.
* 4. O(n * number of primes).
* 5. Choose several primes as keys to hash.
* 6. The number of primes determine the accuracy of hash.
* */ static unsigned int factor[N]; static void prepare(int N) {
unsigned int tmp[PRIMESTOT];
for(int i = ; i < PRIMESTOT; ++i) tmp[i] = PRIMES[i];
for(int i = ; i < N; ++i) {
factor[i] = tmp[i % PRIMESTOT];
tmp[i % PRIMESTOT] *= tmp[i % PRIMESTOT];
}
} int head[N], son[N * ], nex[N * ], tot;
int n;
int rot, bfsList[N], father[N], size[N], maxSubtree[N];
unsigned int hashCode[N];
vector<int> child[N]; inline void init(int m) {
n = m;
for(int i = ; i < n; ++i) head[i] = -;
tot = ;
} inline void addEdge(int u, int v) {
son[tot] = v, nex[tot] = head[u];
head[u] = tot++;
} inline void bfs(int st) {
int len = ;
bfsList[len++] = st, father[st] = -;
for(int idx = ; idx < len; ++idx) {
int u = bfsList[idx];
for(int tab = head[u], v; tab != -; tab = nex[tab])
if(father[u] != (v = son[tab]))
father[v] = u, bfsList[len++] = v;
}
} inline int getBarycentre() {
bfs(); for(int i = ; i < n; ++i) size[i] = ;
for(int i = n - ; i >= ; --i) {
int u = bfsList[i];
++size[u];
if(father[u] != -) size[father[u]] += size[u]; maxSubtree[u] = n - size[u];
for(int tab = head[u], v; tab != -; tab = nex[tab])
if(father[u] != (v = son[tab]))
maxSubtree[u] = max(maxSubtree[u], size[v]);
} int rot = ;
for(int i = ; i < n; ++i)
if(maxSubtree[rot] > maxSubtree[i]) rot = i;
int anotherRot = -;
for(int i = ; i < n; ++i)
if(i != rot && maxSubtree[rot] == maxSubtree[i]) {
anotherRot = i;
break;
} if(anotherRot != -) {
int newRot = n++;
head[newRot] = -;
addEdge(newRot, rot), addEdge(newRot, anotherRot); for(int tab = head[rot]; tab != -; tab = nex[tab])
if(son[tab] == anotherRot) {
son[tab] = newRot;
break;
}
for(int tab = head[anotherRot]; tab != -; tab = nex[tab])
if(son[tab] == rot) {
son[tab] = newRot;
break;
} rot = newRot;
}
return rot;
} inline int hashTree() {
int rot = getBarycentre();
bfs(rot); for(int i = n - ; i >= ; --i) {
int u = bfsList[i]; child[u].clear(), hashCode[u] = ;
for(int tab = head[u], v; tab != -; tab = nex[tab])
if(father[u] != (v = son[tab])) child[u].push_back(v);
sort(all(child[u]), cmpByHashCode(hashCode));
for(int idx = ; idx < sz(child[u]); ++idx)
hashCode[u] += factor[idx] * hashCode[child[u][idx]];
}
return rot;
}
} treeA, treeB;
unsigned int TreeHash::factor[N]; inline void init() {
nameA.init(), nameB.init(), treeA.init(n), treeB.init(n);
} inline void buildBijection(int u, int v) {
if(u < n && v < n)
cout << nameA.queryString(u) << ' ' << nameB.queryString(v) << "\n";
for(int i = ; i < sz(treeA.child[u]); ++i)
buildBijection(treeA.child[u][i], treeB.child[v][i]);
} inline void solve() {
int u = treeA.hashTree(), v = treeB.hashTree();
buildBijection(u, v);
} int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
TreeHash::prepare(N);
while(cin >> n) {
init();
for(int i = ; i < n; ++i) {
string a, b;
cin >> a >> b;
int u = nameA.queryInt(a), v = nameA.queryInt(b);
treeA.addEdge(u, v), treeA.addEdge(v, u);
}
for(int i = ; i < n; ++i) {
string a, b;
cin >> a >> b;
int u = nameB.queryInt(a), v = nameB.queryInt(b);
treeB.addEdge(u, v), treeB.addEdge(v, u);
}
solve();
}
return ;
}