简体   繁体   中英

Permutation in graphs

There are N students in a line. 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
  • 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. Then different way to satisfy maximum number of nodes is product of B_s. But there are some “BAD” variants, where all cycle nodes are satisfied. But we can subtract “BAD” variants easily. In “BAD” variants for cycle nodes we exactly know who they are satisfying. So different variants will be:

    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).

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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