首页 > 其他分享 >Codeforces 1423C - Dušan's Railway(树分块)

Codeforces 1423C - Dušan's Railway(树分块)

时间:2023-05-16 15:56:47浏览次数:35  
标签:log int ec pb 1423C MAXN Railway Du typ

首先 \(k>3\) 当 \(k=3\) 做,也就是说题目等价于找不超过 \(10n\) 条路径使得任意两点间的路径都可以表示为选定的这些路径中不相交的三者的并。

先考虑链怎么做,关于这个 \(3\),很自然的想法是取若干关键点,关键点之间两两连边,其余点再像相邻两关键点连边,因此考虑分块,每 \(B\) 个点设个关键点,这样不同块内的点就 ok 了,但是同一块内的点还不符合条件。比较蠢的想法是令 \(B=n^{1/3}\) 然后块内两两连边,这样边数大概是 \(n^{4/3}\)。但是其实块内也是一个结构类似的子问题,可以递归处理。由于一个数最多开方 \(\log\log n\) 次就会变成 \(1\),因此直接令 \(B=\sqrt{n}\) 然后递归处理复杂度大概可以达到 \(n\log\log n\)。

树的情况也是类似的,直接上树分块即可。我是按照 Kubic 题解里随机撒点然后建虚树的做法来写的,其实一直不太理解这个写法为啥不会被卡掉,所以前几天特地去 u 群问了一圈,然后 skip2004 说了一些我听不懂的东西,说划分出来的连通块大小的 \(\max\) 期望是 sqrt log 的,因此实际上撒的点数要略多余 \(\sqrt{n}\)。不过作为 oier,其实我们不必特别在意这个差异,一般出题人也不会可以造出卡到期望上界的数据。

const int MAXN=10000;
mt19937 rng(114514);
int n,k,hd[MAXN+5],to[MAXN*2+5],nxt[MAXN*2+5],ec;
void adde(int u,int v){to[++ec]=v;nxt[ec]=hd[u];hd[u]=ec;}
vector<pii>res;
int typ[MAXN+5],vis[MAXN+5];
queue<vector<int> >q;
pair<vector<int>,int>dfs(int x,int f){
	vector<pair<vector<int>,int> >v;int sum=0;
	for(int e=hd[x];e;e=nxt[e]){
		int y=to[e];if(y==f||!typ[y])continue;
		v.pb(dfs(y,x));sum+=(v.back().se>0);
	}
	if(typ[x]==2||sum>=2){
		for(auto t:v){
			q.push(t.fi);
			for(int z:t.fi){
				if(t.se)res.pb(mp(z,t.se));
				res.pb(mp(z,x));
			}
		}typ[x]=2;return mp(vector<int>{},x);
	}else{
		int pt=0;vector<int>tot;for(auto t:v)pt|=t.se;
		for(auto t:v)for(int y:t.fi)tot.pb(y);tot.pb(x);
		return mp(tot,pt);
	}
}
void work(vector<int>v){
	if(v.size()<=1)return;
	memset(typ,0,sizeof(typ));memset(vis,0,sizeof(vis));
	for(int x:v)typ[x]=1;shuffle(v.begin(),v.end(),rng);
	int lim=(int)sqrt(v.size());for(int i=0;i<lim;i++)typ[v[i]]=2;
//	printf("work {");for(int i=0;i<v.size();i++)printf("%d%c",v[i],",}"[i+1==v.size()]);printf("\n");
//	for(int i=0;i<lim;i++)printf("%d ",v[i]);printf("\n");
	while(!q.empty())q.pop();dfs(v[0],0);vector<int>nd;
	for(int x:v)if(typ[x]==2)nd.pb(x);
	for(int i=0;i<nd.size();i++)for(int j=i+1;j<nd.size();j++)
		res.pb(mp(nd[i],nd[j])); 
	vector<vector<int> >tmp;
	while(!q.empty())tmp.pb(q.front()),q.pop();
	for(vector<int>vv:tmp)work(vv);
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1,u,v;i<n;i++)scanf("%d%d",&u,&v),adde(u,v),adde(v,u);
	vector<int>vec;for(int i=1;i<=n;i++)vec.pb(i);work(vec);
	printf("%d\n",(int)res.size());
	for(pii p:res)printf("%d %d\n",p.fi,p.se);
	return 0;
}

标签:log,int,ec,pb,1423C,MAXN,Railway,Du,typ
From: https://www.cnblogs.com/tzcwk/p/Codeforces-1423C.html

相关文章

  • APSscheduler
    APSchedulerAPScheduler(advancededpythonscheduler)是一款Python开发的定时任务工具。文档地址https://apscheduler.readthedocs.io/en/latest/userguide.html#starting-the-scheduler特点:不依赖于Linux系统的crontab系统定时,独立运行可以动态添加新的定时任务,如下单......
  • Commonly Used Prompts for Reducing Duplicate Rate
    Simplerewrite:Tryhardtorewritethefollowingcontent,makesurethemeaningisthesameastheoriginalmeaningbutjusttrytousedifferentwordsespeciallyformalwords:Rewriteabstractorsomecopiedtextsfromapaper:Rewritethefollowing......
  • Brief Introduction
    BriefSummariesofTopics0.1LinearAlgebra0.2RealAnalysis0.3DifferentialVector-ValuedFunctions0.4PointSetTopology0.5ClassicalStokes'Theorems0.6DifferentialFormsandStrokes'Theorem0.7CurvatureforCurvesandSurfaces0.8G......
  • mapreduce
     MapReduce是一种分布式计算模型,用于处理大规模数据集的并行计算。它是由Google首先提出,并在ApacheHadoop项目中得到广泛实现和应用的MapReduce模型的优势在于它的可扩展性和容错性。它可以在大规模的计算集群上并行处理数据,提供高性能和高可靠性。MapReduce适用于各种数据处......
  • hdu:LCIS(线段树+区间合并)
    ProblemDescriptionGivennintegers.Youhavetwooperations:UAB:replacetheAthnumberbyB.(indexcountingfrom0)QAB:outputthelengthofthelongestconsecutiveincreasingsubsequence(LCIS)in[a,b].InputTinthefirstline,indicatingt......
  • hdu:这是真正的水题(RMQ)
    ProblemDescription在缺水的地方,水是非常有限的资源,所以人们常常为争夺最大的水源而战。给定一系列水源,用a1,a2,a3,…,an代表水源的大小。给定一组查询,每个查询包含2整数L和R,请找出L和R之间最大的水源。Input输入数据首先给定一个整数T(T≤10)表示测试用例的数量。......
  • hdu:Party(2-SAT)
    ProblemDescription有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n个人同时列席?Inputn:表示有n对夫妻被邀请(n<=1000)m:表......
  • hdu:Let's go home(2-SAT)
    ProblemDescription小时候,乡愁是一枚小小的邮票,我在这头,母亲在那头。——余光中集训是辛苦的,道路是坎坷的,休息还是必须的。经过一段时间的训练,lcy决定让大家回家放松一下,但是训练还是得照常进行,lcy想出了如下回家规定,每一个队(三人一队)或者队长留下或者其余两名队员同时留下;每......
  • hdu:How far away ?(树链剖分)
    ProblemDescriptionTherearenhousesinthevillageandsomebidirectionalroadsconnectingthem.Everydaypeolealwaysliketoasklikethis“HowfarisitifIwanttogofromhouseAtohouseB”?Usuallyithardtoanswer.Butluckilyintthisvilla......
  • hdu:Arbitrage(最短路变形)
    ProblemDescriptionArbitrageistheuseofdiscrepanciesincurrencyexchangeratestotransformoneunitofacurrencyintomorethanoneunitofthesamecurrency.Forexample,supposethat1USDollarbuys0.5Britishpound,1Britishpoundbuys10.0......