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 ...
随机推荐
- pwd, cd, ls, touch, mkdir, rmdir, rm
学习Shell命令最好的资料当然的是$man, 绝对是查找命令的第一大杀器,但是我们有时只是想实现某个功能,甚至连这个命令是什么都不知道,又或者不想淹没在man里大段大段的英文里,大家可以参考Linu ...
- C++杂谈(三)产生随机数与time函数
产生随机数在程序中很有用,这篇文章简单介绍一下产生随机数的方法. 伪随机数 使用标准库<cstdlib>中的rand()函数产生随机数. #include<iostream> ...
- corefile 设置
程序运行的过程中,可能会因为一些隐藏的bug导致崩溃,为了在出问题时,及时记录所在环境的情况,所以要设置core文件的产生.其实其本质就是把进程的内存保存到文件中去. 1.core文件的生成开关和大小 ...
- Windows Azure文件共享服务--File Service
部署在Windows Azure上的虚拟机之间如何共享文件?例如:Web Server A和Web Server B组成负载均衡集群,两个服务器需要一个共享目录来存储用户上传的文件.通常,大家可能首先 ...
- NET Core中实现一个Token base的身份认证
NET Core中实现一个Token base的身份认证 注:本文提到的代码示例下载地址> How to achieve a bearer token authentication and au ...
- 原生的强大DOM选择器querySelector
在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...
- UVa10820 Send a Table[欧拉函数]
Send a TableInput: Standard Input Output: Standard Output When participating in programming contests ...
- Vijos1680距离/openjudge2988计算字符串的距离[DP]
描述 设有字符串X,我们称在X的头尾及中间插入任意多个空格后构成的新字符串为X的扩展串,如字符串X为”abcbcd”,则字符串“abcb_c_”,“_a_bcbcd_”和“abcb_c_”都是X的扩展 ...
- AC日记——有趣的跳跃 openjudge 1.6 07
07:有趣的跳跃 总时间限制: 1000ms 内存限制: 65536kB 描述 一个长度为n(n>0)的序列中存在“有趣的跳跃”当前仅当相邻元素的差的绝对值经过排序后正好是从1到(n-1). ...
- java 22 - 7 多线程之控制线程的方法
线程休眠(让线程休息一会再运行) public static void sleep(long millis) 在自定义线程类中添加该方法. 更改后,运行测试类,结果就是每执行完一轮,就休息1秒(这里设 ...