人生有信仰 数据有梯度 暴力不爆零


bzoj 1006: [HNOI2008]神奇的国度 -- 弦图(最大势算法)

1006: [HNOI2008]神奇的国度

Time Limit: 20 Sec  Memory Limit: 162 MB

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)

 

Source

弦图什么的还是看cdq冬令营的课件吧

然后这题就是那最大势算法搞一搞,用链表的话时间复杂度最优O(n+m)//可是我太弱辣,并没有写出来。。

#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 1000000007
#define ll long long
#define M 2000010 
#define N 10010
inline int rd()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int lj[N],fro[M],to[M],cnt;
inline void add(int a,int b){fro[++cnt]=lj[a];to[cnt]=b;lj[a]=cnt;}
int num[N],ji[N],col[N],hs[N];
priority_queue<pair<int,int> >q;
#define mp make_pair
int n,m,p[N],mx;
bool vs[N];
int main()
{
    n=rd();m=rd();
    for(int i=1,x,y;i<=m;i++)
    {
        x=rd();y=rd();
        add(x,y);add(y,x);
    }for(int i=1;i<=n;i++) q.push(mp(0,i));
    for(int i=n,x;i;i--)
    {
        while(vs[q.top().second]) q.pop();
        x=q.top().second;q.pop();
        vs[x]=1;num[i]=x;
        for(int j=lj[x];j;j=fro[j])
            if(!vs[to[j]]) q.push(mp(++ji[to[j]],to[j]));
    }
    for(int i=n,x;i;i--)
    {
        x=num[i];
        for(int j=lj[x];j;j=fro[j])
            if(col[to[j]]) hs[col[to[j]]]=i;
        for(int j=1;j<=mx;j++) if(hs[j]!=i){col[x]=j;break;}
        if(!col[x]) col[x]=++mx;
    }
    printf("%d\n",mx);
    return 0;
}

 

posted @ 2017-09-13 19:19  lkhll  阅读(311)  评论(0编辑  收藏  举报