tags: Graph Theory greedy
Well-known foodie Xiao A was invited to the ATM Hotel to taste the dishes. ATM hotel is small get ready 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 。
Due to the taste of the dishes, some dishes must be made before other dishes, specifically Strip as " No. 'required' prior to The limitation of the production of dishes, we will abbreviate this limitation as 。
Now, the hotel hopes to find the order in which an optimal dish is made, making it small Try to eat high quality dishes first:
That is,
(1) Subject to all restrictions, No. dishes "as far as possible" priority production;
(2) in meeting all restrictions, No. dishes "as far as possible" under the premise of making No. dishes "as far as possible" priority production;
(3) in meeting all restrictions, Number and No. dishes "as far as possible", No. dishes "as far as possible" priority production;
(4) in meeting all restrictions, Number and Number and No. dishes "as far as possible", No. dishes "as far as possible" priority production;
(5) and so on.
Example 1: Total Road dishes, two restrictions , then the order of production is 。
Example 2: Total Road dishes, two restrictions , then the order of production is 。
In Example 1, first consider Because there are restrictions with So only finished with After making And according to , The number should be as "as far as possible" Priority is given, so it is currently determined that the order of the first three dishes is Next consideration To determine the final production order is 。
In Example 2, first make Is not against the restrictions; next consider When Limit, so make it first Reproduction Next consideration When Limit, so make it first Reproduction , so the final order is . Now you need to find the best order for cooking this dish. No solution output "Impossible!" (without quotes, initial capitalization, lowercase letters)
The first line is a positive integer , indicating the number of data sets. Next is Group data. For each set of data: the first line of two positive integers separated by spaces with , the number of entries indicating the number of dishes and the order of production. Next Row, two positive integers per line , indicating" No. dishes must be made before the y dishes. (Note: There may be identical restrictions in the M restrictions)
Output file only contains Line, each line An integer that indicates the optimal order in which the dishes are made, or "Impossible!" means no solution (without quotes).
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
1 5 3 4 2
Impossible!
1 5 2 4 3
[example explanation]
The second set of data also requires dishes Prior to the dishes Production, dishes Prior to the dishes Production, dishes Prior to the dishes Production, and this is not possible in any case, resulting in no solution.
100% of the data is met 。
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 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("");
}
}
}
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...
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...
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 ...
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...
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...
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...
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...
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...
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...
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>...