日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hdu 3836 Equivalent Sets

發布時間:2025/3/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 3836 Equivalent Sets 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目連接

http://acm.hdu.edu.cn/showproblem.php?pid=3836??

Equivalent Sets

Description

To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent.
You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets.
Now you want to know the minimum steps needed to get the problem proved.

Input

The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000.
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.

Output

For each case, output a single integer: the minimum steps needed.

Sample Input

4 0
3 2
1 2
1 3

Sample Output

4
3

題目大意:給你一張有向圖要求最少加多少條邊時該圖變成強連通圖。
Tarjan縮點。。

#include<bits/stdc++.h> using namespace std; const int N = 20100; struct Tarjan_scc {stack<int> s;bool instack[N];struct edge { int to, next; }G[N * 3];int idx, scc, tot, in[N], out[N], dfn[N], low[N], head[N], sccnum[N];inline void init(int n) {idx = scc = tot = 0;while (!s.empty()) s.pop();for (int i = 0; i < n + 2; i++) {head[i] = -1;instack[i] = false;in[i] = out[i] = dfn[i] = low[i] = sccnum[i] = 0;}}inline void add_edge(int u, int v) {G[tot].to = v, G[tot].next = head[u], head[u] = tot++;}inline void built(int m) {int u, v;while (m--) {scanf("%d %d", &u, &v);add_edge(u, v);}}inline void tarjan(int u) {dfn[u] = low[u] = ++idx;instack[u] = true;s.push(u);for (int i = head[u]; ~i; i = G[i].next) {int &v = G[i].to;if (!dfn[v]) {tarjan(v);low[u] = min(low[u], low[v]);} else if (instack[v] && dfn[v] < low[u]) {low[u] = dfn[v];}}if (dfn[u] == low[u]) {int v = 0;scc++;do {v = s.top(); s.pop();instack[v] = false;sccnum[v] = scc;} while (u != v);}}inline void solve(int n, int m) {init(n);built(m);for (int i = 1; i <= n; i++) {if (!dfn[i]) tarjan(i);}int x1 = 0, x2 = 0;for (int u = 1; u <= n; u++) {for (int i = head[u]; ~i; i = G[i].next) {int v = G[i].to;if (sccnum[u] != sccnum[v]) {in[sccnum[v]]++;out[sccnum[u]]++;}}}for (int i = 1; i <= scc; i++) {if (!in[i]) x1++;if (!out[i]) x2++;}printf("%d\n", 1 == scc ? 0 : max(x1, x2));} }go; int main() { #ifdef LOCALfreopen("in.txt", "r", stdin);freopen("out.txt", "w+", stdout); #endifint n, m;while (~scanf("%d %d", &n, &m)) {go.solve(n, m);}return 0; }

?

轉載于:https://www.cnblogs.com/GadyPu/p/5003447.html

總結

以上是生活随笔為你收集整理的hdu 3836 Equivalent Sets的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。