列表

详情


NC209480. InvestigatingLegions

描述

ZYB likes to read detective novels and detective cartoons. One day he is immersed in such story:

As a spy of country B, you have been lurking in country A for many years. There are $n$ troops and $m$ legions in country A, and each troop belongs to only one legion. Your task is to investigate which legion each troop belongs to.

One day, you intercept a top secret message--a paper which records all the pairwise relationship between troops. You can learn from the paper that whether troop  and troop  belongs to the same legion for each . However, each piece of data has a independent probability of  being reversed.
Formally, the troop is numbered from  to , and the region is numbered from  to  . You are given  boolean numbers, indicating whether   and  belong to the same region (1 for yes and 0 for no). However, every number will be reversed(i.e., 0 to 1 and 1 to 0) in a probablity of . You can assume that the data is generated like this:



be[i] means the region that troop  belongs to. rand() can be considered as a uniform random function. i.e., select a integer from interval  with equal probability.

Note that you have already known  and , but you don't know the exact value of $m$. Please help country B construct the original data.

输入描述

The input contains multiple cases. The first line of the input contains a single integer , the number of cases.

For each case, the first line of the input contains two integers  (), denoting the number of troops and the integer parameter to generate data. There is a binary string (i.e. the string only contains '0' and '1') with length  in the next line, decribing the pairwise relationship. It is arrayed like this: .

 is not given but it is guaranteed that . And the sum of  over  cases doesn't exceed .

输出描述

For each case, output  integers seperated by space, the $i$-th integer describes be[i]. The value of be[i] is meaningless, so please output the solution with the smallest lexicographical order. For example, if there are 4 troops and 2 regions , you should output .

示例1

输入:

1
10 20
101110101010101010100010010101010100101010010

输出:

0 0 1 0 1 0 1 0 1 0

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 508K, 提交时间: 2020-08-13 13:09:36

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
int ans[305];bool e[305][305];char str[90005];
int main(){
	int T;scanf("%d",&T);
	while(T--){
		int n,s,now=0,cc=-1;vector<int>v;
		scanf("%d%d",&n,&s);scanf("%s",str+1);
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++)e[i][j]=e[j][i]=(str[++now]=='1');
			e[i][i]=true;ans[i]=-1;
		}
		for(int i=1;i<=n;i++)if(ans[i]==-1){
			v.clear();cc++;
			for(int j=1;j<=n;j++)if(ans[j]==-1&&e[i][j])v.pb(j);
			for(int j=1,cnt;j<=n;j++)if(ans[j]==-1){
				cnt=0;for(auto x:v)if(e[j][x])cnt++;
				if(cnt>=(v.size()>>1))ans[j]=cc;
			}
		}
		for(int i=1;i<=n;i++)printf("%d ",ans[i]);puts("");
	}
	return 0;
}

上一题