当前位置:网站首页>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 */
边栏推荐
- 第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用
- matlab画图,仅显示部分图例
- 剑指 Offer 05. 替换空格
- AT4108 [ARC094D] Normalization
- Smart pointer implementation conjecture
- jsArray array copy method performance test 2207300040
- Scala基础:数组(Array)、映射(Map)、元组(Tuple)、集合(List)
- 20220729 Securities, Finance
- [PostgreSQL] - Storage structure and cache shared_buffers
- 第十五天笔记
猜你喜欢

ML之PDP:基于FIFA 2018 Statistics(2018年俄罗斯世界杯足球赛)球队比赛之星分类预测数据集利用DT决策树&RF随机森林+PDP部分依赖图可视化实现模型可解释性之详细攻略

第十三天笔记

缓存一致性

【河北工业大学】考研初试复试资料分享

一本通循环结构的程序设计题解(2)

树形dp小总结(换根,基环树,杂七杂八的dp)

Apache Log4j2漏洞

第十五天笔记

ENVI Image Processing (6): NDVI and Vegetation Index

No-code development platform all application settings introductory tutorial
随机推荐
程序员修炼之道:务以己任,实则明心——通向务实的最高境界
权威推荐!腾讯安全DDoS边缘安全产品获国际研究机构Omdia认可
阿里 P7 到底是怎样的水平?
ES6 Set与Map是什么,如何使用
jsArray array copy method performance test 2207300040
dolphinscheduler simple task definition and complex cross-node parameter transfer
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(ylim、修改可视化图像y轴坐标轴数值范围)
TaskDispatcher source code parsing
自从外包干了四年,基本废了...
二手手机销量突破3亿部,与降价的iPhone夹击国产手机
【河北工业大学】考研初试复试资料分享
Yilian: Activating the Value Potential of Data Elements and Unleashing the Innovation Dividend of SAS SSD
[PostgreSQL] - explain SQL analysis introduction
“12306” 的架构到底有多牛逼
CF338E Optimize!
CF1320E Treeland and Viruses
grep时排除指定的文件和目录
CF1677E Tokitsukaze and Beautiful Subsegments
机器学习——特征选择
一本通循环结构的程序设计题解(2)