Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- typedef vector<int> vi;
- vector<vi> List;
- vi visited;
- void dfs(int s){
- visited[s] = 1;
- for(int i = 0; i<List[s].size(); i++){
- if(visited[List[s][i]] == 0){
- dfs(List[s][i]);
- }
- }
- }
- int main() {
- int V,E,a,b;
- cin>>V>>E;
- for(int i = 1; i<=E; i++){
- cin>>a>>b;
- List[a].push_back(b);
- }
- List.assign(V+1, vi());
- visited.assign(V+1, 0);
- for(int i = 0; i<V; i++){
- dfs(i);
- cout << i << " ";
- for(int j=0; j<List[i].size(); j++){
- cout << List[i][j] << " ";
- }
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement