NC209480. InvestigatingLegions
描述
输入描述
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; }