codeforces781C Underground Lab

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:codeforces781C Underground Lab

正解:生成树+欧拉序列

解题报告:

  很容易发现,原图和图的一棵生成树其实是等价的,都是需要遍历全图,是一个类似于$dfs$的东西。

  欧拉序列正可以满足题目所给的性质,所以我只要搞出一棵原图的生成树,然后把欧拉序列分成$k$段即可。

//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 200011;
const int MAXM = 400011;
int n,m,k,father[MAXN],ecnt,first[MAXN],next[MAXM],to[MAXM];
int dfn[MAXN*2],cnt,ans[MAXN],out;
inline int find(int x){ if(father[x]!=x) father[x]=find(father[x]); return father[x]; }
inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}

inline void dfs(int x,int fa){
	dfn[++cnt]=x;
	for(int i=first[x];i;i=next[i]) {
		int v=to[i]; if(v==fa) continue;
		dfs(v,x);
		dfn[++cnt]=x;
	}
}

inline void work(){
	n=getint(); m=getint(); k=getint(); int x,y;
	for(int i=1;i<=n;i++) father[i]=i;
	for(int i=1;i<=m;i++) {
		x=getint(); y=getint();
		if(find(x)==find(y)) continue;
		link(x,y); link(y,x);
		father[find(x)]=find(y);
	}
	dfs(1,0); int now=0,cc,lim;
	lim=2*n/k; if(lim*k<2*n) lim++;
	for(int i=1;i<=k;i++) {
		cc=0; out=0;
		if(now<cnt) { while(cc<lim && now<cnt) now++,ans[++out]=dfn[now],cc++; }
		else ans[out=1]=1;
		printf("%d ",out);
		for(int j=1;j<=out;j++) printf("%d ",ans[j]);
		puts("");
	}
}

int main()
{
    work();
    return 0;
}

  

原文地址:https://www.cnblogs.com/ljh2000-jump/p/6511071.html