HUST 1103 School Match adjacency table-topological sorting

Source: Internet
Author: User

Descriptionn students were invited to attend a party, every student have some friends, only if someone's all friends attend This party, this one can attend the party (ofcourse if he/she have no friends, he/she also can attend it.), now I give the Friendship between these students, you need to tell me whether all of them can attend the party. Note that the friendship isn't mature, for instance, if A is B's friend, but B was not necessary A's friend.

Inputinput starts with a integer t (1 <= t <=), denoting the number of test case. For each test case, first line contains an integer N (1 <= N <= 100000), denoting the number of students. Next n lines, each lines first contains a integer K, denoting the number of friends belong to student I (indexed from 1). Then following k integers, denoting the K friends. You can assume the number of friendship are no more than 100000, and the relation like student A is himself's friend W Ill not being existed.

Outputfor Each test case, if all of the students can attend the party, print Yes, otherwise print No.

Sample Input
231 21 31 131 201 1
Sample Output
NoYes
Hintfor the first case:

Student 1 can attend party only if Student 2 attend it.
Student 2 can attend party only if Student 3 attend it.
Student 3 can attend party only if Student 1 attend it.
So no one can attend the party. Test Instructions:given n individuals, I personally rely on k individuals, only k individuals have participated, then I will participate. Ask if everyone can participate. Ideas:The amount of data is somewhat large and may not be used and checked for a loop solution. Seniors give the method is to use a topological sort, and finally the number of nodes and N can be compared to judge.
The topology sort is basically to find all the nodes with zero degrees, press into the queue, pop up each, and delete the point and the pro side at the same time. using the vector adjacency table, found that the storage map is really useful. But my code still has a problem, vector does not need to open two-dimensional, one dimension is enough.
1#include <stdio.h>2#include <iostream>3#include <string.h>4#include <algorithm>5#include <vector>6#include <queue>7#include <utility>8 #defineMAXX 1000109 using namespacestd;Ten Const intINF =0x3f3f3f3f; Onepair<vector<int,int>x; Avector< vector<int> >bel (100010); -queue< vector<int> >Qu; - intA[maxx]; the intCNT; -  - intFindpush (intN) - { +     intFlag =1; -      for(inti =1; I <= N; i++) +     { A         if(A[i] = =0) at         { -Flag =0; -             if(Bel[i].size ()) - Qu.push (Bel[i]); -             Else  -cnt--; inA[i] =INF; -         } to     } +     returnFlag; - } the  * intMain () $ {Panax Notoginseng     intT, N, T, ed; -scanf"%d", &T); the      while(t--) +     { Ascanf"%d",&n); theCNT =N; +  -          for(inti =1; I <= N; i++) $A[i] =0, Bel[i].clear (); $          for(inti =1; I <= N; i++) -         { -scanf"%d", &t); the            /*if (!t) - cnt--;*/Wuyi              while(t--) the             { -scanf"%d", &ed); Wu Bel[ed].push_back (i); -a[i]++; About             } $         } -          while(Qu.empty ()) -         { -             intFlag =Findpush (n); A             if(Flag = =1) +             { the                 if(CNT) -printf"no\n"); $                 Elseprintf"yes\n"); the                  Break; the             } the             inttemp =qu.size (); the              while(temp--) -             { in  the                  for(inti =0; I < (Qu.front ()). Size (); i++) the                 { Abouta[(Qu.front ()) [i]]--; the                 } the Qu.pop (); thecnt--;  +             }         -         } the     }Bayi}

HUST 1103 School Match adjacency table-topological sorting

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.