-
[SWEA][8424번] 유일한 사이클Algorithm/SWExpertAcademy 2020. 4. 1. 21:57
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWyxsBd6lyADFAVP
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
소스코드
더보기#include<stdio.h> #include<vector> #include<string.h> using namespace std; vector<vector<int>> map; int dp[1001]; int N; int answer = 0; void solution(int prev, int cur, int depth) { dp[cur] = depth; for (int i = 0; i < map[cur].size(); i++) { int next = map[cur][i]; if (answer != 0) { return; } if (dp[next] != 0) { if (next != prev) { answer = dp[cur] - dp[next] + 1; return; } } else { solution(cur, next, depth + 1); } } } int main() { int tc; scanf("%d", &tc); for (int t = 1; t <= tc; t++) { scanf("%d", &N); for (int i = 0; i < map.size(); i++) { map[i].clear(); } memset(dp, 0, sizeof(dp)); answer = 0; for (int i = 0; i <= N; i++) { vector<int> v; map.push_back(v); } for (int i = 0; i < N; i++) { int x, y; scanf("%d %d", &x, &y); map[x].push_back(y); map[y].push_back(x); } solution(0, 1, 1); printf("#%d %d\n", t, answer); } }
'Algorithm > SWExpertAcademy' 카테고리의 다른 글
[SWEA][9092번] 마라톤 (0) 2020.03.30 [SWEA][9280번] 진용이네 주차타워 (0) 2020.03.29 [SWEA]5653. [모의 SW 역량테스트] 줄기세포배양 (1) 2020.03.16 [SWEA]5658. [모의 SW 역량테스트] 보물상자 비밀번호 (0) 2020.03.15 [SWEA] 2112. [모의 SW 역량테스트] 보호 필름 (0) 2020.03.12