当前位置:网站首页>51nod2861 2-sat
51nod2861 2-sat
2022-08-08 23:29:00 【Lqingyyyy】
列如 h1 m2则 转换成 选了m1 必须选 m2即可
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N = 2010,M = 2010;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
char a,b;
int head[N],to[M],last[M],cnt = 1;
void add(int a,int b){
to[++cnt] = b;
last[cnt] = head[a];
head[a] = cnt;
}
int n,m;
int dfn[N],low[N],sta[N],top,scc_cnt,times;
int flag[N],id[N];
void tarjan(int x){
dfn[x] = low[x] = ++times;
sta[++top] = x,flag[x] = 1;
for(int i = head[x]; i != -1; i = last[i]){
int j = to[i];
if(!dfn[j]){
tarjan(j);
low[x] = min(low[x],low[j]);
}else if(flag[j]){
low[x] = min(low[x],dfn[j]);
}
}
if(dfn[x] == low[x]){
++scc_cnt;
int y;
do{
y = sta[top--];
id[y] = scc_cnt;
flag[y] = 0;
}while(y != x);
}
}
int main(){
int t;
cin >>t;
while(t--){
scanf("%d%d",&n,&m);
memset(head,-1,sizeof head);
for(int i = 2; i <= n * 2 + 2; i++){
id[i] = 0;
dfn[i] = 0,low[i] = 0;
}
times = 0;
scc_cnt = 0;
cnt = 1;
for(int i = 1; i <= m; i++){
int c,d;
getchar();
scanf("%c%d %c%d",&a,&c,&b,&d);
if(a == 'm' && b == 'm'){
add(c * 2,d * 2 + 1);
add(d * 2,c * 2 + 1);
}else if(a == 'h' && b== 'm'){
add(c * 2 + 1,d * 2 + 1);
add(d * 2,c * 2);
}else if(a == 'm' && b == 'h'){
add(c * 2,d * 2);
add(d * 2 + 1,c * 2 + 1);
}else{
add(c * 2 + 1,d * 2);
add(d * 2 + 1,c * 2);
}
}
for(int i = 2; i <= n * 2 + 1; i++){
if(!dfn[i]) tarjan(i);
}
bool flag = true;
for(int i = 1; i <= n; i++){
if(id[i * 2] == id[i * 2 + 1]){
flag = false;
puts("BAD");
break;
}
}
if(flag) puts("GOOD");
}
return 0;
}
边栏推荐
猜你喜欢
MySQL 原理与优化,Group By 优化 技巧
洛谷P4197 Peaks 线段树合并
(2022牛客多校五)H-Cutting Papers(签到)
(2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)
2022杭电多校五 C - Slipper (dijkstra+虚拟结点)
C语言中指针的介绍
想要精准营销,从学习搭建一套对的标签体系开始丨DTVision分析洞察篇
2022牛客多校六 B-Eezie and Pie (dfs)
如何搭建一套自己公司的知识共享平台
[Bug solution] ValueError: Object arrays cannot be loaded when allow_pickle=False
随机推荐
Virtual router redundancy protocol VRRP - double-machine hot backup
Kubernetes 资源核心原理
用工具实现 Mock API 的整个流程
(2022牛客多校三)J-Journey(dijkstra)
LeetCode:正则表达式匹配
2022牛客多校六 J-Number Game(简单推理)
WeChat applet error undefined Expecting 'STRING','NUMBER','NULL','TRUE','FALSE','{','[', got ]Solution
(2022杭电多校三)1002-Boss Rush(状压DP+二分)
2022牛客多校六 B-Eezie and Pie (dfs)
mysql 高级知识【order by 排序优化】
flutter 书写json解析类
设计分享|基于单片机的P0口驱动LED闪烁
MySQL indexes a field in a table
flutter 基本类写法
考证必看 | PMP扫盲贴+PMP材料
小程序banner图展示
Use Mongoose populate to implement multi-table associative storage and query, with complete code included
PHP 类函数和对象函数
(2022杭电多校六)1010-Planar graph(最小生成树)
最详树莓派4B装机流程及ifconfig不到wlan0的解决办法