【BZOJ】1006: [HNOI2008]神奇的国度 弦图消除完美序列问题

1006: [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)

参考文献:弦图与区间图-陈丹琦

弦图的定义:任何一个长度大于3的环中,至少有一个弦;

使用MCS()求完美消除序列问题;

即先字典序广度优先搜索出之后涂色的点的顺序,(所谓的完美消除就是每次都消除一个三角形)。

lable[i]表示第i个点与多少个已标记的点相连;id[i]表示在第i个完美消除序列中的点是哪个;之后直接涂色即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l, m, rt << 1
#define rson m+1, r, rt << 1|1
typedef pair<int,int> PII;
#define A first
#define B second
#define MK make_pair
//typedef __int64 ll;
template<typename T>
void read1(T &m)
{
    T 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();}
    m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
    if(a>9) out(a/10);
    putchar(a%10+'0');
}
const int N = 10005;
const int M = 1000005;
int tot,head[N];
struct Edge{
    int to,Next;
}e[M<<1];
void ins(int u,int v)
{
    e[++tot].Next = head[u],e[tot].to = v,head[u] = tot;
}
priority_queue<PII> pq;
int lable[N],id[N],del[N];
void MCS(int n)
{
    MS0(lable);
    MS0(del);
    pq.push(MK(0,n));// 每次选未删除的lable最大的进行拓展;
    while(!pq.empty()){
        int u = pq.top().B;
        pq.pop();
        if(del[u]) continue;
        del[u] = 1;id[n--] = u;// 记录id才是目的,lable只是拓展的依据;
        for(int t = head[u];t;t = e[t].Next){
            int v = e[t].to;
            if(del[v]) continue;
            lable[v]++;
            pq.push(MK(lable[v],v));
        }
    }
}
int color(int n)
{
    int ans = 0;
    MS0(lable);
    rep_1(i,n,1){
        int cnt = 1,u = id[i];
        for(int t = head[u];t;t = e[t].Next){
            int v = e[t].to;
            if(lable[v]) cnt++;
        }
        lable[u] = cnt;// 继续涂色;
        ans = max(ans,lable[u]);
    }
    return ans;
}
int main()
{
    int n,m,u,v;
    read2(n,m);
    rep0(i,0,m){
        read2(u,v);
        ins(u,v);ins(v,u);
    }
    MCS(n);
    out(color(n));
    return 0;
}

 

posted @ 2016-03-05 13:55  hxer  阅读(208)  评论(0编辑  收藏  举报