Last active
September 19, 2023 05:20
-
-
Save cjnghn/35f7dd1e3a8c4658764a46e9849e35ef to your computer and use it in GitHub Desktop.
코드트리 - 카드 사이클 만들기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int main() { | |
int n; | |
cin >> n; | |
vector<int> arr(n); | |
vector<int> graph(n, 0); | |
for (int i = 0; i < n; i++) | |
cin >> arr[i]; | |
int k; | |
for (int i = 0; i < n; i++) { | |
cin >> k; | |
if (k != arr[i]) | |
graph[k] = arr[i]; | |
} | |
int cycleCount = 0; | |
int maxCycleSize = -1; | |
vector<bool> visited(n, false); | |
for (int cur : arr) { | |
if (!visited[cur] && graph[cur] != 0) { | |
cycleCount++; | |
int cycleSize = 0; | |
while (!visited[cur]) { | |
visited[cur] = true; | |
cycleSize++; | |
cur = graph[cur]; | |
} | |
maxCycleSize = max(maxCycleSize, cycleSize); | |
} | |
} | |
cout << cycleCount << " " << maxCycleSize; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment