[SWEA]최장 경로

BFS

Posted by kyoungIn on March 20, 2019

최장 경로

링크

풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
queue<int> q;
vector<vector<int>>v;
int ans,n,m;
void dfs(int i,int sum, int*arr){
    
    ans=max(ans,sum);
    
    for(int j=0;j<v[i].size();j++){
        if(arr[v[i][j]]!=1){
            arr[v[i][j]]=1;
            dfs(v[i][j],sum+1,arr);
            arr[v[i][j]]=0;
               }
    }         
    return;
}
int main(){
    int tc;
    cin >> tc ;
    for(int t=1;t<=tc;t++){
        ans=-1100000;
        int arr[11]={0};
        cin >> n >> m ;
        v.resize(n+1);
        for(int i=0;i<m;i++){
            int a,b;
            cin >> a >> b;
            v[a].push_back(b);
            v[b].push_back(a);
            q.push(a);
        }
        for(int i=1;i<=n;i++){
            arr[i]=1;
            dfs(i,1,arr);
            arr[i]=0;
        }
        v.clear();
        cout <<"#"<<t<<" "<<ans <<'\n';
    }
}