QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#114949 | #5956. Paradox Sort | sjc061031 | 0 | 4ms | 3612kb | C++14 | 3.3kb | 2023-06-24 10:53:44 | 2023-06-24 10:53:44 |
Judging History
answer
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
//#define int long long
using namespace __gnu_pbds;
using namespace std;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int n,X,vis[110],num[110],nxt[110],nt[110],ok[110][110];
vector<int> v[110],g[110],res;
inline void dfs(int x)
{
vis[x]=1;
for(int i=0;i<(int)v[x].size();i++) if(!vis[v[x][i]]){
dfs(v[x][i]);
}
res.push_back(x);
}
inline void rdfs(int x,int id)
{
vis[x]=1;num[x]=id;
for(int i=0;i<(int)g[x].size();i++) if(!vis[g[x][i]]){
rdfs(g[x][i],id);
}
}
signed main()
{
ios::sync_with_stdio(false);cin.tie(0);
int t;cin>>t;
int tot=0;
while(t--){
cin>>n>>X;X++;tot++;
for(int i=1;i<=n;i++) v[i].clear(),g[i].clear();
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) ok[i][j]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
char ch;cin>>ch;
if(ch=='Y'){
ok[i][j]=1;
v[i].push_back(j);
g[j].push_back(i);
}
}
}
for(int i=1;i<=n;i++) vis[i]=0;
res.clear();
for(int i=1;i<=n;i++) if(!vis[i]) dfs(i);
for(int i=1;i<=n;i++) vis[i]=0,num[i]=0;
int cnt=0;
for(int i=(int)res.size()-1;i>=0;i--) if(!vis[res[i]]){
cnt++;rdfs(res[i],cnt);
}
vector<int> vec,nw;
for(int i=1;i<=n;i++){
if(num[i]==1) vec.push_back(i);
else nw.push_back(i);
}
bool flag=false;
for(int i=0;i<(int)vec.size();i++) if(vec[i]==X) flag=true;
if(!flag){
cout<<"IMPOSSIBLE\n";continue;
}
if((int)vec.size()==1){
cout<<"Case #"<<tot<<": ";
for(int i=0;i<(int)vec.size();i++) cout<<vec[i]-1<<' ';
for(int i=0;i<(int)nw.size();i++) cout<<nw[i]-1<<' ';
cout<<'\n';
continue;
}
int l=vec[0],r=vec[0];
for(int i=1;i<(int)vec.size();i++){
int u=vec[i];
if(ok[u][l]) nxt[u]=l,l=u;
else if(ok[r][u]) nxt[r]=u,r=u;
else{
for(int j=l;j!=r;j=nxt[j]){
if(ok[j][u]&&ok[u][nxt[j]]){
nxt[u]=nxt[j];nxt[j]=u;
break;
}
}
}
}
for(int i=l;i!=r;i=nxt[i]) nt[i]=nxt[i];
int now=-1;
for(int i=l;;i=nxt[i]){
if(now==-1){
if(ok[i][l]) nt[i]=l,now=i;
}
else{
int s=nt[now];
if(ok[i][s]){
for(int j=now;j!=i;j=nxt[j]) nt[j]=nxt[j];
nt[i]=s;now=i;
}
else{
for(int j=nt[s],pre=s;j!=s;j=nt[j],pre=nt[pre]){
if(ok[i][j]){
nt[i]=j;nt[pre]=nxt[now];now=i;
break;
}
}
}
}
if(i==r) break;
}
vector<int> cyc;
for(int i=now;;i=nt[i]){
cyc.push_back(i);
if(nt[i]==now) break;
}
int pos=-1;
for(int i=0;i<(int)cyc.size();i++) if(cyc[i]==X){
pos=i;break;
}
cout<<"Case #"<<tot<<": ";
for(int i=pos-1;i>=0;i--) cout<<cyc[i]-1<<' ';
for(int i=(int)cyc.size()-1;i>=pos;i--) cout<<cyc[i]-1<<' ';
for(int i=0;i<(int)nw.size();i++) cout<<nw[i]-1<<' ';
cout<<'\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3512kb
input:
100 3 0 -YN N-Y YN- 2 0 -Y N- 5 0 -YNNN N-YNN YN-YN YYN-Y YYYN- 5 1 -NYYY Y-NNN NY-NY NYY-N NYNY- 6 5 -YYNNY N-YYNY NN-NYN YNY-NY YYNY-Y NNYNN- 4 0 -YYY N-YN NN-N NYY- 2 0 -Y N- 5 1 -NYNY Y-YYY NN-YY YNN-N NNNY- 7 5 -YYYYYY N-NNYYN NY-YNNN NYN-NYN NNYY-NN NNYNY-N NYYYYY- 8 0 -YNNNNNN N-YNNNNN YN-YNN...
output:
Case #1: 2 1 0 Case #2: 0 1 Case #3: 4 3 2 1 0 Case #4: 2 3 4 0 1 Case #5: 1 0 3 4 2 5 Case #6: 0 1 2 3 Case #7: 0 1 Case #8: 1 0 2 3 4 IMPOSSIBLE Case #10: 7 6 5 4 3 2 1 0 Case #11: 0 1 2 Case #12: 0 1 Case #13: 0 1 IMPOSSIBLE IMPOSSIBLE Case #16: 8 7 6 5 4 3 1 2 0 Case #17: 4 2 5 3 0 ...
result:
wrong answer 1st lines differ - expected: 'Case #1: 1 2 0', found: 'Case #1: 2 1 0 '
Subtask #2:
score: 0
Wrong Answer
Test #2:
score: 0
Wrong Answer
time: 4ms
memory: 3612kb
input:
100 39 0 -YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN N-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYYYN-YNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN YYYYYYN-YNN...
output:
Case #1: 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Case #2: 40 13 38 41 51 52 28 49 42 43 30 34 46 23 0 33 5 1 17 32 15 29 19 10 16 47 48 9 4 27 6 7 18 31 8 11 26 50 3 37 25 35 45 20 24 39 22 12 44 36 2 21 14 Case #3: 1 0 4 2 5 3 6 8...
result:
wrong answer 1st lines differ - expected: 'Case #1: 37 38 36 35 34 33 32 ...13 12 11 10 9 8 7 6 5 4 3 2 1 0', found: 'Case #1: 38 37 36 35 34 33 32 ...3 12 11 10 9 8 7 6 5 4 3 2 1 0 '