Nyoj 120 Campus Network

Source: Internet
Author: User
Campus NetworkTime Limit: 3000 MS | memory limit: 65535 kb difficulty: 5
Description

Nanyang Institute of Technology has a total of M departments, numbered 1 ~ M. There is a certain agreement between various systems. If a system has new software available, the system will allow some other systems to copy and use the software. However, this allowed relationship is unidirectional. That is, when system a allows system B to use software of system A, system B may not necessarily allow a to use software of system B.

Now, please write a program and calculate the minimum number of allowed relationships between two systems based on the Protocol reached between different systems, in order to make any system have software, all other systems also have software available.

 
Input
Enter an integer T in the first line, indicating the number of test data groups (T <10)
The first row of each group of test data is an integer m, indicating a total of M systems (2 <= m <= 100 ).
In the subsequent m rows, each row has some integers, and the I-th line indicates that department I allows these systems to copy and use the software of department I. The end of each row is 0, indicating that the input of the row ends. If a system does not allow any other system to use the software, the Bank has only one 0.
Output
For each group of test data, the minimum number of allowed relationships to be added is output.
Sample Input
152 4 3 04 5 0001 0
Sample output
2
Source
Poj adaptation
Uploaded
Zhang yuncong

Solution: Strongly Connected Algorithm + point reduction. Then, let's look at the scaled-down graph. The inbound degree of a certain point is 0, and 1 is added. If the outbound degree is 0, 1 is added.

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #include <stack>10 #define LL long long11 #define INF 0x3f3f3f12 using namespace std;13 const int maxn = 110;14 vector<int>g[maxn],mp[maxn];15 stack<int>s;16 int low[maxn],dfn[maxn],belong[maxn],iindex;17 bool instack[maxn],in[maxn],out[maxn];18 int scc,n;19 void tarjan(int u) {20     dfn[u] = low[u] = ++iindex;21     instack[u] = true;22     s.push(u);23     int v;24     for(int i = 0; i < g[u].size(); i++) {25         v = g[u][i];26         if(!dfn[v]) {27             tarjan(v);28             low[u] = min(low[u],low[v]);29         } else if(instack[v] && low[u] > dfn[v]) low[u] = dfn[v];30     }31     if(low[u] == dfn[u]) {32         scc++;33         do {34             v = s.top();35             instack[v] = false;36             belong[v] = scc;37             s.pop();38         } while(v != u);39     }40 41 }42 int main() {43     int t,i,j,u,v,ans;44     scanf("%d",&t);45     while(t--) {46         scanf("%d",&n);47         for(i = 0; i <= n; i++) {48             g[i].clear();49             low[i] = dfn[i] = 0;50             instack[i] = false;51             mp[i].clear();52         }53         for(i = 1; i <= n; i++) {54             while(scanf("%d",&v)&&v) {55                 g[i].push_back(v);56             }57         }58         while(!s.empty()) s.pop();59         ans = scc = iindex = 0;60         for(i = 1; i <= n; i++)61             if(dfn[i]) tarjan(i);62         for(i = 1; i <= n; i++) {63             for(j = 0; j < g[i].size(); j++) {64                 if(belong[i] != belong[g[i][j]]) {65                     mp[belong[i]].push_back(belong[g[i][j]]);66                 }67             }68         }69         ans = 0;70         memset(in,false,sizeof(in));71         memset(out,false,sizeof(out));72         for(i = 1; i <= n; i++) {73             for(j = 0; j < g[i].size(); j++) {74                 out[i] = true;75                 in[g[i][j]] = true;76             }77         }78         for(i = 1; i <= n; i++) {79             if(!in[i]) ans++;80             if(!out[i]) ans++;81         }82         printf("%d\n",ans);83     }84     return 0;85 }
View code

 

 

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.