-
Notifications
You must be signed in to change notification settings - Fork 11
/
1546E
118 lines (118 loc) · 2.82 KB
/
1546E
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<bits/stdc++.h>
using namespace std;
const int maxn=500;
const long long mod=998244353;
typedef pair<int,int> pii;
int n,x,y,s,t;
int a[maxn*2+5][maxn+5],b[maxn+5][maxn+5],f[maxn+5];
vector <pii> v;
vector <int> c[maxn+5][maxn+5];
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
b[i][j]=0;
c[i][j].clear();
}
}
for (int i=1;i<=n*2;i++)
{
f[i]=0;
for (int j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
b[j][a[i][j]]++;
c[j][a[i][j]].push_back(i);
}
}
int top=0,tail=0;
int cnt=0,idx=1;
long long ans=1;
v.clear();
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if (b[i][j]==1)
{
tail++;
v.push_back(pii(i,j));
}
}
}
while (cnt<n)
{
if (top<tail)
{
x=v[top].first;
y=v[top].second;
if (b[x][y]!=1)
{
top++;
continue;
}
for (int i=0;i<c[x][y].size();i++)
{
if (f[c[x][y][i]]==0)
{
t=c[x][y][i];
break;
}
}
}
else
{
while (f[idx]!=0)
{
idx++;
}
t=idx;
ans=ans*2%mod;
}
f[t]=1;
cnt++;
for (int i=1;i<=n;i++)
{
b[i][a[t][i]]=0;
}
for (int i=1;i<=n;i++)
{
for (int j=0;j<c[i][a[t][i]].size();j++)
{
s=c[i][a[t][i]][j];
if (f[s]==0)
{
f[s]=2;
for (int k=1;k<=n;k++)
{
b[k][a[s][k]]--;
if (b[k][a[s][k]]==1)
{
tail++;
v.push_back(pii(k,a[s][k]));
}
}
}
}
}
top++;
}
s=0;
printf("%lld\n",ans);
for (int i=1;i<=n*2;i++)
{
if (f[i]==1)
{
s++;
if (s<n) printf("%d ",i); else printf("%d\n",i);
}
}
}
}