当前位置:网站首页>CF1320E Treeland and Viruses
CF1320E Treeland and Viruses
2022-07-30 13:40:00 【With a cool moon】
CF1320E Treeland and Viruses
仔细观察题目,Go directly to the virtual tree.
Consider the trick again dp,I found that there are many priorities that are not very easy to do.
考虑 spfa,After a spot is found to be infected,Other viruses can't get past this point,不太好做.
Consider priority queues,The optimal point is updated each time,Clearly correct.
时间复杂度 O ( n log n ) \mathcal O(n \log n) O(nlogn).
#include<bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define ha putchar(' ')
#define he putchar('\n')
inline int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = getchar();
return x * f;
}
inline void write(int x)
{
if(x < 0)
{
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
const int _ = 2e5 + 10;
int n, Q, k, m, t, s[_], w[_];
bool vis[_];
int cnt, dfn[_], fa[_], siz[_], hson[_], top[_], dep[_];
vector<int> d[_], v, p, q, a, b, g[_];
bool cmp(int u, int v)
{
return dfn[u] < dfn[v];
}
void dfs1(int u, int D = 1)
{
dep[u] = D, siz[u] = 1, dfn[u] = ++cnt;
for(int v : d[u])
{
if(siz[v]) continue;
fa[v] = u;
dfs1(v, D + 1);
siz[u] += siz[v];
if(siz[hson[u]] < siz[v]) hson[u] = v;
}
}
void dfs2(int u, int tf)
{
top[u] = tf;
if(hson[u]) dfs2(hson[u], tf);
for(int v : d[u])
{
if(top[v]) continue;
dfs2(v, v);
}
}
int LCA(int u, int v)
{
// cout << "1\n";
while(top[u] != top[v])
{
// cout << "2\n";
if(dep[top[u]] < dep[top[v]]) swap(u, v);
u = fa[top[u]];
}
// cout << "3\n";
return dep[u] > dep[v] ? v : u;
}
int Dis(int x, int y)
{
return dep[x] + dep[y] - 2 * dep[LCA(x, y)];
}
void build()
{
q = p;
for(int x : p)
{
int lca = LCA(x, s[t]);
if(lca != s[t])
{
while(t && dep[s[t - 1]] >= dep[lca])
{
g[s[t]].push_back(s[t - 1]), g[s[t - 1]].push_back(s[t]);
--t;
}
if(s[t] != lca)
{
g[s[t]].push_back(lca), g[lca].push_back(s[t]);
s[t] = lca;
q.push_back(lca);
}
}
s[++t] = x;
}
if(t)
while (--t) g[s[t]].push_back(s[t + 1]), g[s[t + 1]].push_back(s[t]);
p = q;
}
struct abc
{
int d, i, x, r;
abc()
{
}
abc(int d, int i, int x) : d(d), i(i), x(x)
{
// if(!w[x])
// {
cout << x << "\n";
cout << "!!!!!!!!!\n";
// exit(0);
// }
r = d ? (d - 1) / w[x] : -1;
}
bool operator < (const abc o) const
{
return r == o.r ? i < o.i : r < o.r;
}
} dis[_];
struct Node
{
abc a; int id;
bool operator < (const Node &t) const
{
return a.r == t.a.r ? a.i > t.a.i : a.r > t.a.r;
}
};
void dij()
{
priority_queue<Node> q;
for(int x : p)
{
dis[x].r = dis[x].i = n;
vis[x] = 0;
}
for(int i = 0; i < a.size(); i++)
{
dis[a[i]] = abc(0, i + 1, a[i]);
q.push({
dis[a[i]], a[i]});
}
while(!q.empty()) {
int x = q.top().id;
q.pop();
if (vis[x]) continue;
vis[x] = 1;
// cout <<x << "\n";
for (int y : g[x])
{
// cout << ": " << y << "\n";
abc nw = abc(dis[x].d + Dis(x, y), dis[x].i, dis[x].x);
if (nw < dis[y])
{
dis[y] = nw;
q.push({
dis[y], y});
}
}
}
for(int x : b) write(dis[x].i), ha;
he;
}
signed main()
{
n = read();
for(int i = 1, u, v; i < n; ++i)
{
u = read(), v = read();
d[u].push_back(v);
d[v].push_back(u);
}
dfs1(1), dfs2(1, 1);
Q = read();
while(Q--)
{
k = read(), m = read();
v.clear(), p.clear(), a.clear(), q.clear(), b.clear();
for(int i = 1, x, y; i <= k; ++i)
{
x = read(), y = read();
a.push_back(x);
v.push_back(x), w[x] = y;
}
for(int i = 1, x; i <= m; ++i)
{
x = read();
b.push_back(x);
v.push_back(x);
}
sort(v.begin(), v.end(), cmp);
for(int i = 0; i < v.size() - 1; ++i)
if(v[i] != v[i + 1]) p.push_back(v[i]);
p.push_back(v[v.size() - 1]);
build();
// for(int x : p) cout << x <<" ";
// cout << "!!\n";
dij();
for(int x : p) g[x].clear();
}
}
/* 7 1 2 1 3 2 4 2 5 3 6 3 7 3 2 2 4 1 7 1 1 3 2 2 4 3 7 1 1 3 3 3 1 1 4 100 7 100 1 2 3 */
边栏推荐
- 【软考软件评测师】自动化测试章节下篇
- There is a risk of water ingress in the battery pack tray and there is a potential safety hazard. 52,928 Tang DMs are urgently recalled
- Mac Brew 安装PHP
- dolphinscheduler simple task definition and complex cross-node parameter transfer
- R语言ggplot2可视化时间序列数据(默认时间中断部分前后自动连接起来)、创建时间分组、使用分面图(faceting)可视化时间序列数据
- 阿里 P7 到底是怎样的水平?
- 电池包托盘有进水风险,存在安全隐患,紧急召回52928辆唐DM
- ES6 Set与Map是什么,如何使用
- 腾讯称电竞人才缺口200万;华为鸿蒙3.0正式发布;乐视推行每周工作4天半?...丨黑马头条...
- R语言时间序列数据算术运算:使用log函数将时间序列数据的数值对数化(平方、开平方、指数化等函数类似使用)
猜你喜欢
随机推荐
jsArray array copy method performance test 2207300823
58. 最后一个单词的长度
canvas彩虹桥动画js特效
Markdown 1 - 图文音视频等
【河北工业大学】考研初试复试资料分享
阿里 P7 到底是怎样的水平?
odoo--qweb模板介绍(一)
Analysis of AI recognition technology and application scenarios of TSINGSEE intelligent video analysis gateway
cpu/CS and IP
腾讯称电竞人才缺口200万;华为鸿蒙3.0正式发布;乐视推行每周工作4天半?...丨黑马头条...
SQL 改写系列七:谓词移动
群晖系统安装相关文件分享
There is a risk of water ingress in the battery pack tray and there is a potential safety hazard. 52,928 Tang DMs are urgently recalled
jsArray数组复制方法性能测试2207292307
Study Notes - Becoming a Data Analyst in Seven Weeks "Week 2: Business": Business Analysis Metrics
strlen跟sizeof区别
第十三天笔记
Eleven BUUCTF questions (06)
for循环的3个表达式执行顺序
二手手机销量突破3亿部,与降价的iPhone夹击国产手机









