problem solving
[SWEA]2814. 최장 경로
Shinuk Yi
2020. 3. 15. 09:54
삽질....
0< N < 10인줄 알고 범위 잘못해서 몇시간동안 고생...
#include <iostream>
#include <algorithm>
using namespace std;
bool check[11][11];
bool posCheck[11];
int final;
int lastX;
void DFS(int x, int count) {
if(count > final) {
final = count;
lastX = x;
}
for(int idx = 1; idx < 11; idx++) {
if(!check[x][idx] || posCheck[idx]) { continue; }
posCheck[idx] = true;
DFS(idx, count + 1);
posCheck[idx] = false;
}
}
int main(int argc, char** argv)
{
int test_case;
int T;
cin>>T;
int N, M;
for(test_case = 1; test_case <= T; ++test_case) {
cin >> N >> M;
for(int idx = 1; idx < 11; idx++) {
fill(check[idx], check[idx] + 11, false);
}
int y, x;
for(int i = 0; i < M; i++) {
cin >> y >> x;
check[y][x] = true;
check[x][y] = true;
}
lastX = x;
final = 1;
if(N > 1) {
fill(posCheck, posCheck + 11, false);
posCheck[x] = true;
DFS(x, 1);
fill(posCheck, posCheck + 11, false);
posCheck[lastX] = true;
DFS(lastX, 1);
}
cout << "#" << test_case << " " << final << "\n";
}
return 0;
}
|
cs |