Skip to content

Instantly share code, notes, and snippets.

@hanjae-jea
Created July 11, 2019 10:38
Show Gist options
  • Save hanjae-jea/93d8560ff2a1c07bd084fe2451bcacd0 to your computer and use it in GitHub Desktop.
Save hanjae-jea/93d8560ff2a1c07bd084fe2451bcacd0 to your computer and use it in GitHub Desktop.
#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