(并查集 建立关系)Parity game -- POJ -1733
链接:
http://poj.org/problem?id=1733
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/H
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std; #define N 110
#define oo 0xfffffff map<int, int>f; ///f[i]=x, i是int类型的,x也是int类型的
map<int, int>r;
int n, m; int Find(int x)
{
if(!f[x])
return x;
int k=f[x];
f[x]=Find(f[x]);
r[x]=(r[x]+r[k])%; return f[x];
} int main()
{
while(scanf("%d", &n)!=EOF)
{
scanf("%d", &m);
int i, a, b, fa, fb, ans=, flag=;
char s[N]; for(i=; i<=m; i++)
{
int num=;
scanf("%d%d%s", &a, &b, s);
a--; if(strcmp(s, "odd")==)
num=; fa=Find(a), fb=Find(b);
if((a< || a>=n) || (fa==fb && (r[a]+num)%!=r[b]))
flag=;
if(flag==)
continue;
if(fa!=fb)
{
f[fa]=fb;
r[fa]=(r[b]-(r[a]+num)%+)%;
}
ans++;
}
printf("%d\n", ans);
}
return ;
}
(并查集 建立关系)Parity game -- POJ -1733的更多相关文章
- Parity game POJ - 1733 带权并查集
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...
- 【POJ1733】【带标记并查集】Parity game
Description Now and then you play the following game with your friend. Your friend writes down a seq ...
- (并查集 建立关系)食物链 -- POJ-- 1182
链接: http://poj.org/problem?id=1182 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- (并查集)The Suspects --POJ --1611
链接: http://poj.org/problem?id=1611 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- (并查集) Wireless Network --POJ --2236
链接: http://poj.org/problem?id=2236 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- 并查集,是否成树,Poj(1308)
思路: 对于每一条新的边的两个端点,是否是属于一颗树,要是的话,就不是一颗树.否则,就合并. 这里要注意的是,不能是森林,我这里WA了两次了.只不过在最后,查看每个节点的祖先是否是同一个就可以了. # ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 【并查集】 不相交集合 - 并查集 教程(文章作者:Slyar)
最近写了一个多星期的并查集,一瞬间贴出这么多解题报告,我想关于并查集的应用先告一段落吧,先总结一下. 在网上看到一篇关于并查集比较好的教程(姑且允许我这么说吧),不转过来是在可惜.献给爱学习的你 文章 ...
- 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil
题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...
随机推荐
- FileOperator 文件(夹)操作类
public class FileOperator { /** * 复制文件目录 * @param srcDir 要复制的源目录 eg:/mnt/sdcard/DB * @param destDir ...
- oc 中的id类型与类型转换
id是oc语言中一个独特的数据类型.一种通用对象类型.可以转换为任何数据类型,即id类型的变量可以存放任何数据类型的对象. 使用示例: Animal * dog = [[Dog alloc]init] ...
- while and for 2
public class TestWhileAndFor2 { /** * 九九乘法表 * 1!+2!+3!+....+10!=? * */ public static void main(Strin ...
- Java中static代码块,{}大括号代码块,构造方法代码块执行顺序!
注:下列代码中的注释都是JUnit4单元测试运行结果. 首先,没有父类的(父类是Object)的类A package Static.of; public class A { { System.out. ...
- input disable手机端颜色兼容问题
color: #5b636d; -webkit-text-fill-color: #5b636d; opacity: 1; -webkit-opacity: 1; input在移动端会有padding ...
- Java的继承与接口
为什么Java里面只允许单一继承,但允许实现多个接口? 1,为什么不能实现类的多继承?主要是防止多个类有相同的方法名,然而方法体不一样,子类就比较难选择了.所以java和C#不支持类的多继承2,允许具 ...
- 为什么要学习php
前言: 有的人说java是世界上最伟大的编程语言,也有人说php是最伟大的编程语言,对于这个问题的争论,貌似一直都没有停息过. 前段时间也看过一份编程语言使用排行的调查资料,java当仁不让的保持着排 ...
- php 利用http上传协议(表单提交上传图片 )
主要就是利用php 的 fsocketopen 消息传输. 这里先通过upload.html 文件提交,利用chrome抓包,可以看到几个关键的信息. 首先指定了表单类型为multipart/form ...
- Adplus 抓取Crash Dump
本实例在win8.1 安装window kits https://developer.microsoft.com/en-us/windows/hardware/windows-driver-kit 1 ...
- vue1.0学习
vue 一片html代码配合上json,在new出来vue实例 Demo:1 数据双向绑定(v-model="message",{{message}}) <div id=&q ...