2015年辽宁省赛Interesting Tree
题目描述
Recently, Miss Huang want to receive a Tree as her birthday gift! (What a interesting person!) As a interesting person, Miss Huang likes interesting tree.
The interesting tree is a tree with the Max interesting degree. The interesting degree equals LCM(H(1),H(2), .. , H(N)), H(x) means the height of node x in the tree and LCM means least common multiple.
Now Mr Chen has some non-rooted tree, he wants you to choose a root of the non-rooted tree and make the strange degree of tree be maximum;
The answer may be very large please print the answer mod 10^9+7.
输入
Your program will be tested on one or more test cases. In each test case:
First line a number N(1<=N<=10^5) represent the number of node.
The next N-1 line has two number u, v indicates that there is a edge between u, v.
输出
For every test case, print the following line:
answer
where answer is the maximum LCM.(mod 10^9+7)
样例输入
41 21 31 421 2
样例输出
62
题解:问题分两步:
1.求树的直径:即树种距离最长的两个点之间的距离
2.求从1到n这n个数的最小公倍数.
先说第一个问题,有两种方法:深搜和广搜.
先说深搜:对于树的某个节点,它的最深的两个儿子m1,m2.
那么很显然m1+m2+1有可能是树的直径.用这个数更新树的直径
再说广搜:1.随机选取一个节点A进行广搜,从而找到距离它最远的结点B.当然B可能不唯一.
2.B必然是此树某个直径的一个端点(树的直径可能不唯一).
从B开始广搜找到离B最远的C结点.BC极为直径.
再说第二个问题:有两种方法,其中一种是错的.
先说正确的:比如从1到9.既然有8,那么4,2就没法说话了,山上有老虎,猴子怎能称大王.
既然有9,那么3就没法说话了.
规律出来了:全体质数打表求出来,2,3,5,7.
再说错误的: 对于任意一个序列a[],求其全部元素的最小公倍数.怎么求?
求其全部元素的最大公约数怎么求?这个你会.两两求,从头扫描到尾.
但是这道题里不行,必须进行质因数分解,因为有取模运算!!!!!!!!!! 取模之后就没法再求最大公倍数了!!!!!!!!!!!!!!!!
比赛的时候,我就错在了这里,我真傻真的
#include<iostream> #include<list> #include<math.h> #include<stdio.h> #include<string.h> using namespace std; const int maxn = 1e5 + 7; const int big = 1e9 + 7; int N; struct Edge{ int to, next; }e[maxn*2]; int g[maxn],ei; bool vis[maxn]; int maxLen; int prime[maxn / 5], psize; void init(){ bool is[maxn]; psize = 0; memset(is, 1, sizeof(is)); for (int i = 2; i < maxn; i++){ if (is[i]){ prime[psize++] = i; } for (int j = 0; i*prime[j] < maxn; j++){ is[i*prime[j]] = 0; if (i%prime[j] == 0)break; } } } void push_back(int from, int to){ e[ei].next = g[from]; e[ei].to = to; g[from] = ei++; } int deep(int x){ int m1, m2; vis[x] = 1, m1 = m2 = 0; for (int i = g[x]; i; i = e[i].next){ int t = e[i].to; if (vis[t] == false){ int d = deep(t); if (m2<d){ if (m1<d){ m2 = m1, m1 = d; } else m2 = d; } } } int len = m1 + m2 + 1; if (len > maxLen)maxLen = len; return m1 + 1; } int main(){ //freopen("in.txt", "r", stdin); init(); while (~scanf("%d", &N)){ ei = 1, memset(g, 0, sizeof(int)*(N + 1)); int x, y; for (int i = 1; i < N; i++)scanf("%d%d", &x, &y), push_back(x, y), push_back(y, x); memset(vis, 0, sizeof(bool)*(N + 1)); maxLen = 0; deep(1); long long int ans = 1; for (int i = 0; i < psize; i++){ int mi = floor(log(maxLen) / log(prime[i])); if (mi == 0)break; ans *= pow(prime[i], mi); ans %= big; } cout << ans << endl; } return 0; } /************************************************************** Problem: 1578 User: 20124003 Language: C++ Result: 正确 Time:181 ms Memory:9568 kb ****************************************************************/
2015年辽宁省赛Interesting Tree的更多相关文章
- 2015北京网络赛 D-The Celebration of Rabbits 动归+FWT
2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x ...
- 2015北京网络赛 J Scores bitset+分块
2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...
- 2015北京网络赛 Couple Trees 倍增算法
2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道. 解法来自 q ...
- 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)
2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...
- ACM-ICPC 2015辽宁省赛
省赛之于ACM 就是让省内的队伍互相比较而已~~~(何况弱省(本渣校 四个二等四个三等(其实是六个三道题 两个两道题,是院长后来和主办方沟通了下- - (本弱很水,但还是要吐槽:好水的省赛啊!!
- HDU 5534/ 2015长春区域H.Partial Tree DP
Partial Tree Problem Description In mathematics, and more specifically in graph theory, a tree is an ...
- 浙江理工2015.12校赛-A
孙壕请一盘青岛大虾呗 Time Limit: 5 Sec Memory Limit: 128 MB Submit: 577 Solved: 244 Description 话说那一年zstu与gdut ...
- HDU 5531 Rebuild (2015长春现场赛,计算几何+三分法)
Rebuild Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- HDU 5510 Bazinga (2015沈阳现场赛,子串判断)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
随机推荐
- 不错的flash,动漫,小插件小集
(来源:http://abowman.com/google-modules/) embed code <object width="220" height="166 ...
- WPF 程序Form自的控件自适应方式之一
<Window x:Class="MapEditor2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...
- php实现SESSION跨域
稍微大一点的网站,通常都会有不只一个服务器,每个服务器运行着不同的功能模块或者不同的子系统,他们使用不同的二级域名,比如www.a.com.i.a.com.bbs.a.com.而一个整体性强的网站,用 ...
- stanford coursera 机器学习编程作业 exercise4--使用BP算法训练神经网络以识别阿拉伯数字(0-9)
在这篇文章中,会实现一个BP(backpropagation)算法,并将之应用到手写的阿拉伯数字(0-9)的自动识别上. 训练数据集(training set)如下:一共有5000个训练实例(trai ...
- openjudge6047分蛋糕[DP]
描述 有一块矩形大蛋糕,长和宽分别是整数w .h.现要将其切成m块小蛋糕,每个小蛋糕都必须是矩形.且长和宽均为整数.切蛋糕时,每次切一块蛋糕,将其分成两个矩形蛋糕.请计算:最后得到的m块小蛋糕中,最大 ...
- ural Infernal Work
Infernal Work Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descr ...
- java分层开发
既然是分层开发,首先我们需要知道的是分为那几个层,并且是干什么的? 1.实体层(entity) 对应数据库中的一张表,有了它可以降低耦合性,同时也是数据的载体. 2.数据访问对象(data acces ...
- Student学生管理系统
1.定义各个层 2.添加各个层之间的引用 DAL 层调用Model BLL层调用DAL和Model UI层调用BLL和Model层 Model层供各个层调用 3.根据数据库建立实体类,每张表对应一个实 ...
- AC日记——欧几里得的游戏 洛谷 P1290
题目描述 欧几里德的两个后代Stan和Ollie正在玩一种数字游戏,这个游戏是他们的祖先欧几里德发明的.给定两个正整数M和N,从Stan开始,从其中较大的一个数,减去较小的数的正整数倍,当然,得到的数 ...
- Socket用法详解
在客户/服务器通信模式中,客户端需要主动创建与服务器的Socket(套接字),服务端收到了客户端的请求,也会创建与客户端连接的Socket. Scoket可以看作两端通信的收发器,服务端和客户端都通过 ...