2 条题解

  • 0
    @ 2023-11-2 19:53:37

    最终代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 2e5 + 5;
    int n;
    long long ans;
    bool vis[maxn];
    vector<int> g[maxn];
    
    int get_size(int u, int p) { // 求连通块大小 
    	if (vis[u]) return 0;
    	int sum = 1;
    	for (auto v : g[u])
    		if (v != p)
    			sum += get_size(v, u);
    	return sum;
    }
    // weight center 更新重心 
    int cal_wc(int u, int p, int tot, int &wc) {
    	if (vis[u])
    		return 0;
    	int sum = 1, ms = 0;
    	for (auto v : g[u]) {
    		if (v != p) {
    			int tmp = cal_wc(v, u, tot, wc);
    			sum += tmp;
    			ms = max(ms, tmp);
    		}
    	}
    	ms = max(ms, tot - sum);
    	if (ms <= tot/2) wc = u;
    	return sum;
    }
    
    void dfs2(int u, int p, int d, long long c[2]) {
    	if (vis[u]) return;
    	c[d]++;
    	for (auto v : g[u])
    		if (v != p)
    			dfs2(v, u, d^1, c);
    }
    
    void dfs(int u) {
    	if (vis[u])
    		return;
    	cal_wc(u, -1, get_size(u, -1), u);
    	// 处理
    	long long cnt[2] = {1}; // cnt[0] = 1, cnt[1] = 0
    	for (auto v : g[u]) {
    		long long c[2] = {}; // c[0] = c[1] = 0
    		dfs2(v, u, 1, c);
    		ans += (long long) cnt[0] * c[0] + (long long) cnt[1] * c[1];
    		cnt[0] += c[0];
    		cnt[1] += c[1];
    	}
    	vis[u] = true;
    	for (auto v : g[u])
    		dfs(v);
    }
    
    int main() {
    	scanf("%d", &n);
    	for (int i = 1; i < n; i++) {
    		int u, v;
    		scanf("%d%d", &u, &v);
    		g[u].push_back(v);
    		g[v].push_back(u);
    	}
    	dfs(1);
    	printf("%lld\n", ans);
    	return 0;
    }
    
    • 0
      @ 2023-11-2 19:32:39
      #include <bits/stdc++.h>
      using namespace std;
      const int maxn = 2e5 + 5;
      int n;
      long long ans;
      bool vis[maxn];
      vector<int> g[maxn];
      
      int get_size(int u, int p) {
      	if (vis[u]) return 0;
      	int sum = 1;
      	for (auto v : g[u])
      		if (v != p)
      			sum += get_size(v, u);
      	return sum;
      }
      // weight center
      int cal_wc(int u, int p, int tot, int &wc) {
      	if (vis[u])
      		return 0;
      	int sum = 1, ms = 0;
      	for (auto v : g[u]) {
      		if (v != p) {
      			int tmp = cal_wc(v, u, tot, wc);
      			sum += tmp;
      			ms = max(ms, tmp);
      		}
      	}
      	ms = max(ms, tot - sum);
      	if (ms <= tot/2) wc = u;
      	return sum;
      }
      
      void dfs(int u) {
      	if (vis[u])
      		return;
      	cal_wc(u, -1, get_size(u, -1), u);
      	// u 变成重心了
      	cout << u << " .." << endl;
      	vis[u] = true;
      	for (auto v : g[u])
      		dfs(v);
      }
      
      int main() {
      	scanf("%d", &n);
      	for (int i = 1; i < n; i++) {
      		int u, v;
      		scanf("%d%d", &u, &v);
      		g[u].push_back(v);
      		g[v].push_back(u);
      	}
      	dfs(1);
      	return 0;
      }
      
      • 1

      信息

      ID
      23
      时间
      1000ms
      内存
      256MiB
      难度
      9
      标签
      递交数
      11
      已通过
      4
      上传者