cf963b Destruction of a Tree

越靠近叶子越优先删掉

#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
int n, uu, hea[200005], cnt, deg[200005], fa[200005];
bool vis[200005];
vector<int> shu;
vector<int> ans;
struct Edge{
    int too, nxt;
}edge[400005];
void add_edge(int fro, int too){
    edge[++cnt].nxt = hea[fro];
    edge[cnt].too = too;
    hea[fro] = cnt;
}
void dfs(int x, int f){
    shu.push_back(x);
    fa[x] = f;
    for(int i=hea[x]; i; i=edge[i].nxt){
        int t=edge[i].too;
        if(t!=f)    dfs(t, x);
    }
}
void shanchu(int x){
    vis[x] = true;
    ans.push_back(x);
    for(int i=hea[x]; i; i=edge[i].nxt){
        int t=edge[i].too;
        deg[t]--;
        if(t!=fa[x] && !vis[t]){
            if(deg[t]%2==0)
                shanchu(t);
        }
    }
}
int main(){
    cin>>n;
    for(int i=1; i<=n; i++){
        scanf("%d", &uu);
        if(!uu) continue;
        add_edge(uu, i);
        add_edge(i, uu);
        deg[i]++; deg[uu]++;
    }
    dfs(1, 0);
    for(int i=shu.size()-1; i>=0; i--){
        int x=shu[i];
        if(/*vis[x] ||*/deg[x]&1)   continue;
        shanchu(x);
    }
    if(ans.size()!=n)   printf("NO\n");
    else{
        printf("YES\n");
        for(auto i:ans) printf("%d\n", i);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/poorpool/p/8899069.html

时间: 2024-08-30 17:43:03

cf963b Destruction of a Tree的相关文章

CodeForces - 963B Destruction of a Tree (dfs+思维题)

B. Destruction of a Tree time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a tree (a graph with n vertices and n?-?1 edges in which it's possible to reach any vertex from any ot

Codeforces Round #475 (Div. 2) D. Destruction of a Tree

1 You are given a tree (a graph with n vertices and n?-?1 edges in which it's possible to reach any vertex from any other vertex using only its edges). 2 3 A vertex can be destroyed if this vertex has even degree. If you destroy a vertex, all edges c

Codeforces 963B Destruction of a Tree 【贪心】

本题的贪心策略是:每次删除连到叶子结点的dfs链上离根最远的偶数度的结点 greed is good 实现方法是先维护一个degree[i]表示第i个点有多少个度,然后dfs,当每一个结点的所有子节点被访问后准备返回时判断当前结点degree的奇偶性,如果是偶数就删除,是奇数就什么都不做.这样能保证你删除的结点[的子孙]度数都是奇数,及保证删除了[离根最远的偶数度的结点].每次删除要把它父亲和son的degree都减1,并且如果son的degree减完以后是偶数的话就把son也删除.(以为这样能

CF911F Tree Destruction 解题报告

CF911F Tree Destruction 题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 输入输出格式 输入格式: The first line contains one integer number n \(n\) ( \(2 \le n \le 2 \times 10^{5}\) ) - the number of vertices in the tree. Next \(n-1\) lines d

Codeforces 911F Tree Destruction(贪心 &amp;&amp; 树的直径)

题目链接  Tree Destructi 题意  给定一棵树,每次可以选定树上的两个叶子,并删去其中的一个.答案每次加上两个选定的叶子之间的距离. 求最后答案的最大值. 首先求出树的某一条直径,令其端点分别为L, R. 把L看成树的根,那么R一定是叶子结点. 对于那些非直径上的点,离他们最远的点肯定是L或R中的一个(可能也有其他的,但是L或R肯定已经最大了) 所以依次把这些非直径上的点删掉,删掉的时候在L和R中选择一个就行了. 最后把直径删掉即可. 时间复杂度$O(nlogn)$  (应该是可以

CF911F Tree Destruction

题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 题目描述 You are given an unweighted tree with n n n vertices. Then n−1 n-1 n−1 following operations are applied to the tree. A single operation consists of the following steps: choose t

CF.911F.Tree Destruction(构造 贪心)

题目链接 \(Description\) 一棵n个点的树,每次可以选择树上两个叶子节点并删去一个,得到的价值为两点间的距离 删n-1次,问如何能使最后得到的价值最大,并输出方案 \(Solution\) 树上距离,求最大,可以考虑下树的直径 假如已知树的直径u->v,那么任意一点x到达其他点的最远距离就是u,v中一点(如果不是这样,那直径一定可以更长而不是uv) 假设x距u最远,那肯定是删x 删直径上的点(直径端点)会导致一些点取不到最远距离 既然这样按顺序删非直径上的点,最后删直径端点 #in

「CF911F」Tree Destruction

传送门 Luogu 解题思路 显然的贪心策略,因为每次都要尽量使得删点后的收益最大. 我们可以求出树的直径(因为树上的任意一个节点与其距离最远的点一定是直径的端点). 然后我们对于所有不是直径上的点,从叶子开始,从下往上删点,最后再由深而浅删掉直径. 最后输出答案即可. 细节注意事项 有些地方的计算不要写错式子之类的 参考代码 #include <algorithm> #include <iostream> #include <cstring> #include <

easyui js取消选中 Tree 指定节点

取消所有选中 var rootNodes = treeObject.tree('getRoots'); for ( var i = 0; i < rootNodes.length; i++) { var node = treeObject.tree('find', rootNodes[i].id); treeObject.tree('uncheck', node.target); }