【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 ...
随机推荐
- win10 死机
其实Win10系统还是不错的,如果你的电脑升级Win10后中招经常死机,可以用下面的方案来应对. 1.下载安装支持兼容Win10的软件版本,下载软件之前看一下兼容列表里是否有Win10系统.虽然Win ...
- python学习笔记02:运行python程序
1.启动cmd命令行,输入python后回车,运行python解释器: 输入python代码后回车: print('Hello World')
- iOS AVAudioPlayer播放音频时声音太小
iOS AVAudioPlayer播放音频时声音太小 //引入AVFoundation类库,设置播放模式就可以了 do { try AVAudioSession.sharedInstance().ov ...
- Qt Creator子目录项目-类似VS解决方案
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt Creator子目录项目-类似VS解决方案 本文地址:http://techie ...
- vi/sed等遵循的搜索正则语法
转自:http://blog.csdn.net/lanxinju/article/details/5731843 一.查找 查找命令 /pattern<Enter> :向下查找patter ...
- WebKit 源码分析 -- loader
原文地址: http://peirenlei.iteye.com/blog/1718569 摘要:本文介绍 WebCore 中 Loader 模块是如何加载资源的,分主资源和派生资源分析 loader ...
- MySQL event调度
基本命令 //查看事件调度是否开启 show variables like '%event_scheduler%'; //开启事件调度 SET GLOBAL event_scheduler = ON; ...
- cf Round 587
A.Duff and Weight Lifting(思维) 显然题目中只有一种情况可以合并 2^a+2^a=2^(a+1).我们把给出的mi排序一下,模拟合并操作即可. # include <c ...
- POJ1741:Tree——题解+树分治简要讲解
http://poj.org/problem?id=1741 题目大意:给一棵树,求点对间距离<=k的个数. ———————————————————— 以这道题为例记录一下对于树分治的理解. 树 ...
- BZOJ4815 [CQOI2017]小Q的表格 【数论 + 分块】
题目链接 BZOJ4815 题解 根据题中的式子,手玩一下发现和\(gcd\)很像 化一下式子: \[ \begin{aligned} bf(a,a + b) &= (a + b)f(a,b) ...