Daniel's Road II

Source: Internet
Author: User

Time limit: 500MS memory limit: 1000K commits: 138 Hits: 31

Question types: programming language: C + +; C

Description
To become an ACM Daniel, you need to master a lot of necessary knowledge points. Some knowledge points can be pushed to export other knowledge points, so the new problems encountered in the game, often can be derived from the knowledge that you have learned. Now the derivation of all the knowledge points and knowledge points to be mastered is given. Note that if a knowledge can directly (indirectly) derive B knowledge, B knowledge may also directly (indirectly) derive a knowledge. A novice wants to have the ability to solve all the points of knowledge as soon as possible, at least he needs to grasp how much knowledge?
Input format
Multiple sets of data, each set of data formats: first line 1<=n<=18,0<=m<=n*n. n indicates the number of knowledge points that must be mastered, number 0~n-1. M deduces the total number of relationships between knowledge points. Next M line, a b two number per line, indicates that knowledge from a can be pushed to export b knowledge. When N==0 && m==0, end the program. There is a blank line between the two case.
Output format
A number x, which indicates the minimum number of knowledge to master.
Input sample
8 40 10 21 31 42 20 11 00 0
Output sample
41


Here is a brief description of the role of the flag array, first initializing all the flag elements to 0, in this problem
3 2
0 1
0 2
The result of this kind of example should be 2 (that is, 0 2 should learn) flag as Sentinel, after each calculation to change the result to 1, the next encounter directly skip
#include <cstdio>#include<cstring>intpre[ +];intflag[ -];intFindintx) {    returnPre[x]==x?x: (pre[x]=find (Pre[x]));}intMain () {inti,n,m;  while(1) {scanf ("%d%d",&n,&m); memset (Flag,0,sizeof(flag)); if(!n&&!m) Break;  for(i=0; i<n;i++) Pre[i]=i; intA,b,total; Total=N;  for(i=0; i<m;i++) {scanf ("%d%d",&a,&b); if(Flag[b])Continue; A=find (a); b=find (b); FLAG[B]=1; if(a!=b) {pre[a]=b;total--;} } printf ("%d\n", total); }    return 0;}
View Code

Daniel's Road II

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.