[CF1239D]Catowice City

题目

传送门

题解

巧妙地将\(2-sat\) 与图的连通性结合起来的题。

考虑题目的特性:

  • 对于一对组合(指一个人与一只猫),我们只能选择二者之一;
  • 对于组 \(i\) 的人,如果他认识 \(j\) 组的猫,那么 \(i,j\) 两组只能同时选人(由于猫和人地位相同,其他情况也是一样的);

询问是否有一种分组情况满足所有条件,这是否有点像 \(2-sat\)

由于第一个特性,我们可以将人、猫缩成一个点。

考虑建图,如果 \(i\) 组的人认识 \(j\) 组的猫,那么连有向边 \((i,j)\),表示如果 \(i\) 组选人,那么 \(j\) 组一定选人。

对于所有条件这样处理之后的图如何进行求解?直接跑 \(2-sat\)

其实有更好的办法,在这张图上,对于一个 \(scc\),由于边的定义,我们可知——如果这个 \(scc\) 中有一个人选择人,那么其他都必须选择人。

那么我们对于这个图跑一边 \(tarjan\) 将点缩起来,如果只有一个 \(scc\) 那么一定无解,否则考虑如何构造答案。

由边的定义,如果有强联通点 \(u\) 连向 \(v\),则表示若 \(u\) 选择人,那么 \(v\) 也一定选择人,所有点都有这样的限制,除了没有入点的 \(scc\),这个(些)\(scc\) 可以自由进行选择是猫还是人,其他的都会被自己的入点限制。

考虑简单地构图——我们只需将其中一个无入点的 \(scc\) 钦定选猫,其他强制选人即可。

考虑 \(tarjan\) 对于 \(scc\) 编号的特性,我们可以保证编号为 \(scc\_cnt\)\(scc\) 一定是无入点的,我们只需这样快速得出答案。

需要注意的是,这道题多组数据需进行清空,但是不要使用 memset,因为这个东西会把所有空间刷一遍,在这道题中慢得一批我们并不需要将所有空间清空,只需清理掉需要使用的部分即可。

代码

#include<cstdio>
#include<cstring>

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define erep(i,u) for(signed i=tail[u],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
typedef long long LL;
// typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef unsigned uint;
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void read(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T read(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
template<class T>inline T Max(const T x,const T y){return x<y?y:x;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int maxn=1e6;
const int maxm=1e6;

struct edge{int to,nxt;}e[maxm+5];
int tail[maxn+5],ecnt;
inline void add_edge(const int u,const int v){
    e[++ecnt]=edge{v,tail[u]};tail[u]=ecnt;
}

int n,m;

int stk[maxn+5],st;bool inst[maxn+5];
int dfn[maxn+5],low[maxn+5],idx;
int scc,bel[maxn+5];

inline void tarjan(const int u,const int pre){
    inst[stk[++st]=u]=true;
    dfn[u]=low[u]=++idx;
    erep(i,u)if(!dfn[v])tarjan(v,u),low[u]=Min(low[u],low[v]);
        else if(inst[v])low[u]=Min(low[u],dfn[v]);
    if(dfn[u]==low[u]){
        ++scc;int v;do{
            inst[v=stk[st--]]=false;
            bel[v]=scc;
        }while(v^u);
    }
}

inline void Writans(){
    int ans=n;
    rep(i,1,n)if(bel[i]==scc)--ans;
    writc(ans,' '),writc(n-ans,'\n');
    rep(i,1,n)if(bel[i]!=scc)writc(i,' ');Endl;
    rep(i,1,n)if(bel[i]==scc)writc(i,' ');Endl;
}

inline void Init(){
    n=read(1),m=read(1);
    ecnt=idx=css=0;
    rep(i,1,n)tail[i]=dfn[i]=0;
    int u,v;
    rep(i,1,m){
        u=read(1),v=read(1);
        if(u^v)add_edge(u,v);
    }
}

signed main(){
    rep(i,1,read(1)){
        Init();
        rep(i,1,n)if(!dfn[i])tarjan(i,0);
        // printf("css == %d\n",css);
        if(scc<=1)puts("No");
        else puts("Yes"),Writans();
    }
    return 0;
}
posted @ 2020-08-20 22:08  Arextre  阅读(131)  评论(0编辑  收藏  举报