Viral infection

Link: https://ac.nowcoder.com/acm/contest/11371/B
Source: Niuke

The title describes
that clccle and rqy were walking on the streets of a certain country one day, but the witty rqy found that the pedestrians around were not quite right. They muttered words and said "sqn tql!", while walking aimlessly, clccle also After discovering this, they were surprised to find that this strange virus will spread to the surrounding cities and eventually infect the entire country. Because the Internet has collapsed, they have forgotten their city. The only thing they know is that the virus is from The current city where they are located has begun to spread, and the distance between all cities in this country and this city is the smallest (the distance between all roads is 1), now given you a map of the entire country, please help rqy and Clccle finds which city in this country they may be in now.
Enter the description:
two integers n, m, representing that there are n cities in this country, and there are only m roads between the cities

In the next m lines, two integers a, b in each line represent a connected road between cities a and b.
Output description:
multiple integers, output the possible points where clccle and rqy are currently located.
Example 1
Input
Copy
2 1
1 2
Output
Copy
1 2
Remarks:
For all data, 1<=m<=n<=50000

(The type of graph guarantees that there is no ring with a size greater than or equal to 3)

树形dp求出每个点到所有点的距离和,树形dp即可,关键是讨论出来父亲
对孩子的贡献,和孩子对父亲的贡献
第一次dfs的时候从下向上传递 , 此时dp[u]表示u点到子树中所有
点的最短距离之和 , x节点对当前节点的贡献是 dp[x] + size[x] , 其
含义在孩子节点x的基础上面,所有的子节点全部深度全部加一 ,
 也就是 + size[x]
 从上到下传递的时候,当前节点u对孩子节点的贡献是 dp[u] - dp[x]
  - size[x] + size[u] - size[x] ,解释: 先将当前节点的dp[u] 剪掉 所
  - 要传孩子节点x的dp[x] 贡献删掉, 根据第一步 , 其贡献是dp[x]
  -  + size[x] , size[u] 要减掉size[x] , 这两个dp[u] - dp[x] - size[x] , 和
  -  size[u] - size[x] , 都相当于在下传贡献的时候, 先将孩子节点的
  - 贡献删掉 , 然后将孩子节点数量改变一下,因为下一次x节点就变
  - 成了根节点 , dp值要变, size孩子节点也要变, 之后要用到


Generally speaking, it is very necessary to maintain the size of the node in the tree dp. If the contribution of the child node to the parent node is discussed, the information of the child node is generally obtained recursively, and then the contribution of the child node to the parent node, and the father When a node contributes to a child node, it first maintains the information of the parent node, and then seeks the information of the child node

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)

const int mod = 1e9 + 7;

inline int read(int out = 0)
{
    
    
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 500010;
const int M = 1e6 + 10;

int n, m;
int h[N], e[N], ne[N], idx;
int f[N], size1[N];

void add(int a, int b){
    
    
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

void dfs(int u, int father){
    
    
	size1[u] = 1;
	for (int i = h[u]; ~i; i = ne[i]){
    
    
		int j = e[i];
		if (j == father)   continue;
		dfs(j, u);
		size1[u] += size1[j];
		f[u] += (f[j] + size1[j]);
	}
}

void dfs1(int u, int father, int sum, int res){
    
    
	size1[u] += res;
	f[u] += sum;
	
	for (int i = h[u]; ~i; i = ne[i]){
    
    
		int j = e[i];
		if (j == father)   continue;
		dfs1(j, u, f[u] - f[j] - 2 * size1[j] + size1[u], size1[u] - size1[j]);
	}
}

signed main(){
    
    
	ios;
	
	gt(n), gt(m);
	memset(h, -1, sizeof h);
	
	while(m --){
    
    
		int a, b;
		gt(a), gt(b);
		add(a, b), add(b, a);
	}
	
	dfs(1, -1);
	dfs1(1, -1, 0, 0);
	
	int ans = 0x7f7f7f7f;
	
	for (int i = 1; i <= n; i ++){
    
    
		ans = min(ans, f[i]);
	}
	
	for (int i = 1; i <= n; i ++){
    
    
		if (f[i] == ans){
    
    
			cout << i << " ";
		}
	}
	cout << endl;
	
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/112638939