Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <algorithm>
- #include <iostream>
- #include <map>
- #include <queue>
- using namespace std;
- vector <int> tin;
- vector <int> up;
- vector <bool> was;
- vector <vector<int>> gr;
- vector <bool> cut_p;
- int P = 0;
- queue <pair <int, int> > buf;
- map < pair <int,int> , int> marker;
- int color = 1;
- void dfs(int v, int last, int timer) {
- was[v] = true;
- tin[v] = up[v] = timer;
- int child = 0;
- for (int u: gr[v]){
- if (u == last) {
- continue;
- }
- else if (was[u] == true){
- up[v] = min(up[v], tin[u]);
- if (tin[u] < tin[v]){
- buf.push({min(u,v), max(u,v)});
- }
- }
- else if (was[u] == false){
- dfs(u, v, timer + 1);
- up[v] = min(up[v], up[u]);
- if (up[u] >= tin[v]){
- if (!cut_p[v]) P++;
- cut_p[v] = true;
- while (true){
- pair <int,int> edge = buf.back();
- buf.pop();
- marker[{min(u,v), max(u,v)}] = color;
- if (edge.first == min(v,u) && edge.second == max(v,u) ){
- color++;
- break;
- }
- }
- }
- child++;
- }
- }
- if (v == last && child > 1) {
- if (!cut_p[v]) P++;
- cut_p[v] = true;
- }
- }
- int main()
- {
- int n, m;
- cin >> n >> m;
- gr.resize(n);
- cut_p.resize(n, false);
- vector <pair<int,int> > Edges;
- for (int i = 0; i < m; ++i) {
- int a, b;
- cin >> a >> b;
- a--;
- b--;
- gr[a].push_back(b);
- gr[b].push_back(a);
- marker[{min(a,b), max(a,b)}] = -1;
- Edges.push_back({min(a,b), max(a,b)});
- }
- tin.resize(n, -4);
- up.resize(n, 1e9);
- was.resize(n, false);
- for (int i = 0;i<n;++i){
- if (!was[i]) {
- dfs(i, -1, 0);
- }
- }
- /*cout<<"\ntin\n";
- for (int i = 0;i<n;++i){
- cout<<i+1<<": "<<tin[i]<<'\n';
- }cout<<'\n';
- cout<<"up\n";
- for (int i=0;i<n;++i){
- cout<<i+1<<": "<<up[i]<<'\n';
- }cout<<'\n';
- cout <<"amount of cut_points = "<< P<< '\n';
- for (int i= 0;i<n;++i){
- if (cut_p[i]){
- cout<<i+1<<'\n';
- }
- }*/
- was.assign(n, false);
- int cnt = -1;
- for (int i = 0;i<n;++i){
- if (!was[i]){
- cnt++;
- dfs(i, -1, cnt);
- }
- }
- cout<<"\nmarking\n";
- for (auto x: Edges){
- cout<<"("<<x.first + 1<<","<<x.second + 1<<") = "<<marker[x]<<'\n';
- }
- cout<<"DONE\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement