当前位置:网站首页>codeforces 1708E - DFS Trees
codeforces 1708E - DFS Trees
2022-07-27 14:14:00 【juraws】
E - DFS Trees
prob.: Give me a n spot m Undirected graph of edges and a wrong solution MST Code for , Ask which points are the roots MST That's right.
ideas: There is no edge with equal weight , combination MST The nature of , When there is a ring ,MST Always round off the largest edge in the ring
Consider that each point on the ring is the first point traversed by the algorithm given by the question in the point set on the ring , It is found that only when this point is the endpoint of the largest edge ( This is the picture 1 Medium u or v)
Consider the whole picture , Some points as a starting point must be illegal ( On the division ring u,v The direction of other points ( To put it another way, only u,v And their non ring direction is feasible ( This is the picture 1 Part of the middle green circle )
Consider marking points -> Dyeing on trees -> The difference in the tree ( Refer to Xinjun b Station video solution )
In the right MST Build up trees , The largest edge of each ring is not MST On the edge of
For each point, if point weight >0, Indicates that it has been marked , That is, it is an illegal point in at least one ring
Consider two situations ( Pictured 2 Shown )
- u,v Of lca No u or v, Consider the weight of the whole number ++ then u,v Weight of subtree –
- u,v Of lca yes u or v, set up lca==u, Consider for u My son's weight ++,v Weight of subtree –

code:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
vector<int> g[N];
int pa[N], depth[N], f[N][30];
int a[N], b[N];
int Find(int x) {
if (pa[x] == x) return x;
return pa[x] = Find(pa[x]);
}
void Union(int x, int y) {
int fax = Find(x);
int fay = Find(y);
if (fax == fay) return;
pa[fax] = fay;
}
struct edge {
int u, v;
};
vector<edge> edges, vec;
void bfs(int rt) {
queue<int> que;
que.push(rt);
while (!que.empty()) {
int tmp = que.front();
que.pop();
for (auto u : g[tmp]) {
if (u == pa[tmp]) continue;
depth[u] = depth[tmp] + 1;
que.push(u);
f[u][0] = pa[u];
for (int k = 1; k < 20; ++k) {
f[u][k] = f[f[u][k - 1]][k - 1];
}
}
}
}
int lca(int a, int b) {
if (depth[a] < depth[b]) swap(a, b);
for (int k = 19; k >= 0; --k) {
if (depth[f[a][k]] >= depth[b]) {
a = f[a][k];
}
}
if (a == b) return a;
for (int k = 19; k >= 0; --k) {
if (f[a][k] != f[b][k]) {
a = f[a][k];
b = f[b][k];
}
}
return f[a][0];
}
int getPa(int x, int num) {
for (int k = 19; k >= 0; --k) {
if ((num >> k) & 1) x = f[x][k];
}
return x;
}
void dfs(int x, int sum){
a[x] = sum + b[x];
for(auto to : g[x]) {
if(to == pa[x]) continue;
dfs(to, a[x]);
}
}
void dfsPa(int x) {
for(auto to : g[x]) {
if(to == pa[x]) continue;
pa[to] = x;
dfsPa(to);
}
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) pa[i] = i;
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
edges.push_back({
u, v});
}
for (auto tt :edges) {
int u = tt.u;
int v = tt.v;
if (Find(u) == Find(v)) {
vec.push_back(tt);
continue;
}
g[u].push_back(v);
g[v].push_back(u);
Union(u, v);
}
int rt = 1;
pa[rt] = -1;
dfsPa(rt);
depth[rt] = 1, depth[0] = 0;
pa[rt] = 0;
bfs(rt);
for (auto tt : vec) {
int u = tt.u;
int v = tt.v;
if (lca(u, v) != u && lca(u, v) != v) {
b[rt]++;
b[u]--;
b[v]--;
} else {
if (lca(u, v) == v) swap(u, v);
int s = getPa(v, depth[v] - depth[u] - 1);
b[s]++;
b[v]--;
}
}
dfs(rt, 0);
for (int i = 1; i <= n; ++i) {
cout << (a[i] > 0 ? 0 : 1);
}
return 0;
}
边栏推荐
- 阿里最新股权曝光:软银持股23.9% 蔡崇信持股1.4%
- A Keypoint-based Global Association Network for Lane Detection
- SLAM综述阅读笔记四:A Survey on Deep Learning for Localization and Mapping: Towards the Age of Spatial 2020
- 知识关联视角下金融证券知识图谱构建与相关股票发现
- log4j2 jdbc appender
- 【2022-07-25】
- Cognition -- classic of the road to success of hardware engineers
- Jing Xiandong and other senior executives of ant group no longer serve as Alibaba partners to ensure independent decision-making
- Chapter 3 business function development (add clues and remarks, and automatically refresh the added content)
- VSCode -- 创建模板文件
猜你喜欢

Design of LR1 compiler based on C language

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

Converter registration of easyexcel

【笔记】逻辑斯蒂回归

基于预训练模型的多标签专利分类研究

架构——MVC的升华

10 practical uses of NFT

How to make computers have public IP

Is it still time to take the PMP Exam in September?

VSCode -- 创建模板文件
随机推荐
Golang excellent open source project summary
【笔记】逻辑斯蒂回归
MySQL advanced II. Logical architecture analysis
np.arange()和 range()的用法及区别
【论文精读】Grounded Language-Image Pre-training(GLIP)
The universe has no end. Can utonmos shine the meta universe into reality?
[luogu_p4556] [Vani has an appointment] tail in rainy days / [template] segment tree merging
C#测量工具示意图
Leetcode · daily question · 592. fraction addition and subtraction · simulation
Is it still time to take the PMP Exam in September?
Utnet hybrid transformer for medical image segmentation
Download address of each version of libtorch
codeforces 1708E - DFS Trees
Small program completion work wechat campus laundry small program graduation design finished product (2) small program function
Lesson 3: reverse word order
spark job 使用log4j appender 追加日志到本地文件或者mysql
How to test and decrypt the encryption interface
达科为生物IPO过会:年营收8.37亿 吴庆军父女为实控人
Flexible and easy to use WYSIWYG visual report
Cultural tourism and data collection | travel to Yunnan in an artistic way