-
个人简介
#include <iostream> #include <vector> #include <queue> #include <stack> using namespace std; vector<int> topologicalSort(int v, vector<vector<int>>& adj) { vector<int> inDegree(v + 1, 0); vector<int> result; queue<int> q; // 计算入度(考虑重边) for (int i = 1; i <= v; ++i) { for (int neighbor : adj[i]) { inDegree[neighbor]++; } } // 初始时按照1~n的顺序将入度为0的点加入队列 for (int i = 1; i <= v; ++i) { if (inDegree[i] == 0) { q.push(i); } } while (!q.empty()) { int u = q.front(); q.pop(); result.push_back(u); for (int neighbor : adj[u]) { if (--inDegree[neighbor] == 0) { q.push(neighbor); } } } return result; } int main() { int v, e; cin >> v >> e; vector<vector<int>> adj(v + 1); for (int i = 0; i < e; ++i) { int a, b; cin >> a >> b; adj[a].push_back(b); // 重边会被多次添加,但入度计算会正确处理 } vector<int> sorted = topologicalSort(v, adj); if (sorted.size() != v) { cout << "has circle" << endl; } else { for (int node : sorted) { cout << node << " "; } cout << endl; } return 0; }
guage #include<bits/stdc++.h> using namespace std; int main(){ long long n; cin>>n; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ for(int m=1;m<=n;m++){ if(i!=j && i!=m && j!=m) cout<<i<<" "<<j<<" "<<m<<endl; }
} } return 0;
}
代一升是我~~爸爸~~儿子 ```language #include<bits/stdc++.h> using namespace std; int a[100],v[100],n; void dfs(int step){ if(step==n+1){ for(int i=1;i<=n;i++) cout<<setw(5)<<a[i]; cout<<endl; return ; } for(int i=1;i<=n;i++){ if(v[i]==0){ a[step]=i; v[i]=1; dfs(step+1); v[i]=0; } } } int main(){ cin>>n; dfs(1); return 0; }
-
通过的题目
-
最近活动
This person is lazy and didn't join any contests or homework.
题目标签
- 循环嵌套
- 1
- GESP二级
- 1