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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

bzoj1051: [HAOI2006]受欢迎的牛

發布時間:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 bzoj1051: [HAOI2006]受欢迎的牛 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

tarjan縮點。判斷是否只有一個沒有出邊。是則輸出該點點數

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<stack> using namespace std; #define rep(i,n) for(int i=1;i<=n;i++) #define clr(x,c) memset(x,c,sizeof(x)) #define REP(i,s,t) for(int i=s;i<=t;i++) #define op() clr(head,0);pt=edges; #define qwq(x) for(edge *o=head[x];o;o=o->next) int read(){int x=0;char c=getchar();bool f=true;while(!isdigit(c)){if(c=='-') f=false;c=getchar();}while(isdigit(c)) x=x*10+c-'0',c=getchar();return f?x:-x; } const int nmax=10005; const int maxn=50005; const int inf=0x7f7f7f7f; struct edge{int to;edge *next; }; edge edges[maxn],*pt,*head[nmax]; int pre[nmax],sccno[nmax],out[nmax],scc_cnt,dfs_clock; stack<int>s; void adde(int u,int v){pt->to=v;pt->next=head[u];head[u]=pt++; } int dfs(int x){int lowu=pre[x]=++dfs_clock;s.push(x);qwq(x) {int to=o->to;if(!pre[to]) lowu=min(lowu,dfs(to));else if(!sccno[to]) lowu=min(lowu,pre[to]);}if(lowu==pre[x]){scc_cnt++;while(1){int X=s.top();s.pop();sccno[X]=scc_cnt;if(x==X) break;}}return lowu; } void init(){op();clr(out,false);clr(pre,0);clr(sccno,0);dfs_clock=scc_cnt=0; } int main(){init();int n=read(),m=read(),u,v,ans=0,cur,cnt=0;rep(i,m) u=read(),v=read(),adde(u,v);rep(i,n) if(!pre[i]) dfs(i);rep(i,n) qwq(i) if(sccno[i]!=sccno[o->to]) out[sccno[i]]=true;rep(i,scc_cnt) if(!out[i]) cnt++,cur=i;if(cnt==1) rep(i,n) if(sccno[i]==cur) ans++;printf("%d\n",ans); }

  

1051: [HAOI2006]受歡迎的牛

Time Limit:?10 Sec??Memory Limit:?162 MB
Submit:?4144??Solved:?2219
[Submit][Status][Discuss]

Description

每一頭牛的愿望就是變成一頭最受歡迎的牛。現在有N頭牛,給你M對整數(A,B),表示牛A認為牛B受歡迎。 這 種關系是具有傳遞性的,如果A認為B受歡迎,B認為C受歡迎,那么牛A也認為牛C受歡迎。你的任務是求出有多少頭 牛被所有的牛認為是受歡迎的。

Input

第一行兩個數N,M。 接下來M行,每行兩個數A,B,意思是A認為B是受歡迎的(給出的信息有可能重復,即有可 能出現多個A,B)

Output

  一個數,即有多少頭牛被所有的牛認為是受歡迎的。

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

HINT

?

100%的數據N<=10000,M<=50000

?

Source

[Submit][Status][Discuss]

轉載于:https://www.cnblogs.com/fighting-to-the-end/p/5672879.html

總結

以上是生活随笔為你收集整理的bzoj1051: [HAOI2006]受欢迎的牛的全部內容,希望文章能夠幫你解決所遇到的問題。

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