-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasa.cpp
153 lines (131 loc) · 2.94 KB
/
asa.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <vector>
#include <stack>
#include <stdio.h>
using namespace std;
struct Node {
bool onStack;
int value;
int d;
int low;
Node* rep;
Node() : onStack(false)
{}
};
vector<Node*> nodes;
vector< vector<Node*> > edges;
vector< vector<Node*> > sccs;
void makeSet(Node* node){
node->rep = node;
}
void unionSet(Node* node1, Node* node2){
node2->rep = node1->rep;
}
Node* findSet(Node* node){
if(node->rep == node){
return node;
}
node->rep = findSet(node->rep);
return NULL;
}
void tarjanVisit(Node* nodeU, int* pvisited, stack<Node*> *pL){
nodeU->d = *pvisited;
nodeU->low = *pvisited;
*pvisited = (*pvisited) + 1;
pL->push(nodeU);
nodeU->onStack = true;
for (unsigned int i=0; i<edges[nodeU->value-1].size(); i++){
Node* nodeV = edges[nodeU->value-1][i];
if(nodeV->d == -1){
tarjanVisit(nodeV, pvisited, pL);
nodeU->low = min(nodeU->low, nodeV->low);
}else{
if(nodeV->onStack){
nodeU->low = min(nodeU->low, nodeV->d);
}
}
}
/* SCC was found */
if(nodeU->d == nodeU->low){
vector<Node*> vecNodes;
makeSet(nodeU);
vecNodes.push_back(nodeU);
Node* nodeV = pL->top();
pL->pop();
nodeV->onStack = false;
while(nodeV != nodeU){
vecNodes.push_back(nodeV);
unionSet(nodeU, nodeV);
nodeV = pL->top();
pL->pop();
nodeV->onStack = false;
}
sccs.push_back(vecNodes);
}
}
void scc_Tarjan(vector<Node*> nodes, vector< vector<Node*> > edges){
int visitedVal = 0;
int* pvisited = &visitedVal;
stack<Node*> L;
stack<Node*> *pL = &L;
for(unsigned int i=0; i<nodes.size(); i++){
nodes[i]->d = -1;
}
for(unsigned int i=0; i<nodes.size(); i++){
if(nodes[i]->d == -1){
tarjanVisit(nodes[i], pvisited, pL);
}
}
}
int main(){
int nrPeople;
int nrShares;
if( scanf ("%d %d", &nrPeople, &nrShares) != 2){
printf("Expected two numbers as input\n");
return 1;
}
for(int i=1; i<nrPeople+1; i++){
Node* pnode = new Node();
pnode->value = i;
nodes.push_back(pnode);
}
edges.resize(nrPeople);
for(int i=0; i<nrShares; i++){
int valNode1, valNode2;
if( scanf("%d %d", &valNode1, &valNode2) != 2){
printf("Expected two numbers as input\n");
return 1;
}
Node* pnode2 = nodes[valNode2-1];
edges[valNode1-1].push_back(pnode2);
}
scc_Tarjan(nodes, edges);
printf("%zu\n", sccs.size());
int big = 0;
for(unsigned int i=0; i< sccs.size(); i++){
if(sccs[i].size() > (unsigned) big){
big = sccs[i].size();
}
}
printf("%d\n" ,big);
vector<bool> toErase;
toErase.resize(sccs.size(), false);
for(unsigned int i=0; i<sccs.size(); i++){
for(unsigned int j=0; j<sccs[i].size(); j++){
if(!(toErase[i])){
int valSccs = (sccs[i][j]->value)-1;
for(unsigned int h=0; h<edges[valSccs].size(); h++){
if(nodes[valSccs]->rep != edges[valSccs][h]->rep){
toErase[i] = true;
}
}
}
}
}
for(unsigned int i=0; i<toErase.size(); i++){
if(toErase[i]){
sccs.erase(sccs.begin()+i);
}
}
printf("%zu\n", sccs.size());
return 0;
}