厦门理工学院OJ 1570 Zzz,Pddddddd and Hikari1149 were sworn brothers

Description

Hikari1149 got a job recently, so he has to go to work every day. But the traffic on the way to his company is very terrible. The road on his way always been ruined. It cause a huge trouble for Hikari1149 to find a way to company, So, as his sworn brothers, Zzz and Pddddddd, of course, will not let Hikari1149 feels that way. Given n places, m bidirectional roads that currently exist and q operations. There are two types of operation:

  1. P k (It means the kth road is ruined now.)
  2. Z x y (It means a query that does there currently exists a way between place x and place y.)

Input

The first line contains three integers n, m and q, which indicates the number of places, the number of roads and the number of operations.
Then each line of next m lines contains two integers u and v. The ith line of next m lines means the ith road is between place u and place v.
Then each line of next q lines contains one operation, its format is as mentioned in the previous description.
It’s guaranteed will not exists multiple edges, self-loop and one road will not been ruined repeatedly, but the graph maybe not connected.
1<=n, m, q<=10^5; 1<=k<=m; 1<=u, v, x, y<=n, x≠y.

Output

For each operation 2, if there currently exists a way between place x and place y, then output “Yes”, otherwise, output “No”.

Sample Input

5 4 5
1 2
2 3
3 4
4 5
Z 1 2
P 1
Z 1 3
P 3
Z 2 4

Sample Output

Yes
No
No

题目大意

n个点m条路q个询问,每次询问有以下两种:
·P k 摧毁第k条路。
·Z x y 询问点x和点y是否联通。

题解

反向并查集。把所有询问存下来,先对所有没有被摧毁的边的跑一遍并查集的merge操作。之后反向遍历所有询问,碰到Z操作就查询两点是否在同一集合,若在同一集合则联通;碰到P操作就把该边merge,最后再把询问的结果反向输出。

代码

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

struct edge {
    int from;
    int to;
};


struct query {
    char op;
    int from;
    int to;
};

int f[100005];
edge e[100005];
query qu[100005];
bool cut[100005] = { 0 };
bool ans[100005]={ 0 };

inline int read()
{
    int s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        s = s * 10 + ch - '0', ch = getchar();
    return s * w;
}

int getf(int x) {
    if (x == f[x])return x;
    return f[x] = getf(f[x]);
}

void merge(int x, int y) {
    int fa = getf(x);
    int fb = getf(y);
    if (fa > fb) {
        swap(fa, fb);
    }
    f[fb] = fa;
}

int main() {
    int n, m, q;
    n = read();
    m = read();
    q = read();
    for (int i = 1; i <= n; ++i) {
        f[i] = i;
    }
    for (int i = 0; i < m; ++i) {
        e[i].from = read();
        e[i].to = read();
    }
    char op;
    for (int i = 0; i < q; ++i) {
        op = getchar();
        qu[i].op = op;
        if (op == 'Z') {
            qu[i].from = read();
            qu[i].to = read();
        }
        else {
            qu[i].from = read();
            cut[qu[i].from] = 1;
        }
    }

    for (int i = 0; i < m; ++i) {
        if (cut[i+1]) {
            continue;
        }
        else {
            merge(e[i].from, e[i].to);
        }
    }

    int max_ans = 0;

    for (int i = q - 1; i >= 0; --i) {
        if (qu[i].op == 'Z') {
            if (getf(qu[i].from) == getf(qu[i].to)) {
                ans[max_ans++] = 1;
            }
            else {
                max_ans++;
            }
        }
        else {
            merge(e[qu[i].from-1].from, e[qu[i].from-1].to);
        }
    }

    for (int i = max_ans - 1; i >= 0; --i) {
        if (ans[i] == 1) {
            puts("Yes");
        }
        else {
            puts("No");
        }
    }

    return 0;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值