BZOJ1194: [HNOI2006]潘多拉的盒子

138 篇文章 0 订阅
14 篇文章 0 订阅

先判互相的包含关系,因为n<=50,直接bfs,(x,y)表示在A上走到x,B上走到y,若走到的x是输出源而y不是则B不包含y
判完包含关系后缩环拓扑序dp

code:

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

inline void up(int &x,const int &y){if(x<y)x=y;}
inline void down(int &x,const int &y){if(x>y)x=y;}
const int maxn = 55;

int n;

int N[maxn],c[maxn][maxn];
int pi[maxn][maxn][2];
bool e[maxn][maxn];

struct node
{
    int x,y;
    node(){}
    node(const int _x,const int _y){x=_x;y=_y;}
};
bool v[maxn][maxn];

queue<node>q;
// a < b
bool bfs(const int a,const int b)
{
    for(int i=0;i<N[a];i++)
        for(int j=0;j<N[b];j++) v[i][j]=false;
    while(!q.empty()) q.pop();

    q.push(node(0,0));
    while(!q.empty())
    {
        const node x=q.front(); q.pop();
        int tx,ty;

        tx=pi[a][x.x][0],ty=pi[b][x.y][0];
        if(c[a][tx]&&!c[b][ty]) return false;
        if(!v[tx][ty]) v[tx][ty]=true,q.push(node(tx,ty));

        tx=pi[a][x.x][1],ty=pi[b][x.y][1];
        if(c[a][tx]&&!c[b][ty]) return false;
        if(!v[tx][ty]) v[tx][ty]=true,q.push(node(tx,ty));
    }
    return true;
}

struct edge
{
    int y,nex;
    edge(){}
    edge(const int _y,const int _nex){y=_y;nex=_nex;}
}a[maxn*maxn<<1]; int len,fir[maxn];
inline void ins(const int x,const int y){a[++len]=edge(y,fir[x]);fir[x]=len;}

int id,dfn[maxn],low[maxn];
int cnt,bel[maxn],s[maxn];
int t[maxn],tp;
bool ev[maxn];
void tarjan(const int x)
{
    dfn[x]=low[x]=++id;
    t[++tp]=x;
    ev[x]=true;
    for(int k=fir[x];k;k=a[k].nex)
    {
        const int y=a[k].y;
        if(!dfn[y]) tarjan(y),down(low[x],low[y]);
        else if(ev[y]) down(low[x],dfn[y]);
    }
    if(low[x]==dfn[x])
    {
        int la=0; ++cnt;
        while(la!=x)
        {
            ev[la=t[tp--]]=false;
            bel[la]=cnt;
            s[cnt]++;
        }
    }
}
int d[maxn];
void build()
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)if(e[i][j])
            ins(i,j);
    tp=0;id=0;
    for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);

    len=0; memset(fir,0,sizeof fir);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)if(e[i][j]&&bel[i]!=bel[j])
            ins(bel[i],bel[j]),d[bel[j]]++;
}
int f[maxn];
queue<int>Q;
int DP()
{
    for(int i=1;i<=cnt;i++) if(!d[i]) Q.push(i);
    while(!Q.empty())
    {
        const int x=Q.front(); Q.pop();
        f[x]+=s[x];
        for(int k=fir[x];k;k=a[k].nex)
        {
            const int y=a[k].y;
            d[y]--; if(!d[y]) Q.push(y);
            up(f[y],f[x]);
        }
    }
    int ans=0;
    for(int i=1;i<=cnt;i++) up(ans,f[i]);
    return ans;
}
int solve()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)if(i!=j)
            if(bfs(i,j)) e[i][j]=true;
    }
    build();
    return DP();
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int k; scanf("%d%d",&N[i],&k);
        while(k--)
        {
            int x; scanf("%d",&x);
            c[i][x]=1;
        }
        for(int j=0;j<N[i];j++) scanf("%d%d",&pi[i][j][0],&pi[i][j][1]);
    }
    printf("%d\n",solve());

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值