Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n, m, pasi, sume[100005], bifa[100005];
- vector<int> c, l[100005];
- void topsort(priority_queue<int>q)
- {
- for(int i = 1; i <= n; i++)
- {
- if(!sume[i] && !bifa[i])
- {
- q.push(-i);
- bifa[i] = 1;
- }
- }
- while(!q.empty())
- {
- int e = -q.top();
- q.pop();
- cout << e << ' ';
- for(const int it : l[e])
- {
- sume[it]--;
- if(sume[it] == 0)
- q.push(-it);
- }
- }
- }
- int main()
- {
- cin >> n >> m;
- priority_queue<int>q;
- pasi = n;
- for(int i = 1; i <= m; i++)
- {
- int x, y;
- cin >> x >> y;
- l[x].push_back(y);
- sume[y]++;
- }
- topsort(q);
- return 0;
- }
- ////////////////////////////////////////////
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("sortaret.in");
- ofstream fout("sortaret.out");
- vector<int>l[500001];
- bitset<500001>bifa;
- int n, m, i, j, suma[500001], pas;
- queue<int>q;
- void topSort()
- {
- while(pas)
- {
- for(int i = 1; i <= n; i++)
- {
- if(!suma[i] && !bifa[i])
- {
- fout << i << ' ';
- pas--;
- bifa[i] = 1;
- q.push(i);
- }
- }
- while(!q.empty())
- {
- int i = q.front();
- for(const int it : l[i])
- suma[it]--;
- q.pop();
- }
- }
- }
- int main()
- {
- fin >> n >> m;
- pas = n;
- for(int i = 1; i <= m; i++)
- {
- int x, y;
- fin >> x >> y;
- l[x].push_back(y);
- suma[y]++;
- }
- topSort();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement