题意:求问数列区间奇偶信息正确与否

和上一题一样(甚至弱化),加个离散就ok啦

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 2e5+11;
const int oo = 0x3f3f3f3f;
const double eps = 1e-7;
typedef long long ll;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int p[maxn],r[maxn];
void init(int n){
memset(r,0,sizeof r);
rep(i,0,n+2) p[i]=i;
}
int find(int x){
if(x==p[x])return x;
int oldp=p[x];
int t=find(p[x]);
r[x]=(r[x]+r[oldp])&1;
return p[x]=t;
}
void link(int fa,int fb,int a,int b,int w){
p[fb]=fa;
r[fb]=(r[a]-r[b]+w+4)&1;
}
int b[maxn],n,m,tot;
struct P{
int a,b;
bool odd;
}a[maxn];
char s[66];
inline int pos(int i){
return lower_bound(b+1,b+1+tot,i)-b;
}
int main(){
while(cin>>n>>m){
tot=0;
rep(i,1,m){
scanf("%d%d%s",&a[i].a,&a[i].b,s);
if(s[0]=='e')a[i].odd=0;
else a[i].odd=1;
if(a[i].a>a[i].b)swap(a[i].a,a[i].b);
a[i].a--;
b[++tot]=a[i].a;
b[++tot]=a[i].b;
}
sort(b+1,b+1+tot);
int nn=unique(b+1,b+1+tot)-b-1;
tot=nn;
init(nn);
int flag=0;
rep(i,1,m){
int x=pos(a[i].a),y=pos(a[i].b);
int odd=a[i].odd;
int fx=find(x),fy=find(y);
if(flag)continue;
if(fx==fy){
if((r[y]-r[x]+4&1)!=odd){
flag=i;
}
}else{
link(fx,fy,x,y,odd);
}
}
printf("%d\n",flag==0?m:flag-1);
}
return 0;
}

POJ - 1733 离散化 并查集的更多相关文章

  1. BZOJ4195 [Noi2015]程序自动分析(离散化+并查集)

    4195: [Noi2015]程序自动分析 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 689  Solved: 296 [Submit][Sta ...

  2. AcWing:237. 程序自动分析(离散化 + 并查集)

    在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xjxi=x ...

  3. POJ 2513 Colored Sticks (离散化+并查集+欧拉通路)

    下面两个写得很清楚了,就不在赘述. http://blog.sina.com.cn/s/blog_5cd4cccf0100apd1.htmlhttp://www.cnblogs.com/lyy2890 ...

  4. poj 2524 (并查集)

    http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...

  5. [POJ 2588]--Snakes(并查集)

    题目链接:http://poj.org/problem?id=2588 Snakes Time Limit: 1000MS   Memory Limit: 65536K   Description B ...

  6. poj 1456 Supermarket - 并查集 - 贪心

    题目传送门 传送点I 传送点II 题目大意 有$n$个商品可以销售.每个商品销售会获得一个利润,但也有一个时间限制.每个商品需要1天的时间销售,一天也只能销售一件商品.问最大获利. 考虑将出售每个物品 ...

  7. poj 2492(关系并查集) 同性恋

    题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...

  8. poj 1182 (关系并查集) 食物链

    题目传送门:http://poj.org/problem?id=1182 这是一道关系型并查集的题,对于每个动物来说,只有三种情况:同类,吃与被吃: 所以可以用0,1,2三个数字代表三种情况,在使用并 ...

  9. PAT1034. Head of a Gang ——离散化+并查集

    题意:成员A与成员B通话 ,成员B与成员C通话,则 ABC即为一个团伙,一共有若干个团伙,每个团伙的人数大于2且相互通话时间超过一定值即为黑帮,每个黑帮伙里有一个BOSS,boss是与各个成员打电话最 ...

随机推荐

  1. 3-java中String值为空字符串与null的判断方法

    java中String值为空字符串与null的判断方法 2018年01月21日 14:53:45 阅读数:1189 Java空字符串与null的区别 1.类型 null表示的是一个对象的值,而不是一个 ...

  2. pip镜像源配置

    #配置文件路径:%HOME%\pip\pip.ini,不存在新建即可 #在 Windows 2000 以上版本里,%HOME% 目录指的是系统盘下的“\Documents and Settings\你 ...

  3. Yii2验证登录得User类

    Yii2中的  Class yii\web\User 是如果进行验证登录,如果我们使用User类验证登录会给我们减少很多麻烦.在此就拿Yii2中自带的登录功能进行说明. 配置.在应用配置文件compo ...

  4. npm 升级

    当我们运行某个项目是 会提示 > my-first-vue-project@1.0.0 dev C:\Users\ASUS\my-project > node build/dev-serv ...

  5. c# 获取非托管指针长度

    public List<string> GetPDFValues() { List<string> strs = new List<string>(); unsaf ...

  6. Shiro——认证

    引入shiro依赖 <!-- shiro --> <dependency> <!-- shiro-core Required in all environments. - ...

  7. App测试从入门到精通之App分类和场景操作系统

    App概要 APP是application的缩写.通常指的是手机软件上的应用,或称为手机客户端.手机app就是手机的应用程序.随着智能手机的越发普及,用户越发依赖手机软件商品店,app开发的需求与发展 ...

  8. python3安装 feedparser

    在看<集体智慧编程>时碰到python3环境下安装feedparser的问题,搜索发现很多人碰到此问题,最终找以下方法解决. how to install feedparser on py ...

  9. Java 错误结果Throw/Throws

    目录 java处理异常方式    throw的作用    throws的作用    方法原理    举例    总结 个人实例 1.java处理异常方式 在java代码中如果发生异常的话,jvm会抛出 ...

  10. js任意位数求和

    <script> //任意位数求和 function sum(){ if(arguments.length==1) { console.log(arguments[0]) return; ...