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; 
                   } 
         } 
  • 相关阅读:
    .NET Core 使用Dapper 操作MySQL
    .NET Core HtmlAgilityPack HTML解析利器
    ASP.NET Core 开发-缓存(Caching)
    .NET Core 调用WCF 服务
    ASP.NET Core 开发-Logging 使用NLog 写日志文件
    Qt动态生成界面并通过拉姆达获取其返回值
    Qt启动C++线程并在线程中修改界面
    Vector求最大值最小值
    C/C++取消结构体字节对齐
    Matlab定时器
  • 原文地址:https://www.cnblogs.com/noip/p/2603432.html
Copyright © 2011-2022 走看看