码迷,mamicode.com
首页 > 其他好文 > 详细

[HIHO1184]连通性二·边的双连通分量(双连通分量)

时间:2016-06-10 16:16:33      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://hihocoder.com/problemset/problem/1184

题意裸,写个博客记下输出姿势。

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35 using namespace std;
 36 #define fr first
 37 #define sc second
 38 #define cl clear
 39 #define BUG puts("here!!!")
 40 #define W(a) while(a--)
 41 #define pb(a) push_back(a)
 42 #define Rint(a) scanf("%d", &a)
 43 #define Rll(a) scanf("%lld", &a)
 44 #define Rs(a) scanf("%s", a)
 45 #define Cin(a) cin >> a
 46 #define FRead() freopen("in", "r", stdin)
 47 #define FWrite() freopen("out", "w", stdout)
 48 #define Rep(i, len) for(int i = 0; i < (len); i++)
 49 #define For(i, a, len) for(int i = (a); i < (len); i++)
 50 #define Cls(a) memset((a), 0, sizeof(a))
 51 #define Clr(a, x) memset((a), (x), sizeof(a))
 52 #define Full(a) memset((a), 0x7f7f, sizeof(a))
 53 #define lrt rt << 1
 54 #define rrt rt << 1 | 1
 55 #define pi 3.14159265359
 56 #define RT return
 57 #define lowbit(x) x & (-x)
 58 #define onenum(x) __builtin_popcount(x)
 59 typedef long long LL;
 60 typedef long double LD;
 61 typedef unsigned long long ULL;
 62 typedef pair<int, int> pii;
 63 typedef pair<string, int> psi;
 64 typedef pair<LL, LL> pll;
 65 typedef map<string, int> msi;
 66 typedef vector<int> vi;
 67 typedef vector<LL> vl;
 68 typedef vector<vl> vvl;
 69 typedef vector<bool> vb;
 70 
 71 const int maxn = 50020;
 72 const int maxm = 500100;
 73 typedef struct Edge {
 74     int u, v;
 75     int next, i;
 76     bool cut;
 77     Edge() {}
 78     Edge(int uu, int vv, int ii) : u(uu), v(vv), i(ii) {}
 79 }Edge;
 80 
 81 Edge edge[maxm];
 82 int head[maxn], ecnt;
 83 int n, m;
 84 int dfn[maxn];
 85 int low[maxn];
 86 bool vis[maxn];
 87 bool cut[maxn];
 88 vi bridge;
 89 int st[maxn], top;
 90 int belong[maxn], bcnt;
 91 vi tmp;
 92 int ret;
 93 
 94 void init() {
 95     Clr(head, -1); Cls(low); Cls(dfn);
 96     Cls(vis); Cls(cut); Cls(st); ret = 0;
 97     bcnt = 1; top = 0; ecnt = 0; bridge.cl();
 98 }
 99 
100 void adde(int uu, int vv, int ii) {
101     edge[ecnt] = Edge(uu, vv, ii);
102     edge[ecnt].cut = 0;
103     edge[ecnt].next = head[uu];
104     head[uu] = ecnt++;
105 }
106 
107 
108 void tarjan(int u, int d, int p) {
109     low[u] = dfn[u] = d;
110     st[++top] = u;
111     vis[u] = 1;
112     for(int i = head[u]; ~i; i=edge[i].next) {
113         int v = edge[i].v;
114         int id = edge[i].i;
115         if(p == id) continue;
116         if(!dfn[v]) {
117             tarjan(v, d+1, id);
118             low[u] = min(low[u], low[v]);
119             if(low[v] > dfn[u]) {
120                 bridge.pb(i);
121                 edge[i].cut = edge[i^1].cut = 1;
122             }
123         }
124         else low[u] = min(low[u], dfn[v]);
125     }
126     if(low[u] == dfn[u]) {
127         tmp.cl();
128         int v;
129         bcnt = 0x7f7f7f;
130         do {
131             v = st[top--];
132             vis[v] = 0;
133             bcnt = min(bcnt, v);
134             tmp.pb(v);
135         }while(v != u);
136         Rep(i, tmp.size()) belong[tmp[i]] = bcnt;
137         ret++;
138     }
139 }
140 
141 int main() {
142     // FRead();
143     int u, v;
144     while(~scanf("%d%d", &n, &m)) {
145         init();
146         For(i, 1, m+1) {
147             Rint(u); Rint(v);
148             adde(u, v, i);
149             adde(v, u, i);
150         }
151         For(i, 1, n+1) {
152             if(!dfn[i]) tarjan(i, 1, -1);
153         }
154         printf("%d\n", ret);
155         For(i, 1, n+1) printf("%d ", belong[i]);
156         printf("\n");
157     }
158     RT 0;
159 }

 

[HIHO1184]连通性二·边的双连通分量(双连通分量)

标签:

原文地址:http://www.cnblogs.com/vincentX/p/5573745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!