因为有重复数据,所以不得不等输入完以后再进行有向图的遍历。
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n, m; 4 set<int> graph[1005]; 5 vector<bool> visited(1005, false); 6 vector<pair<int, int>> degree(1005, make_pair(0, 0)); //(入度,出度) 7 void dfs(int p) { 8 visited[p] = true; 9 degree[p].second = graph[p].size(); 10 for (auto i : graph[p]) { 11 degree[i].first++; 12 if (!visited[i]) { 13 dfs(i); 14 } 15 } 16 } 17 int main() { 18 cin >> n >> m; 19 int a, b; 20 while (m --) { 21 cin >> a >> b; 22 graph[a].insert(b); 23 } 24 for (int i = 0; i < n; ++i) { 25 if (!visited[i]) { 26 dfs(i); 27 } 28 } 29 for (int i = 0; i < n; ++i) { 30 cout<<degree[i].first - degree[i].second<<endl; 31 } 32 }
标签:degree,int,graph,DFS,BFS,++,dfs,visited,计蒜客 From: https://www.cnblogs.com/coderhrz/p/18526055