HDU-4324 Triangle Love (topological sort)

Source: Internet
Author: User

https://vjudge.net/problem/HDU-4324

Test instructions

Each group of data a n represents n individuals, and then the matrix of N*n represents the relationship between these people, input must be satisfied if a does not like B then B must like a, and will not appear a and b mutually like the situation, ask you whether there is a triangle love.

Analysis

is to ask if there is a ternary ring. To determine whether a graph exists in a loop, you can use topological ordering, and the sorting fails with a loop. So how to judge the existence of the ring is ternary ring it?

Suppose there is an n-ary ring, and there is such an adjacent three element x--"y--" Z on the ring. If it is z--x, then there is ternary ring, otherwise, there will be x--"Z, then you can ignore the Y, it becomes the n-1 yuan ring. In this way, there must be a ternary ring. Therefore, the problem is to ask whether there is a ring.

#include <iostream>#include<cmath>#include<cstring>#include<queue>#include<vector>#include<cstdio>#include<algorithm>#include<map>#include<Set>#include<bitset>#defineRep (i,e) for (int i=0;i< (e); i++)#defineREP1 (i,e) for (int i=1;i<= (e); i++)#defineREPX (i,x,e) for (int i= (x); i<= (e); i++)#defineX First#defineY Second#definePB push_back#defineMP Make_pair#defineMset (Var,val) memset (Var,val,sizeof (VAR))#defineSCD (a) scanf ("%d", &a)#defineSCDD (A, b) scanf ("%d%d", &a,&b)#defineSCDDD (a,b,c) scanf ("%d%d%d", &a,&b,&c)#definePD (a) printf ("%d\n", a)#defineSCL (a) scanf ("%lld", &a)#defineSCLL (A, b) scanf ("%lld%lld", &a,&b)#defineSclll (a,b,c) scanf ("%lld%lld%lld", &a,&b,&c)#defineIOS Ios::sync_with_stdio (false); Cin.tie (0)using namespaceStd;typedefLong Longll;template<classT>voidTest (T a) {cout<<a<<Endl;} Template<classTclassT2>voidTest (T a,t2 b) {cout<<a<<" "<<b<<Endl;} Template<classTclassT2,classT3>voidTest (T a,t2 b,t3 c) {cout<<a<<" "<<b<<" "<<c<<Endl;}Const intN = 1e6+Ten;//const int MAXN =;Const intINF =0x3f3f3f3f;Constll INF =0x3f3f3f3f3f3f3f3fll;Constll mod =1000000007;intT;voidtestcase () {printf ("Case #%d:",++T);}Const intMAXN =2500;Const intMAXM = -;CharMA[MAXN];int inch[MAXN];intHEAD[MAXN], CNT;intN, K;structNode {intu, V, next;}; Node EDGE[MAXN*MAXN];voidinit () {CNT=0; memset (Head,-1,sizeof(head)); memset (inch,0,sizeof(inch));}voidAddintUintv) {edge[cnt]={u, V, Head[u]}; Head[u]= cnt++;}voidTopu () {testcase (); Queue<int>Q; intans=0;  for(intI=0; i<n;++i) {        if(!inch[i])            {Q.push (i); Ans++; }    }     while(!Q.empty ()) {        intu=Q.front ();        Q.pop ();  for(inti=head[u];i!=-1; i=Edge[i].next) {            intv=edge[i].v; inch[v]--; if(!inch[v])                {Q.push (v); Ans++; }        }    }    if(n==ans) printf ("no\n"); Elseprintf ("yes\n");}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif //LOCAL    intT; scanf ("%d", &t); T=0;  while(t--) {init (); scanf ("%d", &N);  for(inti =0; I < n; ++i)            {gets (MA);  for(intj =0; J < N; ++j)if(Ma[j] = ='1') {Add (I, j); inch[j]++;    }} topu (); }    return 0;}

HDU-4324 Triangle Love (topological sort)

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.