CodeForces 780 E Underground Lab
生活随笔
收集整理的這篇文章主要介紹了
CodeForces 780 E Underground Lab
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Underground Lab
題解:
如果遍歷一棵樹,我們可以發現最多需要走的步數也不會超過2 * n步。
所以我們選出一棵樹,然后遍歷一邊這顆樹。
然后把序列分成k塊就好了。
?
代碼:
#include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); #define LL long long #define ULL unsigned LL #define fi first #define se second #define pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lch(x) tr[x].son[0] #define rch(x) tr[x].son[1] #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) typedef pair<int,int> pll; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const LL INF = 0x3f3f3f3f3f3f3f3f; const LL _INF = 0xc0c0c0c0c0c0c0c0; const LL mod = (int)1e9+7; const int N = 2e5 + 100; int pre[N]; int Find(int x){if(pre[x] == x) return x;return pre[x] = Find(pre[x]); } vector<int> ans; vector<int> c[N]; vector<int> vc[N]; void dfs(int o, int u){ans.pb(u);for(int v : vc[u]){if(v == o) continue;dfs(u, v);ans.pb(u);} } int Ac(){int n, m, k, u, v;scanf("%d%d%d", &n, &m, &k);for(int i = 1; i <= n; ++i) pre[i] = i;for(int i = 1; i <= m; ++i){scanf("%d%d", &u, &v);int fu = Find(u), fv = Find(v); // cout << fu << " with " << fv << endl;if(fu == fv) continue;pre[fu] = fv;vc[u].pb(v); vc[v].pb(u);}dfs(0, 1); // cout << "____" << endl;int can = (2*n + k-1)/k;for(int i = 0; i < ans.size(); ++i){c[i/can].pb(ans[i]);}for(int i = 0; i < k; ++i){if(c[i].size() == 0) c[i].pb(1);printf("%d", c[i].size());for(int v : c[i]){printf(" %d", v);}puts("");}return 0; }int main(){Ac();return 0; } View Code?
轉載于:https://www.cnblogs.com/MingSD/p/10857353.html
總結
以上是生活随笔為你收集整理的CodeForces 780 E Underground Lab的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 你真的了解 Cookie 和 Sessi
- 下一篇: 使用 Vue + Flask 搭建单页应