Tourists
Tourists
时间限制: 5 Sec 内存限制: 64 MB
题目描述
You are a member of the Tree City planning committee. After much research into tourism, your committee has discovered a very interesting fact about tourists: they LOVE number theory! A tourist who visits an attraction with label x will then visit another attraction with label y if y > x and y is a multiple of x. Moreover, if the two attractions are not directly connected by a road thetourist will necessarily visit all of the attractions on the path connecting x and y, even if they aren’t multiples of x. The number of attractions visited includes x and y themselves. Call this the length of a path.
Consider this city map:

Here are all the paths that tourists might take, with the lengths for each:
1 → 2 = 4, 1 → 3 = 3, 1 → 4 = 2, 1 → 5 = 2, 1 → 6 = 3, 1 → 7 = 4,
1 → 8 = 3, 1 → 9 = 3, 1 → 10 = 2, 2 → 4 = 5, 2 → 6 = 6, 2 → 8 = 2,
2 → 10 = 3, 3 → 6 = 3, 3 → 9 = 3, 4 → 8 = 4, 5 → 10 = 3
To take advantage of this phenomenon of tourist behavior, the committee would like to determine the number of attractions on paths from an attraction x to an attraction y such that y > x and y is a multiple of x. You are to compute the sum of the lengths of all such paths. For the example above, this is: 4 + 3 + 2 + 2 + 3 + 4 + 3 + 3 + 2 + 5 + 6 + 2 + 3 + 3 + 3 + 4 + 3 = 55.
输入
integers i and j (1 ≤ i < j ≤ n), denoting that attraction i and attraction j are directly connected by a road. It is guaranteed that the set of attractions is connected.
输出
样例输入
10
3 4
3 7
1 4
4 6
1 10
8 10
2 8
1 5
4 9
样例输出
55
分析:LCA裸题;
注意dfs层数太深会爆,所以需要手写栈;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,p[maxn<<],all,dep[maxn],tot,h[maxn],vis[maxn<<],vis1[maxn],fa[maxn],st[][maxn<<];
void init()
{
for(int i=;i<=all;i++)p[i]=+p[i>>];
for(int i=;i<=;i++)
for(int j=;(j+(<<i)-)<=(ll)all;j++)
st[i][j]=min(st[i-][j],st[i-][j+(<<(i-))]);
}
int query(int l,int r)
{
int x=p[r-l+];
return min(st[x][l],st[x][r-(<<x)+]);
}
struct node
{
int to,nxt;
}e[maxn<<];
void add(int x,int y)
{
tot++;
e[tot].to=y;
e[tot].nxt=h[x];
h[x]=tot;
}
stack<int>S;
void dfs()
{
S.push();
while(!S.empty())
{
int now = S.top();
if(vis1[now] == )// if node is gray, then color black
{
vis1[now] = ;
st[][++all]=dep[fa[now]];
// do things after dfs children.
S.pop();
}
else if(vis1[now] == )// if node is white, then color gray
{
vis1[now] = ;
st[][++all]=dep[now];
vis[now]=all;
// do things before dfs children.
for(int i=h[now];i;i=e[i].nxt)
{
int to=e[i].to;
if(!vis1[to])
{
dep[to]=dep[now]+;
fa[to]=now;
S.push(to);
}
}
}
}
}
ll ans;
int main()
{
int i,j;
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
ans=;
all=;
tot=;
memset(dep,,sizeof(dep));
memset(vis,,sizeof(vis));
memset(h,,sizeof(h));
memset(p,,sizeof(p));
memset(vis1,,sizeof(vis1));
rep(i,,n-)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b),add(b,a);
}
dfs();
init();
for(i=;i<=n;i++)
{
for(j=i*;j<=n;j+=i)
{
ans+=dep[i]+dep[j]-*query(min(vis[i],vis[j]),max(vis[i],vis[j]))+;
}
}
printf("%lld\n",ans);
}
//system("Pause");
return ;
}
Tourists的更多相关文章
- 【Codefoces487E/UOJ#30】Tourists Tarjan 点双连通分量 + 树链剖分
E. Tourists time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard inpu ...
- CF487 E. Tourists [点双连通分量 树链剖分 割点]
E. Tourists 题意: 无向连通图 C a w: 表示 a 城市的纪念品售价变成 w. A a b: 表示有一个游客要从 a 城市到 b 城市,你要回答在所有他的旅行路径中最低售价的最低可能值 ...
- 【CF487E】Tourists(圆方树)
[CF487E]Tourists(圆方树) 题面 UOJ 题解 首先我们不考虑修改,再来想想这道题目. 我们既然要求的是最小值,那么,在经过一个点双的时候,走的一定是具有较小权值的那一侧. 所以说,我 ...
- 每日英语:Foreign Tourists Skip Beijing
Overseas tourists continued to shun Beijing through 2013. shun:避开,避免,回避 Amid rising pollution and a ...
- L192 Virgin Galactic Completes Test of Spaceship to Carry Tourists
Virgin Galactic says its spacecraft designed to launch tourists into space completed an important te ...
- UOJ #30. [CF Round #278] Tourists
UOJ #30. [CF Round #278] Tourists 题目大意 : 有一张 \(n\) 个点, \(m\) 条边的无向图,每一个点有一个点权 \(a_i\) ,你需要支持两种操作,第一种 ...
- [UOJ30]/[CF487E]Tourists
[UOJ30]/[CF487E]Tourists 题目大意: 一个\(n(n\le10^5)\)个点\(m(m\le10^5)\)条边的无向图,每个点有点权.\(q(q\le10^5)\)次操作,操作 ...
- Tourists——圆方树
CF487E Tourists 一般图,带修求所有简单路径代价. 简单路径,不能经过同一个点两次,那么每个V-DCC出去就不能再回来了. 所以可以圆方树,然后方点维护一下V-DCC内的最小值. 那么, ...
- 【学习笔记】圆方树(CF487E Tourists)
终于学了圆方树啦~\(≧▽≦)/~ 感谢y_immortal学长的博客和帮助 把他的博客挂在这里~ 点我传送到巨佬的博客QwQ! 首先我们来介绍一下圆方树能干什么呢qwq 1.将图上问题简化到树上问题 ...
随机推荐
- jQuery获取元素的兄弟节点的几种方法
$('#id').siblings() //当前元素所有的兄弟节点 $('#id').prev() //当前元素前一个兄弟节点 $('#id').prevaAll() //当前元素之前所有的兄弟节点 ...
- apache动态添加模块
Apache已经安装完毕并投入运行,但是后来却发现部分模块没有加载,当然有两个方法: 1. 一是完全重新编译Apache, 再安装 2. 编译模块为SO文件,使用LoadModule指令加载扩展模块. ...
- post请求和get请求
get请求在链接后面带参数,容易出现乱码,是坑(慎用),有固定的长度 一般的用的就是post方式 <form action="<%=basePath%>upload&quo ...
- POJ 1922 Ride to School#贪心
(- ̄▽ ̄)-* //C跟着a君骑,然后更快的b君来了,C又跟着b君骑, //接着最快的d君来了,C就去跟着d君了, //最后最快的d君到达目的地时,C也就到了 //所以C的到达时间,就是最早到达的那 ...
- #if defined和#if !defined(c语言的宏定义)
我们要检查a是否定义 #if defined a #undef a #define a 200 #endif 上述语句检验a是否被定义,如果被定义,则用#undef语句解除定义,并重新定义a为200 ...
- HDU 2216 Game III(BFS)
Game III Problem Description Zjt and Sara will take part in a game, named Game III. Zjt and Sara wil ...
- Gentoo 无线网络配置 wpa_supplicant
安装 安装net-wireless/wpa_supplicant包 emerge --ask wpa_supplicant 启动网络 为wpa_supplicant添加无线接口 在wpa_suppli ...
- Win7/Win8右键菜单管理工具(Easy Context Menu) v1.5 绿色版
软件名称: Win7/Win8右键菜单管理工具(Easy Context Menu)软件语言: 简体中文授权方式: 免费软件运行环境: Win8 / Win7 / Vista / WinXP软件大小: ...
- 【Machine Learning in Action --3】决策树ID3算法预测隐形眼睛类型
本节讲解如何预测患者需要佩戴的隐形眼镜类型. 1.使用决策树预测隐形眼镜类型的一般流程 (1)收集数据:提供的文本文件(数据来源于UCI数据库) (2)准备数据:解析tab键分隔的数据行 (3)分析数 ...
- Prime Path(BFS)
Prime Path Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...