Codeforces Round #430 (Div. 2) C Ilya And The Tree SET暴力
来源:互联网 发布:cf刷枪软件视频教程 编辑:程序博客网 时间:2024/06/07 00:24
原题链接:http://codeforces.com/contest/842/problem/C
大致题意:给出一颗有点权的树,令一个点的beauty值=在可以修改一个点的点权为零也可以不修改的情况下,从根节点到这个点的路上所有点的点权的总GCD。
就是说,对于每一各点v, beauty(v)=gcd( val[a1],val[a2],val[a3]……,val[ak] ),a1……ak是从点v到根节点(1号节点)的路径上所有点或路径上所有点除去某一个点,val[x]代表节点x的权值。
问:每个点最大的beauty值是多少。
每个点的beauty值都有很多种可能,但是,获得每个点的beauty值的决策方法和其父亲有一定的关系,就是要么是修改其父亲或是其父亲的祖先节点的权值为0,要么是修改此节点的权值为0,如果用个容器V记录每个点可能的beauty值,那么V(son)=gcd( V( fa ) , val[ son ] )∪ gcd ( 从父节点到根节点的所有点的权值 ) )。这样子做的话看起来会使得复杂度变为n^2,因为对于每个点x上的决策种类有deep[x]个。但是其实V(x)内的不同的beauty值的数量非常少,想想看,根节点root和节点x其中至少有一个不会被修改权值,则点x的beauty值必定是val [root] 或 val[ x ]的因子,而一个数n的因子数量肯定不会超过log(n)。所以其实去重以后beauty集合的大小不会超过log(n)个,所以即使获得所有节点的可能beauty值的集合,总大小也不会超过n*log(n)个。于是如果用set来去重的话,总时间复杂度就是O(n*log(n)*log(n) )
代码:
#include <bits/stdc++.h>using namespace std;inline void read(int &x) { char ch; bool flag = false; for (ch = getchar(); !isdigit(ch); ch = getchar())if (ch == '-') flag = true; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()); x = flag ? -x : x;}inline void read(long long &x) { char ch; bool flag = false; for (ch = getchar(); !isdigit(ch); ch = getchar())if (ch == '-') flag = true; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()); x = flag ? -x : x;}const int maxn=3200000;int val[maxn];int n;set<int> V[maxn];vector<int> g[maxn];int gcd(int a,int b){if (b==0) return a;return gcd( b , a%b );}void dfs(int x, int fa ,int tmp ){for ( auto s:V[fa] ) V[ x ] .insert( gcd ( s , val[x] ) );V[ x ] .insert ( tmp );tmp = gcd ( tmp , val[ x ] );for ( auto son:g[x] ) if ( son != fa ) dfs( son , x , tmp);}int main(){ read(n); for (int i=1;i<=n;i++) read(val[i]); for (int i=1;i<n;i++) { int x,y; read(x); read(y); g[x].push_back( y ); g[y].push_back( x ); } V[1].insert( val[ 1 ] ); dfs(1,0,0); for (int i=1;i<=n;i++) printf("%d ", *V[i].rbegin() ); return 0;}
- Codeforces Round #430 (Div. 2) C Ilya And The Tree SET暴力
- Codeforces Round #430 (Div. 2) 842C Ilya And The Tree(暴力)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs+最大公约数+因子+树)
- Codeforces Round #430 (Div. 2)C. Ilya And The Tree dfs D
- Codeforces Round #430(Div.2)Problem C Ilya And The Tree(DFS)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree 树dp 统计
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(补题)
- Codeforces 430 Div 2 C.Ilya And The tree
- Codeforces Round #430 (Div. 2) Ilya And The Tree 树上因子 思维 + dfs
- Codeforces Round #430 (Div. 2)-搜索&STL-Ilya And The Tree
- Codeforces Round #297 (Div. 2) C.Ilya and Sticks
- Codeforces Round #297 (Div. 2) C - Ilya and Sticks
- Codeforces Round #297 (Div. 2) C. Ilya and Sticks
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks
- Python学习笔记 --- python求昨天的时间
- 朋友数(PAT-1064)
- 丰富
- Ubuntu下编译VLC Android源码so库详解
- Tomcat编译JSP页面生成Servlet文件(*.class & *.java)的存放位置
- Codeforces Round #430 (Div. 2) C Ilya And The Tree SET暴力
- Pycharm界面的子窗口不见了,怎么办?
- 第十二课记录 Redis
- NDK开发——JNI必须掌握的常用例子
- mysql导入dmp文件
- 仿照springmvc 手打框架
- 2017广西邀请赛/hdu6186 CS Course (维护前缀和后缀)
- cmd打开mysql的正确方式
- 欢迎使用CSDN-markdown编辑器