Https://vijos.org/p/1790
Good God's greedy question ..
At first I thought of greed, but it was wrong .. Sad
It is because the number of each node is related to the subtree of the inverse graph, and the number must be greater than or equal to the subtree's son + 1. But I want to quickly find out what I can't do .. This DFS involves too many things.
Then I thought about whether these constraints are related to the difference constraint, and then I made up my mind, but later I found that this was not feasible because the difference constraint was not a number. Sad ..
You have no choice but to answer the question.
Sad
This is related to my first thought...
Because the number must be greater than or equal to the Child tree son + 1 of the inverse graph, it is clear that each time the number is placed in a large-to-small manner, this property is satisfied.
Create an inverse graph, that is, the point with the maximum label degree of 0 each time, then the vertex number is the remaining number.
#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }#define printarr1(a, b) for1(_, 1, b) cout << a[_] << ‘\t‘; cout << endlinline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }priority_queue<int> id;const int N=100005, M=200005;int ihead[N], cnt, n, m, tot, in[N], a[N];struct ED { int to, next; }e[M];void add(int u, int v) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;}int main() {read(n); read(m); tot=n+1;rep(i, m) {int u=getint(), v=getint();add(v, u); ++in[u];}for1(i, 1, n) if(in[i]==0) id.push(i);while(!id.empty()) {int u=id.top(); id.pop();a[u]=--tot;for(int i=ihead[u]; i; i=e[i].next) {--in[e[i].to];if(in[e[i].to]==0) id.push(e[i].to);}}for1(i, 1, n) printf("%d ", a[i]);return 0;}
Description
H there are n cities in China, and m one-way roads exist between cities, so that no city can return to itself through a certain path.
Now the King wants to rename the city and set the new number of the city I to a [I]. The new numbers of all cities are different, and the numbers are [1, an integer between N. The King thinks that a numbering scheme is beautiful. If I can reach J only for any two cities, a [I] should be <A [J].
There are many beautiful numbering schemes. The King wants to make the number of city 1 as small as possible. On this premise, the number of city 2 is as small as possible.
Format input format
The first line reads n, m, which indicates n cities and M has a directed path.
Next, read the M line. Each line has two integers: X, Y.
Indicates that there is a directed path between city X and city y.
Output Format
Output line: N Integers
The I-th integer indicates the new number A [I] of the I-th city. The output must be an arrangement of 1 to n.
Example 1 input 1 [copy]
5 44 11 35 32 5
Sample output 1 [copy]
2 3 5 1 4
Restrictions
1 s for each test point
Prompt
30% of test points met: n <= 10, m <= 10
70% of test points met: n <= 1000, m <= 10000
100% of test points met: n <= 100000, m <= 200000
The input data may have duplicate edges and may not be connected, but it must be a directed acyclic graph.
Source
Topcoder
[Vijos] 1790 topology No. (topology + greedy)