bzoj1051: [HAOI2006]受欢迎的牛
生活随笔
收集整理的這篇文章主要介紹了
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 MBSubmit:?4144??Solved:?2219
[Submit][Status][Discuss]
Description
每一頭牛的愿望就是變成一頭最受歡迎的?!,F在有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 31 2
2 1
2 3
Sample Output
1HINT
?
100%的數據N<=10000,M<=50000?
Source
[Submit][Status][Discuss]轉載于:https://www.cnblogs.com/fighting-to-the-end/p/5672879.html
總結
以上是生活随笔為你收集整理的bzoj1051: [HAOI2006]受欢迎的牛的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 串操作指令
- 下一篇: [shiro学习笔记]第二节 shiro