阿裏在線測評解析
阿新 • • 發佈:2018-08-02
point lis pac bool main ring 出現問題 阿裏巴巴 依賴
題幹:
今天我們看到的阿裏巴巴提供的任何一項服務後邊都有著無數子系統和組件的支撐,子系統之間也互相依賴關聯,
其中任意一個環節出現問題都可能對上遊鏈路產生影響。小明做為新人接收到的第一個任務就是去梳理所有的依賴關系,
小明和每個系統的負責人確認了依賴關系,記錄下調用對應系統的耗時,用這些數據分析端到端鏈路的數目和鏈路上最長的耗時。
輸入: 小明搜集到的系統耗時和依賴列表
5 4 // 表示有5個系統和 4個依賴關系
3 // 調用1號系統耗時 3 ms
2 // 調用2號系統耗時 2 ms
10 // 調用3號系統耗時 10 ms
5 // 調用4號系統耗時 5 ms
7 // 調用5號系統耗時 7 ms
1 2 // 2號系統依賴1號系統
1 3 // 3號系統依賴1號系統
2 5 // 2號系統依賴5號系統
4 5 // 4號系統依賴5號系統
輸出: 調用鏈路的數目 和最大的耗時, 這裏有三條鏈路1->2->5,1->3, 4->5,最大的耗時是1到3的鏈路 3+10 = 13,無需考慮環形依賴的存在。
3 13
思路:
一個很不錯的題目,不過能在半個小時內做出來還是有點小難度的,主要是用到了一個深搜的思路,將所有依賴關系存在一個map中,從頭節點開始向下搜索,有點鏈表的意思。
每搜到一個尾節點就將結果加1;
代碼:
#include <iostream> #include <map> #include<algorithm> #include <vector> #include <stdlib.h> #include <string.h> using namespace std; vector <int > timeUse; vector <int > path; multimap <int ,int > dependency; bool head[100000+5]; int maxTime = 0,Time = 0; int res = 0; void findPath(int point) { multimap<int ,int >::iterator iter; path.push_back(point); Time += timeUse[point]; iter = dependency.find(point); if(iter != dependency.end()) { int num = dependency.count(point); while(num--) { findPath(iter->second); iter++; } } else { res++; maxTime = max(maxTime,Time); } Time -= timeUse[point]; path.pop_back(); } int main() { int sysNum, dependList; while(cin >> sysNum >> dependList) { timeUse.clear(); dependency.clear(); path.clear(); maxTime = 0,res = 0; memset(head,1,100005); timeUse.push_back(0); for(int i = 1; i <= sysNum; i++){ cin >> Time; timeUse.push_back(Time); } for(int i = 1; i <= dependList; i++) { int sysLeader, sysFollower; cin >> sysLeader >> sysFollower; dependency.insert(make_pair(sysLeader,sysFollower)); head[sysFollower] = 0; } for(int i = 1; i <= sysNum; i++) if(head[i]) { Time = 0; findPath(i); } cout << res << " " << maxTime << endl; } return 0; } /* 5 4 3 2 10 5 7 1 2 1 3 2 5 4 5 */
阿裏在線測評解析