2
Solution: Just start thinking is to use the map to store the relationship, with DFS search n points to form the number of numbers, with an out array to record whether the current point and other nodes to form a relationship, draw a way to the tree, more trouble;
Code:
1#include <stdio.h>2#include <string.h>3 Const intmaxn= the;4 intMAP[MAXN][MAXN], out[MAXN];5 intVIS[MAXN];6 intt,m;7 voidDfsintx) {8 if(Vis[x])return;9vis[x]=1;Ten for(intI=1; i<=m;i++){ One if(Map[x][i]) { A DFS (i); - } - } the } - intMain () { -scanf"%d",&T); - while(t--){ +scanf"%d",&M); -memset (Map,0,sizeof(map)); +Memset out,0,sizeof( out)); Amemset (Vis,0,sizeof(Vis)); at for(intI=1; i<=m;i++){ - intA; - while(SCANF ("%d",&a), a) { -map[i][a]=1; - out[i]=1; - } in } - for(intI=1; i<=m;i++){ to DFS (i); + } - intans=0; the for(intI=1; i<=m;i++){ * //printf ("%d%d\n", Vis[i],out[i]); $ if(!vis[i]) ans++;Panax Notoginseng if(! out[i]) ans++; - } theprintf"%d\n", ans); + } A return 0; the}
And then read someone else to write, I think of the complex, just need to consider whether each point in degrees and out of the degree, my idea is just into the degree, in the form of a tree, that is, the VIS array;
Another solution:
1#include <stdio.h>2#include <string.h>3 Const intmaxn= the;4 int inch[MAXN], out[MAXN];5 intMain () {6 intt,m;7scanf"%d",&T);8 while(t--){9Memsetinch,0,sizeof(inch));TenMemset out,0,sizeof( out)); Onescanf"%d",&M); A for(intI=1; i<=m;i++){ - intA; - while(SCANF ("%d",&a), a) { the inch[a]=1; - out[i]=1; - } - } + intans=0; - for(intI=1; i<=m;i++){ + if(!inch[i]) ans++; A if(! out[i]) ans++; at } -printf"%d\n", ans); - } - return 0; -}