【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语言开发环境(vim配置,gcc) 部署J2EE网站(jdk,tomcat)
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . 博客总结 : 设置SecureCRT ...
- Java微笔记(1)
一,Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现数组的排序.搜索 1. 排序 语法: Arrays.sort(数 ...
- @ModelAttribute使用详解
1.@ModelAttribute注释方法 例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个control ...
- LintCode-38.搜索二维矩阵 II
搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没有重复 ...
- 201621044079WEEK作业08-集合
作业08-集合 1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 如 ...
- WPF值转换实例
WPF绑定功能非常方便,有时候点击某值时在另t一处显示此值的另一表现形式或调用其对应的其它值,用WPF值转换功能会很方便,下面就一LISTBOX和TEXTBLOCK控件,把LISTBOX中的值转换成除 ...
- 文件上传C:\fakepath\解决方案
1.设置IE:工具 -> Internet选项 -> 安全 -> 自定义级别 -> 找到“其他”中的“将本地文件上载至服务器时包含本地目录路径”,选中“启用”即可 2.利用js ...
- java.util.Stack类简介(栈)
Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来 Deque(双端队列)比起stack具有更好的完整性和一致性,应该被优先使用 ...
- POJ2942:Knights of the Round Table——题解
http://poj.org/problem?id=2942 所写的tarjan练习题最难的一道. 说白了难在考得不是纯tarjan. 首先我们把仇恨关系处理成非仇恨关系的图,然后找双连通分量,在双连 ...
- 【转】OSI详解
本文转自牛客网友CZ❤♡ღQM对在ISO/OSI参考模型中,网络层的主要功能是一题的回答. OSI ( Open System Interconnect ),即开放式系统互联. 一般都叫 OSI 参考 ...