QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#707175#9453. Graph Generatorpropane#AC ✓170ms8588kbC++202.5kb2024-11-03 15:00:412024-11-03 15:00:41

Judging History

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

  • [2024-11-03 15:00:41]
  • 评测
  • 测评结果:AC
  • 用时:170ms
  • 内存:8588kb
  • [2024-11-03 15:00:41]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<numeric>
#include<set>
#include<algorithm>
using namespace std;
using LL = long long;

struct DSU{
    vector<int> p, sz;
    DSU(int n) : p(n + 1), sz(n + 1, 1){ 
        iota(p.begin(), p.end(), 0); 
    }

    int find(int x){
        return p[x] == x ? x : p[x] = find(p[x]);
    }

    bool same(int x, int y) { 
        return find(x) == find(y); 
    }

    bool merge(int x, int y){
        x = find(x), y = find(y);
        if (x == y) return false;
        if (sz[x] < sz[y]) swap(x, y);
        sz[x] += sz[y];
        p[y] = x;
        return true;
    }
    
    int size(int x) { 
        return sz[find(x)];
    }
};

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    int T;
    cin >> T;
    while(T--){
        int n, m;
        cin >> n >> m;
        vector<vector<int> > g(n);
        for(int i = 0; i < m; i++){
            int a, b;
            cin >> a >> b;
            a--, b--;
            g[a].push_back(b);
            g[b].push_back(a);
        }
        vector<int> id(n);
        iota(id.begin(), id.end(), 0);
        sort(id.begin(), id.end(), [&](int a, int b){
            return g[a].size() < g[b].size();
        });
        vector<vector<int> > ans(n);


        DSU dsu(n);
        vector<bool> v(n);
        bool ok = true;
        for(int i = 0; i < n; i++){
            int x = id[i];
            v[x] = true;
            set<int> s;
            int cnt = 0;
            for(auto j : g[x]){
                if (v[j]){
                    cnt += 1;
                }
                if (v[j] and !s.contains(dsu.find(j))){
                    ans[i].push_back(j);
                    s.insert(dsu.find(j));
                    cnt -= dsu.size(j);
                }
            }
            if (cnt != 0){
                ok = false;
                break;
            }
            for(auto j : g[x]){
                if (v[j]){
                    dsu.merge(x, j);
                }
            }
        }
        if (!ok){
            cout << "No" << '\n';
            continue;
        }
        cout << "Yes" << '\n';
        for(int i = 0; i < n; i++){
            cout << id[i] + 1 << ' ' << ans[i].size();
            for(auto x : ans[i]) cout << ' ' << x + 1;
            cout << '\n';
        }
    }

}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3616kb

input:

3
3 0
4 4
1 2
2 3
3 4
2 4
5 5
1 2
2 3
3 4
4 5
2 4

output:

Yes
1 0
2 0
3 0
Yes
1 0
3 0
4 1 3
2 2 1 3
No

result:

ok 3 cases passed

Test #2:

score: 0
Accepted
time: 170ms
memory: 8588kb

input:

4176
10 15
1 4
9 1
3 7
8 1
2 1
5 4
5 1
10 1
7 1
10 7
10 3
6 4
3 1
6 1
9 2
7 10
6 7
1 7
1 4
7 2
4 2
5 2
3 1
1 6
2 6
5 1
6 8
5 2
3 1
4 6
2 1
5 1
1 4
6 2
6 1
9 15
5 1
4 2
7 1
1 8
2 3
5 8
1 9
4 3
6 5
9 2
3 1
4 1
7 2
9 7
1 6
6 11
1 2
3 5
3 1
3 2
4 3
1 6
4 2
1 4
5 4
5 1
5 2
6 6
1 6
6 3
1 3
1 5
4 2
2 1
10 ...

output:

Yes
8 0
2 0
5 0
6 0
9 1 2
3 0
4 2 5 6
7 1 3
10 1 7
1 4 4 9 8 10
No
No
No
Yes
6 0
2 0
3 1 2
4 1 3
5 1 3
1 2 2 6
No
No
Yes
2 0
3 0
7 1 3
4 0
5 1 4
6 1 4
1 3 4 2 7
Yes
4 0
5 0
7 0
8 0
9 0
6 0
10 0
3 2 6 10
2 5 5 10 7 9 8
1 2 8 4
Yes
9 0
4 0
6 0
7 0
8 0
5 2 7 8
2 2 5 6
3 1 6
1 2 3 4
Yes
3 0
4 0
5 1 4
6 ...

result:

ok 4176 cases passed

Extra Test:

score: 0
Extra Test Passed