QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#438283#8544. Colorful Graph 2ucup-team045#RE 147ms3844kbC++203.5kb2024-06-10 15:00:122024-06-10 15:00:13

Judging History

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

  • [2024-06-10 15:00:13]
  • 评测
  • 测评结果:RE
  • 用时:147ms
  • 内存:3844kb
  • [2024-06-10 15:00:12]
  • 提交

answer

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

struct DSU{
    vector<int> p;
    DSU(int n) : p(n + 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;
        p[y] = x;
        return true;
    }
};

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);
        vector<pair<int, int> > edge(m);
        for(int i = 0; i < m; i++){
            int a, b;
            cin >> a >> b;
            g[a].push_back(b);
            g[b].push_back(a);
            edge[i] = {a, b};
        }
        for(int i = 0; i < n; i++){
            int a = i, b = (i + 1) % n;
            g[a].push_back(b);
            g[b].push_back(a);
            edge.push_back({a, b});
        }

        vector<int> d(n);
        for(auto [a, b] : edge){
            d[a] += 1;
            d[b] += 1;
        }
        vector<bool> del(n);

        vector<int> v;

        for(int i = 0; i < n; i++){
            if (d[i] <= 2){
                del[i] = true;
                v.push_back(i);
            }
        }

        vector<int> order, id(n);
        int cnt = 0;
        while(!v.empty()){
            for(auto x : v){
                order.push_back(x);
                id[x] = cnt;
            }
            cnt++;
            vector<int> nv;
            for(auto x : v){
                for(auto j : g[x]){
                    if (--d[j] <= 2 and !del[j]){
                        del[j] = true;
                        nv.push_back(j);
                    }
                }
            }
            v.swap(nv);
        }

        reverse(order.begin(), order.end());
        vector<int> color(n, -1);
        for(auto x : order){
            if (color[x] != -1) continue;
            queue<int> q;
            q.push(x);
            color[x] = 0;
            while(!q.empty()){
                int t = q.front();
                q.pop();
                for(auto j : g[t]){
                    if (id[j] == id[t] or id[j] == id[t] - 1){
                        if (color[j] == -1){
                            color[j] = color[t] ^ 1;
                            q.push(j);
                        }
                    }
                }
            }
        }

        for(auto x : color) cout << (x ? 'R' : 'B');
        cout << '\n';

        auto check = [&](){
            DSU dsu(n);
            for(auto [a, b] : edge){
                if (color[a] == color[b]){
                    if (dsu.same(a, b)){
                        return false;
                    }
                    dsu.merge(a, b);
                }
            }
            return true;
        };
        // if (!check()){
        //     for(auto [a, b] : edge){
        //         cout << a << ' ' << b << '\n';
        //     }
        //     return 1;
        // }
        assert(check());
    }

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
3 0
4 1
1 3
6 3
0 2
2 4
4 0

output:

RRB
RRRB
BRRBRR

result:

ok ok (3 test cases)

Test #2:

score: 0
Accepted
time: 147ms
memory: 3548kb

input:

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

output:

RBBRBRRBR
RRB
RBRRB
BRRBBR
BRBRRBBBR
RRB
BRBRRBR
RBBRRRB
RRRB
BRRBRR
BRBRBR
RBRBRRB
RRRBRBRB
RRB
BBRRRBRB
RRRBRBRB
RRB
RBBRBBRBRR
RRBBRBRB
BRBRRBBRBR
BBRRRBBBRR
RRBBRRBRBR
RRB
BBRBRBR
BRBRBR
BRRRBRBR
RRRB
RBRRBRB
BRRBRBRBRB
BRBRBRR
BBRBRBRR
BRBRBR
BRRBRR
RRB
RRB
BRRBRBRBR
BRBRRBR
BRBRR
BRRBRBRRBR
BR...

result:

ok ok (100000 test cases)

Test #3:

score: 0
Accepted
time: 135ms
memory: 3796kb

input:

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

output:

RBBRBRRB
RRBRBRB
RBRB
BRBRBRBR
RRB
RRB
BRRBRBRR
RRB
BRBRBRRBR
RBRRBRB
RBRRBR
BRBRRBR
RBRBR
BRRRBBRBBR
RRBRB
RRB
RRBRBRBBR
RRB
BRRBR
RBRBBRBRB
BBRRBR
BRRBRBRR
RBBRB
BRBBBRBR
RBRBR
BRBRBRBR
BBBRRBR
BRBRBBRBRR
RBRBRBRBB
RBRRRBRBR
RBRBRB
BRBRBBR
RRB
BRBRRBRBRB
RRBRBB
RBRRBRBRB
RRRRB
RBBRBBRBRB
RBRRB
BBR...

result:

ok ok (100000 test cases)

Test #4:

score: -100
Runtime Error

input:

19452
78 75
50 52
61 64
19 21
21 27
52 54
75 5
47 58
15 13
47 66
69 71
66 68
33 36
27 32
15 17
66 60
74 7
63 61
41 13
45 71
30 28
68 71
18 13
13 42
47 55
76 1
19 32
61 66
5 2
22 24
74 71
42 44
59 47
66 46
26 21
49 52
56 58
54 47
52 48
21 25
19 41
10 42
45 74
48 54
39 41
41 18
75 6
39 33
33 37
31 28
...

output:


result: