Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- #define MAXN 500069
- bool isFine[MAXN];
- vector <vector <int> > SCC(MAXN);
- vector <int> whichSCC(MAXN, -1);
- int n, m, cnt, a, b;
- void Answer()
- {
- int cnt = 0;
- for (int i=0; i!=n; ++i) if(isFine[i]) cnt++;
- cout << cnt << "\n";
- for (int i=0; i!=n; ++i) if (isFine[i]) cout << i+1 << " ";
- }
- queue <pair<int,int>> Rebuild;
- stack <int> Order;
- void DFS_SCC1 (int node, vector <vector <int> >&G, vector <bool> &V)
- {
- V[node] = true;
- for (auto nei:G[node]) if (V[nei] == false) DFS_SCC1(nei, G, V);
- Order.push(node);
- }
- int idx;
- void DFS_Create (int node, vector <vector <int>> &T, vector <bool> &V)
- {
- if (V[node]) return;
- V[node] = true;
- whichSCC[node] = idx;
- SCC[idx].push_back(node);
- for (auto nei:T[node]) DFS_Create(nei, T, V);
- }
- inline void scan(int &number)
- {
- register int c;
- number = 0;
- c = getchar();
- for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48;
- }
- void Proceed (vector <vector <int>> &Graph)
- {
- vector <int> degIn(idx, 0);
- for (auto node:Graph) for (auto nei:node) degIn[nei]++;
- queue <int> Q;
- for (int i=0; i!=n; i++) if (degIn[i] == 0) Q.push(i);
- vector <int> TopOrd;
- while (Q.empty() == false)
- {
- int k = Q.size();
- while (k--)
- {
- int v = Q.front();
- Q.pop();
- TopOrd.push_back(v);
- for (auto nei:Graph[v])
- {
- degIn[nei]--;
- if (degIn[nei] == 0) Q.push(nei);
- }
- }
- }
- vector <int> Push(TopOrd.size());
- for (int i=0; i!=Push.size(); ++i) Push[TopOrd[i]] = i;
- vector <vector <int> > topGraf(idx);
- for (int i=0; i!=Graph.size(); ++i)
- {
- for (auto nei:Graph[i])
- {
- //cout << Push[i]+1 << " -> " << Push[nei]+1 << endl;
- topGraf[Push[i]].push_back(Push[nei]);
- }
- }
- vector <int> fR(idx, n+1);
- for (int i=0; i!=idx; ++i)
- {
- for (auto nei:topGraf[i]) fR[i] = min(fR[i], nei);
- }
- stack <int> Res;
- for (int i=0; i!=idx; ++i)
- {
- cout << i << "-" << TopOrd[i] << "-" << fR[i] << endl;
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- for (int i=0; i!=MAXN; ++i) isFine[i] = true;
- scan(n);
- scan(m);
- vector <vector <int> > Graph(n);
- vector <vector <int> > Trans(n);
- while (m--)
- {
- scan(a);
- scan(b);
- a--,--b;
- Rebuild.push(make_pair(a,b));
- Graph[a].push_back(b);
- Trans[b].push_back(a);
- }
- vector <bool> Vis(n,false);
- for (int i=0; i!=n; ++i) if (Vis[i] == false) DFS_SCC1(i, Graph, Vis);
- for (int i=0; i!=n; ++i) Vis[i] = false;
- idx = 0;
- while (Order.empty() == false)
- {
- int v = Order.top();
- Order.pop();
- if (Vis[v] == false)
- {
- DFS_Create(v, Trans, Vis);
- idx++;
- }
- }
- for (int i=0; i!=n; ++i) Graph[i].clear(), Trans[i].clear();
- while (Rebuild.empty() == false)
- {
- a = Rebuild.front().first;
- b = Rebuild.front().second;
- Rebuild.pop();
- if (whichSCC[a] != whichSCC[b])
- {
- a = whichSCC[a];
- b = whichSCC[b];
- Graph[a].push_back(b);
- Trans[b].push_back(a);
- }
- }
- Proceed(Graph);
- Proceed(Trans);
- // Answer();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement