2016 Multi-University Training Contest 1 J.Subway
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 ;
}
2016 Multi-University Training Contest 1 J.Subway的更多相关文章
- 2016 Al-Baath University Training Camp Contest-1 J
Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- 2016 Al-Baath University Training Camp Contest-1 E
Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...
- 2016-2017 CT S03E05: Codeforces Trainings Season 3 Episode 5 (2016 Stanford Local Programming Contest, Extended) J
链接:http://codeforces.com/gym/101116 题意:给出n个点,要求一个矩形框将(n/2)+1个点框住,要面积最小 解法:先根据x轴选出i->j之间的点,中间的点(包括 ...
- 2016 Al-Baath University Training Camp Contest-1 F
Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- [CFGym101028] 2016 Al-Baath University Training Camp Contest-1
比赛链接:http://codeforces.com/gym/101028/ 由于实习,几乎没有时间刷题了.今天下午得空,断断续续做了这一套题,挺简单的. A.读完题就能出结果. /* ━━━━━┒ギ ...
- 2014 Multi-University Training Contest 1 - J Rating
题目链接: pid=4870">http://acm.hdu.edu.cn/showproblem.php?pid=4870 题目大意: 题意:一个人注冊两个账号,初始rating都是 ...
- 2016 Al-Baath University Training Camp Contest-1 I
Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...
随机推荐
- ElasticSearch-5.0.0安装中文分词插件IK
Install IK 源码地址:https://github.com/medcl/elasticsearch-analysis-ik,git clone下来. 1.compile mvn packag ...
- MySQL 5.6 新参数对binlog日志量的优化
数据库版本:5.6.* 1.row日志image类型 参数binlog_row_image 控制着这种image类型,默认为FULL(log all columns),即记录before&af ...
- eclipse中导入jar文件的源码
有时候想看看一个jar包的源码是怎么写的,想按Ctrl+鼠标左键点击来自动导航这时候就需要先把源码给导入到eclipse中,步骤如下:首先准备jar包和源文件包比如:
- C#中Trim()、TrimStart()、TrimEnd()的用法
string s = " from dual union all "; s = s.Trim().TrimEnd("union all".To ...
- 5、Servlet的使用
一.什么是Servlet:用于开发动态Web资源的的技术.使用Servlet可以读取来自用户端的数据,而实现了用户与服务器之间的动态数据交互.更简单的说就是连接页面和代码. 1.开发一个动态的Web资 ...
- [Python] 机器学习库资料汇总
声明:以下内容转载自平行宇宙. Python在科学计算领域,有两个重要的扩展模块:Numpy和Scipy.其中Numpy是一个用python实现的科学计算包.包括: 一个强大的N维数组对象Array: ...
- CSS高效开发实战:CSS 3、LESS、SASS、Bootstrap、Foundation --读书笔记(2)CSS3利用图层叠加实现多背景
CSS 3允许设置多个背景图片,每个背景图片占一层,层的上下按照在CSS中书写的顺序来定,最先写的背景在最上层,每层图片定义使用英文逗号隔开. 例如下面的代码: background:url(http ...
- JS跨域(ajax跨域、iframe跨域)解决方法及原理详解(jsonp)
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- 《征服 C 指针》摘录2:C变量的 作用域 和 生命周期(存储期)
在开发一些小程序的时候,也许我们并不在意作用域的必要性.可是,当你书写几万行,甚至几十万行的代码的时候,没有作用域肯定是不能忍受的. C 语言有如下 3 种作用域. 1.全局变量 在函数之外声明的变量 ...
- sqlserver 自增ID插入指定数据
set identity_insert 表名 ON --允许对自增列Id插入指定数据 insert into table_name(Id,Name) values(1,'test') set iden ...