zoukankan      html  css  js  c++  java
  • T103492 【模板】点双连通分量

    题目地址 


    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int SIZE = 1e5;
    int head[SIZE], ver[SIZE * 2], Next[SIZE * 2];
    int dfn[SIZE], low[SIZE], stack[SIZE], new_id[SIZE], c[SIZE];
    int n, m, tot, num, root, top, cnt;
    vector<int> dcc[SIZE];
    void add(int x, int y) {
    	ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
    }
    void tarjan(int x) {
    	dfn[x] = low[x] = ++num;
    	stack[++top] = x;
    	if (x == root && head[x] == 0) { // 孤立点
    		dcc[++cnt].push_back(x);
    		return;
    	}
    	for (int i = head[x]; i; i = Next[i]) {
    		int y = ver[i];
    		if (!dfn[y]) {
    			tarjan(y);
    			low[x] = min(low[x], low[y]);
    			if (low[y] >= dfn[x]) {
    				//if (x != root || flag > 1) cut[x] = true;
    				cnt++;
    				int z;
    				do {
    					z = stack[top--];
    					dcc[cnt].push_back(z);
    				} while (z != y);
    				dcc[cnt].push_back(x);
    			}
    		}
    		else low[x] = min(low[x], dfn[y]);
    	}
    }
    
    int main() {
    	cin >> n >> m;
    	tot = 1;
    	for (int i = 1; i <= m; i++) {
    		int x, y;
    		scanf("%d%d", &x, &y);
    		if (x == y) continue;
    		add(x, y), add(y, x);
    	}
    	for (int i = 1; i <= n; i++)
    		if (!dfn[i]) root = i, tarjan(i);
    	for (int i = 1; i <= cnt; i++) {
    		for (int j = 0; j < dcc[i].size(); j++)
    			printf("%d ", dcc[i][j]);
    		puts("");
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    地图相关
    爬虫机器人检测网站
    Git 工作区、暂存区和版本库概念
    linux镜像下载地址
    selenium基本使用
    socket 编程
    视频观看时间统计
    油猴脚本
    (II)第十三节:使用注解创建Dao、Service、Controller Bean 组件
    (II)第十一节:SpEL 表达式
  • 原文地址:https://www.cnblogs.com/zbsy-wwx/p/11742089.html
Copyright © 2011-2022 走看看