大意:$n$结点,$m$条边无向图, 有$k$个人, 每个人最多走$\left\lceil\frac {2n}{k}\right\rceil$步, 求一种方案使得$k$个人走遍所有的点
$n$结点树的欧拉序长度为$2n-1$, 直接取$dfs$树的欧拉序即可
#include <iostream> #include <algorithm> #include <math.h> #include <cstdio> #include <vector> #define pb push_back #define REP(i,a,n) for(int i=a;i<=n;++i) using namespace std; const int N = 4e5+10, INF = 0x3f3f3f3f; int n, m, k, mx; int vis[N]; vector<int> g[N], path; void dfs(int x) { vis[x] = 1, path.pb(x); for (int y:g[x]) { if (!vis[y]) dfs(y),path.pb(x); } } int main() { cin>>n>>m>>k; REP(i,1,m) { int x, y; cin>>x>>y; g[x].pb(y),g[y].pb(x); } dfs(1),path.pop_back(); for (int i=1; i<=k; cout<<‘\n‘,++i) { if (path.empty()) { cout<<"1 1"; continue; } int mx = (2*n+k-1)/k; if (mx>path.size()) mx = path.size(); cout<<mx<<‘ ‘; REP(j,1,mx) { cout<<path.back()<<‘ ‘; path.pop_back(); } } }
原文地址:https://www.cnblogs.com/uid001/p/10340843.html
时间: 2024-10-30 10:45:44