简体   繁体   English

图中的排列

[英]Permutation in graphs

There are N students in a line. 一行中有N个学生。 We want to take the best photo. 我们想拍最好的照片。 A photo is considered to be the best when the maximum number of students are satisfied. 当满足最大学生人数时,照片被认为是最好的。 A student is satisfied if he is the right neighbor of his best friend. 如果学生是他最好的朋友的正确邻居,他会感到满意。 Each student has only one best friend. 每个学生只有一个最好的朋友。 You have to find the number of satisfied students in the best photo and the number of different best photos. 您必须找到最佳照片中满意的学生数量以及不同最佳照片中的数量。

Approach: 做法:

.

  • every node is in cycle. 每个节点都处于循环中。 (each node has exactly one incoming and one outgoing edge). (每个节点恰好具有一个输入边缘和一个输出边缘)。 Here we can satisfy all nodes except one (leftmost node). 在这里,我们可以满足除一个节点(最左边的节点)以外的所有节点。 So satisfied nodes are N−1 and different variants are N 因此,满足的节点为N-1,不同的变量为N
  • which have at least one incoming edge can satisfy exactly one node. 具有至少一个进入边缘的节点可以恰好满足一个节点。 So, it's easy to calculate number of satisfied nodes. 因此,很容易计算出满足的节点数。 Let's make array B where B[i] is number of incoming edges of i. 让我们制作数组B,其中B [i]是i的输入边数。 Then different way to satisfy maximum number of nodes is product of B_s. 那么满足最大节点数的不同方法是B_s的乘积。 But there are some “BAD” variants, where all cycle nodes are satisfied. 但是有些“ BAD”变体可以满足所有循环节点的要求。 But we can subtract “BAD” variants easily. 但是我们可以轻松减去“ BAD”变体。 In “BAD” variants for cycle nodes we exactly know who they are satisfying. 在循环节点的“ BAD”变体中,我们确切地知道它们满足谁。 So different variants will be: 因此,不同的变体将是:

    B[K1]*B[K2]....B[Kn] - B[P1]*B[P2]....B[Pm] B [K1] * B [K2] .... B [Kn]-B [P1] * B [P2] .... B [Pm]

where K is array of nodes in component (which have at least 1 incoming edge) and P is array of nodes which aren't in cycle of this component (which have at least 1 incoming edge). 其中K是组件中的节点数组(具有至少1个输入边),P是不在该组件周期中的节点数组(具有至少1个输入边)。

I could not understand the concept of Bad variants why we are subtracting them. 我不明白不良变体的概念,为什么我们要减去它们。

Please Explain and can me some useful links for this type of problems 请解释一下,我能为您提供一些有用的链接来解决此类问题

I'd rather write another editorial than try to figure out what's going on with that one. 我宁愿写另一篇社论,也不愿尝试弄清楚那是怎么回事。

Prepare a "best friends" directed graph, where each vertex is a student, and each student has an arc to his best friend. 准备一个“最好的朋友”有向图,其中每个顶点都是一个学生,每个学生都有一条通往他的最好的朋友的弧线。 Extract the weakly connected components of this graph. 提取该图的弱连接组件。 Within each component, we can satisfy all but one student, but only if the students in that component stand contiguously. 在每个组成部分中,我们只能满足一名学生的要求,但前提是该组成部分中的学生必须连续站立。 Thus we can work out the number of possibilities for each component, multiply them together, and multiply by the number of permutations of the number of components (ie, number of components factorial). 因此,我们可以计算出每个分量的可能性数量,将它们相乘,然后乘以分量数量的排列数量(即,阶乘的分量数量)。

For a given component, there are two possibilities. 对于给定的组件,有两种可能性。 The first possibility is that one vertex has exactly no incoming arcs, one vertex has exactly two, and the rest have one. 第一种可能性是,一个顶点完全没有进入弧,一个顶点恰好有两个,而其余顶点只有一个。 The former student must be rightmost, so there is one valid arrangement. 以前的学生必须在最右边,所以有一个有效的安排。 The second possibility is that all vertices have exactly one incoming arc. 第二种可能性是所有顶点都恰好有一个传入弧。 In this case, the number of valid arrangements is equal to the number of vertices in the component (choose the rightmost student arbitrarily and then go around the cycle). 在这种情况下,有效排列的数量等于组件中顶点的数量(任意选择最右边的学生,然后遍历循环)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM