首页 > 其他分享 >Codeforces Round 787 (Div. 3)D. Vertical Paths

Codeforces Round 787 (Div. 3)D. Vertical Paths

时间:2023-12-14 15:00:26浏览次数:30  
标签:Paths 787 Vertical rep long vis ans 节点 define

题目链接
题意:给定一棵树,将这棵树划分成几天互不相交的链,要求最小化链的数量
思路:每个叶子节点一定在一条链中,所以链的数量就是叶子节点的数量,从叶子节点往上跳直到根节点,边跳边标记,路径上所有点都属于这条链。
坑:

  1. 数据大时,不要轻易使用memset不然会t到起飞
  2. vector不要开太多就比如不要vector<int>a[N]这样也会t
#include <bits/stdc++.h> 
#define rep(i,a,b) for(register int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(register int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f

using namespace std;
const int N = 2e5+10;
int t; 
int d[N], p[N], vis[N], n;
void solve()
{
	cin >> n;
	rep(i,1,n)
	{
		int x; cin >> x;
		p[i] = x;
		d[x]++;
	}
	if(n==1)
	{
	    cout << 1 << endl << 1 << endl << 1 << endl;
	    cout << endl;
	    return;
	}
	int ans = 0;
	vector<int>a[n];
	rep(i,1,n)
		if(!d[i])
		{
			int k = i;
			while(!vis[k] && k != p[k])
			{
				a[ans].push_back(k);
				vis[k] = 1;
				k = p[k];
			}	
			if(k == p[k] && !vis[k])
			{
				a[ans].push_back(k);
				vis[k] = 1;
			}		
			ans++;
		}
	rep(i,1,n)	d[i] = 0, vis[i] = 0;
	cout << ans << endl;
	rep(i,0,ans-1)
	{
		cout << a[i].size() << endl;
		fep(i,a[i].size()-1,0)	cout << x << ' ';
		cout << endl;
	}
	cout << endl;
}

int main()
{
	IOS	
  	freopen("1.in", "r", stdin);
	cin >> t;
	while(t --)	
	solve();
	return 0;
}

标签:Paths,787,Vertical,rep,long,vis,ans,节点,define
From: https://www.cnblogs.com/cxy8/p/17901183.html

相关文章

  • css vertical-align \ text-align 居中
    vertical-align:1、只能作用在子元素display 值为inline,inline-block,inline-table,table-cell的元素上,2、子元素设置vertical-align3、父元素高度是由line-height决定(不要乱给父元素添加height)<divclass="father"><spanclass="son">a</span></div&g......
  • [Codeforces] CF1675D Vertical Paths
    题目描述给定一棵由\(n\)个顶点组成的有根树。顶点由\(1\)到\(n\)编号。任何顶点都可以是树的根。请在树上找出这样一组路径:每个顶点恰好属于一条路径,每条路径可以包含一个或多个顶点;在每条路径中,每个节点的下一个节点是当前节点的子节点(即路径总是向下——从父节点......
  • Xcode 15 and iOS 17 - Error: DT_TOOLCHAIN_DIR cannot be used to evaluate LIBRARY
    热烈欢迎,请直接点击!!!进入博主AppStore主页,下载使用各个作品!!!注:博主将坚持每月上线一个新app!!Podfile文件添加如下内容后,重新podinstall:post_installdo|installer|#fixxcode15DT_TOOLCHAIN_DIR-removeafterfixoficially-https://github.com/CocoaPods/CocoaPod......
  • Solution Set - CF787
    ViveleR&M!还被种草了Hurt,真的颇有感触,但这是SolutionSet,就不写了。A.TheMonsterexgcd,但是发现\(1\leqa,b,c,d\leq100\)直接暴力枚举即可。我认为这是\(O(1)\)的,但题解认为是\(O(n)\),感觉不如原神。B.NotAfraid每一组里面只要有来自同一个宇宙的Rick......
  • Passable Paths (hard version)
    先写正常写法:我的评价是,后面的分讨我直接树剖拿下。我觉得这样分讨方便一点。lca(u,v)=v(或者u,反证就是一条链的形状),那么lca(u,i)==i,保证i在链上。然后还有Y字形路径,lca(u,v)=t,则lca(u,i)=i且d[i]>=d[t]。统一起来就是\(lca(u,i)==i,d_{lca(u,i)}\led_i\)。自己的想法很......
  • Creating HTML table with vertically oriented text as table header 表头文字方向
    ASanoldquestion,thisismorelikeinfoorreminderaboutverticalmarginorpaddingin%thattakesparent'swidthasreference.Ifyouuseapseudoelementandvertical-padding,youmaybasiclydrawasquareboxor<td>:http://jsfiddle.n......
  • 归并排序 Acwing 787
    归并排序最重要的一部便是归并,我们的模板顺序为:定义一个中间值,将我们的区间范围一分为二,我们将这两部分看成两个数组,我们分别将这两个数组进行归并排序,并且定义一个新的数组,将这两个数组排序好后导入到这个新数组中,并最后将这个定义的数组输出为原数组,即可实现归并排序。1......
  • P2787 语文1(chin1)- 理理思维
    \(P2787\)语文\(1\)(\(chin1\))-理理思维题目背景蒟蒻\(HansBug\)一、题目描述考试开始了,可是蒟蒻\(HansBug\)脑中还是一片空白。哦不!准确的说是乱七八糟的。现在首要任务就是帮蒟蒻\(HansBug\)理理思维。假设\(HansBug\)的思维是一长串字符串(字符串中包含且仅包含\(26\)1......
  • Gitlab upgrade paths
    UpgradepathsUpgradingacrossmultipleGitLabversionsinonegois onlypossiblebyacceptingdowntime.Ifyoudon’twantanydowntime,readhowto upgradewithzerodowntime.Foradynamicviewofexamplesofsupportedupgradepaths,trythe UpgradePa......
  • P9019 [USACO23JAN] Tractor Paths P 题解
    Description有\(n\)个区间,第\(i\)个区间为\([l_i,r_i]\)。保证\(l_1<l_2<\cdots<l_n\)且\(r_1<r_2<\cdots<r_n\)。其中一部分区间是特殊的,输入会给定。如果第\(i\)个区间和第\(j\)个区间相交,那么\(i,j\)之间有一条边。保证\(1,n\)联通。给定\(Q\)组询问,每次......