首页 > 其他分享 >【2-sat】P5782 [POI2001] 和平委员会

【2-sat】P5782 [POI2001] 和平委员会

时间:2024-08-15 17:39:59浏览次数:8  
标签:P5782 int POI2001 scc st dfn low include sat

P5782 [POI2001] 和平委员会

大意:n个集合 每个集合有两个点i,i+1 一共2n个点 每个集合选一个点到另一个空集 S 里面 有m个约束条件 i和j不能在一起 求可行的集合S

思路:2-sat 对i j而言 建图(i,j邻居) 和(j,i的邻居),邻居就是他们所属的集合的另一个点 然后判断同同一个集合的两个点是否同时在S里面 输出那个scc[i]小的

#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 = 4e5+9;

using namespace std;

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++;
}
int neighbor(int x){
    if(x%2==0) return x-1;
    return x+1;
}
void bd(){
    memset(head, -1, sizeof head);
    cin >> n >> m;
    for(int k = 1; k <= m; ++k){
        int i,j;
        cin>>i>>j;
        //i和j的邻居
        //j和i的邻居
        add(i , neighbor(j) , 0);       
        add(j , neighbor(i) , 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 solve(){
    bd();
    for(int i = 1; i <= 2 * n; ++i)
        if(!dfn[i])
            tarjan(i);
    bool ok = 1;
    for(int i = 1; i <= 2*n; i+=2){
        if(scc[i] == scc[neighbor(i)]){
            ok = 0;
            break;
        }
    }
    if(ok){
        for(int i=1 ; i<=2*n ; i+=2){
            cout<<(scc[i] < scc[i+1] ? i:i+1);
            cout<<"\n";
        }
    }
    else cout<<"NIE";
}

int main(){
    ios;
    solve();
    return 0;
}

标签:P5782,int,POI2001,scc,st,dfn,low,include,sat
From: https://www.cnblogs.com/Phrink734/p/18361440

相关文章

  • 【2-sat】P4171 [JSOI2010] 满汉全席
    P4171[JSOI2010]满汉全席-洛谷大意:n个点m个条件形如m1,h32,满足n个条件思路:2-sat让m=0,h=1,然后转换为imjh的建图,注意傻逼题目的数字可能是多位数不能直接x[1]-'0'#include<cstdio>#include<stack>#include<iostream>#include<cstring>#include<cma......
  • 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......