• 题解【CF911F】Tree Destruction


    题面

    细节比较多的题。。

    看到距离之和最大很容易想到树的直径。

    于是我们可以先找出一条直径,先删掉不是这条直径上的点(因为一个点距离最远的点一定是直径的一个端点),最后再删掉这条直径。

    按照什么顺序删掉不是直径上的点?我的做法是将这些点按照深度从大到小排序依次删除,因为一个点的深度总是比父亲的深度大,这样可以避免删除非叶节点的情况。

    注意这里的深度是指以直径的一个端点为根时的深度,如果像我一样设成以 1 为根时的深度就会 WA on test 28。

    然后注意一些细节就行了。

    我写的代码比较复杂。。。( exttt{/yun})

    #include <bits/stdc++.h>
    #define DEBUG fprintf(stderr, "Passing [%s] line %d
    ", __FUNCTION__, __LINE__)
    #define File(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout)
    #define DC int T = gi <int> (); while (T--)
    
    using namespace std;
    
    typedef long long LL;
    typedef pair <int, int> PII;
    typedef pair <int, PII> PIII;
    
    template <typename T>
    inline T gi()
    {
    	T f = 1, x = 0; 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 f * x;
    }
    
    const int INF = 0x3f3f3f3f, N = 200003, M = N << 1;
    
    int n;
    int tot, head[N], ver[M], nxt[M];
    int mx, id, dis[N], pre[N];
    bool vis[N];
    int fa[N], dep[N], son[N], sz[N], topp[N];
    int seq[N], cnt;
    vector <int> zj;
    struct Ans {int a, b, c;};
    vector <Ans> Program;
    int dep1[N];
    
    inline void add(int u, int v) {ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;}
    
    inline void bfs(int u)
    {
    	queue <int> q;
    	q.push(u);
    	memset(dis, 0, sizeof dis);
    	memset(vis, false, sizeof vis);
    	memset(pre, 0, sizeof pre);
    	vis[u] = true;
    	mx = id = 0;
    	while (!q.empty())
    	{
    		int u = q.front(); q.pop();
    		for (int i = head[u]; i; i = nxt[i])
    		{
    			int v = ver[i];
    			if (vis[v]) continue;
    			vis[v] = true;
    			dis[v] = dis[u] + 1;
    			pre[v] = u;
    			if (dis[v] > mx) mx = dis[v], id = v;
    			q.push(v);
    		}
    	}
    }
    
    void dfs_dep(int u, int f)
    {
    	dep1[u] = dep1[f] + 1;
    	for (int i = head[u]; i; i = nxt[i])
    	{
    		int v = ver[i];
    		if (v == f) continue;
    		dfs_dep(v, u);
    	}
    }
    
    inline bool cmp(int x, int y) {return dep1[x] > dep1[y];}
    
    namespace LCA
    {
    	void dfs1(int u, int f)
    	{
    		sz[u] = 1, dep[u] = dep[f] + 1, fa[u] = f;
    		for (int i = head[u]; i; i = nxt[i])
    		{
    			int v = ver[i];
    			if (v == f) continue;
    			dfs1(v, u);
    			sz[u] += sz[v];
    			if (sz[v] > sz[son[u]]) son[u] = v;
    		}
    	}
    	void dfs2(int u, int f)
    	{
    		topp[u] = f;
    		if (!son[u]) return; dfs2(son[u], f);
    		for (int i = head[u]; i; i = nxt[i])
    		{
    			int v = ver[i];
    			if (v == fa[u] || v == son[u]) continue;
    			dfs2(v, v);
    		}
    	}
    	int getLCA(int u, int v)
    	{
    		while (topp[u] != topp[v])
    		{
    			if (dep[topp[u]] < dep[topp[v]]) swap(u, v);
    			u = fa[topp[u]];
    		}
    		if (dep[u] < dep[v]) return u;
    		return v;
    	}
    } //namespace LCA
    
    int main()
    {
    	//File("");
    	n = gi <int> ();
    	for (int i = 1; i < n; i+=1)
    	{
    		int u = gi <int> (), v = gi <int> ();
    		add(u, v), add(v, u);
    	}
    	bfs(1);
    	int lft = id;
    	bfs(lft);
    	int rght = id;
    	LCA :: dfs1(1, 0); LCA :: dfs2(1, 1);
    	memset(vis, false, sizeof vis);
    	int now = rght; vis[now] = true; zj.push_back(now);
    	while (now != lft) now = pre[now], vis[now] = true, zj.push_back(now);
    	for (int i = 1; i <= n; i+=1) if (!vis[i]) seq[++cnt] = i;
    	dfs_dep(lft, 0);
    	sort(seq + 1, seq + 1 + cnt, cmp);
    	LL ans = 0;
    	for (int i = 1; i <= cnt; i+=1)
    	{
    		int u = seq[i];
    		int lca1 = LCA :: getLCA(u, lft), lca2 = LCA :: getLCA(u, rght);
    		int dis1 = dep[u] + dep[lft] - 2 * dep[lca1], dis2 = dep[u] + dep[rght] - 2 * dep[lca2];
    		if (dis1 >= dis2) ans += dis1, Program.push_back((Ans){u, lft, u});
    		else ans += dis2, Program.push_back((Ans){u, rght, u});
    	}
    	for (int i = 0; i < (int)zj.size() - 1; i+=1)
    		ans += mx - i, Program.push_back((Ans){zj[i], lft, zj[i]});
    	printf("%lld
    ", ans);
    	for (int i = 0; i < n - 1; i+=1) printf("%d %d %d
    ", Program[i].a, Program[i].b, Program[i].c);
    	return 0;
    }
    
  • 相关阅读:
    个人阅读作业Week7
    2015年团队项目选择
    实验与作业(Python)-02 Python函数入门与温度转换程序(函数、input、eval、int、float、列表)
    实验与作业(Python)-03 Python程序实例解析(函数、循环、range、turtle)
    实验与作业(Python)-04 数据类型、数学函数与字符串
    Java第05次实验提纲(Java图形界面编程)
    Python能做些什么?
    Python教学相关资料
    使用邮件交流的好处
    Java9相关资料(JShell简易教程等)
  • 原文地址:https://www.cnblogs.com/xsl19/p/14404978.html
Copyright © 2020-2023  润新知