Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- void mb(const vector<vector<int>>& edges, int sp,vector<bool>& was){
- was[sp]=1;
- for (int x:edges[sp]) if (!was[x]){
- mb(edges,x,was);
- }
- }
- int findroot(const vector<vector<int>>& edges){
- vector<bool> was(edges.size());
- int ret;
- for (int i=0; i!=was.size();++i) if (!was[i]){
- mb(edges,i,was);
- ret=i;
- }
- return ret+1;
- }
- int main(){
- int N,M;
- cin>>N>>M;
- vector<vector<int>> e(N),eT(N);
- for (int i=0; i!=M;++i){
- int a,b;
- cin>>a>>b;
- --a;--b;
- e[a].push_back(b);
- eT[b].push_back(a);
- }
- cout<<findroot(eT)<<" "<<findroot(e)<<"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement