Codeforces Round #656 (Div. 3) F. Removing Leaves 贪心 + 模拟
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #656 (Div. 3) F. Removing Leaves 贪心 + 模拟
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳送門
文章目錄
- 題意:
- 思路:
題意:
思路:
首先有一個貪心策略就是每次都找一個葉子節點最多的點,讓后刪掉他的kkk個葉子節點,現在我們就來考慮如何模擬這個過程。
我們整一個vector<set<int>>vector<set<int>>vector<set<int>>來存邊和葉子,方便刪除點,讓后再整一個setsetset按照leaf[i].size()leaf[i].size()leaf[i].size()排序,由于是按照可變大小的值來排序的,那么必須要先從setsetset中刪除這個點,讓后再修改leafleafleaf,再加入這個點才可以,不然出大問題。之后我們就可以模擬刪除點了,要提前預處理出來leafleafleaf,而且刪除點的時候有肯能某個點也成為葉子節點,這個時候也需要修改leafleafleaf,注意細節就好。
由于我nt了,先該的值再刪點,代碼已經面目全非,跟題解差不多了。這個題主要還是要找到合適的方法去模擬刪邊就比較好做辣。
最后還要注意特判k==1k==1k==1的情況。
// 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,tune=native") //#pragma GCC optimize(2) #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<map> #include<cmath> #include<cctype> #include<vector> #include<set> #include<queue> #include<algorithm> #include<sstream> #include<ctime> #include<cstdlib> #define X first #define Y second #define L (u<<1) #define R (u<<1|1) #define pb push_back #define mk make_pair #define Mid (tr[u].l+tr[u].r>>1) #define Len(u) (tr[u].r-tr[u].l+1) #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 pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f; const double eps=1e-6;int n,k; vector<set<int>>v,leaf;struct cmp {bool operator () (int a,int b) const {if(leaf[a].size()==leaf[b].size()) return a<b;return leaf[a].size()>leaf[b].size();} };int solve() {if(k==1) return n-1;set<int,cmp>s;int ans=0;for(int i=0;i<n;i++) s.insert(i);while(1) {int now=*s.begin();if(leaf[now].size()<k) break;for(int i=1;i<=k;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);v=vector<set<int>>(n);leaf=vector<set<int>>(n);for(int i=1;i<=n-1;i++) {int a,b; scanf("%d%d",&a,&b);a--; b--;v[a].insert(b); v[b].insert(a);}for(int i=0;i<n;i++) if(v[i].size()==1) {leaf[*v[i].begin()].insert(i);}printf("%d\n",solve());}return 0; } /**/總結
以上是生活随笔為你收集整理的Codeforces Round #656 (Div. 3) F. Removing Leaves 贪心 + 模拟的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谷歌浏览器安装QQ旋风插件
- 下一篇: mac 版webstorm 破解终极版本