Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <vector>
- #include <algorithm>
- using namespace std;
- void DFS(int ** G, int v, int vertices);
- void DFS2(int ** G, int v, int vertices, vector<int> *);
- bool * vfound;
- int ** graph;
- stack<int> F;
- vector<vector<int> *> scc;
- int main () {
- int vertices, edges;
- cin >> vertices;
- cin >> edges;
- vfound = new bool[vertices];
- for (int i = 0; i < vertices; i++) {
- vfound[i] = false;
- }
- graph = new int *[vertices];
- for (int i = 0; i < vertices; i++) {
- graph[i] = new int[vertices];
- }
- for (int i = 0; i < vertices; i++) {
- for (int j = 0; j < vertices; j++) {
- graph[i][j] = 0;
- }
- }
- int input1, input2;
- for (int i = 0; i < edges; i++) {
- //cout << "Accepting input " << i << "th edge" << endl;
- cin >> input1;
- cin >> input2;
- graph[input1][input2] = 1;
- }
- for(int i = 0; i < vertices; i++){
- for(int j = 0; j < vertices; j++){
- //cout << graph[i][j] << " ";
- }
- //cout << endl;
- }
- while(true){
- int allfound = -1;
- for(int i = vertices - 1; i > -1; i--){
- if(!vfound[i]){
- allfound = i;
- }
- }
- //cout << "got here!" << allfound << endl;
- if(allfound == -1){
- break;
- }
- //cout << "calling DFS1 for " << allfound << endl;
- DFS(graph, allfound, vertices);
- }
- int ** graphT = new int *[vertices];
- for (int i = 0; i < vertices; i++) {
- graphT[i] = new int[vertices];
- }
- for (int i = 0; i < vertices; i++) {
- for (int j = 0; j < vertices; j++) {
- graphT[i][j] = graph[j][i];
- }
- }
- for (int i = 0; i < vertices; i++) {
- vfound[i] = false;
- }
- int i = 0;
- while (!F.empty()) {
- //cout << "calling DFS for " << F.top() << endl;
- if (!vfound[F.top()]) {
- //cout << "It was not already found" << endl;
- scc.push_back(new vector<int>);
- DFS2(graphT, F.top(), vertices, scc[i]);
- i++;
- }
- F.pop();
- }
- //cout << "scc list: ";
- for(int k = 0; k < scc.size(); k++){
- for(int l = 0; l < scc[k]->size(); l++){
- //cout << scc[k]->at(l) << " ";
- }
- }
- //cout << endl;
- for (int j = 0; j < vertices; j++) {
- //cout << "j is " << j << endl;
- for (int k = 0; k < scc.size(); k++) {
- if (find(scc[k]->begin(), scc[k]->end(), j) != scc[k]->end()) {
- int min = 40000;
- for(int l = 0; l < scc[k]->size(); l++){
- if(scc[k]->at(l) < min){
- min = scc[k]->at(l);
- }
- }
- cout << min << endl;
- }
- }
- }
- }
- void DFS (int ** G, int v, int vertices) {
- if (!vfound[v]) {
- vfound[v] = true;
- for (int i = 0; i < vertices; i++) {
- if (graph[v][i] == 1) {
- DFS(G, i, vertices);
- }
- }
- F.push(v);
- }
- }
- void DFS2 (int ** G, int v, int vertices, vector<int> * scc) {
- if (!vfound[v]) {
- vfound[v] = true;
- for (int i = 0; i < vertices; i++) {
- if (G[v][i] == 1) {
- DFS2(G, i, vertices, scc);
- }
- }
- //cout << "adding " << v << " to scc" << endl;
- scc->push_back(v);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement