zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",ques);
    64     return 0;
    65 }
    View Code

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

    “这些年我一直提醒自己一件事情,千万不要自己感动自己。大部分人看似的努力,不过是愚蠢导致的。什么熬夜看书到天亮,连续几天只睡几小时,多久没放假了,如果这些东西也值得夸耀,那么富士康流水线上任何一个人都比你努力多了。人难免天生有自怜的情绪,唯有时刻保持清醒,才能看清真正的价值在哪里。”
  • 相关阅读:
    wpf学习笔记StackPanel
    wpf学习笔记DockPanel
    wpf学习笔记Viewbox
    C#.NET 中的类型转换
    超简单U盘PE启动完全攻略(U盘上仅四个文件)
    Web 应用的 UML 建模与 .NET 框架开发
    100多个很有用的JavaScript函数以及基础写法大集合
    Asp.net(C#)显示所有缓存 清除所有缓存
    grub引导U盘(集成常用工具/深山红叶PE工具箱V30/完美者U盘维护系统V8.1)
    GRUB启动命令详解
  • 原文地址:https://www.cnblogs.com/Blogggggg/p/7217052.html
Copyright © 2011-2022 走看看