首页 > 编程语言 >P1955 [NOI2015] 程序自动分析

P1955 [NOI2015] 程序自动分析

时间:2023-01-01 18:56:19浏览次数:79  
标签:P1955 hash int ++ tot NOI2015 自动 return find

[NOI2015] 程序自动分析

题目简述

输入的第一行包含一个正整数 \(t\),表示需要判定的问题个数。注意这些问题之间是相互独立的。

对于每个问题,包含若干行:

第一行包含一个正整数 \(n\),表示该问题中需要被满足的约束条件个数。接下来 \(n\) 行,每行包括三个整数 \(i,j,e\),描述一个相等/不等的约束条件,相邻整数之间用单个空格隔开。若 \(e=1\),则该约束条件为 \(x_i=x_j\)。若\(e=0\),则该约束条件为 \(x_i\neq x_j\)。


思路

题目本身不难,但可以从中学习一点离散化以及哈希表相关的知识(还是不戳滴)
哈希表思路详见

代码

方法一(离散化)

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
struct Node {
  int x,y,op;
}s[N];
int fa[N*2],kdl[N*2];
bool cmp(Node x,Node y){
  return x.op>y.op;
}
inline void in(int &x){
  x=0;
  int f=1;
  char c=getchar();
  while(c<'0'||c>'9'){
    if(c=='-')f=-1;
    c=getchar();
  } 
  while(c>='0'&&c<='9'){
    x=(x<<1)+(x<<3)+c-'0';
    c=getchar();
  }
  x*=f;
}
inline int get_fa(int x){
  return (x==fa[x])?x:(fa[x]=get_fa(fa[x]));
}
int main(){
  freopen("1955.in","r",stdin);
  freopen("1955.out","w",stdout);
  int t;
  in(t);
  while(t--){
    int n;
    bool ok=1;
    in(n);
    int cnt=0;
    for(int i=1;i<=n;i++){
      in(s[i].x);in(s[i].y);in(s[i].op);
      kdl[++cnt]=s[i].x;
      kdl[++cnt]=s[i].y;
    }
    sort(kdl+1,kdl+1+cnt);
    sort(s+1,s+1+n,cmp);
    int tot=unique(kdl+1,kdl+1+cnt)-kdl-1;
    for(int i=1;i<=n;i++){
      s[i].x=lower_bound(kdl+1,kdl+1+tot,s[i].x)-kdl;
      s[i].y=lower_bound(kdl+1,kdl+1+tot,s[i].y)-kdl;
      fa[s[i].x]=s[i].x,fa[s[i].y]=s[i].y;
    }
    for(int i=1;i<=n;i++){
      switch(s[i].op){
        case 1:{
          int fx=get_fa(s[i].x),fy=get_fa(s[i].y);
          fa[fx]=fy;
          break;
        }
        case 0:{
          int fx=get_fa(s[i].x),fy=get_fa(s[i].y);
          if(fx==fy){
            ok=0;
            puts("NO");
          }
          break;
        }
      }
      if(!ok)break; 
    }
    if(ok)
      puts("YES");
  }
  return 0;
}

方法二(哈希表)(代码并非笔者所写qwq)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector> 
#define Max 100010
#define mod 99991 

using namespace std;

int fa[2*Max],d[2*Max];
struct node{
    int real,map;
}un[Max];
vector <node> hash[Max]; 
int tot,cnt;
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int ask(int i,int j,bool k)
{
    if(find(i)==find(j)) return 1;
    else if(k){
        fa[find(j)] = find(i);
        return 0;
    }
    return 0;
}
int map(int i,int j,bool k)
{
    int x,y,ok=1;
    int a = i % mod, b = j % mod;
    if(!hash[a].empty()){
        for(int l = 0; l < hash[a].size(); l++)
            if(i==hash[a][l].real) x = hash[a][l].map,ok = 0;
        if(ok) hash[a].push_back((node){i,++tot}),x = tot;
    }
    else hash[a].push_back((node){i,++tot}),x = tot;
    ok = 1;
    if(!hash[b].empty()){
        for(int l = 0; l < hash[b].size(); l++)
            if(j==hash[b][l].real) y = hash[b][l].map,ok = 0;
        if(ok) hash[b].push_back((node){j,++tot}),y = tot;
    }
    else hash[b].push_back((node){j,++tot}),y = tot;
    return ask(x,y,k); 
}
int main()
{
    int n;
    cin >> n;

    for(int i = 1; i <= n; i++)
    {
        for(int i = 1; i <= 200010; i++)
            fa[i] = i;
        for(int i = 0; i < 100000; i++)
            hash[i].clear();
        memset(d,0,sizeof(d));
        memset(un,0,sizeof(un));
        //以上每步的初始化不能丢
        cnt = tot = 0;
        int num,now = 1;
        cin >> num;
        for(int i = 1; i <= num; i++)
        {
            int x,y,z;
            cin >> x >> y >> z;
            if(z) map(x,y,1);
            else un[++cnt].real = x, un[cnt].map = y;   
        }
        for(int i = 1; i <= cnt; i++)
            if(map(un[i].real,un[i].map,0)){
                now = 0;
                cout << "NO" <<endl;
                break;
            }
        if(now) cout << "YES" << endl;
    }
    return 0;
}

标签:P1955,hash,int,++,tot,NOI2015,自动,return,find
From: https://www.cnblogs.com/fleabag/p/17018407.html

相关文章

  • spring boot——spring boot的基本配置——关闭某个特定的自动配置
                  ===========================================================================             ......
  • MySQL Workbench 8.0 关键字自动填充大写设置
    MySQLWorkbench8.0安装后关键字自动填充默认为小写,为了区分关键字,增加可读性,有必要设置为大写,按TAB键完成自动填充。 ......
  • 自动填充产品
    1、安装selenium2、编写程序,注意延时time.sleep3、部署到公网:https://www.cnblogs.com/clayyjh/p/17016145.html4、chromedriver:https://www.cnblogs.com/clayyjh/p/170......
  • C++用finally函数实现当前函数运行结束自动执行一段代码
    我们的需求可能有这样的需求,fun(){    xx;    xx;    xx;    //希望在这里能自动执行一段设定好的代码,实现一些自动清除啥啥啥的操作}核心......
  • 自动化测试训练营——环境搭建(jdk+mysql+maven+IDEA)
    前言虽然已经有了写自动化脚本的经验,但是从来没有系统的学习过,在工作过程中也一直是懵懵懂懂,因为公司一切东西都已经是流程化了,现成的框架和技术,我不懂原理,也没有人问。在......
  • 自动化中可执行bat文件
    自动化中批处理文件,一键运行代码#创建一个run.bat​#run.bat文件里面编写cd./testCasepytest-s--alluredir../outFiles/report/tmp--clean-alluredirallures......
  • HPA 自动水平伸缩 POD
    ​前戏我们知道,初始Pod的数量是可以设置的,同时业务也分流量高峰和低峰,那么怎么即能不过多的占用K8s的资源,又能在服务高峰时自动扩容pod的数量呢,在K8s上的答案是HorizontalP......
  • 后缀自动机
    时间跨度2weeks,我到现在才刚刚看懂一个构造,果然是过于菜了……时间原因,写得非常潦草而缺少细节,题也还没刷,所以建议点开大佬的链接进行学习qwq啊就各种鹤(%%%KesdiaelKen......
  • Nginx 代理webSocket时60s自动断开, 保持长连接
    利用nginx代理websocket的时候,发现客户端和服务器握手成功后,如果在60s时间内没有数据交互,连接就会自动断开,如下图:为了保持长连接,可以采取来两种方式.1.nginx.conf文件里locati......
  • Eolink神技之五、API自动化——定时任务
    Eolink神技之五、API自动化——定时任务目录​​Eolink神技之五、API自动化——定时任务​​​​前言​​​​演示步骤​​​​一、项目创建​​​​1.1选择API自动化测试功......