[POJ3678] Katu Puzzle | 2-SAT 入门

简介: DescriptionKatu Puzzle is presented as a directed graph G ( V , E ) with each edge e ( a , b ) labeled by a boolean operator op (oneofAND,OR,XOR) and an integer c ( 0 ≤ c ≤ 1 ) . One Katu is solvable if one can find each vertex Vi a value X i ( 0 ≤ X i ≤ 1 )

Description


Katu Puzzle is presented as a directed graph G ( V , E )  with each edge e ( a , b )  labeled by a boolean operator op (oneofAND,OR,XOR) and an integer c ( 0 ≤ c ≤ 1 ) . One Katu is solvable if one can find each vertex  Vi a value X i ( 0 ≤ X i ≤ 1 ) such that for each edge e(a, b) labeled by op and c, the following formula holds:


Xa op Xb = c


The calculating rules are:


AND 0 1

0 0 0

1 0 1

OR 0 1

0 0 1

1 1 1

XOR 0 1

0 0 1

1 1 0

Given a Katu Puzzle, your task is to determine whether it is solvable.


Input


The first line contains two integers N ( 1 ≤ N ≤ 1000 )and M,( 0 ≤ M ≤ 1 , 000 , 000 ) (0 ≤ M ≤ 1,000,000) indicating the number of vertices and edges.

The following M lines contain three integers a ( 0 ≤ a < N ) , b ( 0 ≤ b < N ), c and an operator op each, describing the edges.


Output


Output a line containing “YES” or “NO”.


Sample Input


4 4
0 1 1 AND
1 2 1 OR
3 2 0 AND
3 0 0 XOR


Sample Output


YES


Hint


X0 = 1, X1 = 1, X2 = 0, X3 = 1.

int n, m;
struct node {
  int to,nex;
} e[maxm << 2];
int cnt = 0, head[maxn << 1];
bool inStk[maxn << 1];
int low[maxn << 1], dfn[maxn << 1],dfc, pos[maxn << 1];
void init() {
  dfc = cnt = 0;
  for(int i=0; i <=2* n; i++) {
    head[i] = -1;
    low[i] = dfn[i] = 0;
  }
}
stack<int> stk;
void add(int u,int v) {
  e[cnt].to = v;
  e[cnt].nex = head[u];
  head[u] = cnt ++;
}
int cntSCC;
void Tarjan(int u) {
//  cout << u << endl;
  dfn[u] = low[u] = ++ dfc;
  inStk[u] = 1;
  stk.push(u);
  for(int i=head[u]; ~i; i=e[i].nex) {
    itn to = e[i].to;
    if(!dfn[to]) {
      Tarjan(to);
      low[u] = min(low[u],low[to]);
    } else if(inStk[to] && !pos[to]) {
      low[u] = min(low[u],dfn[to]);
    }
  }
  if(low[u] == dfn[u]) {
    int tp;
    ++ cntSCC;
    do {
      tp = stk.top();
      stk.pop();
      inStk[tp] = 0;
      pos[tp] = cntSCC;
    } while(tp != u);
  }
}
int a,b,c;
string op;
int main() {
  n=read,m=read;
  init();
  for(int i=1; i<=m; i++) {
    a = read,b = read,c = read;
    cin >> op;
    int u=a * 2,u1=2 * a+1;
    int v=b * 2,v1=2 * b+1;
    if(op == "AND") {
      if(c == 1) {/// u,v
        add(u1,u);
        add(v1,v);
      } else {
//        add(u,u1);
        add(u,v1);
//        add(v,u1);
        add(v,u1);
      }
    } else if(op == "OR") {
      if(c == 1) {
//        add(u1,u);
        add(u1,v);
//        add(v1,v);
        add(v1,u);
      } else {
        add(u,u1);
        add(v,v1);
      }
    } else if(op == "XOR") {
      if(c == 1) {
        add(u,v1);
        add(v,u1);
        add(u1,v);
        add(v1,u);
      } else {
        add(u,v);
        add(v,u);
        add(u1,v1);
        add(v1,u1);
      }
    }
  }
  for(int i=0; i <= 2*n; i++) {
    if(!dfn[i]) Tarjan(i);
  }
  for(int i=0; i <= 2*n; i++) {
    if(pos[i] == pos[i+1]) {
      puts("NO");
      return 0;
    }
    i ++;
  }
  puts("YES");
  return 0;
}


目录
相关文章
|
SQL 监控 Java
分布式任务调度之xxl-job
分布式任务调度之xxl-job
|
监控 定位技术
ETF套利及交易者如何进行套利的
ETF套利及交易者如何进行套利的
228 0
|
SQL 前端开发 Java
【毕业设计之python系列】基于java的幸福婚庆策划网管理系统
【毕业设计之python系列】基于java的幸福婚庆策划网管理系统
356 0
|
机器学习/深度学习 并行计算 PyTorch
CUDA和显卡驱动以及pytorch版本的对应关系
CUDA和显卡驱动以及pytorch版本的对应关系
8319 0
|
4天前
|
弹性计算 运维 搜索推荐
三翼鸟携手阿里云ECS g9i:智慧家庭场景的效能革命与未来生活新范式
三翼鸟是海尔智家旗下全球首个智慧家庭场景品牌,致力于提供覆盖衣、食、住、娱的一站式全场景解决方案。截至2025年,服务近1亿家庭,连接设备超5000万台。面对高并发、低延迟与稳定性挑战,全面升级为阿里云ECS g9i实例,实现连接能力提升40%、故障率下降90%、响应速度提升至120ms以内,成本降低20%,推动智慧家庭体验全面跃迁。
|
4天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
380 93
|
5天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
5天前
|
SQL 人工智能 自然语言处理
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
随着生成式AI的普及,Geo优化(Generative Engine Optimization)已成为企业获客的新战场。然而,缺乏标准化流程(Geo优化sop)导致优化效果参差不齐。本文将深入探讨Geo专家于磊老师提出的“人性化Geo”优化体系,并展示Geo优化sop标准化如何帮助企业实现获客效率提升46%的惊人效果,为企业在AI时代构建稳定的流量护城河。
389 156
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%