SPOJ 962 Intergalactic Map (maximum network Stream)

Source: Internet
Author: User

SPOJ 962 Intergalactic Map (maximum network Stream)

 

 

962. Intergalactic MapProblem code: IM

 

 

Jedi knights, Qui-Gon Jinn and his young apprentice Obi-Wan Kenobi, are entrusted by Queen Padm é Amidala to saveNabooFrom an invasion by the Trade Federation. They must leave Naboo immediately and goTatooineTo pick up the proof of the Federation's edevil design. They then must proceed on to the Republic's capital planetCoruscantTo produce it in front of the Republic's Senate. to help them in this endeavor, the queen's captain provides them with an intergalactic map. this map shows connections between planets not yet blockaded by the Trade Federation. any pair of planets has at most one connection between them, and all the connections are two-way. to avoid detection by enemy spies, the knights must embark on this adventure Without visiting any planet more than once. Can you help them by determining if such a path exists?

Note-In the attached map, the desired path is shown in bold.

Input Description

The first line of the input is a positive integer t ≤ 20, which is the number of test cases. the descriptions of the test cases follow one after the other. the first line of each test case is a pair of positive integers n, m (separated by a single space ). 2 ≤ n ≤ 30011 is the number of planets and m ≤ 50011 is the number of connections between planets. the planets are indexed with integers from 1 to n. the indices of Naboo, Tatooine and Coruscant are 1, 2, 3 respectively. the next m lines contain two integers each, giving pairs of planets that have a connection between them.

Output Description

The output shoshould contain t lines. The ith line corresponds to the ith test case. The output for each test case shoshould beYESIf the required path exists andNOOtherwise.

Example


Input
2
3 3
1 2
2 3
1 3
3 1
1 3

Output
YES
NO



Question:

An undirected graph is given. It is required to go from 1 to 2, then from 2 to 3, and each point goes through one more time to ask if it is possible.

Analysis:

When each point goes through multiple times, it is obvious that the network streams are directed, and the point is obviously split. However, it is obviously not easy to handle requests from 1 to 2 and then from 2 to 3. Because each vertex passes through at most once, the path from 1 to 2 is obviously completely different from the path from 2 to 3, and it is still an undirected graph, we may consider looking for two different paths from 2 to 1 and 3 respectively. In this way, the image is created: s-> 2, the capacity is 2; 1-> t, 3-> t capacity is 1, and all the sides in the figure are 1, in this figure, you can run the maximum stream. Note that points in the input that are not in the range [1, n] should be discarded.

 

 

/* * * Author : fcbruce 
 
   * * Time : Wed 19 Nov 2014 04:39:23 PM CST * */#include 
  
   #include 
   
    #include 
    
     #include 
     
      #include #include 
      
       #include 
       
        #include 
        
         #include 
         
          #include 
          
           #include 
           
            #include 
            
             #include 
             
              #include 
              
               #include
               #include 
                
                 #define sqr(x) ((x)*(x))#define LL long long#define itn int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#define eps 1e-10#ifdef _WIN32 #define lld %I64d#else #define lld %lld#endif#define maxm 65555<<3#define maxn 33333<<1using namespace std;int n,m;int fir[maxn];int u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm];int e_max;int lv[maxn],q[maxn],iter[maxn];inline void add_edge(int s,int t,int c){ int &e=e_max; u[e]=s;v[e]=t;cap[e]=c; nex[e]=fir[u[e]];fir[u[e]]=e++; u[e]=t;v[e]=s;cap[e]=0; nex[e]=fir[u[e]];fir[u[e]]=e++;}void dinic_bfs(int s){ int f,r; memset(lv,-1,sizeof lv); q[f=r=0]=s; lv[s]=0; while (f<=r) { int x=q[f++]; for (int e=fir[x];~e;e=nex[e]) { if (cap[e]>flow[e] && lv[v[e]]<0) { lv[v[e]]=lv[x]+1; q[++r]=v[e]; } } }}int dinic_dfs(int s,int t,int f){ if (s==t) return f; for (int &e=iter[s];~e;e=nex[e]) { if (cap[e]>flow[e] && lv[s]
                 
                  0) { flow[e]+=d; flow[e^1]-=d; return d; } } } return 0;}int max_flow(int s,int t){ memset(flow,0,sizeof flow); int total_flow=0; for (;;) { dinic_bfs(s); if (lv[t]<0) break; memcpy(iter,fir,sizeof fir); int f; while ((f=dinic_dfs(s,t,INF))>0) total_flow+=f; } return total_flow;}inline int in(int i){ return i;}inline int out(int i){ return i+n;}int main(){#ifdef FCBRUCE freopen(/home/fcbruce/code/t,r,stdin);#endif // FCBRUCE int T_T; scanf(%d,&T_T); while (T_T--) { e_max=0; memset(fir,-1,sizeof fir); scanf(%d%d,&n,&m); int s=0,t=n*2+2; add_edge(s,out(2),2); add_edge(in(1),t,1); add_edge(in(3),t,1); for (int i=4;i<=n;i++) add_edge(in(i),out(i),1); for (int i=0,u,v;i
                  
                   n || v<1 || v>n) continue; add_edge(out(u),in(v),1); add_edge(out(v),in(u),1); } if (max_flow(s,t)==2) puts(YES); else puts(NO); } return 0;}
                  
                 
                
              
             
            
           
          
         
        
       
      
     
    
   
  
 


 

Related Article

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.