P6378 [PA2010] Riddle 2-sat + 前缀和优化建图

传送门

文章目录

题意:

给你 n n n个点 m m m调变的无向图被分成 k k k个部分,每个部分包含若干点,请选择一些关键点,使得每个部分恰好有一个关键点,且每条边至少有一个是关键点。

1 ≤ k , w ≤ n ≤ 1 e 6 1\le k,w\le n\le 1e6 1k,wn1e6

思路:

首先每条边至少一个是关键点,这个是 2 − s a t 2-sat 2sat的套路建边了,假设这个边是 ( a , b ) (a,b) (a,b),那么应该 a ′ − > b , b ′ − > a a'->b,b'->a a>b,b>a

其次每个部分恰好一个关键点,这个看起来并不是很好建图,因为暴力建图的话是 n 2 n^2 n2级别的边,显然不行。我们对于这种区间的建边,不是线段树优化就是前缀和优化,这里使用前缀和优化。

p r e i , j pre_{i,j} prei,j代表第 i i i个部分的前 j j j个点是否存在关键点,以下简称为 p r e j pre_j prej

根据这个来限制一下每个部分,写出如下限制:
( 1 ) (1) (1) a j − > p r e j , p r e j ′ − > a j ′ a_j->pre_j,pre_j'->a_j' aj>prej,prej>aj

( 2 ) (2) (2) p r e j − 1 − > a j ′ , a j − > p r e j − 1 ′ pre_{j-1}->a_j',a_j->pre_{j-1}' prej1>aj,aj>prej1

( 3 ) (3) (3) p r e j − 1 − > p r e j , p r e j ′ − > p r e j − 1 ′ pre_{j-1}->pre_j,pre_j'->pre_{j-1}' prej1>prej,prej>prej1

不难发现这些条件已经足够了,直接跑个 2 − s a t 2-sat 2sat即可。

// Problem: F. Radio Stations
// Contest: Codeforces - Codeforces Round #585 (Div. 2)
// URL: https://codeforces.com/contest/1215/problem/F
// Memory Limit: 256 MB
// Time Limit: 7000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<assert.h>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=4001010,M=N*20,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,m,k;
int e[M],ne[M],h[N],idx;
int dfn[N],low[N],id[N],tot,cnt;
int stk[N],top;
bool in[N];

void add(int a,int b)
{
    e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

void tarjan(int u)
{
    dfn[u]=low[u]=++tot;
    stk[++top]=u; in[u]=true;
    for(int i=h[u];~i;i=ne[i])
    {
        int ver=e[i];
        if(!dfn[ver])
        {
            tarjan(ver);
            low[u]=min(low[u],low[ver]);
        }
        else if(in[ver]) low[u]=min(low[u],dfn[ver]);
    }
    if(dfn[u]==low[u])
    {
        int y; ++cnt;
        do
        {
            y=stk[top--];
            in[y]=false; id[y]=cnt;
        }while(y!=u);
    }
}

int yes(int x) { return x<<1; }

int no(int x) { return x<<1|1; }

void link(int x,int y) {
	add(x,y); 
	add(y^1,x^1);
}

bool check()
{
    for(int i=0;i<=n+n;i++) if(id[yes(i)]==id[no(i)]) return false;
    return true;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

    scanf("%d%d%d",&n,&m,&k);
    
    idx=0;
    memset(h,-1,sizeof(h));
    
    for(int i=1;i<=m;i++) {
    	int a,b; scanf("%d%d",&a,&b);
    	a--; b--;
    	link(no(a),yes(b));
    }
    
    for(int i=1,st=n;i<=k;i++) {
    	int cnt; scanf("%d",&cnt);
    	for(int j=st;j<=st+cnt-1;j++) {
    		int x; scanf("%d",&x); 
    		x--;
    		link(yes(x),yes(j)); 
    		if(j!=st) link(yes(j-1),no(x));    	
    		if(j!=st+cnt-1) link(yes(j),yes(j+1));	
    	}
    	st+=cnt;
    }
    
    for(int i=0;i<=no(n+n);i++) if(!dfn[i]) tarjan(i);
    if(check()) {
    	puts("TAK");
    } else puts("NIE");

	return 0;
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值