Skip to content

Instantly share code, notes, and snippets.

@cjnghn
Last active September 19, 2023 05:20
Show Gist options
  • Save cjnghn/35f7dd1e3a8c4658764a46e9849e35ef to your computer and use it in GitHub Desktop.
Save cjnghn/35f7dd1e3a8c4658764a46e9849e35ef to your computer and use it in GitHub Desktop.
코드트리 - 카드 사이클 만들기
#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