회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
이 코드가 틀린 이유를 잘 모르겠습니다!
9466번 - 텀 프로젝트
fantasy7772
2년 전
0
게시판의 반례들을 많이 찾아보면서 넣어보았는데 어느 부분에서 틀린 것일까요? 1퍼센트도 안되고 바로 틀려버립니다..
#include <iostream> #include <algorithm> #include <stdlib.h> #include <string> #include <string.h> #include <stack> #include <unordered_map> using namespace std; int T, n; int answer = 0; int students[100'001] = { 0, }; int visited[100'001] = { false, }; void dfs(int current, unordered_map<int, int> road, int count) { int nextStudent = students[current]; if (!visited[nextStudent]) { visited[nextStudent] = true; road.insert(make_pair(nextStudent, ++count)); dfs(nextStudent, road, count); } else { auto record = road.find(nextStudent); if (record != road.end()) { answer += record->second - 1; } else { answer += count; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; for (int i = 0; i < T; i++) { cin >> n; for (int j = 1; j <= n; j++) { int student; cin >> student; students[j] = student; } for (int j = 1; j <= n; j++) { if (!visited[j]) { visited[j] = true; unordered_map<int, int> road; road.insert(make_pair(j, 1)); dfs(j, road, 1); } } cout << answer << endl; answer = 0; memset(&visited, false, n); memset(&students, 0, n * 4); } return 0; }
댓글을 작성하려면
로그인
해야 합니다.
fantasy7772 2년 전
게시판의 반례들을 많이 찾아보면서 넣어보았는데 어느 부분에서 틀린 것일까요? 1퍼센트도 안되고 바로 틀려버립니다..