bzoj1006 神奇的国度

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)

题解: 本题为弦图,参见cdq论文 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<algorithm>
 5 #include<cstring>
 6 #define inf 0x7fffffff
 7 #define ll long long
 8 using namespace std;
 9 inline ll read()
10 {
11     ll x=0,f=1;char ch=getchar();
12     while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
13     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
14     return x*f;
15 }
16 int n,m,cnt,ans;
17 int head[10005],d[10005],q[10005],col[10005],hash[10005];
18 bool vis[10005];
19 struct data{int to,next;}e[2000005];
20 void ins(int u,int v)
21 {e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;}
22 int main()
23 {
24     n=read();m=read();
25     for(int i=1;i<=m;i++)
26     {
27         int u=read(),v=read();
28         ins(u,v);ins(v,u);
29     }
30     for(int i=n;i;i--)
31     {
32         int t=0;
33         for(int j=1;j<=n;j++)
34         {
35             if(!vis[j]&&d[j]>=d[t])t=j;
36         }
37         vis[t]=1;q[i]=t;
38         for(int j=head[t];j;j=e[j].next)
39             d[e[j].to]++;
40     }
41     for(int i=n;i>0;i--)
42     {
43         int t=q[i];  
44         for(int j=head[t];j;j=e[j].next)hash[col[e[j].to]]=i;
45         int j;
46         for(j=1;;j++)if(hash[j]!=i)break;
47         col[t]=j;
48         if(j>ans)ans=j;
49     }
50     printf("%d",ans);
51     return 0;
52 }
View Code

 

posted @ 2018-12-18 16:02  zjxxcn  阅读(139)  评论(0编辑  收藏  举报