【 bzoj 4452 】 [cerc2015] Export Estimate - 并查集

  考虑离线,将边权从大到小加边。
  观察得知一个点会被删当且仅当它的度数为2。而一个度数为2的点不被删当且仅当这个点是一个环内最后的点。删的时候不会改变别的点的度数。
  设当前度数为0的点有 cnt0 个,度数为2的点有 cnt2 个,环有 cntcycle 个,显然可以得到剩余点数为 ncnt0cnt2+cntcycle ,剩余边数为 xcnt2+cntcycle ,其中 x <script type="math/tex" id="MathJax-Element-281">x</script>为已经加了的边数。
  至于统计环的个数,并查集记录一下联通块内度数为2的点是否等于联通块大小即可。
  贴一下代码吧虽然有点丑= =
  

/*
    I will chase the meteor for you, a thousand times over.
    Please wait for me, until I fade forever.
    Just 'coz GEOTCBRL.
*/
#include <bits/stdc++.h>
using namespace std;
#define fore(i,u)  for (int i = head[u] ; i ; i = nxt[i])
#define rep(i,a,b) for (int i = a , _ = b ; i <= _ ; i ++)
#define per(i,a,b) for (int i = a , _ = b ; i >= _ ; i --)
#define For(i,a,b) for (int i = a , _ = b ; i <  _ ; i ++)
#define Dwn(i,a,b) for (int i = ((int) a) - 1 , _ = (b) ; i >= _ ; i --)
#define fors(s0,s) for (int s0 = (s) , _S = s ; s0 ; s0 = (s0 - 1) & _S)
#define foreach(it,s) for (__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)

#define mp make_pair
#define pb push_back
#define pii pair<int , int>
#define fir first
#define sec second
#define MS(x,a) memset(x , (a) , sizeof (x))

template <class T> inline void upmax(T&a , T b) { if (a < b) a = b ; }
template <class T> inline void upmin(T&a , T b) { if (a > b) a = b ; }

typedef long long ll;

const int maxn = 300007;
const int maxm = 300007;
const int mod = 1000000007;
const int inf = 0x7fffffff;
const double eps = 1e-7;

typedef int arr[maxn];
typedef int adj[maxm];

#define gc getchar
#define idg isdigit
#define rd RD<int>
#define rdll RD<long long>
template <typename Type>
inline Type RD() {
    char c = getchar(); Type x = 0 , flag = 1;
    while (!idg(c) && c != '-') c = getchar();
    if (c == '-') flag = -1; else x = c - '0';
    while (idg(c = gc()))x = x * 10 + c - '0';
    return x * flag;
}
inline char rdch() {
    char c = gc();
    while (!isalpha(c)) c = gc();
    return c;
}
#undef idg
#undef gc

// beginning

int n , m , q;
struct edge {
    int u , v , w;  
}E[maxm];

inline bool cmp(const edge a , const edge b) {
    return a.w < b.w;
}

pii que[maxn];
arr deg;

void input() {
    n = rd() , m = rd();
    rep (i , 1 , m) E[i].u = rd() , E[i].v = rd() , E[i].w = rd();
    q = rd();
    rep (i , 1 , q) que[i] = pii(rd() , i);
    sort(E + 1 , E + m + 1 , cmp);
    sort(que + 1 , que + q + 1);
}

arr fa , sz , cnt , ans_p , ans_e;

int find(int u) { return fa[u] == u ? u : fa[u] = find(fa[u]); }

inline bool merge(int u , int v) {
    u = find(u) , v = find(v);
    if (u == v) return 0;
    if (sz[u] < sz[v]) swap(u , v);
    fa[v] = u , sz[u] += sz[v] , cnt[u] += cnt[v];
    return 1;
}

int cnt_0 , cnt_2 , cnt_cycle;

inline void add_cnt(int u , int w) {
    cnt[find(u)] += w , cnt_2 += w;
}

inline void go(int &i , int &j) {
    while (i && E[i].w >= que[j].fir) {
        int x = find(E[i].u) , y = find(E[i].v);
        int p1 = cnt[x] == sz[x] , p2 = cnt[y] == sz[y];
        if (!deg[E[i].u] ++) cnt_0 --;
        if (!deg[E[i].v] ++) cnt_0 --;

        if (deg[E[i].u] == 2)
            add_cnt(E[i].u , 1);
        else if (deg[E[i].u] == 3)
            add_cnt(E[i].u , -1);

        if (deg[E[i].v] == 2)
            add_cnt(E[i].v , 1);
        else if (deg[E[i].v] == 3)
            add_cnt(E[i].v , -1);

        if (!merge(E[i].u , E[i].v)) {
            x = find(E[i].u) , p1 &= p2;
            if (p1)
                cnt_cycle --;
            else if (cnt[x] == sz[x])
                cnt_cycle ++;
        } else cnt_cycle -= p1 + p2;

        i --;
    }
}

void solve() {
    cnt_0 = n , cnt_2 = 0 , cnt_cycle = 0;
    rep (i , 1 , n) fa[i] = i , sz[i] = 1;
    int i = m , j = q;
    go(i , j);
    while (j) {
        ans_p[que[j].sec] = n - cnt_0 - cnt_2 + cnt_cycle;
        ans_e[que[j].sec] = (m - i) - cnt_2 + cnt_cycle;

        if (-- j)
            go(i , j);
    }
    rep (k , 1 , q)
        printf("%d %d\n" , ans_p[k] , ans_e[k]);
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("data.txt" , "r" , stdin);
    #endif
    input();
    solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值