BZOJ1006: [HNOI2008]神奇的国度

Time Limit: 20 Sec  Memory Limit: 162 MB
Submit: 4993  Solved: 2350
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

/**************************************************************

    Problem: 1006

    User: ictsing

    Language: C++

    Result: Accepted

    Time:516 ms

    Memory:32784 kb

****************************************************************/

 

#include<iostream>

#include<cstdio>

using namespace std;

const int maxn = 10000+5;

const int maxm = 4000000+5;//要开四倍,2*2

int head[maxn],buck[maxn],q[maxn],col[maxn],f[maxn],mark[maxn];

int to[maxm],nxt[maxm];

bool vis[maxn];

int n,m,c=0,best=0;

void adde(int h[],int x,int y)

{

    c++,to[c]=y,nxt[c]=h[x],h[x]=c;

}

inline int read()

{

    int num=0,flag=1;

    char ch;

    do{

        ch=getchar();

        if(ch=='-') flag=-1;

    }while(ch<'0'||ch>'9');

    do{

        num=num*10+ch-'0';

        ch=getchar();

    }while(ch>='0'&&ch<='9');

    return num*flag;

}

void MCS()

{

    for(int i=1;i<=n;i++) adde(buck,0,i);

    int j;

    for(int i=n;i>=1;i--)

    {

        while(true)

        {

            for(j=buck[best];j;j=nxt[j])

                if(!vis[to[j]]) break;else buck[best]=nxt[j];

            if(j)

            {

               int x=to[j];

               vis[x]=1,q[i]=x;

               for(j=head[x];j;j=nxt[j])

                  if(!vis[to[j]])

                    f[to[j]]++,adde(buck,f[to[j]],to[j]),best=max(best,f[to[j]]);

               break;

            }

            else best--;

        }

    }

}

int main()

{

    n=read(),m=read();int x,y;

    for(int i=1;i<=m;i++)

        x=read(),y=read(),adde(head,x,y),adde(head,y,x);

    MCS();int ans=0;

    for(int i=n;i>=1;i--)

    {

        int x=q[i];

        for(int j=head[x];j;j=nxt[j]) mark[col[to[j]]]=i;

        int k;for(k=1;k<=n&&mark[k]==i;k++);

        col[x]=k,ans=max(ans,k);

    }

    printf("%d\n",ans);

    return 0;

}

/*

4 5

1 2

1 4

2 4

2 3

3 4

 

3

*/

 



评论

热度(5)