bzoj4562: [Haoi2016]食物鏈--記憶化搜尋

來源:互聯網
上載者:User

標籤:

這道題其實比較水,半個小時AC= =對於我這樣的渣渣來說真是極大的鼓舞

題目大意:給出一個有向圖,求入度為0的點到出度為0的點一共有多少條路

從入讀為零的點進行記憶化搜尋,搜到出度為零的點返回1

所有傳回值加起來就是答案。

“注意單獨的一種孤立生物不算一條食物鏈。”出題人都這麼好心的說了,然而第一次交的時候還是忘了= =結果成為此題第一個WA的人hh

 

 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #define maxn 100010 5 using namespace std; 6 struct node{ 7     int from,to,next; 8 }e[maxn*2]; 9 int head[maxn],vis[maxn],dp[maxn],ind[maxn],out[maxn];10 int tot,n,m,u,v;11 12 void insert(int u, int v){13     e[++tot].from=u;14     e[tot].to=v;15     e[tot].next=head[u];16     head[u]=tot;17 }18 19 int dfs(int x){20     if (vis[x]) return dp[x];21     vis[x]=1;22     if (out[x]==0){23         dp[x]=1;24         return 1;25     }26     int sum=0;27     for (int i=head[x]; i!=-1; i=e[i].next){28         int v=e[i].to;29         sum+=dfs(v);30     }31     dp[x]=sum;32     return sum;33 }34 35 int main(){36     scanf("%d%d", &n, &m);37     memset(ind,0,sizeof(ind));38     memset(out,0,sizeof(out));39     memset(head,-1,sizeof(head));40     tot=-1;41     for (int i=1; i<=m; i++){42         scanf("%d%d", &u, &v);43         out[u]++;44         ind[v]++;45         insert(u,v);46     }47     int ans=0;48     memset(vis,0,sizeof(vis));49     memset(dp,0,sizeof(dp));50     for (int i=1; i<=n; i++)51         if (ind[i]==0 && out[i]!=0)52             ans+=dfs(i);53     printf("%d\n", ans);54     return 0;55 }

 

bzoj4562: [Haoi2016]食物鏈--記憶化搜尋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.