首页 > 其他分享 >CodeForces - 1336A Linova and Kingdom

CodeForces - 1336A Linova and Kingdom

时间:2024-08-24 10:48:17浏览次数:7  
标签:Kingdom 200005 int 1336A CodeForces ecnt 题解

CodeForces - 1336A

就差一点点,很可惜,少发现个很显而易见的结论

就是一个点的价值,实际上就是(这个点的深度 - 之后的点的数目) 就是 \(depth_i - size_i\) 然后只要用dfs维护就好了

然后把一个点的价值用STL优先队列放在一起,贪心完成。但是可能也算不上什么贪心,因为是很朴素的东西

突然想到以前看题解更多的是想看这种\(depth\)和\(size\)是怎么维护的,但是怎么都找不到,真的自己写题解还是感觉,不好写这种东西,有时间或许可以尝试写下题解,也只是分析代码了

但是现在还是进步很大的,只要有了思路怎么写都行

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
struct CodeForces{
    int to,nxt;
}edge[400005];
int ecnt=0;
int n,k;
int head[200005]={0};
int vis[200005]={0};
int siz[200005]={0};
int dep[200005]={0};
priority_queue<int> q;
void build(int from,int to){
    edge[++ecnt].to=to;
    edge[ecnt].nxt=head[from];
    head[from]=ecnt;
}
void init(){
    for(int i=1;i<=200000;++i){
        head[i]=-1;
    }
}
void dfs(int x,int now){
    siz[x]++;
    for(int i=head[x];~i;i=edge[i].nxt){
        int to=edge[i].to;
        if(vis[to]==1) continue;
        vis[to]=1;
        dep[to]=now+1;
        dfs(to,now+1);
        siz[x]+=siz[to];
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);    
    init();
    cin>>n>>k;
    for(int i=1;i<n;++i){
        int to,from;
        cin>>to>>from;
        build(from,to);
        build(to,from);
    }
    vis[1]=1;
    dep[1]=1;
    dfs(1,1);
    for(int i=1;i<=n;++i){
        q.push(dep[i]-siz[i]);
    }
    int ans=0;
    for(int i=1;i<=k;++i){
        int tt=q.top();
        q.pop();
        ans+=tt;
    }
    cout<<ans<<endl;
    return 0;
}

标签:Kingdom,200005,int,1336A,CodeForces,ecnt,题解
From: https://www.cnblogs.com/z-zhi/p/18377507

相关文章

  • Codeforces Round 967 (Div. 2)
    A.MakeAllEqual签到题,先确定最终答案,然后把其他数删去,转化为统计众数点击查看代码#include<bits/stdc++.h>usingnamespacestd;#definelllonglong#defineullunsignedlonglongintread(){intx=0;boolf=false;charc=getchar();while(c<......
  • Codeforces Round 967(Div.2)之赛后总结
    CodeforcesRound967(Div.2)传送锚点A.MakeAllEqual1.题面分析废话这么多,说白了就是求总数减去最多出现数的个数的值。2.解法直接用桶装一下,然后扫一遍维护最大值。3.code#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e6+520;......
  • Codeforces Round 967 (Div. 2) ABCD
    来源:CodeforcesRound967(Div.2)做题时间:2024_08_21A.MakeAllEqual使所有元素相等的最小操作次数,就是保留最大的数字所以操作次数就是总数减去数量最多得数B.GeneratePermutation题意构造一个序列\(p\),内部元素是\([1,2,\cdots,n]\)打乱使长度为\(n\)的初始......
  • Solution - Codeforces 1970G3 Min-Fund Prison (Hard)
    时间\(\mathcal{O}(\frac{n\sqrt{n}\logn}{\omega})\)空间\(\mathcal{O}(\frac{n\logn}{w})\)的爆标做法。首先无解当且仅当图联通且无割边。首先考虑加边的贡献。一个比较直观的感受就是只会尽可能少的加边,即加边到整个图连通。证明考虑删掉的边。如果加多了边导致删......
  • Codeforces Round 967 (Div. 2)-D
    CodeforcesRound967(Div.2)-D这些天在留校集训……我之前空余时间在看模电,最近在玩黑猴……九月开学了估计也不能闲下来……但这个博客我还是会抽空更新的╰(°▽°)╯Problem-D-Codeforces虽然代码写得特别丑陋,但好歹是我完全想的思路——自己还de了一天bug(゜ー゜)......
  • Codeforces Round 967 (Div. 2)
    题不难。A.MakeAllEqual题意:一个圆,上面有\(n\)个数,每次可以删去相邻的两个不同数中任意一个,求至少几次使得剩下的数都一样。显然下界是出现次数最多的数且一定能取到,时间复杂度\(O(n)\)。提交记录B.GeneratePermutation题意:要求构造一个排列,使得\(x\)所在位置大......
  • 题解:Codeforces Round 967 (Div. 2) B [思维/构造]
    B.GeneratePermutationtimelimitpertest:1.5secondsmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputThereisanintegersequence\(a\)oflength\(n\),whereeachelementisinitially\(-1\).Misukihastwoty......
  • Codeforces Round 966 (Div. 3)
    A.PrimaryTask#include<bits/stdc++.h>usingnamespacestd;usingvi=vector<int>;voidsolve(){strings;cin>>s;if(s.size()<=2){cout<<"NO\n";return;}if(s[0]!......
  • 题解:Codeforces Round 967 (Div. 2) [暴力/贪心]
    A.MakeAllEqualtimelimitpertest:1secondmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputYouaregivenacyclicarray\(a_1,a_2,\ldots,a_n\).Youcanperformthefollowingoperationon\(a\)atmost\(n-......
  • Codeforces Round 967 (Div. 2) C题 类分治解法
    废话不多说,先上代码t=int(input())whilet>0:n=int(input())pre_d={1:[iforiinrange(2,n+1)]}pair_l=[]whilelen(pre_d)!=0:item=pre_d.items()now_d={}fork,vinitem:forii......