Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("retea1.in");
- ofstream fout("retea1.out");
- vector < int > l[305];
- vector < int > g[305];
- vector < int > rez;
- stack < int > v;
- stack < int > stk;
- bitset < 305 > viz;
- int a[305][305];
- int n;
- void DFS(int nod)
- {
- viz[nod] = 1;
- for (int el : l[nod])
- if (!viz[el])
- DFS(el);
- v.push(nod);
- }
- void DFST(int nod)
- {
- stk.push(nod);
- viz[nod] = 1;
- int c = 1;
- while (!stk.empty())
- {
- nod = stk.top();
- stk.pop();
- for (int el : g[nod])
- {
- if (!viz[el])
- {
- if (c == 1)
- rez.push_back(nod);
- c++;
- rez.push_back(el);
- viz[el] = 1;
- stk.push(el);
- }
- }
- }
- cout << c << ' ' << a[nod][nod] << ' ' << nod;
- if (c == 1 && a[nod][nod])
- rez.push_back(nod);
- }
- int main()
- {
- fin >> n;
- rez.reserve(n);
- for (int i = 1; i <= n; i++)
- {
- for (int j = 1; j <= n; j++)
- {
- int t;
- fin >> t;
- if (t)
- {
- l[i].push_back(j);
- g[j].push_back(i);
- }
- }
- }
- for (int i = 1; i <= n; i++)
- {
- if (!viz[i])
- DFS(i);
- }
- viz.reset();
- while (!v.empty())
- {
- cout << v.top() << ' ';
- if (!viz[v.top()])
- {
- DFST(v.top());
- }
- v.pop();
- }
- for (int el : rez)
- fout << el << ' ' ;
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement