Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 1320 | Accepted: 322 |
Description
After recapturing Sylla, the Company plans to establish a new secure system, a transferring net! The new system is designed as follows:
The Company staff choose N cities around the nation which are connected by "security tunnels" directly or indirectly. Once a week, Sylla is to be transferred to another city through the tunnels. As General ordered, the transferring net must reach a certain security level that there are at least 3 independent paths between any pair of cities a, b. When General says the paths are independent, he means that the paths share only a and b in common.
Given a design of a transferring net, your work is to inspect whether it reaches such security level.
Input
The input consists of several test cases.
For each test case, the first line contains two integers, N ≤ 500 and M ≤ 20000. indicating the number of cities and tunnels.
The following M lines each contains two integers a and b (0 ≤ a, b < N), indicating the city a and city b are connected directly by a tunnel.
The input ends by two zeroes.
Output
For each test case output "YES" if it reaches such security level, "NO" otherwise.
Sample Input
4 6 0 1 0 2 0 3 1 2 1 3 2 3 4 5 0 1 0 2 0 3 1 2 1 3 7 6 0 1 0 2 0 3 1 2 1 3 2 3 0 0
Sample Output
YES NO NO------------
无向图的K连通似乎可以用网络流做,但是不会T^T
枚举删除图中的每个顶点,求图的割点
若有割点-->不是双连通-->加上删除的点不是三连通--->结果为NO
没有割点-->是双连通-->加上删除的点是三连通-->结果为YES
------------
/** head-file **/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstring> #include <string> #include <vector> #include <queue> #include <stack> #include <list> #include <set> #include <map> #include <algorithm> /** define-for **/ #define REP(i, n) for (int i=0;i<int(n);++i) #define FOR(i, a, b) for (int i=int(a);i<int(b);++i) #define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i) #define REP_1(i, n) for (int i=1;i<=int(n);++i) #define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i) #define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i) #define REP_N(i, n) for (i=0;i<int(n);++i) #define FOR_N(i, a, b) for (i=int(a);i<int(b);++i) #define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i) #define REP_1_N(i, n) for (i=1;i<=int(n);++i) #define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i) #define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i) /** define-useful **/ #define clr(x,a) memset(x,a,sizeof(x)) #define sz(x) int(x.size()) #define see(x) cerr<<#x<<" "<<x<<endl #define se(x) cerr<<" "<<x #define pb push_back #define mp make_pair /** test **/ #define Display(A, n, m) { REP(i, n){ REP(j, m) cout << A[i][j] << " "; cout << endl; } } #define Display_1(A, n, m) { REP_1(i, n){ REP_1(j, m) cout << A[i][j] << " "; cout << endl; } } using namespace std; /** typedef **/ typedef long long LL; /** Add - On **/ const int direct4[4][2]={ {0,1},{1,0},{0,-1},{-1,0} }; const int direct8[8][2]={ {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} }; const int direct3[6][3]={ {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} }; const int MOD = 1000000007; const int INF = 0x3f3f3f3f; const long long INFF = 1LL << 60; const double EPS = 1e-9; const double OO = 1e15; const double PI = acos(-1.0); //M_PI; const int maxn=1111; const int maxm=111111; bool dead[maxn]; int cutnum; struct EdgeNode_1{ int to; int w; int next; bool cut; }; struct Bcc_Graph{ int head[maxn]; EdgeNode_1 edges[maxm]; int edge,n; void init(int n){ memset(head,-1,sizeof(head)); this->n=n; edge=0; } void addedge(int u,int v,int c=0){ edges[edge].cut=0,edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++; } //点双连通/割点/桥 int dfn[maxn],low[maxn],dfs_clock; bool iscut[maxn]; int dfs(int u,int fa){ int lowu=dfn[u]=++dfs_clock; int child=0; for (int i=head[u];i!=-1;i=edges[i].next){ int v=edges[i].to; if (v==fa) continue; if (dead[v]) continue; if (!dfn[v]){ child++; int lowv=dfs(v,u); lowu=min(lowu,lowv); if (dfn[u]<=lowv){ if (!iscut[u]) cutnum++; iscut[u]=true;//割点 //cerr<<"u is cut "<<u<<" num="<<cutnum<<endl; } } else if (dfn[v]<dfn[u]){ lowu=min(lowu,dfn[v]); } } if (fa<0&&child==1){ if (iscut[u]) cutnum--; //cerr<<"u is not cut "<<u<<" num="<<cutnum<<endl; iscut[u]=0;//割点 } low[u]=lowu; return lowu; } void find_bcc(){ memset(dfn,0,sizeof(dfn)); memset(iscut,0,sizeof(iscut)); dfs_clock=0; bool ok=false; for (int i=1;i<=n;i++){ if (!dfn[i]&&!dead[i]){ if (ok) { cutnum++; break; } dfs(i,-1); if (cutnum>0) break; ok=true; } } } }solver; int n,m; int main() { while (~scanf("%d%d",&n,&m)){ if (n==0&&m==0) break; solver.init(n); REP(i,m){ int x,y; scanf("%d%d",&x,&y); x++; y++; solver.addedge(x,y); solver.addedge(y,x); } memset(dead,0,sizeof(dead)); bool ans=true; REP_1(i,n){ dead[i]=true; cutnum=0; solver.find_bcc(); //cerr<<i<<" cut="<<cutnum<<endl; if (cutnum>0){ ans=false; break; } dead[i]=false; } if (ans) printf("YES "); else printf("NO "); } return 0; }