[BZOJ1242]Fishing Net
sol
给一个无向图,求判定是不是弦图。
sol
还是弦图那套理论。
复杂度是\(O(n^2)\)的,因为\(m\)本质上和\(n^2\)是同级的。
code
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int gi()
{
int x=0,w=1;char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
const int N = 1005;
bool g[N][N];
int n,m,to[N*N<<1],nxt[N*N<<1],head[N],cnt;
int seq[N],label[N],vis[N],rk[N],s[N],top,best,ans;
vector<int>v[N];
void link(int u,int v){to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;}
void init()
{
for (int i=1;i<=n;++i) head[i]=vis[i]=label[i]=0;cnt=0;
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j) g[i][j]=0;
for (int i=0;i<=n;++i) while (v[i].size()) v[i].pop_back();
ans=1;cnt=best=0;
}
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
if (n+m==0) break;init();
for (int i=1;i<=m;++i)
{
int u=gi(),v=gi();
link(u,v);link(v,u);
g[u][v]=g[v][u]=1;
}
for (int i=1;i<=n;++i) v[0].push_back(i);
for (int i=1,now;i<=n;++i)
{
bool fg=0;
while (!fg)
{
for (int j=v[best].size()-1;j>=0;--j)
if (!vis[v[best][j]]) {fg=1;now=v[best][j];break;}
else v[best].pop_back();
if (!fg) --best;
}
seq[i]=now;rk[now]=i;vis[now]=1;
for (int e=head[now];e;e=nxt[e])
if (!vis[to[e]])
{
v[++label[to[e]]].push_back(to[e]);
best=max(best,label[to[e]]);
}
}
for (int i=1;i<=n;++i)
{
top=0;
for (int e=head[seq[i]];e;e=nxt[e])
if (rk[to[e]]<i) s[++top]=to[e];
for (int j=2;j<=top;++j)
if (!g[s[1]][s[j]]) ans=0;
}
puts(ans?"Perfect":"Imperfect");
puts("");
}
return 0;
}