9月30日机房模拟赛题解

今天AK啦,而且是rank1,真是超开心,这是弱菜的人生第一次AK


T1并查集,BZOJ 冷战


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> tmp;
const int MAXN = 200000 + 10;
int n, m, timer, sz[MAXN], fa[MAXN], tim[MAXN], dep[MAXN];
struct Un{ int x, y; } U[MAXN];

void init( ) { for( register int i = 1; i <= n; i++ ) sz[i] = 1, fa[i] = i; }
void Union( int x, int y ) {
    while( fa[x] != x ) x = fa[x];
    while( fa[y] != y ) y = fa[y];
    if( sz[x] > sz[y] ) swap( x, y );
    fa[x] = y; tim[x] = ++timer;
    sz[y] += sz[x];
}
void get_dep( int x ) {
    if( fa[x] == x ) return;
    get_dep( fa[x] );
    dep[x] = dep[fa[x]] + 1;
}
int query( int x, int y ) {
    get_dep(x); get_dep(y);
    int tmp = 0;
    if( dep[x] < dep[y] ) swap( x, y );
    while( dep[x] > dep[y] && x != y ) tmp = max( tmp, tim[x] ), x = fa[x];
    while( x != y ) { tmp = max( tmp, max( tim[x], tim[y] ) ); x = fa[x]; y = fa[y]; }
    return tmp;
}
int main( ) {
    freopen( "union.in", "r", stdin );
    freopen( "union.out", "w", stdout );
    scanf( "%d%d", &n, &m );
    init( ); int x, y;
    for( register int i = 1; i <= n - 1; i++ ) {
        scanf( 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值