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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

天池 在线编程 课程表(拓扑排序 + 回溯)

發(fā)布時(shí)間:2024/7/5 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 天池 在线编程 课程表(拓扑排序 + 回溯) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

總共有n個(gè)課程,從0到n-1。
有些課程可能有先決條件,例如,你想修課程0,你必須先修一門課程1,這兩門課之間的關(guān)系表示為:[0,1]

給定課程的總數(shù)和先決條件對(duì)的列表,返回你可以得到所有課程的不同方法的數(shù)量。

n <= 10樣例1 輸入: n = 2 prerequisites = [[1,0]] 輸出: 1 說(shuō)明: 你必須按照0->1的順序上課。樣例2 輸入: n = 2 prerequisites = [] Output: 2 輸出: 你可以按0->11->0的順序上課。

https://tianchi.aliyun.com/oj/286601234951741590/302577566233072416

2. 解題

  • 建圖,記錄出入度
  • n 比較小,dfs 搜索
class Solution { public:/*** @param n: an integer, denote the number of courses* @param p: a list of prerequisite pairs* @return: return an integer,denote the number of topologicalsort*/int ans = 0;int topologicalSortNumber(int n, vector<vector<int>> &p) {// Write your code herevector<int> indegree(n, 0);vector<bool> vis(n, false);vector<vector<int>> g(n);for(auto& pi : p){indegree[pi[0]]++;g[pi[1]].push_back(pi[0]);}dfs(g, vis, indegree, 0);return ans;}void dfs(vector<vector<int>> &g, vector<bool>& vis, vector<int> &indegree, int count){if(count == g.size()){ans++;return;}for(int i = 0; i < g.size(); i++){if(vis[i]) continue;if(indegree[i]==0){vis[i] = true;for(auto nt : g[i])indegree[nt]--;dfs(g, vis, indegree, count+1);for(auto nt : g[i])indegree[nt]++;vis[i] = false;}}} };

2490ms C++


我的CSDN博客地址 https://michael.blog.csdn.net/

長(zhǎng)按或掃碼關(guān)注我的公眾號(hào)(Michael阿明),一起加油、一起學(xué)習(xí)進(jìn)步!

總結(jié)

以上是生活随笔為你收集整理的天池 在线编程 课程表(拓扑排序 + 回溯)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。