This repository was archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatapult.cpp
93 lines (85 loc) · 1.41 KB
/
catapult.cpp
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
#include <algorithm>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int n;
vector<vector<int>> adj;
vector<vector<int>> adjT;
vector<bool> visited;
stack<int> order;
void dfs1(int u)
{
visited[u] = true;
for (int v : adjT[u])
{
if (!visited[v])
dfs1(v);
}
order.push(u);
}
void dfs1()
{
visited.assign(n, false);
for (int i = 0; i < n; ++i)
{
if (!visited[i])
{
dfs1(i);
}
}
}
int cnt = 0;
vector<int> Cnt;
void dfs2(int u)
{
visited[u] = true;
for (int v : adj[u])
{
if (!visited[v])
dfs2(v);
}
cnt++;
}
void dfs2()
{
visited.assign(n, false);
while (!order.empty())
{
int u = order.top();
order.pop();
if (!visited[u])
{
cnt = 0;
dfs2(u);
Cnt.push_back(cnt);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
adj.assign(n, vector<int>());
adjT.assign(n, vector<int>());
for (int i = 0; i < n; ++i)
{
int m;
cin >> m;
for (int j = 0; j < m; ++j)
{
int to;
cin >> to;
adj[i].push_back(to);
adjT[to].push_back(i);
}
}
dfs1();
dfs2();
sort(Cnt.begin(), Cnt.end());
for (int i : Cnt)
{
cout << i << ' ';
}
}