2018年 ACM/ICPC亞洲區域賽 青島賽區現場賽 F題(ZOJ 4063 思維)
DreamGrid, the king of Gridland, is making a knight tournament. There are knights, numbered from 1 to , participating in the tournament. The rules of the tournament are listed as follows:
The tournament consists of 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 rounds.
Let , , and , be four distinct integers. If
Knight fights against knight during round , and
Knight fights against knight during round , and
Knight fights against knight during round ,
then knight must fight against knight during round .
As DreamGrid's general, you are asked to write a program to arrange all the duels in all the rounds, so that the resulting arrangement satisfies the rules above.
Input
There are multiple test cases. The first line of the input is an integer , indicating the number of test cases. For each test case:
The first and only line contains two integers and (), indicating the number of knights participating in the tournament and the number of rounds.
It's guaranteed that neither the sum of nor the sum of in all test cases will exceed 5000.
Output
For each test case:
If it's possible to make a valid arrangement, output lines. On the -th line, output integers separated by one space, indicating that in the -th round, knight will fight against knight for all .
If there are multiple valid answers, output the lexicographically smallest answer.
Consider two answers and , let's denote as the -th integer on the -th line in answer , and as the -th integer on the -th line in answer . Answer is lexicographically smaller than answer , if there exists two integers () and (), such that
for all and , , and
for all , , and finally .
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},使得對於任意i!=j都有f_i(a)!=f_j(a)且f_i(f_j(a))=f_j(f_i(a))。
再想一想異或的性質,你馬上就會做了。
#include<bits/stdc++.h>
#define ll long long
#define maxn 200010
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;
}
抄的大神部落格