hihocoer 1190 点双模板

版权声明:_ https://blog.csdn.net/lunch__/article/details/81808222

题目链接快戳我

这题好玄学啊… 它真的让我对 T a r j a n 这个算法绝望了 完全学不懂

首先这个题要我们求每条边所属的集合

当然还是不考虑存边 存完点 D f s 一遍就好了啦

Codes

#include<bits/stdc++.h>

int read() {
    int _ = 0, ___ = 1; char __ = getchar();
    for(; !isdigit(__); __ = getchar()) if(__ == '-') ___ = -1;
    for(; isdigit(__); __ = getchar()) _ = (_ << 3) + (_ << 1) + (__ ^ 48);
    return _ * ___;
}

using namespace std;

const int N = 2e4 + 10, M = 4e5 + 10;
int head[N], nxt[M], to[M], e = 1;
int n, m, ins[N], Sta[M], top, cnt, ans[M];
int dfn[N], low[N], color, be[M], num[M];
vector<int> kuai[N];

void add(int x, int y) {
    to[++ e] = y; nxt[e] = head[x]; head[x] = e;
}

void dfs(int x, int fa) {
    int sz = 0;
    //cerr << x << endl;
    //cout << x << ' ' << fa << endl;
    dfn[x] = low[x] = ++ cnt; Sta[++ top] = x;
    for(int i = head[x]; i; i = nxt[i]) {
        if(!dfn[to[i]]) {
            ++ sz;
            dfs(to[i], x);
            low[x] = min(low[x], low[to[i]]);
            if(low[to[i]] >= dfn[x]) {
                ++ color;
                do {
                    be[Sta[top]] = color;
                    kuai[color].push_back(Sta[top]);
                }while(Sta[top --] != to[i]);
                kuai[color].push_back(x);
            }
        }
        else if(to[i] != fa && !ins[to[i]])
            low[x] = min(low[x], dfn[to[i]]);

    }
}

void Dfs(int x) {
    //cerr << x << endl;
    for(int i = head[x]; i; i = nxt[i]) 
        if(!ans[i >> 1] && be[to[i]] == be[x]) {
            // 这里我原来是用一个数组判定 但是不知道为什么一直Wa
            // 抱着试一下的心情调成这样才过了这个题
            // ins[i] = ins[i ^ 1] = 1;
            ans[i >> 1] = be[x];
            num[be[x]] = min(num[be[x]], i >> 1);
            Dfs(to[i]);
            //ins[i] = ins[i ^ 1] = 0;
        }
}

int main() {
    freopen("1190.in", "r", stdin);
    freopen("1190.out", "w", stdout);
    int x, y;
    n = read(), m = read();
    for(int i = 1; i <= m; ++ i) {
        x = read(), y = read();
        add(x, y); add(y, x);
        num[i] = INT_MAX;
    }
    dfs(1, 0);
    for(int i = 1; i <= color; ++ i) {
        for(int j = 0, sz = kuai[i].size(); j < sz; ++ j)
            be[kuai[i][j]] = i;
        Dfs(kuai[i][0]);
    }
    cout << color << endl;
    for(int i = 1; i <= m; ++ i) 
        printf("%d ", num[ans[i]]);
    puts("");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lunch__/article/details/81808222