1 solutions

  • 0
    @ 2025-10-29 15:30:52

    神奇串串

    • 容易将问题转化为若干个位置对应相等,若干位置不同。
    • 先将相同的合并在一起,然后不同的连边来处理不同的限制。
    • 如果同块连边,即无解。
    • 要求字典序最小,可以依次对于每个位置进行贪心。
    • 如果当前位置相同的块中已经确定了,直接取。
    • 否则找到所有要求不同的位置值的 mex。
    • 直接实现上述做法,复杂度为 O(nmα(n)) O(nm\alpha(n)) ,这题放过暴力(所以数据也是乱造的。
    • 实际上可以通过倍增+并查集来实现合并,不同的关系只有 m m 条,可以直接暴力处理贪心。
    • ps:如果字符集大小有上限,我就不会写了。
    #include<cstdio>
    #define re register
    using namespace std;
    inline int read(){
    	re int t=0;re char v=getchar();
    	while(v<'0')v=getchar();
    	while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
    	return t;
    }
    int fa[1002],n,m,c[1002],tot,head[1002],cnt;
    struct edge{int to,next;}e[2002];
    inline void add(re int x,re int y){e[++cnt]=(edge){y,head[x]},head[x]=cnt;}
    inline int root(re int x){return x==fa[x]?x:fa[x]=root(fa[x]);}
    char v[1002][42];
    inline void Get(re int x){
    	c[x]=0;
    	while(v[x][c[x]])++c[x];
    	for(re int i=head[x];i;i=e[i].next)
    		v[e[i].to][c[x]]=1;
    }
    int main(){
    	freopen("string.in","r",stdin);
    	freopen("string.out","w",stdout); 
       int A[1002],B[1002];
    	n=read(),m=read();
    	for(re int i=1;i<=n;++i)fa[i]=i;
    	for(re int i=1;i<=m;++i){
    		re int x=read(),y=read(),z=read();
    		if(x>y)x^=y^=x^=y;
    		for(re int j=0;j<z;++j)fa[root(x+j)]=root(y+j);
    		if(y+z<=n)A[++tot]=x+z,B[tot]=y+z;
    	}
    	for(re int i=1;i<=tot;++i)if(root(A[i])==root(B[i]))return puts("-1"),0;
    	for(re int i=1;i<=tot;++i)add(root(A[i]),root(B[i])),add(root(B[i]),root(A[i]));
    	for(re int i=1;i<=n;++i){
    		if(!c[root(i)])Get(root(i));
    		printf("%d ",c[root(i)]);
    	}
    }
    
    • 1

    Information

    ID
    114
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    8
    Tags
    (None)
    # Submissions
    36
    Accepted
    7
    Uploaded By