Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("componentebiconexe.in");
- ofstream fout("componentebiconexe.out");
- vector < int > id, low_id;
- vector < vector < int > > G, Components;
- stack < pair < int , int > > S;
- vector < pair < int , int > > bridges;
- unordered_set < int > articulations;
- inline void min_self(int& a, int b) {
- a = min(a, b);
- }
- void BC(int u, int v) {
- vector < int > new_c;
- int x, y;
- do {
- x = S.top().first, y = S.top().second;
- S.pop();
- new_c.emplace_back(x), new_c.emplace_back(y);
- } while(x != u || y != v);
- Components.emplace_back(new_c);
- }
- void DFS(int node, int parent, int val, int root) {
- id[node] = low_id[node] = val;
- int cnt = 0;
- for(int vec : G[node]) {
- if(vec == parent)
- continue;
- if(id[vec] == -1) {
- ++cnt;
- S.emplace(node, vec);
- DFS(vec, node, val + 1, root);
- min_self(low_id[node], low_id[vec]);
- if(low_id[vec] >= id[node]) {
- if(low_id[vec] > id[node])
- bridges.emplace_back(node, vec);
- BC(node, vec);
- if(node != root)
- articulations.insert(node);
- }
- }
- else
- min_self(low_id[node], id[vec]);
- }
- if(node == root && cnt > 1)
- articulations.insert(node);
- }
- int main() {
- fin.sync_with_stdio(false);
- fout.sync_with_stdio(false);
- fin.tie(nullptr);
- fout.tie(nullptr);
- int task, N, M;
- fin >> task >> N >> M;
- G.resize(N + 1), id.assign(N + 1, -1), low_id.resize(N + 1);
- while(M--) {
- int u, v;
- fin >> u >> v;
- G[u].emplace_back(v);
- G[v].emplace_back(u);
- }
- for(int node = 1; node <= N; ++node)
- if(id[node] == -1)
- DFS(node, 0, 0, node);
- if(task == 1) {
- fout << Components.size() << '\n';
- for(auto C : Components) {
- sort(C.begin(), C.end());
- C.erase(unique(C.begin(), C.end()), C.end());
- fout << C.size() << ' ';
- for(int node : C)
- fout << node << ' ';
- fout << '\n';
- }
- }
- else
- if(task == 2) {
- fout << articulations.size() << '\n';
- for(int node : articulations)
- fout << node << ' ';
- }
- else {
- fout << bridges.size() << '\n';
- for(auto b : bridges)
- fout << b.first << ' ' << b.second << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment