題目大意:
給定一個無向圖,每個點的度數爲3,判斷是否能分解成若干個 爪 。每個點可以屬於多個爪,每條邊只能屬於一個爪。
爪如圖所示:
題解:
每個點的度數爲3,這個條件很重要。畫圖分析會發現,如果一個圖可以分解成若干個爪,任意一個作爲爪中心的點,它的所有相鄰的點都是爪的邊緣;作爲爪的邊緣的點,它的所有相鄰的點都是爪的中心。並且作爲爪的中心和邊緣的點可以同時互換。
然後這就是個二分圖了。
所以這題只需要判斷是不是二分圖就沒了。
代碼:
#include <bits/stdc++.h>
#define LL long long
#define LD long double
#define ULL unsigned long long
#define UI unsigned int
#define PII pair<int,int>
#define MPII(x,y) pair<int,int>{x,y}
#define _for(i,j,k) for(int i=j;i<=k;i++)
#define for_(i,j,k) for(int i=j;i>=k;i--)
#define efor(i,u) for(int i=head[u];i;i=net[i])
#define lowbit(x) (x&-x)
#define ls(x) x<<1
#define rs(x) x<<1|1
#define inf 0x3fffffff
//#pragma comment(linker, "/STACK:10240000000,10240000000")
using namespace std;
const int maxn = 2e6 + 5;
const int M = 1e9 + 7;
inline int mad(int a,int b){return (a+=b)>=M?a-M:a;}
int n,head[maxn],e[maxn],net[maxn],cnt;
void add(int u,int v){
e[++cnt]=v;
net[cnt]=head[u];
head[u]=cnt;
}
int color[maxn];//0未着色,1和2着色
bool bip(int u){
for(int i=head[u];i;i=net[i]){
if(color[u]==color[e[i]]) return false;
if(!color[e[i]]){
color[e[i]]=3-color[u];
if(!bip(e[i])) return false;
}
}
return true;
}
bool sol(){
_for(i,1,n) color[i]=0;
_for(i,1,n){
if(!color[i]){
color[i]=1;
if(!bip(i)) return false;
}
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
while(cin>>n,n){
int u,v;
while(cin>>u>>v,u|v){
add(u,v);
add(v,u);
}
if(sol()){
puts("YES");
}
else puts("NO");
_for(i,1,n) head[i]=0;
cnt=0;
}
return 0;
}