Tournament(转载)

ZOJ - 4063

DreamGrid, the king of Gridland, is making a knight tournament. There are nnn knights, numbered from 1 to nnn, participating in the tournament. The rules of the tournament are listed as follows:The tournament consists of kkk rounds. Each round consists of several duels. Each duel happens between exactly two knights.Each knight must participate in exactly one duel during each round.For each pair of knights, there can be at most one duel between them during all the kkk rounds.Let 1≤i,j≤k1 \le i, j \le k1≤i,j≤k, i≠ji \ne ji​=j, and 1≤a,b,c,d≤n1 \le a, b, c, d \le n1≤a,b,c,d≤n, a,b,c,da, b, c, da,b,c,d be four distinct integers. If Knight aaa fights against knight bbb during round iii, andKnight ccc fights against knight ddd during round iii, andKnight aaa fights against knight ccc during round jjj, then knight bbb must fight against knight ddd during round jjj.As DreamGrid’s general, you are asked to write a program to arrange all the duels in all the kkk rounds, so that the resulting arrangement satisfies the rules above. InputThere are multiple test cases. The first line of the input is an integer TTT, indicating the number of test cases. For each test case:The first and only line contains two integers nnn and kkk (1≤n,k≤10001 \le n, k \le 10001≤n,k≤1000), indicating the number of knights participating in the tournament and the number of rounds.It’s guaranteed that neither the sum of nnn nor the sum of kkk in all test cases will exceed 5000.OutputFor each test case:If it’s possible to make a valid arrangement, output kkk lines. On the iii-th line, output nnn integers ci,1,ci,2,…,ci,nc_{i, 1}, c_{i, 2}, \dots, c_{i, n}ci,1​,ci,2​,…,ci,n​ separated by one space, indicating that in the iii-th round, knight jjj will fight against knight ci,jc_{i, j}ci,j​ for all 1≤j≤n1 \le j \le n1≤j≤n.If there are multiple valid answers, output the lexicographically smallest answer.Consider two answers AAA and BBB, let’s denote ai,ja_{i, j}ai,j​ as the jjj-th integer on the iii-th line in answer AAA, and bi,jb_{i, j}bi,j​ as the jjj-th integer on the iii-th line in answer BBB. Answer AAA is lexicographically smaller than answer BBB, if there exists two integers ppp (1≤p≤k1 \le p \le k1≤p≤k) and qqq (1≤q≤n1 \le q \le n1≤q≤n), such that for all 1≤i<p1 \le i < p1≤i<p and 1≤j≤n1 \le j \le n1≤j≤n, ai,j=bi,ja_{i, j} = b_{i, j}ai,j​=bi,j​, andfor all 1≤j<q1 \le j < q1≤j<q, ap,j=bp,ja_{p, j} = b_{p, j}ap,j​=bp,j​, and finally ap,q<bp,qa_{p, q} < b_{p, q}ap,q​<bp,q​.If it’s impossible to make a valid arrangement, output “Impossible” (without quotes) in one line.Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!
Sample Input
2
3 1
4 3
Sample Output
Impossible
2 1 4 3
3 4 1 2
4 3 2 1

题意:T组样例,每组给你n表示n个人,m表示要打m轮比赛。每一轮每个人都要有一个对手。而且每个对手只能打一次。假设a与b打了,c与d打了,那么下一轮如果a与c打了,那么b就必须和d打。问你这m轮比赛怎么安排。如果无法安排,输出Impossible。如果有,m行,每行输出每个人的对手是几号。
思路:比赛的时候思维僵化了。问题转化为找到m个二阶置换{ f i f_i },使得对于任意 i ! = j i!=j 都有 f i ( a ) ! = f j ( a ) f_i(a)!=f_j(a) f i ( f j ( a ) ) = f j ( f i ( a ) ) f_i(f_j(a))=f_j(f_i(a)) 。再想一想异或的性质,你马上就会做了。

#include<iostream>
using namespace std;
int n,m,k;
int main(){
    int T,cas=1;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        if(m>=(n&(-n))) {
		puts("Impossible");
		continue;
	}
        for(int j=1;j<=m;j++){
        	for(int i=0;i<n;i++){
            	printf("%d%c",(i^j)+1,i==n-1?'\n':' ');
        	}
        }
    }
    return 0;
}
发布了71 篇原创文章 · 获赞 80 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_42971794/article/details/104605856
今日推荐