Cod sursa(job #2793679)

Utilizator TUdOr73Minciunescu Tudor TUdOr73 Data 3 noiembrie 2021 21:23:26
Problema Componente tare conexe Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.81 kb
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
#include <algorithm>
using namespace std;

class Graph {
    int nrNodes, nrEdges;
    bool oriented;
    vector<vector<int>> edges;
    vector<vector<int>> transposedGraph;
    vector<int> topoSort;
public:
    Graph(int nrNodes, bool oriented = true) ;
    void addEdge(int x, int y); // sets edge between x and y
    void BFS(int startingNode, ostream& g);
    int connectedComponents();  // returns the number of conencted components from the graph
    //void biConnectedComponents();
    //void DFS_biconnected(int node, vector<int>& levelInTree, vector<int>&minLevelInTree);
    void DFS(int node, vector<int>& visited);
    void topologicalSort(int x, vector<int> &visited);
    void stronglyConnectedComponents(ofstream & g);
    void DFS2(int currentNode, vector<int> &component, int currentComponent, vector<vector<int>> & solution);
    ~Graph();
};
Graph::Graph(int nrNodes, bool oriented) {
    this->nrNodes = nrNodes;
    this->oriented = oriented;
    edges.resize(nrNodes + 1);
    this->transposedGraph.resize(this->nrNodes+1);
}
void Graph::addEdge(int x, int y) {
    this->edges[x].push_back(y);
    if(!oriented) {
        this->edges[y].push_back(x);
    }
    else{
        this->transposedGraph[y].push_back(x);
    }
}
void Graph::BFS(int startingNode, ostream& g) {
    vector<int> cost(this->nrNodes+1, -1);
    vector<int> visited(this->nrNodes+1, 0);
    queue<int> queue;

    cost[startingNode] = 0;
    visited[startingNode] = 1;
    queue.push(startingNode);

    while(!queue.empty()){
        int currentNode = queue.front();
        for(auto node : edges[currentNode]){
            if(!visited[node]){
                visited[node] = 1;
                cost[node] = cost[currentNode] + 1;
                queue.push(node);
            }
        }
        queue.pop();
    }
   for(int i = 1; i<= nrNodes; i++ ){
       g << cost[i] << " ";
   }
}
void Graph::DFS(int currentNode, vector<int> &visited) {
    for(auto node : this->edges[currentNode]) {
        if(!visited[node]) {
            visited[node] = 1;
            DFS(node, visited);
        }
    }
}
int Graph::connectedComponents() {
    int connectedComp = 0;
    vector<int>visited(this->nrNodes + 1, 0);
    for(int i = 1; i<= nrNodes; i++) {
        if(!visited[i]) {
            visited[i] = 1;
            connectedComp++;
            DFS(i, visited);
        }
    }
    return connectedComp;
}
void Graph::topologicalSort(int currentNode, vector<int> &visited) {
    visited[currentNode] = 1;
    for(auto node: this->edges[currentNode]){
        if(!visited[node])
            topologicalSort(node, visited);
    }
    this->topoSort.push_back(currentNode);
}
void Graph::DFS2(int currentNode, vector<int> &component, int currentComponent, vector<vector<int>> & solution) {
    component[currentNode] = currentComponent;
    solution[currentComponent].push_back(currentNode);
    for(auto i: this->transposedGraph[currentNode]){
        if(component[i] == 0){
            DFS2(i,component,currentComponent,solution);
        }
    }
}
void Graph::stronglyConnectedComponents(ofstream &g) {
    vector<vector<int>>solution(nrNodes+1);
    //this->transposedGraph.resize(this->nrNodes+1);
//    for(int i = 1; i<= this->nrNodes; i++){
//        for(auto j : edges[i]){
//            transposedGraph[j].push_back(i);
//        }
//    }
    vector<int>visited(this->nrNodes, 0);
    for(int i = 1; i<= nrNodes; i++){
        if(!visited[i])
            topologicalSort(i, visited);
    }
    int currentComponent = 0;
    reverse(this->topoSort.begin(), this->topoSort.end());
    vector<int> component(this->nrNodes + 1, 0);
    for(auto i: this->topoSort) {
        if(component[i] == 0) {
            currentComponent++;
            DFS2(i, component, currentComponent,solution);
        }
    }
    g << currentComponent << endl;
    for(int i = 1; i<=currentComponent; i++){
        for(auto j : solution[i]){
            g << j << " ";
        }
        g << endl;
    }
}
//void Graph::DFS_biconnected(int node, vector<int> &levelInTree, vector<int> &minLevelInTree) {
//
//}
//void Graph::biConnectedComponents() {
//    vector<int> levelInTree(this->nrNodes + 1, -1); // also visited
//    vector<int> minLevelInTree(this-> nrNodes + 1, 0);
//    for(int i = 1; i<= nrNodes; i++ ){
//        if(levelInTree[i] == -1){
//            levelInTree[i] = 0;
//            DFS_biconnected(i, levelInTree, minLevelInTree);
//        }
//    }
//}
Graph::~Graph(){
    edges.clear();
}
int main()
{
    int nrNodes, nrEdges;
    ifstream f("ctc.in");
    ofstream g("ctc.out");
    f >> nrNodes >> nrEdges;
    Graph G(nrNodes, true); // false - unorientedGraph, true- orientedGraph
    for(int i = 1; i<= nrEdges; i++) {
        int x, y;
        f >> x >> y;
        G.addEdge(x, y);
    }
    G.stronglyConnectedComponents(g);
    return 0;


}