CF--T-decomposition--模拟

D. T-decomposition

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You've got a undirected tree s, consisting of n nodes. Your task is to build an optimal T-decomposition for it. Let's define a T-decomposition as follows.

Let's denote the set of all nodes s as v. Let's consider an undirected tree t, whose nodes are some non-empty subsets of v, we'll call them xi . The tree t is a T-decomposition of s, if the following conditions holds:

  1. the union of all xi equals v;
  2. for any edge (a, b) of tree s exists the tree node t, containing both a and b;
  3. if the nodes of the tree t xi and xj contain the node a of the tree s, then all nodes of the tree t, lying on the path from xi to xj also contain node a. So this condition is equivalent to the following: all nodes of the tree t, that contain node a of the tree s, form a connected subtree of tree t.

There are obviously many distinct trees t, that are T-decompositions of the tree s. For example, a T-decomposition is a tree that consists of a single node, equal to set v.

Let's define the cardinality of node xi as the number of nodes in tree s, containing in the node. Let's choose the node with the maximum cardinality in t. Let's assume that its cardinality equals w. Then the weight of T-decomposition t is value w. The optimal T-decomposition is the one with the minimum weight.

Your task is to find the optimal T-decomposition of the given tree s that has the minimum number of nodes.

Input

The first line contains a single integer n (2 ≤ n ≤ 105), that denotes the number of nodes in tree s.

Each of the following n - 1 lines contains two space-separated integers ai, bi (1 ≤ ai, bi ≤ nai ≠ bi), denoting that the nodes of tree s with indices ai and bi are connected by an edge.

Consider the nodes of tree s indexed from 1 to n. It is guaranteed that s is a tree.

Output

In the first line print a single integer m that denotes the number of nodes in the required T-decomposition.

Then print m lines, containing descriptions of the T-decomposition nodes. In the i-th (1 ≤ i ≤ m) of them print the description of node xi of the T-decomposition. The description of each node xi should start from an integer ki, that represents the number of nodes of the initial tree s, that are contained in the node xi. Then you should print ki distinct space-separated integers — the numbers of nodes from s, contained in xi, in arbitrary order.

Then print m - 1 lines, each consisting two integers pi, qi (1 ≤ pi, qi ≤ mpi ≠ qi). The pair of integers pi, qi means there is an edge between nodes xpi and xqi of T-decomposition.

The printed T-decomposition should be the optimal T-decomposition for the given tree s and have the minimum possible number of nodes among all optimal T-decompositions. If there are multiple optimal T-decompositions with the minimum number of nodes, print any of them.

Examples

input

Copy

2
1 2

output

Copy

1
2 1 2

input

Copy

3
1 2
2 3

output

Copy

2
2 1 2
2 2 3
1 2

input

Copy

4
2 1
3 1
4 1

output

Copy

3
2 2 1
2 3 1
2 4 1
1 2
2 3

题意:

一颗树s,需要根据三个规则构成一个新的树t。

1、s中的一些点可以缩成一个点,从而新成很多缩点构成树t。t中要用到所有的s中点

2、若s中有a-b边,那么t中要有点要包含ab两个点

3、t中的新点若x,y之间有边(x y都包含s中的a点),那么x-y之间的路径上的点都要包含a点。

第一行输出t树中点的个数

接着输出t中点包含点的个数、点的标号

接着输出t中的点和点之间的连接

思路:

相邻的两点形成一个缩点即可。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=100000+66;
const ll mod=1e9+7;
int n;
int a[maxn];
int b[maxn];
vector<int>v[maxn];
int main()
{
    scanf("%d",&n);
    for(int i=1; i<n; i++)
    {
        scanf("%d %d",&a[i],&b[i]);
        v[a[i]].push_back(i);
        v[b[i]].push_back(i);
    }
    printf("%d\n",n-1);
    for(int i=1;i<n;i++)
    {
        printf("2 %d %d\n",a[i],b[i]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<v[i].size()-1;j++)
        {
            printf("%d %d\n",v[i][j],v[i][j+1]);
        }
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dichromatic-decomposition-master是一个开源项目,主要用于解决图像中的色彩问题。在图像处理中,我们常常需要将一张彩色图像拆分成红色通道、绿色通道和蓝色通道三个色彩通道,这样就可以对每个通道进行单独的处理。Dichromatic-decomposition-master项目提供了一种可靠的算法,可以实现彩色图像的分解。 这个项目分为两个部分,一部分是算法的实现,另一部分是示例代码的编写。算法方面,Dichromatic-decomposition-master使用了一种称为二色分解的方法。该方法使用了三个矩阵R、G和B,分别表示图像中红、绿、蓝三个颜色通道。然后通过矩阵运算,可以得到三个分解矩阵Y、U、V。这三个矩阵分别表示亮度、蓝色偏移量和红色偏移量。通过这个算法,我们可以快速准确地分解彩色图像,得到各个色彩通道的信息。 示例代码方面,Dichromatic-decomposition-master提供了一些 Python 脚本,用于演示分解算法的使用。这些脚本可以实现图像的加载、分解、充分和可视化。通过这些示例代码,用户可以更好地了解该算法的原理和应用,并使用其进行图像处理。 总之,Dichromatic-decomposition-master是一个非常实用的图像处理项目,它提供了一种可靠的算法,可以快速准确地将彩色图像分解成更小的色彩通道。如果你需要进行图像处理,它是一个值得推荐的工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值