NOIP模拟题 2016.10.6 [并查集] [联通性] [Tarjan]

T1 ,图的连通性
题意:给一个图,支持删除操作,询问任意时刻两个点的连通性

观察到只有删除操作,那么可以考虑倒着处理
先把所有将来会删除的边删掉,然后从最后一组询问倒着处理
并查集维护连通性
这里的强制在线是骗人的
在任意时刻图中剩下的边数都是可以离线处理出来的
一个小trick
重边,删除的时候只删除一条
用set 或者map判断当前该边的数量 {cnt,disable}
当cnt==disable时,两点不连通

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 100005;
const int maxm = 150005;
const int maxq = 100005;
inline void read(int &x)
{
    x = 0;
    int flag = 1;
    char ch = getchar();
    while(ch<'0' || ch>'9')
    {
        ch=getchar();
        if(ch == '-') flag = -1;
    }
    while(ch>='0' && ch<='9')
    {
        x*=10; x+=ch-'0';
        ch = getchar();
    }
    x *= flag;
}
struct Road
{
    int x,y;
    int cnt,disable;
    bool operator < (const Road t) const
    {
        if(x ^ t.x) return x < t.x;
        else return y < t.y;
    }
}road[maxm];
set <Road> st;
set <Road> ::iterator it;
struct Query
{
    int x,y;
    int id;
    int ans;
}que[maxq];
int n,m,q;
int maxedge;
void init()
{
    maxedge=0;
    read(n); read(m); read(q);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        read(x); read(y);
        if(x>y) swap(x,y);
        it = st.find((Road){x,y,0,0});
        if(it!=st.end())
        {
            Road tmp = (*it);
            st.erase(it);
            tmp.cnt++;
            st.insert(tmp);
        }
        else st.insert((Road){x,y,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值