BZOJ1006:[HNOI2008]神奇的国度(弦图染色)

Description

K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即AB相互认识,BC相互认识,CA相互认识,是简洁高效的.

为了巩固三角关系,K国禁止四边关系,五边关系等等的存在.所谓N边关系,是指N个人 A1A2...An之间仅存在N对认识关系:(A1A2)(A2A3)...(AnA1),而没有其它认识关系.

比如四边关系指ABCD四个人 AB,BC,CD,DA相互认识,而AC,BD不认识.全民比赛时,为了防止做弊,规定任意一对相互认识的人不得在一队,国王相知道,最少可以分多少支队。

Input

第一行两个整数N,M。1<=N<=10000,1<=M<=1000000.表示有N个人,M对认识关系. 接下来M行每行输入一对朋友

Output

输出一个整数,最少可以分多少队

Sample Input

4 5
1 2
1 4
2 4
2 3
3 4

Sample Output

3

HINT

一种方案(1,3)(2)(4)

Solution

弦图染色流程:

初始把所有点的$ID$标为$0$。

每次从没删除的点中取$ID$最大的点$x$,让与$x$相连的点$ID++$,同时删除$x$点。

我用了个$set$维护复杂度也许是$nlogn$的。

Code

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#define N (10009)
using namespace std; struct Edge{int to,next;}edge[N*];
int n,m,ans,vis[N],ID[N];
int head[N],num_edge;
set<pair<int,int> >s;
set<pair<int,int> >::iterator it; inline int read()
{
int x=,w=; char c=getchar();
while (c<'' || c>'') {if (c=='-') w=-; c=getchar();}
while (c>='' && c<='') x=x*+c-'', c=getchar();
return x*w;
} void add(int u,int v)
{
edge[++num_edge].to=v;
edge[num_edge].next=head[u];
head[u]=num_edge;
} int main()
{
n=read(); m=read();
for (int i=; i<=m; ++i)
{
int u=read(),v=read();
add(u,v); add(v,u);
}
for (int i=; i<=n; ++i) s.insert(make_pair(,i));
while (!s.empty())
{
it=s.end(); it--; s.erase(it);
int x=(*it).second; vis[x]=;
for (int i=head[x]; i; i=edge[i].next)
if (!vis[edge[i].to])
{
s.erase(make_pair(ID[edge[i].to],edge[i].to));
s.insert(make_pair(++ID[edge[i].to],edge[i].to));
}
}
for (int i=; i<=n; ++i) ans=max(ans,ID[i]+);
printf("%d\n",ans);
}
上一篇:【转】CentOS 6.3(x86_64)下安装Oracle 10g R2


下一篇:day2-python工具的选择使用