zoukankan      html  css  js  c++  java
  • Cactus

     

    Problem Description

    1. It is a Strongly Connected graph.
    2. Each edge of the graph belongs to a circle and only belongs to one circle.
    We call this graph as CACTUS.



    There is an example as the figure above. The left one is a cactus, but the right one isn’t. Because the edge (0, 1) in the right graph belongs to two circles as (0, 1, 3) and (0, 1, 2, 3).
     
    Input
    The input consists of several test cases. The first line contains an integer T (1<=T<=10), representing the number of test cases.
    For each case, the first line contains a integer n (1<=n<=20000), representing the number of points.
    The following lines, each line has two numbers a and b, representing a single-way edge (a->b). Each case ends with (0 0).
    Notice: The total number of edges does not exceed 50000.
     
    Output
    For each case, output a line contains “YES” or “NO”, representing whether this graph is a cactus or not.

    Sample Input
    2
    4
    0 1
    1 2
    2 0
    2 3
    3 2
    0 0
    4
    0 1
    1 2
    2 3
    3 0
    1 3
    0 0
     
    Sample Output
    YES
    NO
     
     
     
     
     
     
     
     
     
     
    一个图是仙人掌图必须满足条件:必须是强连通图,由一个或多个圆圈粘结而成。
    算法:遍历图中每条边,发现圆圈立即标记圆圈上各点,检查是否有多个圆圈共边。
    #include<iostream>
    //#include<fstream> 
    using namespace std;
    //ifstream fin("fin.in");
    //ofstream fout("fout.out"); 
     
    typedef struct{int u,v,next;}Node;
    
    Node edg[50001];int next[20001],tot=0; //tot edgs 
    
    int t,n; 
    
    void Update(int u,int v){
         edg[tot].u=u;edg[tot].v=v;edg[tot].next=next[u];next[u]=tot++; 
         } 
               
    void Init(){
         memset(next,-1,sizeof(next));
         cin>>n; tot=0; 
         int a,b; 
         while(cin>>a>>b,a||b)
         Update(a,b);  
         } 
     
    int stack[20020],queue[20020],st,qu,start;bool vis[20020],mark[20020];
     
    int Check(){
        int u,v;
        memset(vis,0,sizeof(vis));
        memset(mark,0,sizeof(mark));
         
        queue[0]=0;st=0;qu=1; 
         
        while(qu>0)
        {
           --qu;start=u=queue[qu]; 
           if(next[u]==-1) continue;
    
           stack[st++]=u;vis[u]=1; 
                          
           while(next[u]!=-1) 
           {
            v=edg[next[u]].v; next[u]=edg[next[u]].next;
            if(v!=start&&mark[v]) return 0;
            
            if(!vis[v])
            {u=v;vis[u]=1;queue[++qu]=u;stack[st++]=u;vis[u]=1;}       
            else 
            {
             while(stack[st-1]!=v)
             {mark[stack[st-1]]=1;st--;} 
             u=v;    
                             }                 
                                       } 
                                               
                       
           if(next[u]==-1)
           {
            if(u==start)
            {st--;mark[u]=1;} 
            else return 0; 
                                   }                         
                                               
    
                     } 
         return 1; 
         } 
     
     int main()
     {
         cin>>t;
         while(t>0)
         {
          t--; 
          Init(); 
          if(Check()) cout<<"YES"<<endl;
          else cout<<"NO"<<endl; 
                   } 
         } 
  • 相关阅读:
    在Asp.Net头部动态加载css和js文件的方法
    关于Ajax开发中Response的ContentType的一些问题
    C#获取存储过程的返回值
    在填写表单中输入全角数字的解决方案
    Eclipse3.7中搭建Android开发环境
    THinkPHP 获取客户端IP 与IP地址查询
    php 操作数组 (合并,拆分,追加,查找,删除等)
    HTML5 LocalStorage 本地存储
    [javascript] IE与火狐下window.event对象的区别
    今天有人问是否可以使用vs2005开发,回答了一下,记录下来
  • 原文地址:https://www.cnblogs.com/noip/p/2603432.html
Copyright © 2011-2022 走看看