Campus Network (analog)

Source: Internet
Author: User
Tags in degrees

Campus Network time limit:MS | Memory limit:65535 KB Difficulty:5
Describe

Nanyang Institute of Technology, a total of M-series, respectively numbered 1~m, wherein the departments reached a certain agreement, if a department has new software available, the department will allow some other departments to copy and use the software. However, the permissible relationship is one-way, that is: A system allows B to use the software of a, B may not necessarily allow a to use the software B.

Now, please write a program, according to the agreement between the various departments to calculate the minimum number of two lines to add the allowable relationship between the system, in order to make any one of the software used, all other departments are also available software.

Input
The
first line enters an integer T, which represents the number of groups of test data (T<10)
The first line of each set of test data is an integer m representing a total of M-Systems (2<=M<=100).
The subsequent M-line, with a number of integers in each row, indicates that line I allows the system to replicate and use the software of the system I. The end of each line is a 0, indicating the end of the bank's input. If a department does not allow any other department to use the system software, the bank has only one 0.
Output
for each set of test data, the minimum number of such allowable relationships that need to be added is output.
Sample input
152 4 3 04 5 0001 0
Sample output
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; -}

Campus Network (analog)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.