网站版权备案,在线制作图表,pc网站制作,超市会员管理系统传送门 文章目录题意#xff1a;思路#xff1a;题意#xff1a; 思路#xff1a;
首先有一个贪心策略就是每次都找一个叶子节点最多的点#xff0c;让后删掉他的kkk个叶子节点#xff0c;现在我们就来考虑如何模拟这个过程。 我们整一个vectorsetintve…传送门
文章目录题意思路题意 思路
首先有一个贪心策略就是每次都找一个叶子节点最多的点让后删掉他的kkk个叶子节点现在我们就来考虑如何模拟这个过程。 我们整一个vectorsetintvectorsetintvectorsetint来存边和叶子方便删除点让后再整一个setsetset按照leaf[i].size()leaf[i].size()leaf[i].size()排序由于是按照可变大小的值来排序的那么必须要先从setsetset中删除这个点让后再修改leafleafleaf再加入这个点才可以不然出大问题。之后我们就可以模拟删除点了要提前预处理出来leafleafleaf而且删除点的时候有肯能某个点也成为叶子节点这个时候也需要修改leafleafleaf注意细节就好。
由于我nt了先该的值再删点代码已经面目全非跟题解差不多了。这个题主要还是要找到合适的方法去模拟删边就比较好做辣。
最后还要注意特判k1k1k1的情况。
// Problem: F. Removing Leaves
// Contest: Codeforces - Codeforces Round #656 (Div. 3)
// URL: https://codeforces.com/contest/1385/problem/F
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize(Ofast,no-stack-protector,unroll-loops,fast-math)
//#pragma GCC target(sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tunenative)
//#pragma GCC optimize(2)
#includecstdio
#includeiostream
#includestring
#includecstring
#includemap
#includecmath
#includecctype
#includevector
#includeset
#includequeue
#includealgorithm
#includesstream
#includectime
#includecstdlib
#define X first
#define Y second
#define L (u1)
#define R (u1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].ltr[u].r1)
#define Len(u) (tr[u].r-tr[u].l1)
#define random(a,b) ((a)rand()%((b)-(a)1))
#define db puts(---)
using namespace std;//void rd_cre() { freopen(d://dp//data.txt,w,stdout); srand(time(NULL)); }
//void rd_ac() { freopen(d://dp//data.txt,r,stdin); freopen(d://dp//AC.txt,w,stdout); }
//void rd_wa() { freopen(d://dp//data.txt,r,stdin); freopen(d://dp//WA.txt,w,stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pairint,int PII;const int N1000010,mod1e97,INF0x3f3f3f3f;
const double eps1e-6;int n,k;
vectorsetintv,leaf;struct cmp {bool operator () (int a,int b) const {if(leaf[a].size()leaf[b].size()) return ab;return leaf[a].size()leaf[b].size();}
};int solve() {if(k1) return n-1;setint,cmps;int ans0;for(int i0;in;i) s.insert(i);while(1) {int now*s.begin();if(leaf[now].size()k) break;for(int i1;ik;i) {int to*leaf[now].begin(); s.erase(now); s.erase(to);leaf[now].erase(to);if(leaf[to].count(now)) leaf[to].erase(now);v[now].erase(to); v[to].erase(now);if(v[now].size()1) {int ne*v[now].begin();s.erase(ne); leaf[ne].insert(now);s.insert(ne);}s.insert(now); s.insert(to);}ans;}return ans;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);int _; scanf(%d,_);while(_--) {scanf(%d%d,n,k);vvectorsetint(n);leafvectorsetint(n);for(int i1;in-1;i) {int a,b; scanf(%d%d,a,b);a--; b--;v[a].insert(b); v[b].insert(a);}for(int i0;in;i) if(v[i].size()1) {leaf[*v[i].begin()].insert(i);}printf(%d\n,solve());}return 0;
}
/**/