|
| 1 | +/* |
| 2 | +ID: Koral Kulacoglu |
| 3 | +TASK: test |
| 4 | +LANG: C++ |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <bits/stdc++.h> |
| 8 | + |
| 9 | +using namespace std; |
| 10 | + |
| 11 | +typedef long long ll; |
| 12 | +typedef long double ld; |
| 13 | +typedef unsigned long long ull; |
| 14 | +typedef long double lld; |
| 15 | +typedef complex<ld> cd; |
| 16 | + |
| 17 | +typedef pair<int, int> pi; |
| 18 | +typedef pair<ll,ll> pll; |
| 19 | +typedef pair<ld,ld> pld; |
| 20 | + |
| 21 | +typedef vector<int> vi; |
| 22 | +typedef vector<string> vs; |
| 23 | +typedef vector<char> vc; |
| 24 | +typedef vector<ld> vld; |
| 25 | +typedef vector<ll> vll; |
| 26 | +typedef vector<pi> vpi; |
| 27 | +typedef vector<pll> vpll; |
| 28 | +typedef vector<cd> vcd; |
| 29 | + |
| 30 | +template<class T> using pq = priority_queue<T>; |
| 31 | +template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>; |
| 32 | + |
| 33 | +#define FOR(i, a, b) for (int i=a; i<(b); i++) |
| 34 | +#define F0R(i, a) for (int i=0; i<(a); i++) |
| 35 | +#define FORd(i, a, b) for (int i=(a)-1; i >= b; i--) |
| 36 | +#define F0Rd(i, a) for (int i=(a)-1; i >= 0; i--) |
| 37 | +#define trav(a, x) for (auto& a : x) |
| 38 | +#define uid(a, b) uniform_int_distribution<int>(a, b)(rng) |
| 39 | + |
| 40 | +#define sz(x) (int)(x).size() |
| 41 | +#define all(x) x.begin(), x.end() |
| 42 | +#define mp make_pair |
| 43 | +#define pb push_back |
| 44 | +#define fir first |
| 45 | +#define sec second |
| 46 | +#define ins insert |
| 47 | +#define lbound(a, v) lower_bound(all(a), v)-a.begin() |
| 48 | +#define ubound(a, v) upper_bound(all(a), v)-a.begin() |
| 49 | +#define gcd(a, b) __gcd(a, b) |
| 50 | +#define lcm(a, b) (a*b)/gcd(a, b) |
| 51 | + |
| 52 | +template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } |
| 53 | +template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } |
| 54 | + |
| 55 | +void __print(int x) {cerr << x;} |
| 56 | +void __print(long x) {cerr << x;} |
| 57 | +void __print(long long x) {cerr << x;} |
| 58 | +void __print(unsigned x) {cerr << x;} |
| 59 | +void __print(unsigned long x) {cerr << x;} |
| 60 | +void __print(unsigned long long x) {cerr << x;} |
| 61 | +void __print(float x) {cerr << x;} |
| 62 | +void __print(double x) {cerr << x;} |
| 63 | +void __print(long double x) {cerr << x;} |
| 64 | +void __print(char x) {cerr << '\'' << x << '\'';} |
| 65 | +void __print(const char *x) {cerr << '\"' << x << '\"';} |
| 66 | +void __print(const string &x) {cerr << '\"' << x << '\"';} |
| 67 | +void __print(bool x) {cerr << (x ? "true" : "false");} |
| 68 | + |
| 69 | +template<typename T, typename V> |
| 70 | +void __print(const pair<T, V> &x); |
| 71 | +template<typename T> |
| 72 | +void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";} |
| 73 | +template<typename T, typename V> |
| 74 | +void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';} |
| 75 | +void _print() {cerr << "]\n";} |
| 76 | +template <typename T, typename... V> |
| 77 | +void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} |
| 78 | + |
| 79 | +#ifdef DEBUG |
| 80 | +#define dbg(x...) cerr <<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << endl; |
| 81 | +#else |
| 82 | +#define dbg(x...) |
| 83 | +#endif |
| 84 | + |
| 85 | +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); |
| 86 | + |
| 87 | +struct custom_hash { |
| 88 | + size_t operator()(uint64_t x) const { |
| 89 | + x ^= rng(); |
| 90 | + return x ^ (x >> 16); |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +const int MOD = 1000000007; |
| 95 | +const char nl = '\n'; |
| 96 | +const int MX = 100001; |
| 97 | + |
| 98 | +/* |
| 99 | + * DFS to find end times |
| 100 | + * reverse graph |
| 101 | + * find SCCs in reverse order |
| 102 | +*/ |
| 103 | + |
| 104 | +void solve() { |
| 105 | + int n, m; cin >> n >> m; |
| 106 | + vector<vi> mp(n); |
| 107 | + FOR (i, 0, m) { |
| 108 | + int a, b; cin >> a >> b; a--; b--; |
| 109 | + mp[a].pb(b); |
| 110 | + } |
| 111 | + |
| 112 | + int t = 0; |
| 113 | + vector<bool> vis(n, false); |
| 114 | + vi start(n), end(n); |
| 115 | + |
| 116 | + auto dfs = [&](int node, auto&& self) -> void { |
| 117 | + vis[node] = true; |
| 118 | + start[node] = t++; |
| 119 | + trav (neigh, mp[node]) { |
| 120 | + if (!vis[neigh]) self(neigh, self); |
| 121 | + } |
| 122 | + end[node] = t++; |
| 123 | + }; |
| 124 | + |
| 125 | + FOR (i, 0, n) if (!vis[i]) dfs(i, dfs); |
| 126 | + |
| 127 | + vector<vi> mpR(n); |
| 128 | + FOR (i, 0, n) { |
| 129 | + trav (j, mp[i]) mpR[j].pb(i); |
| 130 | + } |
| 131 | + |
| 132 | + int kingdom = 0; |
| 133 | + vi sccs(n); |
| 134 | + |
| 135 | + auto dfs2 = [&](int node, auto&& self) -> void { |
| 136 | + vis[node] = true; |
| 137 | + sccs[node] = kingdom; |
| 138 | + trav (neigh, mpR[node]) { |
| 139 | + if (!vis[neigh]) self(neigh, self); |
| 140 | + } |
| 141 | + }; |
| 142 | + |
| 143 | + vpi order(n); |
| 144 | + FOR (i, 0, n) order[i] = {end[i], i}; |
| 145 | + sort(order.begin(), order.end(), greater<pi>()); |
| 146 | + |
| 147 | + fill(vis.begin(), vis.end(), false); |
| 148 | + |
| 149 | + FOR (i, 0, n) { |
| 150 | + int node = order[i].sec; |
| 151 | + if (vis[node]) continue; |
| 152 | + kingdom++; |
| 153 | + dfs2(node, dfs2); |
| 154 | + } |
| 155 | + |
| 156 | + cout << kingdom << nl; |
| 157 | + trav (i, sccs) cout << i << ' '; |
| 158 | + cout << nl; |
| 159 | +} |
| 160 | + |
| 161 | +int main() { |
| 162 | + cin.tie(0)->sync_with_stdio(0); |
| 163 | + cin.exceptions(cin.failbit); |
| 164 | + |
| 165 | + int T = 1; |
| 166 | +// cin >> T; |
| 167 | + while(T--) { |
| 168 | + solve(); |
| 169 | + } |
| 170 | + |
| 171 | + return 0; |
| 172 | +} |
| 173 | + |
0 commit comments