QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#604926 | #8759. 小班课 | ucup-team902# | WA | 6ms | 16584kb | C++17 | 4.3kb | 2024-10-02 14:36:45 | 2024-10-02 14:36:45 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N=500;
int n, m;
vector<int> G[505];
int sz[505];
vector<int> c[505];
pair<int, int> mat[505];
vector<int> match[505];
int cur[505];
bool vis[505];
bool dfs(int x) {
if (vis[x]) return 0;
vis[x] = 1;
for (auto v : c[x]) {
for (int &i = cur[v]; i < sz[v]; ++i) {
int nxt=i;
if (match[v][nxt] == -1 || dfs(match[v][nxt])) {
match[v][nxt] = x;
mat[x] = {v, nxt};
return 1;
}
}
}
return 0;
}
set<int> e[N+5][N+5];
int st[N+5],ss;
bool in[N+5];
int dfs2(int u){
if(in[u]) return u;
if(vis[u]) return -1;
vis[u]=1;
int res=-1;
st[++ss]=u; in[u]=1;
for(int j=0;j<m;j++)
if(!e[u][j].empty()){
res=dfs2(j);
if(res!=-1) return res;
}
ss--; in[u]=0;
return res;
}
int e2[N+5][N+5];
void solve(){
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
e[i][j].clear();
for(int i=0;i<n;i++){
auto [j,x]=mat[i];
if(j!=-1){
for(auto k:c[i])
if(k!=j)
e[j][k].insert(i);
else break;
}
}
while(1){
fill(vis,vis+m,0);
fill(in,in+m,0); ss=0;
int u=-1;
for(int i=0;i<m;i++){
if(!vis[i]){
u=dfs2(i);
if(u!=-1) break;
}
}
if(u==-1) break;
vector<int> p;
int ts=ss,tmp=u;
while(st[ts]!=tmp){
int x=st[ts];
int pp=*e[x][u].begin();
p.push_back(pp);
for(auto j:c[pp])
if(j!=x)
e[x][j].erase(pp);
else break;
mat[pp].first=u;
u=x; ts--;
}
{
int x=tmp;
int pp=*e[x][u].begin();
p.push_back(pp);
for(auto j:c[pp])
if(j!=x)
e[x][j].erase(pp);
mat[pp].first=u;
}
for(auto pp:p){
auto [x,t]=mat[pp];
for(auto j:c[pp])
if(j!=x)
e[x][j].insert(pp);
else break;
}
}
// for(int j=0;j<n;j++)
// printf("%d\n",mat[j].first+1);
fill(in,in+n,0);
int t=n,cnt=0;
while(t--){
for(int i=0;i<n;i++){
if(in[i]) continue;
bool ok=1;
for(auto x:c[i])
if(x!=mat[i].first){
if(sz[x]) ok=0;
break;
}
else break;
// if(sz[x]&&x!=mat[i].first){
// ok=0; break;
// }
// else if(x==mat[i].first)
// break;
if(ok){
cout<<i+1<<' ';
cnt++;
if(mat[i].first!=-1)
sz[mat[i].first]--;
in[i]=1;
break;
}
}
}
assert(cnt==n);
cout<<endl;
}
int main() {
// freopen("1.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
cin >> T;
while (T--) {
cin >> n >> m;
for(int i=0;i<n;i++)
mat[i]={-1,-1};
for (int i = 0; i < m; ++i) {
cin >> sz[i];
match[i].resize(sz[i]);
for (int j = 0; j < sz[i]; ++j) match[i][j] = -1;
}
for (int i = 0; i < n; ++i) {
c[i].clear();
int m;
cin >> m;
while (m--) {
int x;
cin >> x;
x--;
c[i].push_back(x);
}
}
fill(vis,vis+n,0);
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) vis[j] = 0;
for (int j = 0; j < m; ++j) cur[j] = 0;
if (dfs(i)) {
ans++;
}
}
cout << ans << endl;
// if(ans==0){
// cout<<n<<' ' <<m<<endl;
// }
solve();
// for (int i = 0; i < n; ++i) cout << mat[i].first + 1 << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 15572kb
input:
3 5 5 1 1 1 1 1 4 1 3 2 4 1 5 4 3 4 2 1 2 3 5 1 1 5 3 1 2 2 2 1 2 2 1 2 2 1 3 2 1 3 2 1 3 5 5 1 1 1 1 1 2 1 2 2 5 4 2 3 2 2 4 3 2 5 1
output:
5 2 4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