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; 
                   } 
         } 
  • 相关阅读:
    DPDK 多进程
    dpdk helloword
    dpdk-ring-ping
    【基于python实现UI自动化】3.0 selenium
    算法题:实现 strStr()函数
    python程序设计:某体操比赛共有10名运动员参加,12名评委将根据运动员表现进行评分(满分10分),请编写Python程序,解决下列问题:
    Scrapy框架实战(五):通用爬虫 CrawlSpider
    python爬虫爬取_高德地图_主要城市迁徙意愿排行榜_19年至今数据
    年轻就该多尝试,教你20小时Get一项新技能
    LeetCode:283.移动零——简单
  • 原文地址:https://www.cnblogs.com/noip/p/2603432.html
Copyright © 2011-2022 走看看