E. Gardener and Tree 拓扑排序板子
2022/7/30 23:24:16
本文主要是介绍E. Gardener and Tree 拓扑排序板子,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
E. Gardener and Tree
将度为1的顶点加入队列,做拓扑排序,记录点的遍历层次,答案为 n-前k层的点
// AC one more times ////////////////////////////////////////INCLUDE////////////////////////////////////////// #include <iostream> #include <algorithm> #include <cstdio> #include<string> #include<string.h> #include <cstring> #include <complex> #include <cmath> #include <vector> #include <stack> #include <queue> #include <map> #include <set> #include <string> #include <list> #include <bitset> #include <assert.h> #include <unordered_set> #include <unordered_map> #include <iomanip> #include <random> #include <iterator> #include <time.h> using namespace std; /////////////////////////////////////////DEFINE////////////////////////////////////////// #define fi first #define se second #define pb push_back #define endl '\n' #define all(x) (x).begin(), (x).end() #define inf64 0x3f3f3f3f3f3f3f3f typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> PII; typedef pair<long long,long long> PLL; ////////////////////////////////////////CONST//////////////////////////////////////////// const int inf = 0x3f3f3f3f; const int maxn = 2e6 + 6; const double eps = 1e-8; const int mod = 1000000; ///////////////////////////////////////FUNCTION////////////////////////////////////////// template<typename T> void init(T q[], int n, T val){ for (int i = 0; i <= n; i++) q[i] = val; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } bool cmp(int c, int d) { return c > d; } /* 2 200000 1 2 */ void solve() { int n; cin>>n; int k; cin>>k; int st[n+10];memset(st,0,sizeof st); vector<int> son[n+10]; int d[n+10];memset(d,0,sizeof d); for(int i=1;i<=n+3;i++) son[i].clear(); for(int i=1;i<=n-1;i++) { int u,v; cin>>u>>v; son[u].pb(v); son[v].pb(u); d[u]++,d[v]++; } queue<int> q; for(int i=1;i<=n;i++) { if(d[i]==1) q.push(i),st[i]=1; } while(!q.empty()) { int t=q.front(); q.pop(); for(auto &it:son[t]) { int j=it; if(--d[j]==1) { st[j]=st[t]+1; q.push(j); } } } int ans=0; for(int i=1;i<=n;i++) if(st[i]<=k) ans++; cout<<n-ans<<endl; return; } int main() { std::ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int T;cin>>T;while(T--) solve(); return 0; } /* * ┌───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐ * │Esc│ │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│ ┌┐ ┌┐ ┌┐ * └───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘ └┘ └┘ └┘ * ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐ * │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │ * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤ * │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │ │ * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │ * │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter │ │ 4 │ 5 │ 6 │ │ * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤ ┌───┐ ├───┼───┼───┼───┤ * │ Shift │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│ Shift │ │ ↑ │ │ 1 │ 2 │ 3 │ │ * ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││ * │ Ctrl│ │Alt │ Space │ Alt│ │ │Ctrl│ │ ← │ ↓ │ → │ │ 0 │ . │←─┘│ * └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘ */
这篇关于E. Gardener and Tree 拓扑排序板子的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?