dfs&& topological sequencing method for HDU3342-to-graph circle

Source: Internet
Author: User

HDU3342 Legal or not

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3342

The title means: A group of Daniel asked each other questions, Daniel has not, will be more powerful Daniel answer, more powerful Daniel is more than Daniel, but sometimes more powerful Daniel will install weak, ask questions, so it was answered by Daniel. This creates a circle. The question is to let you judge whether there is a ring in a graph. We can sort both methods by DFS and topology.

Code for DFS:

//Author:xiaowuga#include <bits/stdc++.h>#defineMaxx Int_max#defineMinn Int_min#defineINF 0x3f3f3f3fConst Long Longn=100000+Ten; using namespaceStd;typedefLong LongL;vector<int>Q;vector<int>P[n];int inch[N];intMain () {Ios::sync_with_stdio (false); Cin.tie (0); intn,m;  while(cin>>n>>m)        {q.clear (); memset (inch,0,sizeof(inch));  for(intI=1; i<=n;i++) p[i].clear ();        Q.clear (); intReward[n];  for(intI=1; i<=n;i++) reward[i]=888;  for(intI=0; i<m;i++){            intb; CIN>>a>>b;            P[b].push_back (a); inch[a]++; }        intct=0, ans=0;  for(intI=1; i<=n;i++)if(!inch[i]) {q.push_back (i); ans+=reward[i];}  while(Q.size ()! =0){            intt=Q.back (); Q.pop_back (); CT++;  for(intI=0; I<p[t].size (); i++){                intx=P[t][i]; if(--inch[x]==0) {q.push_back (x); REWARD[X]=max (reward[x],reward[t]+1); Ans+=Reward[x]; }                Else{reward[x]=max (reward[x],reward[t]+1); }            }        }        if(ct!=N) {cout<<-1<<Endl; }        Elsecout<<ans<<Endl; }    return 0;}

The idea of Dfs, is to walk all the way to do the tag, and then if the traversal has been traversed to indicate the existence of the ring, but a find the ring can break, jump, end DFS, can be pruned, otherwise it will time out, because a point may traverse many times, so Dfs is relatively slow.

The practice of topological sorting

Why can I make a circle? I have made it clear in my other blog post: http://www.cnblogs.com/xiaowuga/p/7218382.html

Or to put a ring into a point of thought, the size of the ring on the equivalent of the relationship, so that can not traverse all points, by traversing the number of points to determine whether to form a ring.

Code:

//Author:xiaowuga#include <bits/stdc++.h>#defineMaxx Int_max#defineMinn Int_min#defineINF 0x3f3f3f3fConst Long Longn=100000+Ten; using namespaceStd;typedefLong LongL;vector<int>Q;vector<int>P[n];int inch[N];intMain () {Ios::sync_with_stdio (false); Cin.tie (0); intn,m;  while(cin>>n>>m&&n&&m)        {q.clear (); memset (inch,0,sizeof(inch));  for(intI=0; i<n;i++) p[i].clear ();        Q.clear ();  for(intI=0; i<m;i++){            intb; CIN>>a>>b;            P[b].push_back (a); inch[a]++; }        intct=0;  for(intI=0; i<n;i++)if(!inch[i]) {q.push_back (i);}  while(Q.size ()! =0){            intt=Q.back (); Q.pop_back (); CT++;  for(intI=0; I<p[t].size (); i++){                intx=P[t][i]; if(--inch[x]==0) {q.push_back (x); }            }        }        if(ct!=N) {cout<<"NO"<<Endl; }        Elsecout<<"YES"<<Endl; }    return 0;}

dfs&& topological sequencing method for HDU3342-to-graph circle

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.