QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#604831#8759. 小班课ucup-team902#WA 5ms15764kbC++173.8kb2024-10-02 14:07:352024-10-02 14:07:40

Judging History

你现在查看的是最新测评结果

  • [2024-10-02 14:07:40]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:15764kb
  • [2024-10-02 14:07:35]
  • 提交

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;
    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(in,in+m,0); ss=0;
        int u=-1;
        for(int i=0;i<m;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[u][x].begin();
            p.push_back(pp);
            for(auto j:c[pp])
                if(j!=u)
                    e[u][j].erase(pp);
            mat[pp].first=x;
            u=x; ts--;
        }
        {
            int x=tmp;
            int pp=*e[u][x].begin();
            p.push_back(pp);
            for(auto j:c[pp])
                if(j!=u)
                    e[u][j].erase(pp);
            mat[pp].first=x;
        }
        for(auto pp:p){
            auto [x,t]=mat[pp];
            for(auto j:c[pp])
                if(j!=x)
                    e[x][j].insert(pp);
        }
    }
    // for(int j=0;j<n;j++)
    //     printf("%d\n",mat[j].first+1);
    fill(in,in+n,0);
    int t=n;
    while(t--){
        for(int i=0;i<n;i++){
            if(in[i]) continue;
            bool ok=1;
            for(auto x:c[i])
                if(sz[x]&&x!=mat[i].first){
                    ok=0; break;
                }
                else if(x==mat[i].first)
                    break;
            if(ok){
                printf("%d ",i+1);
                if(mat[i].first!=-1)
                    sz[mat[i].first]--;
                in[i]=1;
                break;
            }
        }
    }
    puts("");
}
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);
            }
        }
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            if (dfs(i)) {
                ans++;
                for (int j = 0; j < n; ++j) vis[j] = 0;
                for (int j = 0; j < m; ++j) cur[j] = 0;
            }
        }
        cout << ans << 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: 0
Wrong Answer
time: 5ms
memory: 15764kb

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
5
5
2 4 3 5 1 
5 1 2 3 4 
1 5 2 4 3 

result:

wrong answer not a permutation