Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <vector>
- #include <algorithm>
- #include <map>
- #define NIL -1
- using namespace std;
- int components_count = 0;
- map <pair<int, int>, int> components;
- class Graph {
- int V;
- int E;
- list <int> *adj;
- void dfs(int v, bool visited[], int up[], int low[],
- int parent[], list <pair<int, int > > *st);
- public:
- Graph(int V);
- void addEdge(int v, int w);
- void find_components(vector<pair <int, int> > edges);
- };
- Graph::Graph(int V) {
- this->V = V;
- this->E = 0;
- adj = new list <int >[V];
- }
- void Graph::addEdge(int v, int w) {
- adj[v].push_back(w);
- adj[w].push_back(v);
- E++;
- }
- void Graph::dfs(int u, bool visited[], int up[], int low[], int parent[], list <pair<int, int > > *st) {
- static int time = 0;
- visited[u] = true;
- up[u] = low[u] = ++time;
- int children = 0;
- list<int >::iterator i;
- for (i = adj[u].begin(); i != adj[u].end(); ++i) {
- int v = *i;
- if (!visited[v] && v != parent[v]) {
- children++;
- parent[v] = u;
- st->push_back(make_pair(u, v));
- dfs(v, visited, up, low, parent, st);
- low[u] = min(low[u], low[v]);
- if ((parent[u] == NIL && children > 1) || (parent[u] != NIL && low[v] >= up[u])) {
- while (st->back().first != u || st->back().second != v) {
- components[make_pair(st->back().first, st->back().second)] = components_count;
- components[make_pair(st->back().second, st->back().first)] = components_count;
- st->pop_back();
- }
- components[make_pair(st->back().first, st->back().second)] = components_count;
- components[make_pair(st->back().second, st->back().first)] = components_count;
- st->pop_back();
- components_count++;
- }
- } else if (v != parent[u]) {
- low[u] = min(low[u], up[v]);
- if (up[v] < up[u]) {
- st->push_back(make_pair(u, v));
- }
- }
- }
- }
- void Graph::find_components(vector<pair <int, int> > edges) {
- bool *visited = new bool[V];
- int *up = new int[V];
- int *low = new int[V];
- int *parent = new int[V];
- list <pair<int, int > > *st = new list<pair<int, int > >[E];
- for (int i = 0; i < V; i++) {
- parent[i] = NIL;
- visited[i] = false;
- }
- for (int i = 0; i < V; i++) {
- if (visited[i] == false)
- dfs(i, visited, up, low, parent, st);
- int j = 0;
- while (st->size() > 0) {
- j = 1;
- components[make_pair(st->back().first, st->back().second)] = components_count;
- components[make_pair(st->back().second, st->back().first)] = components_count;
- st->pop_back();
- }
- if (j == 1) {
- components_count++;
- }
- }
- cout << components_count << endl;
- for (pair<int,int> edge: edges) {
- cout << components[edge] + 1 << ' ';
- }
- }
- int main() {
- int n, m;
- cin >> n >> m;
- vector<pair <int, int> > edges;
- Graph g(n+1);
- for (int i = 0; i < m; i++) {
- int from = 0;
- int to = 0;
- cin >> from >> to;
- g.addEdge(from, to);
- edges.push_back(make_pair(from, to));
- components[make_pair(from, to)] = NIL;
- }
- g.find_components(edges);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement