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 ...
随机推荐
- android 反编译apktool工具
下载地址:http://pan.baidu.com/s/1bnHANtd 1.将编译的*.apk放在apktool的根目录下:2.双击“解压软件.bat”后,会提示完成:这样就反编译成功以:3.查看反 ...
- 初涉Linux ----------> Ubuntu15.04的安装与美化
“你玩 Linux 吗?” “什么 Linux ?” “你连 Linux 都不知道?还说是学计算机的呢...” “干嘛要用 Linux 啊?windows多好,Linux?没兴趣” 一. 前言 ...
- Hyper-V架构与VMware ESXi的差异
微软的Hyper-V与VMware的ESXi在架构上有众多不同,然而很多虚拟化管理员并未意识到这些差异.而且很多管理员同样对Hyper-V是在主机操作系统上运行感到困惑. 有关微软Hyper-V的一个 ...
- input文本框和img验证码对齐问题
左边放input,右边img验证码,然后一直不能对齐,如图: img老是比input高出一个头,然后调的头都大了还是不行,照例百度之, 给input和img都加一个 vertical-align:mi ...
- NOIP2009靶形数独[DFS 优化]
描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z博士请教,Z 博士拿出了他最近发明的“靶形数独 ...
- guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用
guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...
- IOS第四课——Autolayout_View
这节课,我们要学习MVC.Selector.Access Control.Extension.Auto Layout.Delegate-Protocol.Custom View. Auto Layou ...
- Html中模态框(弹出框)使用入门
作为html学习学习模态框需要二步: 效果图 第一步学习HTML中 div的弹出 ①触发按钮 <input class="btn btn-success" i ...
- 创建Visual studio项目模板 vstemplate关键点纪要
from:http://www.cnblogs.com/stickman/p/3454719.html 经过多次的实验,终于完美生成一个.VSIX的项目模板安装包,其中遇到不少问题与挫折,久经goog ...
- win7 远程桌面关机
在任务管理器中, 打开运行窗口, 执行 shutdown -s 命令, 将在30秒后关闭win7, 如果需要更快, 加上 -t 10 参数 关于 shutdown 的命令行说明: C:\Users\R ...