PisntD

 

Necklace

Description

一个无向图中的项链,是由一系列圈组成,C1,C2,……,Ck(K>=1),并且符合如下规定: 
1.任何两个圈都没有公共边; 
2.任意两个相邻的圈有且仅有一个公共点; 
3.任意两个不相邻的圈没有公共点。 
4.任意一个点,至多只能出现在一个圈中一次。 
现在要求一个从S到T的项链,C1,C2,……,Ck(K>=1),其中S属于C1,T属于Ck。 
给定一个无向图,以及两个点S和T,问是否存在从S到T的项链。 

Input

第一行一个整数Case(Case<=10)表示测试数据组数。 
每组测试数据第一行两个整数N,M N (2<=N<=10, 000) (1<=M<=100, 000)分别表示无向图中点的数目和边的数目。 
接下来M行,每行两个整数A和B(1<=A B<=N,A不等于B)表示有一条无向边从A到B。 
最后两个整数S和T(1<=S T<=N,S不等于T) 

Output

共Case行,输出”YES”或”No”,表示是否存在从S到T的项链。

Sample Input

3
3 3
1 2
2 3
3 1
1 3
4 5
1 2
2 3
1 3
3 4
3 4
1 4
4 5
1 2
1 2
2 3
3 4
3 4
1 4

 

Sample Output

YES
YES
NO

 

 

1 var
2 now,start,d,vd:array[0..10000] of longint;
3 next,st,c,g,f:array[1..200000] of longint;
4 flow,ans,i,j,s,t,n,m,times,cs:longint;
5  procedure make(a,b:longint);
6 begin
7 if start[a]=0 then start[a]:=j
8 else next[now[a]]:=j;
9 now[a]:=j;
10 st[j]:=b;
11 c[j]:=1;
12 g[j]:=1;
13 f[j]:=0;
14 end;
15 function dfs(u,flow:longint):longint;
16 var
17 v,other,j,tmp:longint;
18 function min(a,b:longint):longint;
19 begin
20 if a<b then min:=a
21 else min:=b;
22 end;
23 begin
24 if u=t then exit(flow);
25 dfs:=0;
26 j:=start[u];
27 while j<>0 do
28 begin
29 v:=st[j];
30 if (g[j]>0) and (d[u]=d[v]+1) then
31 begin
32 other:=j+ord(odd(j))*2-1;
33 tmp:=dfs(v,min(flow-dfs,g[j]));
34 dec(g[j],tmp);
35 inc(g[other],tmp);
36 if c[j]>0 then inc(f[j],tmp) else dec(f[other],tmp);
37 inc(dfs,tmp);
38 if dfs=flow then exit(flow);
39 end;
40 j:=next[j];
41 end;
42 if d[s]>=n then exit;
43 dec(vd[d[u]]);
44 if vd[d[u]]=0 then d[s]:=n;
45 inc(d[u]);
46 inc(vd[d[u]]);
47 end;
48 begin
49 readln(times);
50 for cs:=1 to times do
51 begin
52 readln(n,m);
53 fillchar(d,sizeof(d),0);
54 fillchar(vd,sizeof(vd),0);
55 fillchar(start,sizeof(start),0);
56 fillchar(next,sizeof(next),0);
57 fillchar(now,sizeof(now),0);
58 for i:=1 to m do
59 begin
60 readln(s,t);
61 j:=i*2-1;
62 make(s,t);
63 j:=i*2;
64 make(t,s);
65 end;
66 readln(s,t);
67 vd[0]:=n;
68 ans:=0;
69 while d[s]<n do
70 begin
71 flow:=dfs(s,maxlongint);
72 ans:=ans+flow;
73 end;
74 if ans>=2 then writeln('YES')
75 else writeln('NO');
76 end;
77 end.

posted on 2010-11-13 20:28  PisntD  阅读(525)  评论(0编辑  收藏  举报

导航