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 ...
随机推荐
- linux 中/proc 详解
proc 文件系统 在Linux中有额外的机制可以为内核和内核模块将信息发送给进程-- /proc 文件系统.最初设计的目的是允许更方便的对进程信息进行访问(因此得名),现在它被每一个有有趣的东西报告 ...
- Android UI编程(1)——九宫格(GridView)
(转自:http://blog.csdn.net/Thanksgining/article/details/42968847) 参考博客:http://blog.csdn.net/xyz_lmn/ar ...
- Android之drawable state各个属性详解
android:drawable 放一个drawable资源android:state_pressed 是否按下,如一个按钮触摸或者点击.android:state_focused 是否取得焦点,比如 ...
- C++ 笔记(二) —— 不要在构造和析构函数中调用虚函数
ilocker:关注 Android 安全(新手) QQ: 2597294287 class Transaction { //所有交易的 base class public: Transaction( ...
- mysql 性能优化方案 (转)
网 上有不少mysql 性能优化方案,不过,mysql的优化同sql server相比,更为麻烦与复杂,同样的设置,在不同的环境下 ,由于内存,访问量,读写频率,数据差异等等情况,可能会出现不同的结果 ...
- AC小笔记
1:基本库函数的使用 Rand()函数,可以产生0~32767之间的随机数. a+rand()%(b-a) 可以得到 [a,b] 之间的随机数. 2:基本数据类型的使用 可以使用强制类型转换 例如: ...
- 初识Struts2
一.Struts2入门案例 ①引入jar包 ②在src下创建struts.xml配置文件 <?xml version="1.0" encoding="UTF-8&q ...
- Unity Profiler 性能分析
Profiler窗口 1. CPU A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间 B. Overhead: Pro ...
- SpringMVC讲解
2.1.Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职 ...
- 如何自学Android
看到很多人提问非科班该如何学习编程,其实科班也基本靠自学.有句话叫"师傅领进门修行靠个人",再厉害的老师能教你的东西都是很有限的,真正的修行还是要靠自己.博主本科是数学专业,虽研究 ...