This repository was archived by the owner on Sep 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1054.cpp
More file actions
114 lines (102 loc) · 2.22 KB
/
1054.cpp
File metadata and controls
114 lines (102 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
auto nextInt () -> int;
auto readN (int *array, int n) -> void;
template <typename T>
auto print (const T &) -> void;
template <typename T>
auto println (const T &) -> void;
template <typename T>
auto printsp (const T &) -> void;
template <typename T>
struct Greater;
template <typename T>
struct Less;
constexpr int kMaxn = 1e5 + 10;
using si = std::basic_string<int>;
si edges[kMaxn];
int max[kMaxn];
auto dfs (int n) -> void {
for (int to : edges[n]) {
if (max[to] < max[n]) {
max[to] = max[n];
dfs(to);
}
}
}
auto main () -> int {
int n = nextInt(), m = nextInt();
for (int i = 0; i < m; ++i) {
int from = nextInt() - 1, to = nextInt() - 1;
edges[to] += from;
}
for (int i = 0; i < n; ++i) max[i] = i;
for (int i = n - 1; i >= 0; --i) {
if (max[i] == i) dfs(i);
}
for (int i = 0; i < n; ++i) {
printsp(max[i] + 1);
}
putchar('\n');
return 0;
}
auto nextInt () -> int {
int i = 0, sign = 1;
char c;
while (!isdigit(c = getchar())) if (c == '-') sign = -1;
do {
i = i * 10 + c - '0';
} while (isdigit(c = getchar()));
return i * sign;
}
auto readN (int *array, int n) -> void {
for (int i = 0; i < n; ++i) array[i] = nextInt();
}
template <>
auto print<int> (const int &val) -> void {
printf("%d", val);
}
template <>
auto print<char> (const char &val) -> void {
putchar(val);
}
template <>
auto print<char *> (char * const &val) -> void {
printf("%s", val);
}
template <>
auto print<const char *> (const char * const &val) -> void {
printf("%s", val);
}
template <>
auto print<long long> (const long long &val) -> void {
printf("%lld", val);
}
template <>
auto print<unsigned long long> (const unsigned long long &val) -> void {
printf("%llu", val);
}
template <typename T>
auto println (const T &val) -> void {
print(val);
putchar('\n');
}
template <typename T>
auto printsp (const T &val) -> void {
print(val);
putchar(' ');
}
template <typename T>
struct Greater {
auto operator() (const T &lhs, const T &rhs) const -> bool {
return lhs > rhs;
}
};
template <typename T>
struct Less {
auto operator() (const T &lhs, const T &rhs) const -> bool {
return lhs < rhs;
}
};