Bzoj1006: [HNOI2008]神奇的国度

题面

传送门

Sol

弦图最小染色
做法见上上篇博客

暴力模拟

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
const int _(1e5 + 5);
const int __(2e7 + 5);
typedef long long ll;

IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, first[_], cnt, label[_], vis[_], col[_], ans, best, hc[_];
struct Edge{
    int to, next;
} edge[__];
vector <int> P[_];

IL void Add(RG int u, RG int v){
    edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
}

int main(RG int argc, RG char* argv[]){
    Fill(first, -1), n = Input(), m = Input();
    for(RG int i = 1; i <= m; ++i){
        RG int u = Input(), v = Input();
        Add(u, v), Add(v, u);
    }
    for(RG int i = 1; i <= n; ++i) P[0].push_back(i);
    for(RG int i = 1, nw = 0; i <= n; ++i){
        for(RG int flg = 0; !flg; ){
            for(RG int j = P[best].size() - 1; ~j; --j)
                if(vis[P[best][j]]) P[best].pop_back();
                else{
                    flg = 1, nw = P[best][j];
                    break;
                }
            if(!flg) --best;
        }
		vis[nw] = 1;
        for(RG int e = first[nw]; e != -1; e = edge[e].next){
            RG int v = edge[e].to;
            if(!vis[v]) P[++label[v]].push_back(v), best = max(best, label[v]);
            hc[col[v]] = nw;
        }
        for(RG int j = 1; j; ++j)
            if(hc[j] != nw){
                col[nw] = j;
                break;
            }
        ans = max(ans, col[nw]);
    }
    printf("%d\n", ans);
    return 0;
}

或者直接取\(max(label[i]+1)\)

for(RG int i = 1; i <= n; ++i) ans = max(ans, label[i] + 1);
posted @ 2018-04-05 07:57  Cyhlnj  阅读(134)  评论(0编辑  收藏  举报