当前位置:网站首页>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 */
边栏推荐
猜你喜欢

人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准

shell script flow control statement

一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?

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

How to display an Excel table in the body of an email?

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

el-table中el-table-column下的操作切换class样式

创意loadingjs特效小点跳跃动画

jsArray array copy method performance test 2207292307

如何判断自己是否适合IT行业?方法很简单
随机推荐
grep时排除指定的文件和目录
for循环的3个表达式执行顺序
How to display an Excel table in the body of an email?
Smart pointer implementation conjecture
EasyNVS cloud management platform function reconstruction: support for adding users, modifying information, etc.
【Advanced Mathematics】【7】Double Integral
【23考研】408代码题参考模板——链表
Study Notes - Becoming a Data Analyst in Seven Weeks "Week 2: Business": Business Analysis Metrics
第十三天笔记
【VMware虚拟机安装mysql5.7教程】
Lake storehouse which electricity (2) of the project: project using technology and version and the environment
jsArray array copy method performance test 2207292307
CF1677E Tokitsukaze and Beautiful Subsegments
TaskDispatcher source code parsing
如何判断自己是否适合IT行业?方法很简单
自动化测试的生命周期是什么?
shell 编程规范与变量
Raja Koduri澄清Arc GPU跳票传闻 AXG年底前新推四条产品线
当下,产业园区发展面临的十大问题
CF780G Andryusha and Nervous Barriers