首页 > 其他分享 >【2-sat】P4171 [JSOI2010] 满汉全席

【2-sat】P4171 [JSOI2010] 满汉全席

时间:2024-08-15 17:07:56浏览次数:6  
标签:JSOI2010 int scc st dfn P4171 low include sat

P4171 [JSOI2010] 满汉全席 - 洛谷

大意:n个点 m个条件 形如m1,h32,满足n个条件

思路:2-sat 让m=0,h=1 ,然后转换为i m j h的建图,注意傻逼题目的数字可能是多位数 不能直接x[1]-'0'

#include <cstdio>
#include <stack>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#define ep emplace_back 

#define ios std::ios::sync_with_stdio(false);std::cin.tie(0); 
const int N = 1e5+9;

using namespace std;

int trans(char x){
    return x == 'm' ? 0 : 1;
}
//转换函数 把字符串的数字转换成数字
int num(string x){
    int ans=0;
    int len  = x.size()-1;
    for(int i=1 ; i<x.size();++i){
        int ch =x[i]-'0';
        ans+=ch*pow(10,len-1);
        --len;
    }
    return ans;
}
int n, m;
int head[N], idx = 0;
int time__;
int low[N], dfn[N];
struct node{
    int to, val, next;
} e[N<<2];
bool vis[N];
int scc[N], scc_cnt = 0;

void add(int u, int v, int val){
    e[idx] = {v, val, head[u]};
    head[u] = idx++;
}

void bd(){
    cin >> n >> m;
    for(int k = 1; k <= m; ++k){
        string ch1, ch2;
        cin >> ch1 >> ch2;
        int i = num(ch1);
        int j = num(ch2);
        int a = trans(ch1[0]);
        int b = trans(ch2[0]);
        //printf("%d %d\n",i,j);
        add(i + a * n, j + !b * n, 0);       
        add(j + b * n, i + !a * n, 0);
    }
}

stack<int> st;
void tarjan(int u){
    low[u] = dfn[u] = ++time__;
    st.push(u);
    vis[u] = 1;
    for(int i = head[u]; i != -1; i = e[i].next){
        int v = e[i].to;
        if(!dfn[v]){
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(vis[v]){
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(dfn[u] == low[u]){
        scc_cnt++;
        while(true){
            int v = st.top();
            st.pop();
            vis[v] = 0;
            scc[v] = scc_cnt;
            if(u == v) break;
        }
    }
}

void clear(){
    memset(head, -1, sizeof head);
    memset(scc, 0, sizeof scc);
    memset(vis, 0, sizeof vis);
    memset(low, 0, sizeof low);
    memset(dfn, 0, sizeof dfn);
    while(!st.empty()) st.pop();
    time__ = 0, idx = 0, scc_cnt = 0;
}

void solve(){
    clear();
    bd();
    
    for(int i = 1; i <= 2 * n; ++i)
        if(!dfn[i])
            tarjan(i);
    bool ok = 1;
    for(int i = 1; i <= n; ++i){
        if(scc[i] == scc[i + n]){
            ok = 0;
            break;
        }
    }
   /* */
    cout << (ok ? "GOOD" : "BAD") ;
    cout<< "\n";
}

int main(){
    ios;
    int T = 1;
    cin >> T;
    while(T--){
        solve();
    }
    return 0;
}

标签:JSOI2010,int,scc,st,dfn,P4171,low,include,sat
From: https://www.cnblogs.com/Phrink734/p/18361352

相关文章

  • D43 2-SAT+前缀优化 P6378 [PA2010] Riddle
    视频链接: P6378[PA2010]Riddle-洛谷|计算机科学教育新生态(luogu.com.cn)//2-SAT+前缀优化O(n+m)#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;#definex0(x)x//点#definex1(x)x+n//反点#definep0(x)x+2*n......
  • 240814-作物模型DSSAT4.8.2的安装过程
    1.DSSATV4.8.2的下载软件下载需要从DSSAT官网邮件申请,一周左右会反馈下载链接。下面的链接是我于2024年8月从官网申请的链接。https://get.dssat.net/dssat-download-v4-8/?sk=48082410753我下载好后上传到了百度网盘,下面的是百度网盘下载链接。通过百度网盘分享的文件:DSSA......
  • D42 2-SAT+二进制枚举 P3825 [NOI2017] 游戏
    视频链接: P3825[NOI2017]游戏-洛谷|计算机科学教育新生态(luogu.com.cn)//2-SAT+二进制枚举O(2^8*(n+m))#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;constintN=100005;inthead[N],to[N<<1],ne[N<<1],idx;......
  • [算法] 2-SAT简记
    真的是简记2-SAT$2-SAT$用于求解一个变量只有两种情况的适应性问题(就是有没有解以及输出一种方案);其实可以类比二分图最大匹配(但其实两者的差别还是很大的);算法流程对于每一个变量,我们都有两种情况,$true$和$false$;而题目中给我们的,是形如{$A=true/false$或......
  • 2-SAT 学习笔记
    2-SAT用于求解布尔方程组,其中每个方程最多含有两个变量,方程的形式为\((a∨b)=1\),即式子\(a\)为真或式子\(b\)为真。求解的方法是根据逻辑关系式建图,然后求强联通子图,每一个强联通子图的答案都是一样的。建图:这里以模版题为例:题意:给定若干个需要满足的条件,其形式为\(a,1......
  • 【13.PIE-Engine案例——加载Landsat8 Collection2 SR数据集】
    原始链接原始路径欢迎大家登录航天宏图官网查看本案例原始来源结果展示具体代码/***@File:Landsat8Collection2SR*@Time:2021/5/24*@Author:piesat*@Version:1.0*@Contact:400-890-0662*@License:(C)Copyright航......
  • 【15.PIE-Engine案例——加载Landsat 8 SR数据集】
    加载Landsat8SR数据集原始路径欢迎大家登录航天宏图官网查看本案例原始来源最终结果具体代码/***@File:Landsat8SRImages*@Time:2020/7/21*@Author:piesat*@Version:1.0*@Contact:400-890-0662*@License:(C)Copyr......
  • D40 2-SAT POJ3683 Priest John's Busiest Day
    视频链接:D402-SATPOJ3683PriestJohn'sBusiestDay_哔哩哔哩_bilibili   POJ3683--PriestJohn'sBusiestDay(poj.org)#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>usingnamespacestd;......
  • SciTech-Mathematics-Probability+Statistics-Causation vs. Correlation: From Corre
    https://www.statology.org/from-correlation-to-causation-deep-dive-into-data-interpretation/FromCorrelationtoCausation:DeepDiveintoDataInterpretationCorrelationandCausationarekeyconceptsindataanalysis.However,correlationdoesn'tme......
  • D39 2-SAT P3209 [HNOI2010] 平面图判定
    视频链接:D392-SATP3209[HNOI2010]平面图判定_哔哩哔哩_bilibili   图论(十三)——平面图和对偶图_图论(十三)——平面图和对偶图-CSDN博客P3209[HNOI2010]平面图判定-洛谷|计算机科学教育新生态(luogu.com.cn)#include<iostream>#include<cstring>#incl......