Ural 1003 Parity

29 篇文章 0 订阅
23 篇文章 0 订阅

PRO IS HERE



大意:

一段长度为n的路,已知m对信息,信息告诉你某两点之间的距离的奇偶,问你到哪个信息是错的。


解答:

明显的并查集,保存距离的奇偶,find的时候顺便跟新一下。。。我就是忘记了fa[x] = k

查了n久的代码啊,弱菜就是弱啊。。。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>

using namespace std;

#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define nMax 10100
#define inf 0x7fffffff
#define LL long long
#define pb push_back


struct AA{
	int a,id;
	void make(int c,int b) { a=c,id=b; };
	bool operator < (const AA& b) const {
		return a < b.a;
	}
};

struct BB{
	int l,r;
	int op;
	void read(){
		char s[20];
		scanf("%d%d%s",&l,&r,s);
		if(strcmp(s,"even") == 0) op = 0;else op = 1;
	}
};

AA a[nMax];
BB b[nMax];
int n;

void read(){
	scanf("%d",&n);
	int tot = 0;
	FOR(i,1,n) {
		b[i].read();
		a[tot++].make(b[i].l,i);
		a[tot++].make(b[i].r,-i);
	}
}

int fa[nMax],dist[nMax];

void init(int n){
	FOR(i,0,n) fa[i]=i,dist[i]=0;
}
int find(int x) {
	if(fa[x] == x) return fa[x];
	int k = find(fa[x]);
	dist[x] ^= dist[fa[x]];
	return fa[x] = k;
}
bool Add (int l,int r,int op){
	int x = find(l),y = find(r);
	if(x < y) {
		fa[y] = x;
		dist[y] = (op+dist[l]+dist[r])%2;
	}else if( x > y){
		fa[x] = y;
		dist[x] = (op+dist[l]+dist[r])%2;
	}else if(x == y) {
		if((dist[l]+dist[r])%2 != op) return false;
	}
	return true;
}

int N;
void sovle(){
	int m = n << 1;
	sort(a,a+m);
	int now = a[0].a,tot=1;
	FOR(i,0,m-1){
		if(a[i].a!=now){
			now=a[i].a;tot++;
		}
		if(a[i].id > 0) b[a[i].id].l=tot;
		else            b[-a[i].id].r=tot;
	}
	init(tot);
	int ans = n;
	FOR(i,1,n) {
		if(!Add(b[i].l-1,b[i].r,b[i].op)){
			ans = i-1;
			break;
		}
	}
	printf("%d\n",ans) ;
	return ;
}

int main(){
	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
	while(~scanf("%d",&N)){
		if(N==-1) break;
		read();
		sovle();
	}

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值