-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscc_condense.cpp
129 lines (93 loc) · 2.71 KB
/
scc_condense.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <utility>
#include <functional>
#include <queue>
#include <stack>
#include <list>
using namespace std;
#define MOD 1000000007
#define ULL unsigned long long
#define LL long long
typedef vector<ULL> vi;
typedef vector<ULL> vu;
#define AVI(n) vector<ULL>a(n);rvi(a);
#define AVU vector<ULL>b(n);rvi(b);
#define RNI ULL n;scanf("%llu",&n);
#define RNU ULL n;scanf("%llu", &n);
#define WR(a) for(auto b:a)cout << b << ' '; cout << endl;
inline void rvi(vi& a) { for ( ULL i = 0; i <a.size(); ++i) scanf("%llu", &a[i]); }
inline void rvu(vu& a) { for ( ULL i = 0; i <a.size(); ++i) scanf("%llu", &a[i]); }
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// first read in the adjacency list;
// then read in the strongly connected components
int n, m;
scanf("%d %d", &n, &m);
cerr << n <<' ' << m << endl;
vector<vector<int>> adjlist(n);
for ( int i = 0; i < m; ++i ) {
int a, b;
scanf("%d %d", &a, &b);
//--a; --b;
adjlist[a].push_back(b);
}
// read in scc
int scc_cnt;
cerr << "read: " << scanf("%d", &scc_cnt) << endl;
cerr << scc_cnt << endl;
vector<vector<int>> sccs(scc_cnt);
for ( int i = 0; i < scc_cnt; ++i ) {
int comps;
scanf("%d", &comps);
sccs[i] = vector<int>(comps);
for ( int j = 0; j < comps; ++j ) {
scanf("%d", &(sccs[i][j]));
}
}
cerr << "All read!" << endl;
int cnt = 0;
map<int, int> which;
for ( int i = 0; i < sccs.size(); ++i ) {
for ( int j = 0; j < sccs[i].size(); ++j ) {
which[sccs[i][j]] = i;
//cout << sccs[i][j] << ' ';
}
//cout << endl;
}
cerr << "Heirs assigned" << endl;
vector<set<int>> alist(sccs.size());
cerr << sccs.size() << endl;
int t = 0;
for ( int i = 0; i < sccs.size(); ++i ) {
if ( !sccs[i].size() ) continue;
for ( int j = 0; j < sccs[i].size(); ++j ) {
//cerr << "sccs[i][j] = " << sccs[i][j] << endl;
//cerr << "adjlist[sccs[i][j]].size() = " << adjlist[sccs[i][j]].size() << endl;
for ( int k = 0; k < adjlist[sccs[i][j]].size(); ++k ) {
//cerr << i << ' ' << j << ' ' << k << endl;
//cerr << sccs[i][j] << ' ' << adjlist[sccs[i][j]][k] << endl;
//cerr << which[adjlist[sccs[i][j]][k]] << endl;
if ( which[adjlist[sccs[i][j]][k]] != i ) {
alist[i].insert(which[adjlist[sccs[i][j]][k]] );
}
++t;
if ( t % 100000 == 0 ) cerr << t << endl;
//cerr << t << endl;
}
}
}
int i = 0;
for ( auto a : alist ) {
for ( auto b : a ) {
cout << i << ' ' << b << endl;
}
++i;
}
}