poj 1733离散化(map)+并查集
http://blog.sina.com.cn/s/blog_803d08c00100y2yy.html
#include<stdio.h>
#include<iostream>
#include<map>
using namespace std;
const int N =10010;
struct node {
int count,x;
}pre[N];
int find(int n) {
if(n!=pre[n].x) {
int h=pre[n].x;
pre[n].x=find(pre[n].x);
pre[n].count=(pre[n].count+pre[h].count)%2;
}
return pre[n].x;
}
int Union(int x,int y,int d) {
int a=find(x);
int b=find(y);
if(a==b) {
if((d+pre[y].count+pre[x].count)%2==0)//判断是否是真
return 1;
return 0;
}
else {
pre[a].x =b;
pre[a].count=(pre[x].count+pre[y].count+d)%2;//
return 1;
}
}
int main() {
int n,m,a,b,c,i,num,x,y,d,count,flag;
char s[6];
scanf("%d%d",&n,&m);
map<int,int>hash;//离散化
for(i=0;i<=10000;i++) {
pre[i].x=i;
pre[i].count=0;
}
flag=0;num=0;count=0;
while(m--) {
scanf("%d%d%s",&a,&b,s);
d=0;
if(flag)
continue;
a--;count++;
if(hash.find(a)==hash.end())hash[a]=num++;//
x=hash[a];
if(hash.find(b)==hash.end())hash[b]=num++;//
y=hash[b];
if(s[0]=='o')d=1;
if(Union(x,y,d)==0)flag=1;
}
if(flag)
count--;
printf("%d\n",count);
return 0;
}
poj 1733离散化(map)+并查集的更多相关文章
- poj 1733(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...
- POJ 1733 Parity game (并查集)
Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6816 Accepted: 2636 Descr ...
- Parity game POJ - 1733 带权并查集
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...
- poj 1182 食物链 (并查集)
http://poj.org/problem?id=1182 关于并查集 很好的一道题,开始也看了一直没懂.这次是因为<挑战程序设计竞赛>书上有讲解看了几遍终于懂了.是一种很好的思路,跟网 ...
- POJ 2236 Wireless Network (并查集)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 18066 Accepted: 761 ...
- POJ 1562 Oil Deposits (并查集 OR DFS求联通块)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14628 Accepted: 7972 Des ...
- POJ 1182 食物链(并查集拆点)
[题目链接] http://poj.org/problem?id=1182 [题目大意] 草原上有三种物种,分别为A,B,C A吃B,B吃C,C吃A. 1 x y表示x和y是同类,2 x y表示x吃y ...
- POJ - 2912 Rochambeau 种类并查集
题意:有三组小朋友在玩石头剪刀布,同一组的小朋友出的手势是一样的.这些小朋友中有一个是裁判,他可以随便出手势.现在给定一些小朋友的关系,问能否判断出裁判,如果能最早什么时候能够找到裁判. 思路:枚举每 ...
- P2256 一中校运会之百米跑(map+并查集)
思路:首先处理名字,让字符串直接映射唯一一个数字,这就用map<string, int>即可. 然后,直接用并查集 #include<iostream> #include< ...
- poj 1611 The Suspects(并查集输出集合个数)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
随机推荐
- magento 获得当前产品页面的产品id
$product_id = Mage::registry('current_product')->getId();
- 使用Maven将dubbox安装进资源仓库
dubbox网址:https://github.com/dangdangdotcom/dubbox dobbox版本:https://github.com/dangdangdotcom/dubbox/ ...
- ZooKeeper通过ACL修复未授权访问漏洞
默认情况下ZooKeeper允许匿名访问,因此在安全漏洞扫描中暴漏未授权访问漏洞. 一.参考资料 <ZooKeeper 笔记(5) ACL(Access Control List)访问控制列表& ...
- RabbitMQ五:生产者--队列--多消费者
一.生成者-队列-多消费者(前言) 上篇文章,我们做了一个简单的Demo,一个生产者对应一个消费者,本篇文章就介绍 生产者-队列-多个消费者,下面简单示意图 P 生产者 C 消费者 中间队列 ...
- IIS中不让下级应用程序继承主域名的web.config配置
<location path="." allowOverride="true" inheritInChildApplications="fals ...
- IO流的原理和概念
在程序中如何读写文件?不同的编程语言有不同的方式,而 JAVA 则提出了“流”的概念,通过“流”来读写文件 什么是流: 流(Stream)是指一连串的数据(字符或字节),是以先进先出的方式发送信息的通 ...
- 堆排序原理及其js实现
图文来源:https://www.cnblogs.com/chengxiao/p/6129630.html 堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序,它的最坏,最好,平均时 ...
- php防止页面刷新代码
//代理IP直接退出 empty($_SERVER['HTTP_VIA']) or exit('Access Denied'); //防止快速刷新 session_start(); $seconds ...
- 迅为iTOP-4412物联网开发板入门学习高手进阶项目开发超树莓派
免费视频教程: 为初学者精心录制的整套视频教程全部免费,随IT技术发展而不断增添的视频教程仍然免费!一支有经验的工程师团队会始终成为您的后盾. 项目实战---全开源: 手机远程控制开发板 门禁系统 W ...
- jQuery 冒泡事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...