首页 > 代码库 > POJ1733

POJ1733

题目链接:https://vjudge.net/problem/POJ-1733

解题思路:并查集+离散化

AC代码:

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <map>
 6 #include <cstring>
 7 #include <cmath>
 8 using namespace std;
 9 const int maxn=15000;
10 struct query{
11     int l,r;
12     int oe;
13 }q[maxn];
14 map<int,int> lists;
15 int v[maxn],fa[maxn];
16 int finds(int t){
17     if(fa[t]==-1)
18         return t;
19     int tmp=finds(fa[t]);
20     v[t]^=v[fa[t]];
21     return fa[t]=tmp;
22 }
23 int main(){
24 //    freopen("in.txt","r",stdin);
25 //    freopen("out.txt","w",stdout);
26     int vals[maxn<<1];
27     char ooe[10];
28     int ques,len;
29     memset(fa,-1,sizeof(fa));
30     memset(v,0,sizeof(v));
31     scanf("%d",&len);
32     scanf("%d",&ques);
33     int j=1;
34     for(int i=1;i<=ques;i++){
35         scanf("%d%d%s",&q[i].l,&q[i].r,ooe);
36         q[i].l-=1;
37         if(ooe[0]==e)     q[i].oe=0;
38         else    q[i].oe=1;
39         vals[j]=q[i].l;     j++;
40         vals[j]=q[i].r;     j++;
41     }
42     sort(vals+1,vals+j);
43     int m=unique(vals+1,vals+j)-vals-1;
44     for(int i=1;i<=m;i++){
45         lists[vals[i]]=i;
46     }
47     int ending=0;
48     for(int i=1;i<=ques;i++){
49         int t1=finds(lists[q[i].l]),t2=finds(lists[q[i].r]);
50         int vl=v[lists[q[i].l]],vr=v[lists[q[i].r]];
51         if(t1==t2){
52             if(vr^vl!=q[i].oe){
53                 printf("%d\n",i-1);
54                 ending=1;
55                 break;
56             }
57         }
58         else{
59             fa[t2]=t1;
60             v[t2]=q[i].oe^vl^vr;
61         }
62     }
63     if(!ending) printf("%d\n",ques);
64     return 0;
65 }
View Code

 

 

额,先这样吧,晚上或者明天再来详谈。

POJ1733