Created
July 11, 2019 10:38
-
-
Save hanjae-jea/93d8560ff2a1c07bd084fe2451bcacd0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <stdio.h> | |
int nt[100005], team[100005], n; | |
int start, dap; | |
void visit(int v){ | |
int next = nt[v]; | |
team[v] = -start; | |
if( team[next] == 0 ){ | |
visit(next); | |
if( team[next] == 1 && dap > 0 ){ | |
team[v] = 1; | |
} | |
} | |
else if( team[next] == -start ){ | |
dap = next; | |
team[v] = 1; | |
} | |
if( dap == v ){ | |
dap = 0; | |
} | |
} | |
int main(int argc, char const *argv[]) { | |
int test_case; | |
scanf("%d", &test_case); | |
for( int test = 0; test < test_case; test ++ ){ | |
scanf("%d", &n); | |
for( int i = 1 ; i <= n ; i ++ ){ | |
scanf("%d", &nt[i]); | |
} | |
for( int i = 1 ; i <= n ; i ++ ){ | |
if( team[i] == 0 ){ | |
start = i; dap = 0; | |
visit(i); | |
} | |
} | |
int teamcnt = 0; | |
for( int i = 1 ; i <= n ; i ++ ){ | |
if( team[i] == 1 ){ | |
teamcnt++; | |
} | |
team[i] = 0; | |
} | |
printf("\n%d\n", n - teamcnt); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment