【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 ...
随机推荐
- c++SDK c#调用_疑难杂症
在编写过程中,会不时遇到各种问题: 1.dll明显在和exe同一目录下但调用不成功, 2.运行正常,没有报错,参数数值运行过程中也一致,但结果就是达不到预想, 都是dll没有引用完全造成的影响. 推荐 ...
- android入门 — PopupWindow
第一步:自定义xml文件 第二步:获取LayoutInflater对象 第三步:调用inflate()方法获取View对象 第四步:创建PopupWindow对象 第五步:调用PopupWindow的 ...
- Thinkphp5的使用phpmailer实现发邮件功能(163邮箱)
Thinkphp5本身并没有实现发邮件的功能,至少据我所知. 本文利用网易邮箱作为发邮件的邮箱.作为发送邮件的前提是需要开启SMTP服务,打开网易邮件,点击设置按钮,如下图所示 勾选smtp服务 保存 ...
- isset、is_null、empty的区别
版本:PHP 5.4 1.isset() :检测变量是否存在,测试如下: $a = false; $b = null; $c; $d = 0; $e = true; var_dump(isset($a ...
- 判断集合不为空Java
list.size>0判断集合size是否大于0 list.isEmpty()判断集合是否为空,返回布尔值
- 【python】如何查看已经安装的python软件包和版本
pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品. pip freeze可以查看已经安装的python软件包和版本 pip list 也可以
- 【转】自编码算法与稀疏性(AutoEncoder and Sparsity)
目前为止,我们已经讨论了神经网络在有监督学习中的应用.在有监督学习中,训练样本时有类别标签的.现在假设我们只有一个没带类别标签的训练样本集合 ,其中 .自编码神经网络是一种无监督学习算法,它使用了 ...
- 使用Xcode进行调试
目录 知己知彼 百战不殆抽刀断Bug 普通操作 全局断点(Global BreakPoint) 条件断点(Condational Breakpoints)打印的艺术 NSLog 开启僵尸对象(Enab ...
- BZOJ 1297 迷路(矩阵快速幂)
很容易想到记忆化搜索的算法. 令dp[n][T]为到达n点时时间为T的路径条数.则dp[n][T]=sigma(dp[i][T-G[i][n]]); 但是空间复杂度为O(n*T),时间复杂度O(n*n ...
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...