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

You are given a rooted tree consisting of nn vertices. Vertices are numbered from 11 to nn. Any vertex can be the root of a tree.

A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root.

The tree is specified by an array of parents pp containing nn numbers: pipi is a parent of the vertex with the index ii. The parent of a vertex uu is a vertex that is the next vertex on the shortest path from uu to the root. For example, on the simple path from 55 to 33 (the root), the next vertex would be 11, so the parent of 55 is 11.

The root has no parent, so for it, the value of pipi is ii (the root is the only vertex for which pi=ipi=i).

Find such a set of paths that:

  • each vertex belongs to exactly one path, each path can contain one or more vertices;
  • in each path each next vertex — is a son of the current vertex (that is, paths always lead down — from parent to son);
  • number of paths is minimal.

For example, if n=5n=5 and p=[3,1,3,3,1]p=[3,1,3,3,1], then the tree can be divided into three paths:

  1. 3→1→53→1→5 (path of 33 vertices),
  2. 44 (path of 11 vertices).
  3. 22 (path of 11 vertices).

Example of splitting a root tree into three paths for n=5n=5, the root of the tree — node 33.

Input

The first line of input data contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test.

Each test case consists of two lines.

The first of them contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105). It is the number of vertices in the tree.

The second line contains nn integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). It is guaranteed that the pp array encodes some rooted tree.

It is guaranteed that the sum of the values nn over all test cases in the test does not exceed 2⋅1052⋅105.

Output

For each test case on the first line, output an integer mm — the minimum number of non-intersecting leading down paths that can cover all vertices of the tree.

Then print mm pairs of lines containing path descriptions. In the first of them print the length of the path, in the second — the sequence of vertices specifying that path in the order from top to bottom. You can output the paths in any order.

If there are several answers, output any of them.

Example

input

Copy

6
5
3 1 3 3 1
4
1 1 4 1
7
1 1 2 3 4 5 6
1
1
6
4 4 4 4 1 2
4
2 2 2 2

output

Copy

3
3
3 1 5
1
2
1
4

2
2
1 2
2
4 3

1
7
1 2 3 4 5 6 7

1
1
1

3
3
4 1 5
2
2 6
1
3

3
2
2 1
1
3
1
4

思路:可以用链表来存以某个数为头结点的数,当某个数的头结点为他本身的时候我们把他设为根结点,用一个vector数组来存他的路径数组,用一个len来记录路径数组的长度 ,用st数组来记录每个点的状态,从根结点开始搜,当搜的这个点的指针指向-1时说明他是这条路的最后一个数,所以路径条数++,当不等于-1时,我们遍历以这个点为头结点的链表,当遍历到的点没有被搜过的时候就把他加入路径数组中,并改变他的状态,然后搜这个点。最后输出路径条数和每个路径数组中存的点即可。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
int n;
const int N=200005;
int h[N];
int e[N];
int ne[N];
int st[N];
int idx,len,root;
vector<int> ans[N];
void init(){
	memset(h,-1,sizeof h);
	memset(st,0,sizeof st);
	for(int i=1;i<=n;i++){
		ans[i].clear() ;
	}
	idx=0;
	len=1;//答案序列的长度
	root=-1;//根结点(当父节点等于自己的时候赋值给root
}
void add(int a,int b){
	e[idx]=b;
	ne[idx]=h[a];
	h[a]=idx++;
}
void dfs(int u){
	if(h[u]==-1){//说明一条路遍历完了,路的条数++,返回
		len++;
		return ;
	}
	for(int i=h[u];i!=-1;i=ne[i]){
		int j=e[i];
		if(st[j]==0){//当没有走过的时候
			ans[len].push_back(j);
			st[j]=1;
			dfs(j); 
		}
	}
	
}
void sove(){
	cin>>n;
	init();
	for(int i=1;i<=n;i++){
		int num;
		cin>>num;
		if(num==i){//当父节点等于本身的时候说明是根结点
			root=num;
		}else{//否则就把i加入以num为头的链表中
			add(num,i);
		}
	}
	ans[len].push_back(root);
	st[root]=1;
	dfs(root); 
	len--;
	cout<<len<<endl;//输出路径条数
	for(int i=1;i<=len;i++){
		int q=ans[i].size() ;//输出每条路径的长度
		cout<<q<<endl;
		for(int j=0;j<q ;j++){//输出每条路径中存的点
			printf("%d ",ans[i][j]);
		}
		printf("\n");
	}
}
int main(){
	int t;
	cin>>t;
	while(t--){
		sove();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Speed up the execution of important database queries by making good choices about which indexes to create. Choose correct index types for different scenarios. Avoid indexing pitfalls that can actually have indexes hurting performance rather than helping. Maintain indexes so as to provide consistent and predictable query response over the lifetime of an application. Expert Oracle Indexing and Access Paths is about the one database structure at the heart of almost all performance concerns: the index. Database system performance is one of the top concerns in information technology today. Administrators struggle to keep up with the explosion of access and activity driven by the proliferation of computing into everything from phones to tablets to PCs in our increasingly connected world. At the heart of any good-performing database lies a sound indexing strategy that makes appropriate use of indexing, and especially of the vendor-specific indexing features on offer. Few databases fully exploit the wealth of data access mechanisms provided by Oracle. Expert Oracle Indexing and Access Paths helps by bringing together information on indexing and how to use it into one blissfully short volume that you can read quickly and have at your fingertips for reference. Learn the different types of indexes available and when each is best applied. Recognize when queries aren’t using indexes as you intend. Manage your indexing for maximum performance. Confidently use the In Memory column store feature as an alternate access path to improve performance. Let Expert Indexing in Oracle Database 12c be your guide to deep mastery of the most fundamental performance optimization structure in Oracle Database. Explains how indexes help performance, and sometimes hinder it too Demystifies the various index choices so that you can chose rightly Describes the database administration chores associated with indexes Demonstrates the use of the In Memory column store as an alternate access path to the data What You Will Learn Create an overall indexing strategy to guide your decisions Choose the correct indexing mechanisms for your applications Manage and maintain indices to avoid degradation and preserve efficiency Take better advantage of underused index types such as index-organized tables Choose the appropriate columns to index, with confidence Blend partitioning and materialized views into your indexing strategy Who This Book Is For Expert Oracle Indexing and Access Paths is for all levels of database administrators and application developers who are struggling with the database performance and scalability challenge. Any database administrator involved with indexing, which is any database administrator period, will appreciate the wealth of advice packed into this gem of a book. Table of Contents Chapter 1: Introduction to Oracle Indexes Chapter 2: B-tree Indexes Chapter 3: Bitmap Indexes Chapter 4: Index-Organized Tables Chapter 5: Specialized Indexes Chapter 6: Partitioned Indexes Chapter 7: Tuning Index Usage Chapter 8: Maintaining Indexes Chapter 9: SQL Tuning Advisor Chapter 10: In-Memory Column Store

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值