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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

poj 2186 强连通分量

發布時間:2024/4/17 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 2186 强连通分量 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

poj 2186 強連通分量

傳送門

Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33414 Accepted: 13612 DescriptionEvery cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. Input* Line 1: Two space-separated integers, N and M * Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. Output* Line 1: A single integer that is the number of cows who are considered popular by every other cow. Sample Input3 3 1 2 2 1 2 3 Sample Output1 HintCow 3 is the only cow of high popularity.

我們可以將一個強聯通分量看成一個點進行處理,因為這個強連通分量中的點都是相互可達的,那么只要其中一頭牛成為紅人,那么其他牛也是一樣的,同時我們可以得到兩個結論

  • 最終答案必然只是一個強連通分量(如果有兩個,那么根據所有點必須可達這個點的條件,那么這兩個點集必然屬于一個強連通分量,與假設不合,證明成立)
  • 最終答案就是拓撲排序最后的那個強連通分量,這是根據拓撲排序的性質得來的
    所以我們只需要求出那個強連通分量,最終再反向dfs驗證是否可達每個點,這題就解出來了。
  • #include <queue> #include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <vector> #define ll long long #define inf 1000000000LL #define mod 1000000007 using namespace std; int read() {int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f; } const int N=1e4+10; const int M=5e4+10; int A[M],B[M],n,m; vector<int> G[N]; //圖 vector<int>rG[N]; //反向圖 vector<int>vs; //后序遍歷的頂點列表 bool vis[N]; int cmp[N]; //所屬強連通分量的拓撲序 int sum[N]; void Addedge(int from,int to){G[from].push_back(to);rG[to].push_back(from); } void dfs(int v){vis[v]=true;for(int i=0;i<G[v].size();i++){if(!vis[G[v][i]]) dfs(G[v][i]);}vs.push_back(v); } void rdfs(int v,int k){vis[v]=true;cmp[v]=k;sum[k]++;for(int i=0;i<rG[v].size();i++){if(!vis[rG[v][i]]) rdfs(rG[v][i],k);} } int scc(){memset(vis,0,sizeof(vis));vs.clear();for(int v=0;v<n;v++){if(!vis[v]) dfs(v);}memset(vis,0,sizeof(vis));int k=0;for(int i=vs.size()-1;i>=0;i--){if(!vis[vs[i]]) rdfs(vs[i],k++); //遍歷每個聯通分量的點集}return k; } int main(){// while(true){n=read();m=read();for(int i=0; i<m; i++){A[i]=read();B[i]=read();Addedge(A[i]-1,B[i]-1);}int count=scc();int u=0,num=sum[count-1];for(int v=0;v<n;v++){if(cmp[v]==count-1){u=v;break;}}memset(vis,0,sizeof(vis));rdfs(u,0);for(int v=0;v<n;v++){if(!vis[v]){num=0;break;}}printf("%d\n",num);}return 0; }/* 5 5 1 2 1 3 2 4 4 5 5 2 */

    轉載于:https://www.cnblogs.com/zsyacm666666/p/6807398.html

    與50位技術專家面對面20年技術見證,附贈技術全景圖

    總結

    以上是生活随笔為你收集整理的poj 2186 强连通分量的全部內容,希望文章能夠幫你解決所遇到的問題。

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