【bzoj1006】[HNOI2008]神奇的国度

1006: [HNOI2008]神奇的国度

Time Limit: 20 Sec  Memory Limit: 162 MB
Submit: 3114  Solved: 1401
[Submit][Status][Discuss]

Description

  K国是一个热衷三角形的国度,连人的交往也只喜欢三角原则.他们认为三角关系:即AB相互认识,BC相互认识,CA
相互认识,是简洁高效的.为了巩固三角关系,K国禁止四边关系,五边关系等等的存在.所谓N边关系,是指N个人 A1A2
...An之间仅存在N对认识关系:(A1A2)(A2A3)...(AnA1),而没有其它认识关系.比如四边关系指ABCD四个人 AB,BC,C
D,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)

【题解】

由于题中的限制条件,建出来的图一定是一个弦图,要求的答案就是弦图的最小染色方案。

首先用MCS算法求出弦图的完美消除序列,然后从后往前染上可以染的最小颜色,记录一下答案就行了。

那么这么做为什么是对的呢?

证明:我们假设使用了T种颜色,则T>=色数,T=团数<=色数,所以T=色数。

时间复杂度:O(m+n)

参考:陈丹琦——《弦图与区间图》

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<algorithm>
 8 using namespace std;
 9 struct node{int y,next;}e[1000010*2];
10 int n,m,len,ans,Link[10010],label[10010],vis[10010],q[10010],check[10010],col[10010];
11 inline int read()
12 {
13     int x=0,f=1;  char ch=getchar();
14     while(!isdigit(ch))  {if(ch=='-')  f=-1;  ch=getchar();}
15     while(isdigit(ch))  {x=x*10+ch-'0';  ch=getchar();}
16     return x*f;
17 }
18 void insert(int xx,int yy)
19 {
20     e[++len].next=Link[xx];
21     Link[xx]=len;
22     e[len].y=yy;
23 }
24 void MCS()//最大势算法求完美消除序列
25 {
26     for(int i=n;i;i--)
27     {
28         int now=0;
29         for(int j=1;j<=n;j++)
30             if(label[j]>=label[now]&&!vis[j])  now=j;
31         vis[now]=1;  q[i]=now;
32         for(int j=Link[now];j;j=e[j].next)
33             label[e[j].y]++;
34     }
35 }
36 void color()//染色
37 {
38     for(int i=n;i;i--)
39     {
40         int now=q[i],j;
41         for(int j=Link[now];j;j=e[j].next)  check[col[e[j].y]]=i;
42         for(j=1;;j++)  if(check[j]!=i)  break;
43         col[now]=j;
44         if(j>ans)  ans=j;
45     }
46 }
47 int main()
48 {
49     n=read();  m=read();
50     for(int i=1;i<=m;i++)
51     {
52         int x=read(),y=read();
53         insert(x,y);  insert(y,x);
54     }
55     MCS();
56     color();
57     printf("%d
",ans);
58     return 0;
59 }
原文地址:https://www.cnblogs.com/chty/p/5852636.html