# 弦图的判定MCS算法（zoj1015）

for num=n-1 downto 0 do

for num=0 to n-1 do

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
#include"algorithm"
#include"string.h"
#include"string"
#include"math.h"
#include"vector"
#include"stack"
#include"map"
#define eps 1e-4
#define inf 0x3f3f3f3f
#define M 1209
#define PI acos(-1.0)
using namespace std;
int cnt,vis[M],num[M],s[M],g[M][M];
void bfs(int n)
{
int i,j,id;
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
cnt=0;
for(i=n;i>=1;i--)
{
id=1;
for(j=1;j<=n;j++)
{
if(!vis[j]&&num[id]<num[j])
{
id=j;
}
}
s[i]=id;
vis[id]=1;
for(j=1;j<=n;j++)
{
if(id!=j&&g[id][j]&&!vis[j])
num[j]++;
}
}
}
int psq(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
int id;
for(j=i+1;j<=n;j++)
{
if(g[s[i]][s[j]])
{
id=j;
break;
}
}
for(j=i+1;j<=n;j++)
{
if(g[s[i]][s[j]]&&id!=j&&g[s[id]][s[j]]==0)
return 0;
}
}
return 1;

}
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m),m+n)
{
memset(g,0,sizeof(g));
for(i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
g[a][b]=g[b][a]=1;
}
bfs(n);
if(psq(n))
{
printf("Perfect\n\n");
}
else
printf("Imperfect\n\n");
}
return 0;
}

bfs+优先队列

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
#include"algorithm"
#include"string.h"
#include"string"
#include"math.h"
#include"vector"
#include"stack"
#include"map"
#define eps 1e-4
#define inf 0x3f3f3f3f
#define M 1009
#define PI acos(-1.0)
using namespace std;
struct Edge
{
int v;
Edge(int vv)
{
v=vv;
}
};
vector<Edge>edge[M];
struct node
{
int id,num;
friend bool operator<(node a,node b)
{
return a.num<b.num;
}
};
void bfs(int u)
{
priority_queue<node>q;
memset(num,0,sizeof(num));
memset(vis,0,sizeof(vis));
cnt=n;
node now;
now.id=u;
now.num=1;
q.push(now);
while(!q.empty())
{
node cur=q.top();
if(!vis[cur.id])
{
vis[cur.id]=1;
order[cur.id]=cnt;
cnt--;
if(cnt==0)
break;
}
q.pop();
for(int i=0;i<(int)edge[cur.id].size();i++)
{
int v=edge[cur.id][i].v;
num[v]++;
now.id=v;
now.num=num[v];
if(!vis[v])
q.push(now);
}
}
}
int check()
{
int i,j,value;
bfs(1);
for(i=1;i<=n;i++)
{
int mini=n+1;
int t=0;
{
if(order[v]>i)
{
if(mini>order[v])
{
mini=order[v];
value=v;
}
q[t++]=v;
}
}
for(j=0;j<t;j++)
{
if(q[j]!=value&&g[value][q[j]]==0)
return 0;
}
}
return 1;
}
int main()
{
int m,i,a,b;
while(scanf("%d%d",&n,&m),m||n)
{
for(i=1;i<=n;i++)
edge[i].clear();
memset(g,0,sizeof(g));
for(i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
edge[a].push_back(b);
edge[b].push_back(a);
g[a][b]=g[b][a]=1;
}
if(check())
printf("Perfect\n\n");
else
printf("Imperfect\n\n");
}
}


08-11 1723

03-04 16万+
08-11 501
05-03 118
05-10 3640
07-30 1752