[Luogu P3243] [BZOJ 4010] [HNOI2015] dishes making

tags: Graph Theory  greedy

Luogu Portal

BZOJ portal

Description of the topic

Well-known foodie Xiao A was invited to the ATM Hotel to taste the dishes. ATM hotel is small A A get ready N N The dishes are ordered in the order of 1 to N according to the quality of the dishes. The highest quality dishes are estimated to be 1 1

Due to the taste of the dishes, some dishes must be made before other dishes, specifically M M Strip as " i i No. 'required' prior to j j The limitation of the production of dishes, we will abbreviate this limitation as < i , j > <i,j>

Now, the hotel hopes to find the order in which an optimal dish is made, making it small A A Try to eat high quality dishes first:

That is,

(1) Subject to all restrictions, 1 1 No. dishes "as far as possible" priority production;

(2) in meeting all restrictions, 1 1 No. dishes "as far as possible" under the premise of making 2 2 No. dishes "as far as possible" priority production;

(3) in meeting all restrictions, 1 1 Number and 2 2 No. dishes "as far as possible", 3 3 No. dishes "as far as possible" priority production;

(4) in meeting all restrictions, 1 1 Number and 2 2 Number and 3 3 No. dishes "as far as possible", 4 4 No. dishes "as far as possible" priority production;

(5) and so on.

Example 1: Total 4 4 Road dishes, two restrictions < 3 , 1 > < 4 , 1 > <3,1>、<4,1> , then the order of production is 3 , 4 , 1 , 2 3,4,1,2

Example 2: Total 5 5 Road dishes, two restrictions < 5 , 2 > < 4 , 3 > <5,2>、 <4,3> , then the order of production is 1 , 5 , 2 , 4 , 3 1,5,2,4,3

In Example 1, first consider 1 1 Because there are restrictions < 3 , 1 > <3,1> with < 4 , 1 > <4,1> So only finished 3 3 with 4 4 After making 1 1 And according to ( 3 ) (3) 3 3 The number should be as "as far as possible" 4 4 Priority is given, so it is currently determined that the order of the first three dishes is 3 , 4 , 1 3,4,1 Next consideration 2 2 To determine the final production order is 3 , 4 , 1 , 2 3,4,1,2

In Example 2, first make 1 1 Is not against the restrictions; next consider 2 2 When < 5 , 2 > <5,2> Limit, so make it first 5 5 Reproduction 2 2 Next consideration 3 3 When < 4 , 3 > <4,3> Limit, so make it first 4 4 Reproduction 3 3 , so the final order is 1 , 5 , 2 , 4 , 3 1,5,2,4,3 . Now you need to find the best order for cooking this dish. No solution output "Impossible!" (without quotes, initial capitalization, lowercase letters)

Input and output format

Input format:

The first line is a positive integer D D , indicating the number of data sets. Next is D D Group data. For each set of data: the first line of two positive integers separated by spaces N N with M M , the number of entries indicating the number of dishes and the order of production. Next M M Row, two positive integers per line x , y x,y , indicating" x x No. dishes must be made before the y dishes. (Note: There may be identical restrictions in the M restrictions)

Output format:

Output file only contains D D Line, each line N N An integer that indicates the optimal order in which the dishes are made, or "Impossible!" means no solution (without quotes).

Input and output sample

Enter sample #1:

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

Output sample #1:

1 5 3 4 2 
Impossible! 
1 5 2 4 3

Description

[example explanation]

The second set of data also requires dishes 1 1 Prior to the dishes 2 2 Production, dishes 2 2 Prior to the dishes 3 3 Production, dishes 3 3 Prior to the dishes 1 1 Production, and this is not possible in any case, resulting in no solution.

100% of the data is met N , M 100000 , D 3 N,M\le 100000,D\le 3

Problem analysis

The problem is trying to make us believe that this is a sort of topological and lexicographical order, but this is not the third example...

Because we are greedy to make the lexicographical order small in front, but what we want is to try to put the smallest in front as far as possible, so obviously W A WA of.

Then we consider it in turn, try to put the smallest in front as far as possible, just try to put the maximum in the back, just do it again to make the lexicographically largest solution, and then reverse the output.

code show as below:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define R register
#define IN inline
#define W while
#define gc getchar()
#define MX 100500
template <class T>
IN void in(T &x)
{
    x = 0; R char c = gc;
    for (; !isdigit(c); c = gc);
    for (;  isdigit(c); c = gc)
    x = (x << 1) + (x << 3) + c - 48;
}
int n, m;
std::vector <int> nex[MX];
std::priority_queue <int> pq;
int deg[MX], res[MX];
IN bool cmp(R int x, R int y) {return x > y;}
int main(void)
{
    int T, a, b, tot, now;
    in(T);
    W (T--)
    {
        in(n), in(m), tot = 0;
        std::memset(deg, 0, sizeof(deg));
        for (R int i = 1; i <= n; ++i) nex[i].clear();
        for (R int i = 1; i <= m; ++i)
        {
            in(a), in(b); deg[a]++;
            nex[b].push_back(a);
        }
        for (R int i = n; i; --i)
        {
            if (!deg[i]) pq.push(i);
            std::sort(nex[i].begin(), nex[i].end(), cmp);
        }
        W (!pq.empty())
        {
            now = pq.top(), pq.pop();
            res[++tot] = now;
            for (auto i : nex[now])
            {
                --deg[i];
                if (!deg[i]) pq.push(i);
            }
        }
        if (tot != n) puts("Impossible!");
        else
        {
            for (R int i = n; i; --i) printf("%d ", res[i]);
            puts("");
        }
    }
}

Intelligent Recommendation

Luogu P3243 [HNOI2015] Cooking

Title description The well-known gourmet, Xiao A, was invited to the ATM Hotel to evaluate dishes for him. The ATM hotel prepared N dishes for Little A. The hotel numbered the dishes from 1 to N accor...

luogu P3243 [HNOI2015] food production

This question and see that topological order related Consider topological sort queue when the minimum number of sorting each taking then\(\mathcal{GG}\)Because this will only make the lexicographicall...

bzoj 4010: [HNOI2015] food production

Description A little-known chefs are invited to ATM Hotel for its tasting menu. ATM hotel has a small dish N A, according to the quality of the hotel dishes estimated descending administration 1 to N ...

BZOJ 4010 dishes (topology sort)

Title link:BZOJ 4010 Question: Topological sorting is more obvious, two ideas: Positive, every time you find a word, the order is the smallest Pour, every time you find a word ordered If there is a re...

BZOJ4010 [HNOI2015] Dishes making

Label: Topological Sort topic Topic portal Title description The well-known gourmet, Xiao A, was invited to the ATM Hotel to evaluate the dishes. The ATM hotel prepared N dishes for Little A. The hote...

More Recommendation

bzoj4010: [HNOI2015] Dishes making

The subject is here Title: Find a permutation of n, there are some restrictions practice: Note that this is different from seeking the smallest lexicographic order. An example is n=4, restrictions are...

BZOJ 4010: [HNOI2015] Cooking [Topology+Heap]

4010: [HNOI2015] Dishes making Time Limit: 5 Sec Memory Limit: 512 MB Description The well-known gourmet, Xiao A, was invited to the ATM Hotel to evaluate the dishes. The ATM hotel prepared N dishes f...

BZOJ 4010 [Hnoi2015] Cooking Topology Sort

Ideal: Give a direction map, ask for a topological order (may not), meet the number of small numbers (priority 1 pre-priority 2 Before ...) Sol: Recently, it's too decadent, and write some topic. Firs...

BZOJ4010[HNOI2015] dishes making - topological sorting + heap

Description of the topic Well-known foodie Xiao A was invited to the ATM Hotel to taste the dishes. The ATM Hotel prepares N dishes for the small A, and the hotel gives the highest quality to the esti...

[BZOJ4010][HNOI2015] Dishes making (topological sorting + heap)

Seeing the topic, you can think of topological sorting. But if the smallest lexicographic order is required, it is wrong. can give counter-examples: 4 Kinds of dishes, limited to <2,4><3,1>...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top