思路:每一个product都可以作一条边,每次添加一条边,如果这边的加入使得某个集合构成环,就应该refuse,那么就用并查集来判断。


AC代码:

//#define LOCAL
#include <stdio.h>
#include <string.h>
const int maxn = 1e5 + 5;
int par[maxn], rank[maxn];

void init() {
    memset(rank, 0, sizeof(rank));
    for(int i = 0; i <= maxn; i++) {
        par[i] = i;
    }
}

int findRoot(int x) {
    return x != par[x] ? par[x] = findRoot(par[x]) : x;
}

//启发式合并
void unionSet(int x, int y) {
    if(rank[x] > rank[y]) {
        par[y] = x;
    } else {
        par[x] = y;
        if(rank[x] == rank[y]) rank[y]++;
    }
}

int main() {
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif // LOCAL

    int x, y, refuse = 0;
    init();
    while(scanf("%d", &x) == 1) {
        if(x == -1) {
            printf("%d\n", refuse);
            init();
            refuse = 0;
        }else {
            scanf("%d", &y);
            x = findRoot(x);
            y = findRoot(y);
            if(x == y) {
                //refuse it
                refuse++;
            } else {
                //union
                unionSet(x, y);
            }
        }
    }
    return 0;
}

如有不当之处欢迎指出!

UVALive - 3644 X-Plosives (并查集)的更多相关文章

  1. UVALive - 3644 X-Plosives (并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  2. UVALive(LA) 3644 X-Plosives (并查集)

    题意: 有一些简单化合物,每个化合物都由两种元素组成的,你是一个装箱工人.从实验员那里按照顺序把一些简单化合物装到车上,但这里存在安全隐患:如果车上存在K个简单化合物,正好包含K种元素,那么他们就会组 ...

  3. UVALive 4487 Exclusive-OR 加权并查集神题

    已知有 x[0-(n-1)],但是不知道具体的值,题目给定的信息 只有 I P V,说明 Xp=V,或者 I P Q V,说明 Xp ^ Xq=v,然后要求回答每个询问,询问的是 某任意的序列值 Xp ...

  4. UVALive - 3027 Corporative Network (并查集)

    这题比较简单,注意路径压缩即可. AC代码 //#define LOCAL #include <stdio.h> #include <algorithm> using name ...

  5. UVALive 6910 Cutting Tree 并查集

    Cutting Tree 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...

  6. UVALive 6906 Cluster Analysis 并查集

    Cluster Analysis 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemi ...

  7. UVALive 6889 City Park 并查集

    City Park 题目连接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122283#problem/F Description P ...

  8. 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856

    并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...

  9. LA 3644 易爆物 并查集

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

随机推荐

  1. 译-Web Service剖析: XML, SOAP 和WSDL 用于独立于平台的数据交换

    本文是翻译内容,原文参见: Anatomy of a Web Service: XML, SOAP and WSDL for Platform-independent Data Exchange We ...

  2. Newtonsoft.Json 版本冲突时解决方案

    如果同一项目中不同第三方类库分别使用了不同版本的Newtonsoft.Json的情况下,可以在主项目配置文件中添加以下节点,将0.0.0.0-11.0.0.0此区间的Newtonsoft.Json使用 ...

  3. docker入门(二)容器与镜像的理解

    10张图带你深入理解Docker容器和镜像 申明:此篇文章是转载的(原文地址http://dockone.io/article/783),今天意外发现已经有人转载了(复制了),希望大家关注原创 原本打 ...

  4. SpringMVC空字符串转为null

    空字符串转为null 现在我遇到这样一个需求,那就是我想要吧前端传过来的值变为空,因为所谓前端的校验,其实都不是校验,如果前端传给后台一个表单,可是表单未填入值,我们后台进行判断的时候 既需要判断nu ...

  5. API函数详解:API大全总目录(按字母排列)

    API函数详解 http://www.feiesoft.com/api/api.html

  6. bootstrap---treeview使用方法

    1.html部分: <div id="tree"></div> 2.css设置展开/收缩按钮图片: .tree_arrows_down:before{ co ...

  7. vue项目实现记住密码到cookie功能(附源码)

    实现功能: 1.记住密码勾选,点登陆时,将账号和密码保存到cookie,下次登陆自动显示到表单内 2.不勾选,点登陆时候则清空之前保存到cookie的值,下次登陆需要手动输入 大体思路就是通过存/取/ ...

  8. DG环境的日常巡检

    DG环境的日常巡检 目录 1.DG环境的日常巡检4 1.1.主库环境检查4 1.1.1.主库实例启动状态检查4 1.1.2.主库启动模式检查4 1.1.3.主库DG环境的保护模式检查4 1.1.4.主 ...

  9. Python tutorial阅读之Python基本运算与基本变量

    将 Python 当做计算器 除法运算 用/表示除法运算时,一般得到的是浮点数,如果我们需要得到整数,可以用运算符// 余数计算 % 幂乘方 系统内置变量_ 内置变量_,存储了最近的结果.如图 字符串 ...

  10. chrome_options

      用法 from selenium.webdriver.chrome.options import Options chorme_option=Options() chorme_option.add ...