Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // Input:
- // 8 9
- // 3 1 2 5 5 4 3 4 2 3 5 6 6 7 7 8 8 6
- // Output:
- // 6 7 8
- int n, m, x, y, flag, node, bridge;
- int g[10][10];
- char color[10];
- int ans[10];
- int dfs(int x){
- if(color[x] == 'w'){
- color[x] = 'g';
- for(int i=1; i<=n; ++i){
- if(g[x][i]){
- if(color[i] == 'g'){
- flag = 1;
- bridge = i;
- ans[i] = 1;
- return 1;
- }
- else if(color[i] == 'w' && dfs(i)){
- if(flag)
- ans[i] = 1;
- if(i == bridge)
- flag = 0;
- return 1;
- }
- }
- }
- }
- color[x] = 'b';
- return 0;
- }
- int main() {
- cin>>n>>m;
- for(int i=0; i<m; ++i){
- cin>>x>>y;
- g[x][y] = 1;
- }
- for(int i=1; i<=n; ++i)
- color[i] = 'w';
- int found = 0;
- for(int i=1; i<=n; ++i){
- if(color[i] == 'w' && dfs(i)){
- found = 1;
- for(int i=1; i<=n; ++i)
- if(ans[i])
- cout<<i<<" ";
- break;
- }
- }
- if(!found)
- cout<<0<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment