CF1056H

· · 题解

考虑如何暴力判定:

枚举一个串的两个字符 A\to C\to...\to B,如果之前出现过 A\to D\to...\to B 的路径,检查 C 是否等于 D,如果不等于就输出 Human 。

可以枚举 B 然后枚举 B 结尾的路径一起处理。

(设 S=\sum len)

直接用上面的暴力的话,发现串长度小的时候可以接受,如果串长 \le \sqrt{S} 的话复杂度是 S\sqrt{S}。

考虑根号分治,小串对小串的可以暴力算。剩下还有大串对小串,大串对大串。

大串的个数只有根号个,考虑可不可以 O(S) 判断与其他所有串。记录 pos_i 表示 i 在大串中的位置,对于其他串都从后向前扫,记录后面字母的 \max pos,如果现在这个字母的 pos<mx 则判断一下。

复杂度就是 S\sqrt S .

#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
using namespace std;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 300005
#define inf 0x3f3f3f3f

int n,m,B;
vi a[maxn];
bool cmp(vi a,vi b){return a.size()>b.size();}

vector<pii>v[maxn];
int pos[maxn];
int vis[maxn];

void work()
{   
    n=read(),m=read();
    For(i,1,n) v[i].clear();
    For(i,1,m){
        int sz=read(); a[i].resize(sz);
        For(j,0,sz-1) a[i][j]=read();
        if(a[i].size()<=B){
            For(k,1,sz-1)
                For(j,0,k-1)
                    v[a[i][k]].pb(mkp(a[i][j],a[i][j+1]));
        }
    }
    // solve big
    For(i,1,m)
    {
        if(a[i].size()<=B)continue;
        int sz=a[i].size();
        For(j,1,n) pos[j]=-1;
        For(j,0,sz-1) pos[a[i][j]]=j;
        For(j,i+1,m){
            int r=-1;
            Rep(p,(int)a[j].size()-1,0)
                if(pos[a[j][p]]!=-1){
                    if(pos[a[j][p]]>r) r=pos[a[j][p]];
                    else if(a[j][p+1]!=a[i][pos[a[j][p]]+1]) {puts("Human");return;}
                }
        }
    }
    // solve small

    For(i,1,n) vis[i]=0;
    For(i,1,n){
        for(auto it:v[i]){
            if(vis[it.fi] && vis[it.fi]!=it.se) {puts("Human");return;}
            vis[it.fi]=it.se;
        }
        for(auto it:v[i])
            vis[it.fi]=0;
    }
    puts("Robot");
}

signed main()
{
    B=sqrt(300000)/2;
    int T=read();
    while(T--)work();
    return 0;
}