【bzoj1006】 HNOI2008—神奇的国度

http://www.lydsy.com/JudgeOnline/problem.php?id=1006 (题目链接)

题意

  求弦图的最小染色数。

Solution

  弦图,详情参见论文。

  这里我写的加堆的nlogn的,线性的用n个链表维护一下然后打标记删除就行了。

细节

  Wa了2发。。

代码

// bzoj1006
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf 1<<30
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;

const int maxn=10010,maxm=1000010;
int label[maxn],vis[maxn],head[maxn],c[maxn],a[maxn];
int n,m,cnt;

struct edge {int to,next;}e[maxm<<1];
struct data {
	int id,w;
	friend bool operator < (const data a,const data b) {
		return a.w<b.w;
	}
};

void link(int u,int v) {
	e[++cnt]=(edge){v,head[u]};head[u]=cnt;
	e[++cnt]=(edge){u,head[v]};head[v]=cnt;
}
void color(int x) {
	for (int i=head[x];i;i=e[i].next) vis[c[e[i].to]]=x;
	for (int i=1;i<=cnt;i++) if (vis[i]!=x) {c[x]=i;break;}
	if (!c[x]) c[x]=++cnt;
}
int main() {
	scanf("%d%d",&n,&m);
	for (int u,v,i=1;i<=m;i++) {
		scanf("%d%d",&u,&v);
		link(u,v);
	}
	priority_queue<data> q;q.push((data){n,0});
	int tot=0;
	while (!q.empty()) {
		int x=q.top().id;q.pop();
		if (vis[x]) continue;
		vis[x]=1;a[++tot]=x;
		for (int i=head[x];i;i=e[i].next) if (!vis[e[i].to]) {
				label[e[i].to]++;
				q.push((data){e[i].to,label[e[i].to]});
			}
	}
	memset(vis,0,sizeof(vis));
	cnt=0;
	for (int i=1;i<=n;i++) color(a[i]);
	printf("%d",cnt);
    return 0;
}

 

posted @ 2017-01-10 16:20  MashiroSky  阅读(247)  评论(0编辑  收藏  举报