-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathReduce Graph.cpp
136 lines (113 loc) · 2.68 KB
/
Reduce Graph.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
130
131
132
133
134
135
136
//============================================================================
// Name : Reduce.cpp
// Author : Ivan Galban Smith
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <bits/stdc++.h>
using namespace std;
/////////////////////////////////////////////////
typedef complex<double> P;
typedef vector<P> Pol;
typedef long long Int;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> Graph;
/////////////////////////////////////////////////
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, n) for (int i = 1; i <= (int)n; ++i)
#define ITR(c) __typeof((c).begin())
#define foreach(i, c) for (ITR(c) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define DB(x) cout << #x << " = " << x << endl
#define X(c) real(c)
#define Y(c) imag(c)
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define BIT(n) (1 << n)
/////////////////////////////////////////////////
const double EPS = 1e-15;
const int oo = (1 << 30);
const double PI = M_PI;
const int MOD = 1000000000 + 7;
/////////////////////////////////////////////////
const int MaxN = 1000;
struct Edge {
int src, dst, wt;
Edge(int a, int b, int c) : src(a), dst(b), wt(c) {}
};
int n, m;
vector<Edge> g[MaxN];
vector<Edge> gt[MaxN];
vector<Edge> gr[MaxN];
int cur, cur_scc;
int mk[MaxN];
int order[MaxN];
int scc[MaxN];
int vcountSCC[MaxN];
void dfs(int u) {
mk[u] = true;
REP(i, g[u].size()) {
int v = g[u][i].dst;
if (!mk[v])
dfs(v);
}
order[n - 1 - cur++] = u;
}
void dfs_rev(int u) {
scc[u] = cur_scc;
++vcountSCC[cur_scc];
mk[u] = true;
REP(i, gt[u].size()) {
int v = gt[u][i].dst;
if (!mk[v])
dfs_rev(v);
}
}
void make_scc() {
cur = 0;
memset(mk, 0, sizeof(mk));
REP(i, n)
if (!mk[i])
dfs(i);
cur_scc = 0;
memset(mk, 0, sizeof(mk));
REP(i, n) {
int v = order[i];
if (!mk[v]) {
dfs_rev(v);
++cur_scc;
}
}
}
void build_reduce_graph() {
make_scc();
REP(i, n)
REP(j, g[i].size())
if (scc[i] != scc[g[i][j].dst])
gr[scc[i]].pb(Edge(scc[i], scc[g[i][j].dst], g[i][j].wt));
}
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(0);
cin >> n >> m;
REP(i, m) {
int a, b, c;
cin >> a >> b >> c;
g[a].pb(Edge(a, b, c));
gt[b].pb(Edge(b, a, c));
}
build_reduce_graph();
cout << "V " << cur_scc << "\nEdges:\n";
REP(u, cur_scc)
REP(i, gr[u].size()) {
Edge e = gr[u][i];
cout << e.src << " " << e.dst << " " << e.wt << endl;
;
}
return 0;
}