POJ - 1733 离散化 并查集
2024-08-27 02:06:28
题意:求问数列区间奇偶信息正确与否
和上一题一样(甚至弱化),加个离散就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;
}
最新文章
- HDU5977 Garden of Eden(树的点分治)
- Color Transfer between Images code实现
- css定位 与position
- node.js建立简单应用
- 笔记二、本地git命令
- 能在你离开时照顾你的宠物的3D打印机器人
- 动态添加子视图 UIView 的正确方法
- Python入门-----Windows安装
- GNU/Linux超级本ZaReason Ultralap 440体验
- iOS 关于退出键盘两种方法和避免遮挡
- TP手册学习第四内置天
- Java中三种比较常见的数组排序
- class类初始化之后调用赋值问题记录
- jQuery 替换元素
- POJ-1753 Flip Game (BFS+状态压缩)
- EMNLP 2018 | 用强化学习做神经机器翻译:中山大学&;MSRA填补多项空白
- cas单点登陆系统-建立单点登陆系统的应用
- NMS—卷积神经网络
- linux cat显示若干行
- 2018-2019-20172329 《Java软件结构与数据结构》第三周学习总结