Test instructions: Given the relationship between the pre-curriculum required for the N-gate course, it is supposed to be a topological map, but because of some causes of confusion, it may not be a topological diagram. The question now is whether the graph is a topological diagram (that is, a no-loop graph).
Idea: Every time the deletion of all the 0 node, has been deleted must not have any point exists, if not the topological map of the words will have a ring, then certainly a bit of the degree is never 0. If there is no point at the end of the deletion, then it is correct.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intn=100100;4 5vector< vector<int> >Vect;6 intCnt[n];//record the penetration of each point7 8 BOOLCalintN)9 {Tenvector<int> A;//points with a 0-degree deposit One for(intI=1; i<=n; i++)//all points with a degree of 0 are found in CNT first A if(!Cnt[i]) a.push_back (i); -vector<int> b;//temporary storage in 0 points - while(!a.empty ()) the { - b.clear (); - for(intI=0; I<a.size (); i++)//point x with 0 per penetration - { + for(intj=0; J<vect[a[i]].size (); J + +)//each point connected to x - { +cnt[vect[a[i]][j]]--; A if(!cnt[vect[a[i]][j]])//only those points that have changed are likely to be in degrees 0. at B.push_back (Vect[a[i]][j]); - } - vect[a[i]].clear (); - } - a.clear (); - if(!b.empty ()) A.insert (A.end (), B.begin (), B.end ()); in } - for(intI=1; I<=vect.size (); i++) to { + if(cnt[i]>0) - return false; the } * return true; $ }Panax Notoginseng - voidInitintN//initialized with the the { +memset (CNT,0,sizeof(CNT)); A vect.clear (); thevector<int>tmp; + for(intI=0; i<=n; i++) - Vect.push_back (TMP); $ } $ intMain () - { - //freopen ("E://input.txt "," R ", stdin); the intt, N, M, A, b; -Cin>>T;Wuyi while(t--) the { -scanf"%d%d",&n,&m); Wu init (n); - for(intI=0; i<m; i++) About { $scanf"%d%d",&a,&b); - Vect[a].push_back (b); -cnt[b]++; - } A if(CAL (n)) +printf"correct\n"); the Else -printf"wrong\n"); $ } the return 0; the}
AC Code
Hihocoder #1174: Topology sequencing • One (judging cycle graph)