【Foreign】Walk [暴力]
Walk
Time Limit: 20 Sec Memory Limit: 256 MB
Description

Input

Output

Sample Input
3
1 2 3
1 3 9
Sample Output
9
3
0
HINT

Solution

其实吧,就是每次枚举一个d,重新构图,把权值是 d 的倍数的边加入。然后Dfs暴力求一遍直径L,显然 [1, L] 都可以用 d 更新。
重点是在于复杂度的证明吧,证明在上面qwq(BearChild当时不敢写qaq)。
Code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
typedef long long s64; const int ONE = ;
const int INF = ; int n;
int x, y, z;
int Maxval, S[ONE];
int Ans[ONE]; vector <int> D[ONE], G[ONE]; struct power
{
int x, y, z;
}a[ONE]; int get()
{
int res=,Q=;char c;
while( (c=getchar())< || c> )
if(c=='-')Q=-;
res=c-;
while( (c=getchar())>= && c<= )
res=res*+c-;
return res*Q;
} void Dealiv()
{
for(int i = ; i <= n; i++)
{
int Q = sqrt(a[i].z);
for(int j = ; j <= Q; j++)
if(a[i].z % j == )
{
D[j].push_back(i);
if(a[i].z / j != j) D[a[i].z / j].push_back(i);
}
}
} int vis[ONE], length, A1, Record;
int next[ONE], first[ONE], go[ONE], tot; void Add(int u, int v)
{
next[++tot] = first[u]; first[u] = tot; go[tot] = v;
next[++tot] = first[v]; first[v] = tot; go[tot] = u;
} void Dfs1(int u, int father, int dep)
{
if(length < dep) {A1 = u, length = dep;}
for(int e = first[u]; e; e = next[e])
{
int v = go[e];
if(v == father || vis[v]) continue;
Dfs1(v, u, dep + );
}
} void Dfs2(int u, int father, int dep)
{
vis[u] = ;
length = max(length, dep);
for(int e = first[u]; e; e = next[e])
{
int v = go[e];
if(v == father || vis[v]) continue;
Dfs2(v, u, dep + );
}
} int main()
{
n = get();
for(int i = ; i < n; i++)
a[i].x = get(), a[i].y = get(), a[i].z = get(), Maxval = max(Maxval, a[i].z); Dealiv(); int res = ; for(int i = ; i <= Maxval; i++)
{
int len = D[i].size(), cnt = ; tot = ; for(int j = ; j < len; j++)
{
int id = D[i][j];
Add(a[id].x, a[id].y);
S[++cnt] = a[id].x, S[++cnt] = a[id].y;
} Record = ;
for(int j = ; j <= cnt; j++)
if(!vis[S[j]])
{
A1 = length = ; Dfs1(S[j], , );
length = ; Dfs2(A1, , );
Record = max(Record, length);
} for(int j = ; j <= cnt; j++)
first[S[j]] = , vis[S[j]] = ; Ans[Record] = i;
} for(int i = n; i >= ; i--)
Ans[i] = max(Ans[i + ], Ans[i]); for(int i = ; i <= n; i++)
printf("%d\n", Ans[i]);
}
【Foreign】Walk [暴力]的更多相关文章
- HDU 5001 Walk (暴力、概率dp)
Walk Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Sub ...
- Educational Codeforces Round 92 (Rated for Div. 2) B、C题解
TAT 第一场codeforces B. Array Walk #暴力 #贪心 题目链接 题意 有\(a1, a2, ..., an\) 个格子(每个格子有各自分数),最初为1号格(初始分数为\(a1 ...
- 【Foreign】朗格拉日计数 [暴力]
朗格拉日计算 Time Limit: 10 Sec Memory Limit: 128 MB Description Input Output 仅一行一个整数表示答案. Sample Input 5 ...
- 【Foreign】红与蓝 [暴力]
红与蓝 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 2 2 0 1 -1 -1 2 0 ...
- 【Foreign】冒泡排序 [暴力]
冒泡排序 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output 仅一行一个整数表示答案. Sample Input 4 5 ...
- HDU5584 LCM Walk 数论
LCM Walk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分
Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力
D. Vanya and Treasure 题目连接: http://www.codeforces.com/contest/677/problem/D Description Vanya is in ...
- Codeforces Round #352 (Div. 1) A. Recycling Bottles 暴力
A. Recycling Bottles 题目连接: http://www.codeforces.com/contest/671/problem/A Description It was recycl ...
随机推荐
- 让我们一起来做最漂亮的Android界面吧!
让我们一起来做最漂亮的Android界面吧! AndroidiOS产品设计 摘要:如何为Android设备量身定制以打造出最为完美的应用?这是让诸多开发者很是头疼的问题.不同于iOS,Android设 ...
- TensorFlow安装解惑
本文整理自网络,若有侵犯请告知. 1.安装环境 目前TensorFlow社区推荐的环境是Ubuntu, 但是TensorFlow同时支持Mac,Windows上的安装部署. 2.关于GPU版本 因为深 ...
- iOS-修改导航栏文字字体和颜色
//修改导航栏文字字体和颜色 nav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[RGBColor co ...
- Css入门课程 Css文本样式
文字是网页的非常基础的内容,文本的样式设置也是非常重要的 1 字体大小 font-size:绝对大小单位和相对大小单位,绝对大小单位有cm,mm in(厘米,毫米,英寸)等,这是大小不随屏幕分辨率大小 ...
- 【alpha】Scrum站立会议第1次····10.16
小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app 1.任务进度 成员 已完成 当日要完成 李权 搭建好Android Studio环境 数据库设计 于淼 搭建好Andro ...
- MySQL优化之profile
分析SQL执行带来的开销是优化SQL的重要手段.在MySQL数据库中,可以通过配置profiling参数来启用SQL剖析.该参数可以在全局和session级别来设置.对于全局级别则作用于整个MySQL ...
- [剑指Offer] 66.机器人的运动范围
题目描述 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时,机器人能 ...
- ueditor 定制工具栏图标
在使用Ueditor时,如要简化工具栏上的按钮,可以修改配置项的方法: 1. 方法一:修改 ueditor.config.js 里面的 toolbars 2. 方法二:实例化编辑器的时候传入 tool ...
- bzoj3473字符串&bzoj3277串
题意:给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串.注意本质相同的子串多次出现算多次,如1 1 aaa这组数据答案为6,贡献1WA.代码里有些部分是为了 ...
- HTML5 不再支持哪些元素
HTML5中已经不支持以下的元素,不建议在进行开发时再使用以下的元素. 1.acronym(建议abbr):定义首字母缩写 2.applet(建议object):定义applet 3.basefont ...