【HDU 5961】 传递 bitset爆破 或 拓扑排序

43 篇文章 0 订阅
6 篇文章 0 订阅

传送门


思路:

法一:

一开始看到6s,直接用多源最短路看是否每两点距离都是1(若有路径),但是还是T飞了。后来学到bitset的方法,属实不错。这里讲一下自己的理解。
我们把这个联通的概念再深入理解一下。
如果1->2,表示1到2有一条边,那要满足题目限制条件的话,2连向的所有边,1都应该连过去!
如2->3, 2->4, 2->5 有边,那也一定要有1->3, 1->4 , 1->5 。
到此,我们就可以引出bitset了。用二进制串表示当前这个结点和其他点的连接情况,p[i][j]=1则代表i点到j有一条有向边。
那么接着上面的结论,我们可以外层遍历一遍i,内层遍历一遍j。如果p[i][j]本身为1,那就看看是否j连的所有边i都连着——如何做到?直接相与p[i]&p[j],看是否等于p[j]即可。这里的意思就是p[j]所有为1的位置,p[i]相应位也应该为1。看是否全部满足这个限制。
到此,我们就可以在O(n2)的时间复杂度内求出结果了。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<bitset>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int maxn = 2020;
const ll inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') ch = getchar();while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

bitset<maxn> p[maxn];
bitset<maxn> q[maxn];
ll n;

bool check(bitset<maxn> cur[])
{
    rep(i,1,n) rep(j,1,n)
    {
        if(i==j||!cur[i][j]||cur[j]==(cur[j]&cur[i])) continue;
        return false;
    }
    return true;
}

int main()
{
    int kase;
    scanf("%d",&kase);
    while(kase--)
    {
        n = read();
        rep(i,1,n) q[i].reset(), p[i].reset();
        rep(i,1,n)
        {
            string s;
            cin>>s;
            for(int j=0; j<s.size(); j++)
            {
                if(s[j]=='P') p[i][j+1] = 1;
                else if(s[j] == 'Q') q[i][j+1] = 1;
            }
        }
        int flag = check(p)&&check(q);
        puts(flag?"T":"N");
    }
    return 0;
}

法二(拓扑排序):

思路:有个引理就是。
在题意描述的P图和Q图的条件下,若P∪Q和P∪Q’都不存在环即满足题意。其中Q’为Q的反向图。
那么只需要对这两个合并后的图跑一个拓扑排序即可。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<bitset>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int maxn = 2020;
const ll inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline int read(){ int x = 0;char ch = getchar();while(ch>'9'||ch<'0') ch = getchar();while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

const int V = 2020, E = 5e6;
int head1[V], pnt[E], nxt[E], head2[V], e = 1;
int in1[V],vis[V], in2[V];
vector<int> ans;
int n;

 void addedge(int x, int y, int head[])
{
    pnt[e] = y;
    nxt[e] = head[x];
    head[x] = e++;
}

void Topsort(int head[], int in[])
{
    ans.clear();
    rep(i,1,n) vis[i] = 0;
    queue<int> q;
    rep(i,1,n) if(in[i]==0) q.push(i), vis[i] = 1;
    while(!q.empty())
    {
        int cur = q.front(); q.pop();
        if(in[cur] == 0) ans.pb(cur);
        for(int i = head[cur]; i ; i = nxt[i])
        {
            ll v = pnt[i];    in[v] --;
            if(!vis[v]&&in[v]==0)
            {
                q.push(v);
                vis[v] = 1;
            }
        }
    }
}

bool check(int head[], int in[])
{
    Topsort(head,in);
    return ans.size() == n;
}

int main()
{
    //FAST;
   // cin.tie(0), cout.tie(0);
    int kase;
    scanf("%d",&kase);
    while(kase--)
    {
        n = read(); e = 1;
        rep(i,1,n) in1[i] = in2[i] = head1[i] = head2[i] = 0;
        rep(i,1,n)
        {
            char s[2020];
            gets(s);
            for(int j=0; j<n; j++)
            {
                if(s[j]=='P')
                {
                    addedge(i,j+1,head1);
                    addedge(i,j+1,head2);
                    in1[j+1]++;
                    in2[j+1]++;
                }
                else if(s[j] == 'Q')
                {
                    addedge(i,j+1,head1);
                    addedge(j+1,i,head2);
                    in1[j+1]++;
                    in2[i]++;
                }
            }
        }
        int flag = check(head1, in1)&&check(head2, in2);
        puts(flag?"T":"N");
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值